You are on page 1of 8

1: /*

2: Project: Temperature and humidity measurements using DHT


1
3: MCU: PIC16F628A
4: Clock: 4.0MHz external resonator
5: MCLR is enabled
6: Date: Jan 10, 2012
7: Written by: Rajendra Bhatt (www.embedded-lab.com)
8: */
9:
10: // LCD module connections
11: sbit LCD_RS at RB2_bit;
12: sbit LCD_EN at RB3_bit;
13: sbit LCD_D4 at RB4_bit;
14: sbit LCD_D5 at RB5_bit;
15: sbit LCD_D6 at RB6_bit;
16: sbit LCD_D7 at RB7_bit;
17: sbit LCD_RS_Direction at TRISB2_bit;
18: sbit LCD_EN_Direction at TRISB3_bit;
19: sbit LCD_D4_Direction at TRISB4_bit;
20: sbit LCD_D5_Direction at TRISB5_bit;
21: sbit LCD_D6_Direction at TRISB6_bit;
22: sbit LCD_D7_Direction at TRISB7_bit;
23:
24: #define UP RA1_bit
25: #define DOWN RA2_bit
26: #define MENU RA3_bit
27:
28: sbit Data at RA0_bit;
29: sbit DataDir at TRISA0_bit;
30: char message1[] = "Temp = 00.0 C";
31: char message2[] = "RH
= 00.0 %";
32:
33: unsigned short TOUT = 0, CheckSum, i;
34: unsigned short T_Byte1, T_Byte2, RH_Byte1, RH_Byte2;
35:
36: void StartSignal(){
37:
DataDir = 0;
// Data port is output
38:
Data
= 0;
39:
Delay_ms(25);
40:
Data
= 1;
41:
Delay_us(30);
42:
DataDir = 1;
// Data port is input

! "

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:

}
unsigned short CheckResponse(){
TOUT = 0;
TMR2 = 0;
T2CON.TMR2ON = 1;
// start timer
while(!Data && !TOUT);
if (TOUT) return 0;
else {
TMR2 = 0;
while(Data && !TOUT);
if (TOUT) return 0;
else {
T2CON.TMR2ON = 0;
return 1;
}
}
}

unsigned short ReadByte(){


unsigned short num = 0, t;
DataDir = 1;
for (i=0; i<8; i++){
while(!Data);
TMR2 = 0;
T2CON.TMR2ON = 1;
while(Data);
T2CON.TMR2ON = 0;
if(TMR2 > 40) num |= 1<<(7-i); // If time > 40us, Data
is 1
73:
}
74:
return num;
75: }
76:
77: void interrupt(){
78:
if(PIR1.TMR2IF){
79:
TOUT = 1;
80:
T2CON.TMR2ON = 0; // stop timer
81:
PIR1.TMR2IF = 0; // Clear TMR0 interrupt flag
82:
}
83: }
84:

! "

