研究了一下,如何用ATmega 16单片机,来驱动字符型液晶显示芯片,现把研究心得写出来:
我手里的这个RT1601液晶显示模块,使用的是S6A0069显示芯片。
各个引脚简单说明一下:
控制和数据引脚配置如下:
-------------------------------
PORTD_0 -RS
PORTD_1 - R/W
PORTD_2 - E
PORTA - DATA BUS
------------------------------
LCD.h
1#ifndef_LCD_H_
2#define_LCD_H_
3/*****************************************
4RS-PORTD_0
5R/W-PORTD_1
6E-PORTD_2
7DB-PORTA
8******************************************/
9voidLCD_Set_RS(void);
10voidLCD_Clear_RS(void);
11voidLCD_Set_RW(void);
12voidLCD_Clear_RW(void);
13voidLCD_Set_E(void);
14voidLCD_Clear_E(void);
15voidLCD_CheckBF(void);
16//WriteCommand
17voidLCD_Write_Cmd(unsignedcharbyCmd);
18//WriteData
19voidLCD_Write_Data(unsignedcharbyData);
20voidLCD_Delay_5ms(void);
21voidLCD_Delay_500ms(void);
22voidLCD_PortInit(void);
23
24//ApplicaTIonInterface
25voidLCD_Init(void);
26voidLCD_Display(char*pstrText);
27
28#endif
29
LCD.c
这里是液晶驱动的实现部分,对外开放两个接口:LCD_Init, LCD_Display。(我这个模块,是两行显示的,共16个字符)
1#include“LCD.h”
2#include
3#include
4
5/*****************************************
6RS-PORTD_0
7R/W-PORTD_1
8E-PORTD_2
9DB-PORTA
10******************************************/
11
12
13voidLCD_Set_RS(void)
14{
15PORTD“=(1《《0);
16
17return;
18}
19
20voidLCD_Clear_RS(void)
21{
22PORTD&=~(1《《0);
23
24return;
25}
26
27voidLCD_Set_RW(void)
28{
29PORTD”=(1《《1);
30
31return;
32}
33
34voidLCD_Clear_RW(void)
35{
36PORTD&=~(1《《1);
37
38return;
39}
40
41voidLCD_Set_E(void)
42{
43PORTD“=(1《《2);
44
45return;
46}
47
48voidLCD_Clear_E(void)
49{
50PORTD&=~(1《《2);
51
52return;
53}
54
55voidLCD_CheckBF(void)
56{
57DDRA=0x00;//Input
58
59LCD_Clear_RS();
60LCD_Set_RW();
61LCD_Set_E();
62while(PINA&0x80)
63{
64;
65}
66LCD_Clear_E();
67
68DDRA=0xFF;//Output
69
70return;
71}
72
73//WriteCommand
74voidLCD_Write_Cmd(unsignedcharbyCmd)
75{
76LCD_CheckBF();
77
78LCD_Clear_RS();
79LCD_Clear_RW();
80LCD_Set_E();
81PORTA=byCmd;
82LCD_Clear_E();
83
84return;
85}
86
87//WriteData
88voidLCD_Write_Data(unsignedcharbyData)
89{
90LCD_CheckBF();
91
92LCD_Set_RS();
93LCD_Clear_RW();
94LCD_Set_E();
95PORTA=byData;
96LCD_Clear_E();
97
98return;
99}
100
101voidLCD_Delay_5ms(void)
102{
103unsignedinTI=5552;
104while(i--)
105{
106;
107}
108
109return;
110}
111
112voidLCD_Delay_500ms(void)
113{
114unsignedchari=5;
115unsignedcharj=0;
116
117while(i--)
118{
119j=7269;
120while(j--)
121{
122;
123}
124}
125
126return;
127}
128
129//PorTInit
130voidLCD_PorTInit(void)
131{
132PORTD=0x00;
133DDRD=0xFF;//Output
134DDRA=0xFF;
135
136return;
137}
138
139//LCDInit
140voidLCD_Init(void)
141{
142LCD_PortInit();
143
144LCD_Delay_500ms();
145
146LCD_Write_Cmd(0x38);//FunctionSet.8bitdatalength,2-line,5*8font
147LCD_Delay_5ms();
148LCD_Write_Cmd(0x0C);//DisplayON/OFFControl.DisplayON,CursorOFF,BlinkOFF
149LCD_Delay_5ms();
150LCD_Write_Cmd(0x01);//DisplayClear.
151LCD_Delay_5ms();
152LCD_Write_Cmd(0x06);//EntryModeSet.Incrementmode,Entireshiftoff
153
154return;
155}
156
157voidLCD_Display(char*pstrText)
158{
159unsignedchari=0;
160
161LCD_Write_Cmd(0x80”0x00);//1LinePosition
162while(*pstrText!=0x00)
163{
164if(i==8)
165{
166LCD_Write_Cmd(0x80“0x40);//2LinePosition
167}
168
169LCD_Write_Data(*(pstrText++));
170i++;
171}
172
173return;
174}
175
176
client端调用如下:
1//ICC-AVRapplicationbuilder:2008-6-48:41:19
2//Target:M16
3//Crystal:4.0000Mhz
4#include
5#include
6#include”LCD.h“
7
8voidmain(void)
9{
10LCD_Init();
11LCD_Display(”RT1601DemobyWF“);
12
13while(1)
14{
15;
16}
17
18return;
19}