-
Notifications
You must be signed in to change notification settings - Fork 8
/
LCD.c
403 lines (376 loc) · 7.6 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#include <reg51.h>
#include <intrins.h>
#include <stdio.h>
typedef unsigned int uint;
typedef unsigned char uchar;
/********************引脚定义********************/
#define LCD_DATA P0
sbit LCD_RS = P2^0;
sbit LCD_RW = P2^1;
sbit LCD_EN = P2^2;
sbit KEY_1 = P1^7; //功能键
sbit KEY_2 = P1^4; //减少键
sbit KEY_3 = P1^2; //增加按键
sbit DS18B20 = P2^6;
/************************************************/
/********************全局变量********************/
#define KeyTime 200 //200 * 1ms = 200ms
#define DispTime 100 //100 * 1ms = 100ms
#define UartTime 150 //150 * 1ms = 150ms
volatile uchar KEY_1ms = 0;
volatile uchar Disp_1ms = 0;
volatile uchar Uart_1ms = 0;
volatile uchar Menu = 0; //0(显示当前温度), 1(设置最低温度), 2(设置最低温度)
volatile int MaxTemp = 40;
volatile int MinTemp = -10;
volatile float Temperature = 0;
/************************************************/
/********************函数声明********************/
void Delay_us(uchar x);
void Delay_ms(uchar x);
void Timer0Init(void);
void KEY_Proc(void);
void DispTemp(void);
void LCD_WriteCommand(uchar com);
void LCD_WriteData(uchar dat);
void LCD_Init(void);
void LCD_Show_Home(void);
void LCD_Show_Setting(void);
void DS18B20_Init(void);
uchar DS18B20_ReadByte(void);
void DS18B20_WriteByte(uchar dat);
float DS18B20_ReadTmp(void);
void UartInit(void);
void Uart_Proc(void);
/************************************************/
/*********************主函数*********************/
int main(void)
{
LCD_Init();
Temperature = DS18B20_ReadTmp();
Delay_ms(200);
Delay_ms(200);
UartInit();
Timer0Init();
LCD_Show_Home();
while(1)
{
DispTemp();
KEY_Proc();
Uart_Proc();
}
}
/************************************************/
/*****************延时函数******************/
void Delay_us(uchar x) //@12.000MHz
{
while(x--)
{
_nop_();
_nop_();
_nop_();
_nop_();
}
}
void Delay_ms(uchar x) //@12.000MHz
{
uchar i, j;
while(x--)
{
_nop_();
_nop_();
i = 12;
j = 169;
do
{
while (--j);
} while (--i);
}
}
/************************************************/
/*******************计数器0模块*******************/
void Timer0Init(void)
{
TMOD |= 0x01; //选择为定时器0模式,工作方式1,仅用TR0打开启动。
TH0 = 0xFC; //给定时器赋初值,定时1ms
TL0 = 0x18;
ET0 = 1; //打开定时器0中断允许
EA = 1; //打开总中断
TR0 = 1; //打开定时器
}
void Timer0(void) interrupt 1
{
TH0 = 0xFC; //给定时器赋初值,定时1ms
TL0 = 0x18;
KEY_1ms ++;
Disp_1ms ++;
Uart_1ms ++;
}
/************************************************/
/*****************按键模块******************/
void KEY_Proc(void)
{
if (KEY_1ms >= KeyTime)
{
KEY_1ms = 0;
if (KEY_1 == 0)
{
Menu ++;
LCD_Show_Setting();
if (Menu == 3)
{
Menu = 0;
LCD_Show_Home();
}
}
else if (KEY_2 == 0)
{
if (Menu == 1)
{
MinTemp --;
}
else if (Menu == 2)
{
MaxTemp --;
if ((MaxTemp - 1) == MinTemp)
{
MaxTemp ++;
}
}
}
else if (KEY_3 == 0)
{
if (Menu == 1)
{
MinTemp ++;
if ((MaxTemp - 1) == MinTemp)
{
MinTemp --;
}
}
else if (Menu == 2)
{
MaxTemp ++;
}
}
}
}
/************************************************/
/*****************温度显示函数******************/
void DispTemp(void)
{
if (Disp_1ms >= DispTime)
{
uchar i;
uchar Disp[6] = " ";
uchar Disp1[4] = " ";
Disp_1ms = 0;
if (Menu == 0)
{
LCD_WriteCommand(0xC7);
Temperature = DS18B20_ReadTmp();
sprintf(Disp, "%.1f", Temperature);
for(i=0; i<6; i++)
{
LCD_WriteData(Disp[i]);
}
}
else if (Menu == 1)
{
LCD_WriteCommand(0x80);
LCD_WriteData('>');
LCD_WriteCommand(0xC0);
LCD_WriteData(' ');
LCD_WriteCommand(0x8B);
sprintf(Disp, "%d", MinTemp);
for(i=0; i<4; i++)
{
LCD_WriteData(Disp[i]);
}
}
else if (Menu == 2)
{
LCD_WriteCommand(0x80);
LCD_WriteData(' ');
LCD_WriteCommand(0xC0);
LCD_WriteData('>');
LCD_WriteCommand(0xCB);
sprintf(Disp, "%d", MaxTemp);
for(i=0; i<4; i++)
{
LCD_WriteData(Disp[i]);
}
}
}
}
/************************************************/
/*******************LCD模块*******************/
void LCD_WriteCommand(uchar com)
{
LCD_EN = 0; //使能
LCD_RS = 0; //选择发送命令
LCD_RW = 0; //选择写入
LCD_DATA = com; //放入命令
Delay_us(1); //等待数据稳定
LCD_EN = 1; //写入时序
Delay_us(5); //保持时间
LCD_EN = 0;
}
void LCD_WriteData(uchar dat)
{
LCD_EN = 0; //使能清零
LCD_RS = 1; //选择输入数据
LCD_RW = 0; //选择写入
LCD_DATA = dat; //写入数据
Delay_us(1);
LCD_EN = 1; //写入时序
Delay_us(5); //保持时间
LCD_EN = 0;
}
void LCD_Init(void)
{
LCD_WriteCommand(0x38); //开显示
LCD_WriteCommand(0x0C); //开显示不显示光标
LCD_WriteCommand(0x06); //写一个指针加1
LCD_WriteCommand(0x01); //清屏
LCD_WriteCommand(0x80); //设置数据指针起点
}
void LCD_Show_Home(void)
{
uchar i;
uchar Disp[16];
LCD_WriteCommand(0x80);
sprintf(Disp, " Current Temp: ");
for(i=0; i<16; i++)
{
LCD_WriteData(Disp[i]);
}
LCD_WriteCommand(0xC0);
sprintf(Disp, " Temp: C ");
for(i=0; i<16; i++)
{
LCD_WriteData(Disp[i]);
}
LCD_WriteCommand(0xCD);
LCD_WriteData(0xDF);
}
void LCD_Show_Setting(void)
{
uchar i;
uchar Disp[16];
LCD_WriteCommand(0x80);
sprintf(Disp, " Min Temp: %d ", MinTemp);
for(i=0; i<16; i++)
{
LCD_WriteData(Disp[i]);
}
LCD_WriteCommand(0xC0);
sprintf(Disp, " Max Temp: %d ", MaxTemp);
for(i=0; i<16; i++)
{
LCD_WriteData(Disp[i]);
}
}
/************************************************/
/*******************DS18B20模块*******************/
void DS18B20_Init(void)
{
DS18B20 = 1;
Delay_us(1);
DS18B20 = 0;
Delay_us(40);
DS18B20 = 1;
Delay_us(11);
}
uchar DS18B20_ReadByte(void)
{
uchar i,dat=0;
DS18B20 = 1;
for(i=0;i<8;i++)
{
DS18B20 = 1;
Delay_us(1);
DS18B20 = 0;
dat >>= 1;
DS18B20 = 1;
if(DS18B20)
dat |= 0X80;
Delay_us(2);
}
return dat;
}
void DS18B20_WriteByte(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
DS18B20 = 0;
DS18B20 = dat& 0x01;
Delay_us(2);
DS18B20 = 1;
dat >>= 1;
}
Delay_us(2);
}
float DS18B20_ReadTmp(void)
{
float Temp;
uint Temp_Value[]={0, 0};
DS18B20_Init();
DS18B20_WriteByte(0xCC);
DS18B20_WriteByte(0x44);
Delay_us(20);
DS18B20_Init();
DS18B20_WriteByte(0xCC);
DS18B20_WriteByte(0xBE);
Temp_Value[0] = DS18B20_ReadByte();
Temp_Value[1] = DS18B20_ReadByte();
if((Temp_Value[1]&0xF8)==0xF8)
{
Temp = -1 * (128 - 0.0625 * (Temp_Value[0] | ((Temp_Value[1]&0x07)<<8)));
}
else
{
Temp = 0.0625 * (Temp_Value[0] | ((Temp_Value[1]&0x07)<<8));
}
return Temp;
}
/************************************************/
/*******************串口通信模块*******************/
void UartInit(void)
{
SCON = 0x50; //设置为工作方式1
TMOD |= 0x20; //设置计数器工作方式2
PCON = 0x80; //波特率加倍
TH1=0xF3; //计数器初始值设置,注意波特率是4800的
TL1=0xF3;
// ES=1; //打开接收中断
// EA=1; //打开总中断
TR1=1; //打开计数器
}
void Uart_Proc(void)
{
// if (Uart_1ms >= UartTime)
// {
// Uart_1ms = 0;
if (Temperature < MinTemp)
{
SBUF = 'A';
while(!TI);
TI = 0;
}
else if (Temperature > MaxTemp)
{
SBUF = 'B';
while(!TI);
TI = 0;
}
else
{
SBUF = 'O';
while(!TI);
TI = 0;
}
// }
}
/************************************************/