|
|
English Page Click Here
How to interfacing LCD with printer port in C# .NET
Overview of LCD interfacing
  ทำความเข้าใจในการเขียนโปรแกรมด้วย C#.NET ควบคุมการแสดงผลของจอ LCD ผ่าน Printer Port
โปรแกรมแกรมนี้จะควบคุมการทำงานของ LCD ให้แสดงผลออกที่จอ ด้วยการควบคุมสัญญาณและส่งข้อมูล ผ่าน Printer Port
สำหรับ LCD นี้ เราสามารถหาซื้อได้ตามร้านอิเล็คทรอนิคส์ ซึ่งมันมีหลายขนาด เช่น 8x1, 8x2, 16x1, 16x2, 20x2, 20x4, 40x4.
สำหรับโปรเจ็คนี้ผมใช้ IC model HD44780 ขนาด 16x2 คือ แสดงได้ 16 ตัวอักษร 2 แถวล่างบน มี 16 ขา เพิ่มสองขาสำหรับจ่ายไฟหน้าจอ
ตารางนี้จะบอกว่าเราจะจัดการอะไรกับ LCD บ้าง แต่อันนี้ไม่มี ไฟแสดงหน้าจอนะ ซึ่งปกติจะใช้แค่ 14 ขา
| Pin No |
Symbol |
Details |
| 1 |
GND |
Ground |
| 2 |
Vcc |
Supply Voltage
+5V |
| 3 |
Vo |
Contrast
adjustment |
| 4 |
RS |
0->Control
input, 1-> Data input |
| 5 |
R/W |
Read/ Write |
| 6 |
E |
Enable |
| 7 to
14 |
D0 to
D7 |
Data |
| 15 |
VB1 |
Backlight +5V |
| 16 |
VB0 |
Backlight
ground |
Note
สำหรับรายละเอียดอื่นๆเกี่ยวกับก่อนเริ่มที่จะทำโปรเจ็คของเรามีดังนี้
-
เมื่อไหรที่คุณใช้คอมพิวเตอร์ในการแสดงหน้าจอแบบเต็มจอ เช่นเกมส์,ดูหนัง,หรือ โทรทัศน์ ,คุณต้องออกจากโปรแกรมไปรับเอาข้อมูลจากคอมพิวเตอร์BR>
-
ถ้าคุณต้องการดัดแปลงโค๊ด คุณต้องไม่การติดต่อกับ วงจร หรือ โปรแกรมแกรมคำสั่งใหม่ไปที่ ซิป เพราะมันก็เหมือนกับเราทำกับไมโครคอนโทรลเลอร์แหละ
-
คุณต้องใช้เงินในการจัดซื้อ LCD , DB25 แบบ Female และ ตัวต้านทานแบบปรับค่าได้ขนาด 10k โอท์ม และสายไฟในการต่อกับคอมพิวเตอร์ด้วย
ในรูปการต่อ LCDด้านล่างนี้ ,LCD จะต่อกับ LTP port โดยใช้ Connector แบบ DB25
ถ้า ทุกสิ่งพร้อมแล้ว คุณก็จะใช้ LCD ได้ เมื่อเปิดสวิทช์จ่ายไฟ
ถ้าคุณได้ทำให้หน้าจอนี้ทำงาน ดังนั้น เราสามารถเริ่มเขียนโปรแกรมได้แล้ว ,อ้อ อีกอย่างหนึ่ง ตรวจสอบ Connector,ต้องลองปรับค่าโอท์มของตัวต้านทาน ,ถ้าหน้าจอแสดงผล คุณสามารถได้ความชัดสูงสุดโดยใช้ปรับค่าตัวต้านทานนี้
สำหรับ ตัวต้านทานของเราต้องการใช้ให้เกือบเข้าใกล้ 0.
สำหรับตารางนี้จะอธิบาย การเขียนคำสั่งควบคุมอย่างไร. เมื่อไหร RS=0 และ R/W=0 ,จะบอกความหมายของในการควบคุม Pin D0 ถึง D7
|
Instruction |
D7 |
D6 |
D5 |
D4 |
D3 |
D2 |
D1 |
D0 |
Description |
| Clear Display |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
1 |
Clears Display and returns cursor
to home position.
|
Cursor home
|
0 |
0 |
0 |
0 |
0 |
0 |
1 |
X |
Returns cursor to home position.
Also returns display being shifted to the original
position.
|
| Entry mode set |
0 |
0 |
0 |
0 |
0 |
1 |
I/D |
S |
I/D = 0 --> cursor is in
decrement position. I/D =
1 --> cursor is in increment position. S = 0 -->
Shift is invisible. S = 1 -->
Shift is visible |
| Display ON- OFF Control |
0 |
0 |
0 |
0 |
1 |
D |
C |
B |
D- Display, C- cursor, B- Blinking
cursor = 0 --> OFF =1 --> ON |
| Cursor/ Display Shift |
0 |
0 |
0 |
1 |
S/C |
R/L |
X |
X |
S/C = 0 --> Move
cursor. S/C = 1 --> Shift display.
R/L = 0 --> Shift left. R/L = 1
--> Shift right |
| Function Set |
0 |
0 |
1 |
DL |
N |
F |
X |
X |
DL = 0 --> 4 bit interface. DL
= 1 --> 8 bit interface. N = 0 --> 1/8 or 1/11 Duty (1
line). N = 1 --> 1/16 Duty (2 lines). F = 0 --> 5x7
dots. F = 1 --> 5x10
dots. |
ผมต้องการที่จะอธิบายในเรื่องที่เหลือเกี่ยวกับการ อ่านและเขียนกับหน่วยความจำ( LCD RAM) ,เราจะไปดูวิธีการเหล่านั้นจากที่กล่าวไปแล้วด้านบน
การใช้ข้อมูลแล้วนั้น เราจะไปเขียนหน้าที่ ฟังก์ชั่นการทำงานของ LCD
กรุณาดูที่โปรแกรมด้านล่างนี้ ที่นั้นผมเขียน ทุกฟังก์ชั่นในการทำงานควบคุม LCD สำหรับทุกความต้องการของพวกเราในการติดต่อ LCD
ดังนั้น สำหรับโปรแกรมต่อไปของคุณ เราจะไปเปลี่ยน โปรแกรมหลัก( Main function) เท่านั้น
Step by Step
- เปิดโปรแกรม Visual Studio .NET
-
เลือกเมนู File-->New-->Project และ เลือก Visual C# Project จากนั้นเลือก Console Application.
- ไปที่หน้าต่างการเขียนโค๊ดโปรแกรม
using System;
using System.Runtime.InteropServices;
namespace ParallelPort
{
Private class PortAccess
{
Call OutPut function from DLL file.
[DllImport("inpout32.dll", EntryPoint="Out32")]
public static extern void Output(int adress, int value);
|
#include <dos.h>;
#include <char.h>
#include <conio.h>
#define PortAddress 0x378 /* Here ,Please make sure your printer port address */
#define DATA PortAddress+0
#define STATUS PortAddress+1
#define CONTROL PortAddress+2
// * I wrote a func for init ,write ,putch ,puts,goto,clear,home and entry mode for LCD which are: //
private void lcd_init(void);
private void lcd_write(char char2write);
private void lcd_putch(char char2write);
private void lcd_puts(char * str2write);
private void lcd_goto(int row, int column);
private void lcd_clear(void);
private void lcd_home(void);
private void lcd_cursor(int cursor);
private void lcd_entry_mode(int mode);
static void main()
{
Application.Run(new Form1());
lcd_init(); // start LCD function for initial
}
private void button1_Click(object sender, System.EventArgs e)
{
lcd_goto(1,1);
lcd_puts("Welcome To");
lcd_goto(1,0);
lcd_puts("LCD Wolrd");
}
private void lcd_init()
{
outportb(CONTROL, inportb(CONTROL) & 0xDF);
//config data pins as output
outportb(CONTROL, inportb(CONTROL) | 0x08);
//RS is made high: control (register select)
lcd_write(0x0f);
delay(20);
lcd_write( 0x01);
delay(20);
lcd_write( 0x38);
delay(20);
}
private void lcd_write(char char2write)
{
outportb(DATA, char2write);
outportb(CONTROL,inportb(CONTROL) | 0x01); /* Set Strobe */
delay(2);
outportb(CONTROL,inportb(CONTROL) & 0xFE); /* Reset Strobe */
delay(2);
}
private void lcd_putch(char char2write)
{
outportb(CONTROL, inportb(CONTROL) & 0xF7);
//RS=low: data
lcd_write(char2write);
}
private void lcd_puts(char *str2write)
{
outportb(CONTROL, inportb(CONTROL) & 0xF7);
//RS=low: data
while(*str2write)
lcd_write(*(str2write++));
}
private void lcd_goto(int row, int column)
{
outportb(CONTROL, inportb(CONTROL) | 0x08);
if(row==2) column+=0x40;
lcd_write(0x80 | column);
}
private void lcd_clear()
{
outportb(CONTROL, inportb(CONTROL) | 0x08);
lcd_write(0x01);
}
private void lcd_home()
{
outportb(CONTROL, inportb(CONTROL) | 0x08);
lcd_write(0x02);
}
private void lcd_entry_mode(int mode)
{
outportb(CONTROL, inportb(CONTROL) | 0x08);
lcd_write(0x04 + (mode%4));
}
private void lcd_cursor(int cursor)
{
outportb( CONTROL, inportb(CONTROL) | 0x08 );
lcd_write( 0x0c + (cursor%4));
}
|
กดปุ่ม Ctrl หร้อมกับ F5 สำหรับคอมไพล์และรันโปรแกรมของเรา ในหน้าจอแบบ DOS(Cosole Windows) นะ
ซึ่งขณะนั้น เจ้า LCD ควรจะแสดงคำว่า "Welcome To" และ "LCD Wolrd "
|