1176 views|0 replies

2015

Posts

0

Resources
The OP
 

MSP430 driver function for LCD1602 [Copy link]

C file LCD1602_Driver.c



1 /**************Reference header file***************/
2 #include <msp430F149.h> //Depending on the specific MCU model, reference different header files
3 #include "LCD1602_Driver.h"
4 #define uchar unsigned char
5 #define uint unsigned int
6
7 /**************Interface definition***************/
8 #define DataDir P4DIR //8-bit data line = P4 port
9 #define DataPort P4OUT
10 #define DataIn P4IN
11 #define Busy 0x80 //Busy signal is the highest bit of the data line BIT7
12 #define CtrlDir P3DIR
13 #define CLR_RS P3OUT&=~BIT0; //RS = P3.0
14 #define SET_RS P3OUT|=BIT0;
15 #define CLR_RW P3OUT&=~BIT1; //RW = P3.1
16 #define SET_RW P3OUT|=BIT1;
17 #define CLR_EN P3OUT&=~BIT2; //EN = P3.2
18 #define SET_EN P3OUT|=BIT2;
19
20 /*******************************************
21 Function name: LcdWriteCommand
22 Function: write command to LCD module
23 Parameter: cmd--command,
24 chk--busy flag, 1: busy, 0: not
25 Return value: None
26 ********************************************/
27 void LcdWriteCommand(uchar cmd,uchar chk)
28 {
29
30 if (chk) CheckBusy(); // detect busy signal?
31
32 CLR_RS;
33 CLR_RW;
34 _NOP();
35
36 DataPort = cmd; //Write the command word to the data port
37 _NOP();
38
39 SET_EN; //Generate an enable pulse signal
40 _NOP();
41 _NOP();
42 CLR_EN;
43 }
44
45 /*******************************************
46 Function name: LcdWriteData
47 Function: Write display data to the current address of the LCD
48 Parameter: data--display character data
49 Return value: None
50 ********************************************/
51 void LcdWriteData( uchar DataByte )
52 {
53 CheckBusy(); //Wait for the LCD to be not busy
54
55 SET_RS;
56 CLR_RW;
57 _NOP();
58
59 DataPort = data; //Write the displayed data to the data port
60 _NOP();
61
62 SET_EN; //Generate an enable pulse signal
63 _NOP();
64 _NOP();
65 CLR_EN;
66 }
67 /*******************************************
68 Function name: CheckBusy
69 Function: Check whether 1602 is busy and wait for the 1602 LCD to complete internal operations
70 Parameter: None
71 Return value: None
72 ********************************************/
73 void CheckBusy(void)
74 {
75 DataDir &= 0x00; //Switch P4 port to input state
76
77 CLR_RS;
78 SET_RW;
79 _NOP();
80 SET_EN;
81 _NOP();
82 _NOP();
83
84 while((DataIN & Busy)!=0); //Detect busy flag
85
86 CLR_EN;
87
88 DataDir |= 0xFF; //Switch P4 port to output state
89 }
90
91 /***********************************************
92 Function name: Delay5ms
93 Function: Delay about 5ms
94 Parameter: None
95 Return value: None
96 ********************************************/
97 void Delay5ms(void)
98 {
99 uint i=40000;
100 while (i != 0)
101 {
102 i--;
103 }
104 }
105
106 /********************************************
107 Function name: LcdReset
108 Function: Reset the 1602 LCD module
109 Parameter: None
110 Return value: None
111 ********************************************/
112 void LcdReset(void)
113 {
114 CtrlDir |= 0x07; //Set the control line port to output state
115 DataDir = 0xFF; //Set the data port to output state
116
117 LcdWriteCommand(0x38, 0); //Specified reset operation
118 Delay5ms();
119 LcdWriteCommand(0x38, 0);
120 Delay5ms();
121 LcdWriteCommand(0x38, 0);
122 Delay5ms();
123
124 LcdWriteCommand(0x38, 1); //Display mode setting
125 LcdWriteCommand(0x08, 1); //Display off
126 LcdWriteCommand(0x01, 1); //Display clear
127 LcdWriteCommand(0x06, 1); //Write characters without moving
128 LcdWriteCommand(0x0c, 1); //Display on, cursor off, no flashing
129 }
130 /*******************************************
131 Function name: LocateXY
132 Function: Input the coordinate information of the display character position to the LCD
133 Parameter: x--column coordinate of the position 0-15
134 y--row coordinate of the position 0-1
135 Return value: None
136 ********************************************/
137 void LocateXY(uchar x,uchar y)
138 {
139 uchar temp;
140
141 temp = x&0x0f;
142 y &= 0x01;
143 if(y) temp |= 0x40; //If in the second row
144 temp |= 0x80;//The first line is 0x80, the second line is 0x80+0x40
145
146 LcdWriteCommand(temp,1);
147 }
148 /***********************************************
149 Function name: Disp1Char
150 Function: Display a character at a certain position
151 Parameter: x--column coordinate of the position 0-15
152 y--row coordinates of the position 0-1
153 data--displayed character data
154 Return value: None
155 ********************************************/
156 void Disp1Char(uchar x,uchar y,uchar DataByte)
157 {
158 LocateXY( x, y );
159 LcdWriteData( DataByte );
160 }
161 /***********************************************
162 Function name: DispStr
163 Function: Let the LCD display a string continuously from a certain position
164 Parameter: x--column coordinate of the position 0-15
165 y--row coordinate of the position 0-1
166 ptr--pointer to the location where the string is stored
167 Return value: None
168 ***********************************************/
169 void DispStr(uchar x,uchar y,uchar *ptr)
170 {
171 uchar 187 } 188 } 189 190 /*********************************************** 191 Function name: DispNchar 192 Function: Let the LCD
display N characters continuously from a certain position 193 Parameter : x -- column coordinate of the position 0-15 194 y--row coordinate of the position 0-1 192 n--number of characters 193 ptr--pointer to the character storage location 194 Return value: None 195 ********************************************/ 196 void DispNChar(uchar x,uchar y, uchar n,uchar *ptr) 197 { 198 uchar i; 199 200 for (i=0;i<n;i++) 201 { 202 Disp1Char(x++,y,ptr[i]); 203 if (x == 0x0f) 204 { 205 x = 0; 206 y ^= 1; 207 } 208 } 209 } Corresponding header file LCD1602_Driver.h








































1 #define uchar unsigned char
2 #define uint unsigned int
3 /****************dǐcénghánshù****************/
4 void LcdWriteCommand( fly cmd, fly chk);
5 void LcdWriteData( volatile DataByte );
6 void CheckBusy(void);
7 void Delay5ms(void);
8 /****************gōngnénghánshù****************/
9 void LcdReset(void);
10 void LocateXY(fly x,fly y);
11 void Disp1Char(fly x,fly y,fly DataByte);
12 void DispStr(fly x,fly y,fly *ptr);
13 void DispNChar(fly x,fly y,fly n,fly *ptr);

This post is from Microcontroller MCU
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list