85: void Move_Delay() { // Funo usada para movimento do Texto


o
86: Delay_ms(20); // Velocidade do Movimento
87: }
88:
89: void main() {
90:
unsigned short check;
91:
float temp;
92:
int tela = 0;
93:
int rolagem = 5;
94:
char txt1[15];
95:
char txt2[15];
96:
97:
TRISB = 0b00000000;
98:
PORTB = 0;
99:
TRISA = 0b00111101;
100:
CMCON = 7;
101:
INTCON.GIE = 1;
//Enable global interrupt
102:
INTCON.PEIE = 1;
//Enable peripheral interrupt
103:
// Configure Timer2 module
104:
PIE1.TMR2IE = 1; // Enable Timer2 interrupt
105:
T2CON = 0;
// Prescaler 1:1, and Timer2 is off ini
itially
106:
PIR1.TMR2IF =0;
// Clear TMR INT Flag bit
107:
TMR2 = 0;
108:
Lcd_Init();
109:
Lcd_Cmd(_Lcd_Clear);
110:
Lcd_Cmd(_LCD_CURSOR_OFF);
111:
112:
while(1){
113:
114:
do{
115:
// contador de telas para o menu
116:
if(RA4_bit == 1){
117:
delay_ms(300);
118:
tela++;
119:
120:
if(tela > 3){
121:
tela = 0;
122:
}
123:
}
124:
// fim do contador de telas para o menu
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:

Delay_ms(100);
StartSignal();
check = CheckResponse();
if (!check) {
Lcd_Cmd(_Lcd_Clear);
Lcd_Out(1, 1, "No response");
Lcd_Out(2, 1, "from the sensor");
}
else{
RH_Byte1 = ReadByte();
RH_Byte2 = ReadByte();
T_Byte1 = ReadByte();
T_Byte2 = ReadByte();
CheckSum = ReadByte();
// Check for error in Data reception
if (CheckSum == ((RH_Byte1 + RH_Byte2 + T_Byte1 + T_By
yte2) & 0xFF))
{
message1[7] = T_Byte1/10 + 48;
message1[8] = T_Byte1%10 + 48;
message1[10] = T_Byte2/10 + 48;
message2[7] = RH_Byte1/10 + 48;
message2[8] = RH_Byte1%10 + 48;
message2[10] = RH_Byte2/10 + 48;
message1[11] = 223;
// Degree symbol
Lcd_Cmd(_Lcd_Clear);
Lcd_Out(1, 1, message1);
Lcd_Out(2, 1, message2);
}
else{
Lcd_Cmd(_Lcd_Clear);
Lcd_Out(1, 1, "Checksum Error!");
Lcd_Out(2, 1, "Trying Again ...");
}
}
if(T_Byte1 + T_Byte2 >= temp){
portb.f0=0;
}
if(T_Byte1 + T_Byte2 < temp){
portb.f0=1;
}

! "

168:
169:
170:
171:

}while(tela == 0);
for(i=0; i<16; i++) { // Move o texto para a direita 4x
x

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:

Lcd_Cmd(_LCD_SHIFT_RIGHT);
Move_Delay();
}
if(tela == 1){
Lcd_Cmd(_Lcd_Clear);
while(tela==1){
Lcd_Out(1, 1, "SET TEMPERATURA");
message1[7] = T_Byte1/10 + 48;
message1[8] = T_Byte1%10 + 48;
message1[10] = T_Byte2/10 + 48;
message1[11] = 223;
// Degree symbol
Lcd_Out(2, 1, message1);
delay_ms(50);
if (RA2_bit == 1){
//T_Byte1 ++;
T_Byte2 ++;
T_Byte2 ++;
T_Byte2 ++;
}
if (RA3_bit == 1){
//T_Byte1 --;
T_Byte2 --;
T_Byte2 --;
T_Byte2 --;
}
temp = T_Byte1 + T_Byte2;
if(RA4_bit == 1){
delay_ms(500);
tela++;
for(i=0; i<16; i++) { // Move o texto para a direita
a 4x

207:
208:

Lcd_Cmd(_LCD_SHIFT_RIGHT);
Move_Delay();

! "

209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:

}
}
}
}
if(tela == 2){
Lcd_Cmd(_Lcd_Clear);
while(tela==2){
message2[7] = RH_Byte1/10 + 48;
message2[8] = RH_Byte1%10 + 48;
message2[10] = RH_Byte2/10 + 48;
Lcd_Out(1, 1, "SET HUMIDADE");
Lcd_Out(2, 1, message2);
delay_ms(200);
if(RA4_bit == 1){
delay_ms(500);
tela++;
for(i=0; i<16; i++) { // Move o texto para a dir
reita 4x

228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:

Lcd_Cmd(_LCD_SHIFT_RIGHT);
Move_Delay();
}
}
}
}
if(tela == 3){
Lcd_Cmd(_Lcd_Clear);
while(tela==3){
Lcd_Out(1, 1, "SET ROLAGEM ON");
Lcd_Out(2, 5, "5 SEGUNDOS");
delay_ms(200);
if(RA4_bit == 1){
delay_ms(500);
tela=0;
for(i=0; i<16; i++) { // Move o texto para a dir
reita 4x

248:
249:

Lcd_Cmd(_LCD_SHIFT_RIGHT);
Move_Delay();

! "

250:
251:
252:
253:
}
254:
}
255: }

}
}
}

! "

You might also like