-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLCD.c
224 lines (178 loc) · 5.21 KB
/
LCD.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include "stm32f10x.h"
#include "LCD.h"
void LCD_PulseEnablePin(int);
void delayMS(int);
void LCD_sendNibble(uint8_t,int);
void LCD_sendCommand(uint8_t,int);
//void LCD_sendByte(uint8_t, int,int);
int CMD = 0;
int DATA = 1;
void LCD_Init(int chip)
{
delayMS(40);
GPIOC->BSRR = GPIO_Pin_3|GPIO_Pin_2|GPIO_Pin_1|GPIO_Pin_0;
GPIOC->BSRR = GPIO_Pin_13;
GPIOC->BSRR = GPIO_Pin_6;
GPIOC->BSRR = GPIO_Pin_7;
GPIOC->BSRR = GPIO_Pin_5;
GPIOC->BRR = GPIO_Pin_3|GPIO_Pin_2|GPIO_Pin_1|GPIO_Pin_0;
GPIOC->BRR = GPIO_Pin_13;
GPIOC->BRR = GPIO_Pin_6;
GPIOC->BRR = GPIO_Pin_7;
GPIOC->BRR = GPIO_Pin_5;
if (chip == 1){
LCD_sendNibble(0x03,1);
delayMS(5);
LCD_sendNibble(0x03,1);
delayMS(5);
LCD_sendNibble(0x03,1);
delayMS(5);
//set 4bit interface
LCD_sendNibble(0x02,1);
//delayMS(2);
//sending function cmd and two line display
LCD_sendCommand(0x28,1);
delayMS(2);
//sending display cmd,display on and cursor on
LCD_sendCommand(0x0F,1);
delayMS(2);
//sending entry mode cmd and cursor increment
LCD_sendCommand(0x06,1);
//delayMS(1);
//delayMS(1000);
LCD_clearScreen(1);
}
if (chip == 2){
LCD_sendNibble(0x03,2);
delayMS(5);
LCD_sendNibble(0x03,2);
delayMS(5);
LCD_sendNibble(0x03,2);
delayMS(5);
//set 4bit interface
LCD_sendNibble(0x02,2);
//delayMS(2);
//sending function cmd and two line display
LCD_sendCommand(0x28,2);
delayMS(2);
//sending display cmd,display on and cursor on
LCD_sendCommand(0x0F,2);
delayMS(2);
//sending entry mode cmd and cursor increment
LCD_sendCommand(0x06,2);
//delayMS(1);
//delayMS(1000);
LCD_clearScreen(2);
}
}
void LCD_printString(uint8_t *Str,int chip){
uint8_t *c;
c=Str;
while((c!=0)&& (*c!=0)){
LCD_sendByte(*c,1,chip);
c++;
}
}
void LCD_setCursorPosition(uint8_t row, uint8_t col,int chip){
uint8_t address;
if (row==0){
address=0;
}else{
address=40;
}
address|=col;
LCD_sendCommand(0x80|address,chip);
}
void LCD_CtrlLinesConfig(void){
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIO clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
/* Configure NCS in Output Push-Pull mode -Enable Pin(E1)*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* Configure NCS in Output Push-Pull mode -Enable Pin(E2)*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* Configure NWR(RNW),in Output Push-Pull mode -R/W*///read or write
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* Configure RS in Output Push-Pull mode -Reset Pin*///Data /Instruction
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
//PC0 for LCD pin4(PD4)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
//PC1 for LCD pin3(PD5)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
//PC2 for LCD pin2(PD6)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
//PC3 for LCD pin1(PD7)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
void LCD_clearScreen(int chip){
LCD_sendCommand (0x01, chip);
LCD_sendCommand (0x02,chip);
}
void LCD_sendCommand (uint8_t cmdValue,int chip){
LCD_sendByte(cmdValue, CMD,chip);
}
void LCD_sendByte(uint8_t cmdValue,int CMD_DATA,int chip){
if ( CMD_DATA == 0){
GPIOC->BRR = GPIO_Pin_7;
}else{
GPIOC->BSRR = GPIO_Pin_7;
}
//sending high nibble
LCD_sendNibble(((cmdValue & 0xF0)>> 4),chip);
//sending low nibble
LCD_sendNibble((cmdValue & 0x0F),chip);
}
void LCD_sendNibble(uint8_t nibble,int chip){
GPIOC->ODR &=~(GPIO_Pin_3|GPIO_Pin_2|GPIO_Pin_1|GPIO_Pin_0);
GPIOC->ODR |=nibble;
LCD_PulseEnablePin(chip);
}
void LCD_PulseEnablePin(int chip){
if (chip==1){
GPIOC ->BRR = GPIO_Pin_13;
delayMS(200);
GPIOC ->BSRR = GPIO_Pin_13;
delayMS(200);
GPIOC ->BRR = GPIO_Pin_13;
delayMS(200);
}else {
GPIOC ->BRR = GPIO_Pin_5;
delayMS(200);
GPIOC ->BSRR = GPIO_Pin_5;
delayMS(200);
GPIOC ->BRR = GPIO_Pin_5;
delayMS(200);
}
}
void delayMS(int ms)
{
TIM_SetCounter(TIM2,0);
TIM_Cmd(TIM2,ENABLE);
while(TIM_GetCounter(TIM2)<ms){
}
TIM_Cmd(TIM2,DISABLE);
}