You are on page 1of 8

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

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

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

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

"SET TEMPERATURA"). i<16. i<16. $ ! " . T_Byte2 ++.168: 169: 170: 171: }while(tela == 0). T_Byte2 ++. Move_Delay(). 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). message1[8] = T_Byte1%10 + 48. T_Byte2 --. Move_Delay(). } if(tela == 1){ Lcd_Cmd(_Lcd_Clear). // Degree symbol Lcd_Out(2. T_Byte2 --. 1. T_Byte2 ++. 1. message1). for(i=0. message1[10] = T_Byte2/10 + 48. while(tela==1){ Lcd_Out(1. tela++. T_Byte2 --. message1[11] = 223. delay_ms(50). if (RA2_bit == 1){ //T_Byte1 ++. } temp = T_Byte1 + T_Byte2. for(i=0. message1[7] = T_Byte1/10 + 48. i++) { // Move o texto para a direita a 4x 207: 208: Lcd_Cmd(_LCD_SHIFT_RIGHT). } if (RA3_bit == 1){ //T_Byte1 --. if(RA4_bit == 1){ delay_ms(500).

tela=0. 5. if(RA4_bit == 1){ delay_ms(500). for(i=0. 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). 1. Move_Delay(). 1. while(tela==2){ message2[7] = RH_Byte1/10 + 48. tela++.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). Lcd_Out(2. } } } } if(tela == 3){ Lcd_Cmd(_Lcd_Clear). message2[8] = RH_Byte1%10 + 48. i<16. i++) { // Move o texto para a dir reita 4x 248: 249: Lcd_Cmd(_LCD_SHIFT_RIGHT). Move_Delay(). 1. "SET ROLAGEM ON"). message2[10] = RH_Byte2/10 + 48. message2). Lcd_Out(1. Lcd_Out(2. delay_ms(200). "5 SEGUNDOS"). for(i=0. i<16. ! " . delay_ms(200). while(tela==3){ Lcd_Out(1. "SET HUMIDADE"). if(RA4_bit == 1){ delay_ms(500).

250: 251: 252: 253: } 254: } 255: } } } } ! " .