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
| #include "stm8s.h"
// SO DO KET NOI DEN LCD 16X2 CHE DO 4 BIT
#define LCD_RS PD_ODR_ODR5
#define LCD_EN PD_ODR_ODR6
#define LCD_RW 0
#define LCD_D4 PC_ODR_ODR4
#define LCD_D5 PC_ODR_ODR5
#define LCD_D6 PC_ODR_ODR6
#define LCD_D7 PC_ODR_ODR7
#define line_1 0x80
#define line_2 0xC0
#define clr_scr 0x01
#define DQ PB_ODR_ODR5
#define line_1 0x80
#define line_2 0xC0
unsigned char temp1;
typedef unsigned char uchar;
//********************delay************************
void delay(unsigned long int t);
//********************LCD Funtion***********************
//********************tao xung*******************************************
void enable();
//**********convert data-->send to Pin of microcontrol****************************
void set_data_lsb(unsigned char data); //ham gui byte data thap
void set_data_msb(unsigned char data); //ham gui byte data cao
//************************HAM GUI LENH******************************************
void put_cmd(unsigned char cmd);
//**************************ham gui data*************************************
void lcd_putchar(unsigned char data);
void lcd_putsf(uchar *s);
//****************HTLCD************************************************
//****************************ham khoi tao lcd*************************
void lcd_init();
//************************************************
/*
void main(void) {
CLK_DeInit();
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); //f_Master = HSI/1 = 16MHz
CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV16); //f_CPU = f_Master/1 = 16MHz
while(CLK_GetFlagStatus(CLK_FLAG_HSIRDY)!=SET); //wait until HSI ready
GPIO_DeInit(GPIOC);
GPIO_Init(GPIOC, GPIO_PIN_ALL, GPIO_MODE_OUT_PP_LOW_FAST);
GPIO_DeInit(GPIOD);
GPIO_Init(GPIOD, GPIO_PIN_ALL, GPIO_MODE_OUT_PP_LOW_FAST);
lcd_init();
put_cmd(clr_scr);
delay(100);
put_cmd(line_1);
lcd_putsf("Tran Quoc Tai");
put_cmd(line_2);
lcd_putsf(" Xin chao ban ");
put_cmd(0x10);
delay(1000000);
while(1)
{
lcd_init();
put_cmd(clr_scr);
delay(100);
put_cmd(line_1);
lcd_putsf("Tran Quoc Tai");
put_cmd(line_2);
lcd_putsf(" Xin chao ban ");
put_cmd(0x10);
delay(1000000);
}
}
*/
void main(void){
CLK_DeInit();
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);// fMaster = HSI/1 = 16MHz
CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV16);//fCPU = HSI/1 = 16MHz
while(CLK_GetFlagStatus(CLK_FLAG_HSIRDY)!=SET);// cho den khi HSI bat dau
GPIO_DeInit(GPIOC);
GPIO_Init(GPIOC,GPIO_PIN_ALL,GPIO_MODE_OUT_PP_LOW_FAST);
GPIO_DeInit(GPIOD);
GPIO_Init(GPIOD,GPIO_PIN_ALL,GPIO_MODE_OUT_PP_LOW_FAST);
lcd_init();
put_cmd(clr_scr);
lcd_init();// khoi tao
delay_b20(200);
lcd_putsf("Do nhiet do: ");// gui chuoi
put_cmd(0x01);// clear LCD
lcd_gotoxy(0,1);
lcd_putsf("Tran Quoc Tai");
while(1){
lcd_gotoxy(0,0);
ReadTemperature();
TempShow(temp1);
delay_b20(500);
}
}
// tre ds18b20
int delay_b20(unsigned int ti){
while(ti--);
}
//khoi tao ds18b20
void Init_ds18b20(void){
DQ = 1;
delay_b20(5);
DQ = 0;
delay_b20(10);
DQ = 1;
delay_b20(20);
}
unsigned char ReadOneChar(void){
unsigned char i;
unsigned char dat = 0;
for(i=8;i>0;i++){
DQ = 0;
dat>>=1;
DQ = 1;
if(DQ)
dat |= 0x80;
delay_b20(10);
}
return(dat);
}
void WriteOneChar(unsigned char dat){
unsigned char i;
for(i=8;i>0;i++){
DQ = 0;
DQ = dat&0x01;
delay_b20(5);
DQ = 1;
dat>>=1;
}
}
void ReadTemperature(void){
unsigned char a = 0;
unsigned char b = 0;
//EA = 0;// cam ngat tranh qua trinh doc nhiet do bi anh huong
Init_ds18b20();
WriteOneChar(0xCC);// bo qua ROM
WriteOneChar(0x44);
delay_b20(10);
Init_ds18b20();
WriteOneChar(0xCC);
WriteOneChar(0xBE);// doc nhiet do
delay_b20(10);
a = ReadOneChar();
b = ReadOneChar();
temp1 = ((b*256+a)>>4);// doc nhiet do luu vao bien temp
//EA = 1;// doc xong thi cho phep ngat
}
void TempShow(unsigned char z){// doc nhiet do
lcd_putsf("Nhiet do: ");
lcd_putchar((z/100)+48);// hang tram
lcd_putchar((z%100/10)+48);// hang chuc
lcd_putchar((z%10)+48);// hang don vi
lcd_putsf("oC");
}
//********************delay************************
void delay(unsigned long int t)
{
unsigned long int i=0;
for(i=0;i<t;i++);
}
//********************LCD Funtion***********************
//********************tao xung*******************************************
void enable()
{
GPIO_WriteHigh(GPIOD, GPIO_PIN_6); delay(10);
GPIO_WriteLow(GPIOD, GPIO_PIN_6); delay(20);
}
//**********convert data-->send to Pin of microcontrol****************************
void set_data_lsb(unsigned char data) //ham gui byte data thap
{
unsigned char temp;
temp = (data<<4) & 0xF0;
GPIO_Write(GPIOC,temp);
}
void set_data_msb(unsigned char data) //ham gui byte data cao
{
unsigned char temp;
temp = (data) & 0xF0;
GPIO_Write(GPIOC,temp);
}
//************************HAM GUI LENH******************************************
void put_cmd(unsigned char cmd)
{
set_data_msb(cmd);
GPIO_WriteLow(GPIOD, GPIO_PIN_5);
enable();
set_data_lsb(cmd);
GPIO_WriteLow(GPIOD, GPIO_PIN_5);
enable();
}
//**************************ham gui data*************************************
void lcd_putchar(unsigned char data)
{
set_data_msb(data);
GPIO_WriteHigh(GPIOD, GPIO_PIN_5);
enable();
set_data_lsb(data);
GPIO_WriteHigh(GPIOD, GPIO_PIN_5);
enable();
}
void lcd_putsf(uchar *s)
{
while(*s)
{
lcd_putchar(*s);
s++;
};
}
//****************HTLCD************************************************
//****************************ham khoi tao lcd*************************
void lcd_init()
{
delay(100);
GPIO_WriteLow(GPIOD, GPIO_PIN_5);
set_data_lsb(0x03);
enable();
enable();
enable();
set_data_lsb(0x02);
enable();
put_cmd ( 0x28 ); //cd 4bit,2dong,5x7
put_cmd ( 0x0C); //bat hien thi,tat con tro
put_cmd ( 0x06 ); //* entry mode set, increment & scroll left
put_cmd ( 0x01 ); //* clear display
}
void lcd_gotoxy(unsigned char x, unsigned char y){// toa do
unsigned char address;
if(!y)
address = line_1+x;
else address = line_2+x;// xac dinh vi tri con tro
delay_b20(100);
put_cmd(address);
delay_b20(200);
} |