-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
294 lines (271 loc) · 9.15 KB
/
main.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
#include <math.h>
#include <mcs51/8052.h>
#include <stdio.h>
#include <stdlib.h>
#define Data_Port P2
#define Bep P3_6 // Buzzer
#define DQ P3_7 // Data pin of the DS18B20
#define RST P0_2
#define CS2 P0_3
#define CS1 P0_4
#define E P0_5
#define RW P0_6
#define RS P0_7
__code char font[][5] = {
{0x00, 0x00, 0x00, 0x00, 0x00}, // 20 (Space)
{0x7e, 0x11, 0x11, 0x11, 0x7e}, // 41 A
{0x7f, 0x49, 0x49, 0x49, 0x36}, // 42 B
{0x3e, 0x41, 0x41, 0x41, 0x22}, // 43 C
{0x7f, 0x41, 0x41, 0x22, 0x1c}, // 44 D
{0x7f, 0x49, 0x49, 0x49, 0x41}, // 45 E
{0x7f, 0x09, 0x09, 0x09, 0x01}, // 46 F
{0x3e, 0x41, 0x49, 0x49, 0x7a}, // 47 G
{0x7f, 0x08, 0x08, 0x08, 0x7f}, // 48 H
{0x00, 0x41, 0x7f, 0x41, 0x00}, // 49 I
{0x20, 0x40, 0x41, 0x3f, 0x01}, // 4a J
{0x7f, 0x08, 0x14, 0x22, 0x41}, // 4b K
{0x7f, 0x40, 0x40, 0x40, 0x40}, // 4c L
{0x7f, 0x02, 0x0c, 0x02, 0x7f}, // 4d M
{0x7f, 0x04, 0x08, 0x10, 0x7f}, // 4e N
{0x3e, 0x41, 0x41, 0x41, 0x3e}, // 4f O
{0x7f, 0x09, 0x09, 0x09, 0x06}, // 50 P
{0x3e, 0x41, 0x51, 0x21, 0x5e}, // 51 Q
{0x7f, 0x09, 0x19, 0x29, 0x46}, // 52 R
{0x46, 0x49, 0x49, 0x49, 0x31}, // 53 S
{0x01, 0x01, 0x7f, 0x01, 0x01}, // 54 T
{0x3f, 0x40, 0x40, 0x40, 0x3f}, // 55 U
{0x1f, 0x20, 0x40, 0x20, 0x1f}, // 56 V
{0x3f, 0x40, 0x38, 0x40, 0x3f}, // 57 W
{0x63, 0x14, 0x08, 0x14, 0x63}, // 58 X
{0x07, 0x08, 0x70, 0x08, 0x07}, // 59 Y
{0x61, 0x51, 0x49, 0x45, 0x43} // 5a Z
};
// Delay func (not so accurate)
void delay_us(unsigned char t) {
while (t--)
;
}
void delay(unsigned int k) {
int i, j;
for (i = 0; i < k; i++)
for (j = 0; j < 112; j++)
;
}
void GLCD_Command(char Command) {
Data_Port = Command; /* Copy command on data pin */
RS = 0; /* Make RS LOW to select command register */
RW = 0; /* Make RW LOW to select write operation */
E = 1; /* Make HIGH to LOW transition on Enable */
delay_us(1);
E = 0;
delay_us(1);
}
void GLCD_Data(char Data) {
Data_Port = Data; /* Copy data on data pin */
RS = 1; /* Make RS HIGH to select data register */
RW = 0; /* Make RW LOW to select write operation */
E = 1; /* Make HIGH to LOW transition on Enable */
delay_us(1);
E = 0;
delay_us(1);
}
void GLCD_Init(void) {
CS1 = 1;
CS2 = 1; /* Select left & right half of display */
RST = 1; /* Keep reset pin high */
delay_us(20);
GLCD_Command(0x3E); /* Display OFF */
GLCD_Command(0x40); /* Set y address (column=0) */
GLCD_Command(0xB8); /* Set x address (page=0) */
GLCD_Command(0xC0); /* Set z address (start line=0) */
GLCD_Command(0x3F); /* Display ON */
}
void GLCD_ClearAll(void) {
int i, j;
CS1 = 1;
CS2 = 1; /* Select left & right half of display */
for (i = 0; i < 8; i++) {
GLCD_Command((0xB8) + i); /* Increment page */
for (j = 0; j < 64; j++) {
GLCD_Data(0); /* Write zeros to all 64 column */
}
}
GLCD_Command(0x40); /* Set Y address (column=0) */
GLCD_Command(0xB8); /* Set X address (page=0) */
}
void GLCD_Plot_Coordinate_Axes(void) {
int i, j;
unsigned int Page = ((0xB8) + 2), Y_address = 0;
float Page_inc = 0.5;
// Draw Y-axis
CS1 = 1;
CS2 = 0; /* Select right half of display */
for (i = 0; i < 8; i++) {
GLCD_Command((0xB8) + i); /* Increment page */
if (i >= 2) {
GLCD_Command(0x40 + 5);
if (i < 7)
GLCD_Data(0xFF);
else
GLCD_Data(0x3F);
}
}
// Arrow
GLCD_Command(0xB8 + 2); /* Select Page 3 (left) */
GLCD_Command(0x40 + 5 - 2); /* Select Column 1 */
GLCD_Data(0x04);
GLCD_Command(0x40 + 5 - 1); /* Select Column 2 */
GLCD_Data(0x02);
GLCD_Command(0x40 + 5 + 1); /* Select Column 1 */
GLCD_Data(0x02);
GLCD_Command(0x40 + 5 + 2); /* Select Column 2 */
GLCD_Data(0x04);
// Draw X-axis
CS1 = 1;
CS2 = 0; /* Select left half of display */
GLCD_Command((0xB8) + 7);
GLCD_Command(0x40 + 6); /* Set Y address (column=1) */
for (j = 6; j < 64; j++) {
GLCD_Data(0x20);
}
CS1 = 0;
CS2 = 1; /* Select right half of display */
GLCD_Command((0xB8) + 7);
GLCD_Command(0x40); /* Set Y address (column=1) */
for (j = 0; j < 32; j++) {
GLCD_Data(0x20);
}
GLCD_Command(0x40 + 30); /* Select Column 1 */
GLCD_Data(0x70);
GLCD_Command(0x40 + 29);
GLCD_Data(0xA8);
}
void GLCD_WriteChar(unsigned char page_no) {
unsigned int column, Page = 0xB8, Y_address = 0;
float Page_inc = 0.5;
CS1 = 1;
CS2 = 0;
GLCD_Command((0xB8) + page_no);
GLCD_Command(0x40);
for (column = 0; column < 5; column++) {
GLCD_Data(font[1][column]);
if ((Y_address + 1) % 64 == 0) {
CS1 = !CS1;
CS2 = !CS2;
GLCD_Command((Page + Page_inc));
Page_inc = Page_inc + 0.5;
}
Y_address++;
}
}
//------------------------------------------------------------------
// Hàm trễ dành cho DS18B20 (không quá chính xác)
void delay_us_DS18B20(unsigned int time) {
// Trễ us
while (time--)
;
}
// Khởi tạo cảm biến
void init_DS18B20() {
DQ = 1;
delay_us_DS18B20(10);
// Đặt lại xung trong một khoảng thời gian tối thiểu.
DQ = 0;
delay_us_DS18B20(65);
DQ = 1; // Cạnh tăng
delay_us_DS18B20(20); // Đợi DS18B20 phản hồi
}
// Đọc 1 byte từ Scratchpad
unsigned char readByteFromScratchpad() {
unsigned char j = 0;
unsigned char byte = 0;
for (j = 8; j > 0; j--) {
DQ = 0;
byte >>= 1;
DQ = 1; // The master bus releases the 1-Wire bus
if (DQ) byte |= 0x80; // Read 1? Otherwise, Read 0
delay_us_DS18B20(20);
}
return (byte);
}
// Ghi 1 byte vào Scratchpad
void writeByteToScratchpad(unsigned char byte) {
unsigned char j = 0;
for (j = 8; j > 0; j--) {
DQ = 0;
DQ = byte & 0x01;
// delay_us_DS18B20(5);
if (DQ) // Write 1
delay_us_DS18B20(3);
else // Write 0
delay_us_DS18B20(15);
DQ = 1; // The master bus releases the 1-Wire bus
byte >>= 1;
}
}
// Hàm đọc nhiệt độ lưu vào biến temp
int read_temp_DS18B20() {
unsigned char tmp = 0;
unsigned char Byte0 = 0; // lưu trữ byte 0 của Scratchpad
unsigned char Byte1 = 0; // lưu trữ byte 1 của Scratchpad
EA = 0; // Nếu có ngắt thì cấm ngắt để tránh ảnh hưởng quá trình giao tiếp
init_DS18B20(); // Khởi tạo cảm biến DS18B20
writeByteToScratchpad(0xCC); // The master issues Skip ROM [CCh] command
writeByteToScratchpad(0x44); // Convert T [44h] command. To initiate a temp
// measurement and A-to-D conversion.
delay_us_DS18B20(10);
init_DS18B20();
writeByteToScratchpad(0xCC); // The master issues Skip ROM [CCh] command
writeByteToScratchpad(0xBE); // Read temp value.
// Read Scratpad [BEh] command.
delay_us_DS18B20(10);
Byte0 = readByteFromScratchpad(); // Read Byte0 of the Scratchpad (low byte
// of the temp value)
Byte1 = readByteFromScratchpad(); // Read Byte1 of the Scratchpad (high
// byte of the temp value)
// Calculate the temp value from Byte0 & Byte1 and save it to temp variable
tmp = ((Byte1 * 256 + Byte0) >> 4);
EA = 1; // Đọc xong thì cho phép ngắt
if (tmp <= 128)
return tmp;
else
return tmp - 256;
}
//------------------------------------------------------------------
void main(void) {
int temp, i, pageNum = 7, column = 5, cs = 0;
GLCD_Init(); /* Initialize GLCD */
GLCD_ClearAll();
GLCD_Plot_Coordinate_Axes();
GLCD_WriteChar(0);
delay(500);
while (1) {
pageNum = 7;
temp = read_temp_DS18B20();
if (column == 64) {
column = 0;
CS1 = !CS1;
CS2 = !CS2;
cs = 1;
} else
column++;
if (cs == 1 && column == 12) {
GLCD_ClearAll();
GLCD_Plot_Coordinate_Axes();
GLCD_WriteChar(0);
cs = 0;
column = 6;
delay(500);
}
for (i = temp; i >= 0; i -= 8) {
if (i >= 8)
pageNum--;
else
break;
}
GLCD_Command((0xB8) + pageNum);
GLCD_Command((0x40) + column);
GLCD_Data(0x80 >> i);
delay(1000);
}
}