Advertisement
Guest User

Untitled

a guest
Dec 20th, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. sbit Data at RB0_bit;
  2. sbit DataDir at TRISB0_bit;
  3. unsigned short k;
  4.  
  5. unsigned short T_Byte1, T_Byte2, RH_Byte1, RH_Byte2;
  6. char temp[] = "Temperature is 00.0 C";
  7. char hum[] = "Humidity is 00.0 %";
  8.  
  9. void DHT11StartSignal(){
  10. DataDir = 0;
  11. Data = 0;
  12. Delay_ms(25);
  13. Data = 1;
  14. Delay_us(30);
  15. DataDir = 1;
  16. }
  17.  
  18. unsigned short DHT11CheckResponse(){
  19. k = 150;
  20. while(!Data){
  21. Delay_us(2);
  22. k--;
  23. if(k<1) return 0; // time out
  24. }
  25. k = 150;
  26. while(Data){
  27. Delay_us(2);
  28. k--;
  29. if(k<1) return 0; // time out
  30. }
  31. return 1;
  32. }
  33.  
  34.  
  35. unsigned short DHT11ReadByte(){
  36. int i;
  37. unsigned short num = 0;
  38. DataDir = 1;
  39. for (i=0; i<8; i++){
  40. while(!Data);
  41. Delay_us(40);
  42. if(Data) num |= 1<<(7-i);
  43. while(Data);
  44. }
  45. return num;
  46. }
  47.  
  48. void main() {
  49. UART1_Init(9600);
  50. TRISC.RC0 = 0;
  51. TRISC.RC1 = 0;
  52. TRISC.RC2 = 0;
  53.  
  54. while(1){
  55. DHT11StartSignal();
  56. if(!DHT11CheckResponse()) continue;
  57. RH_Byte1 = DHT11ReadByte();
  58. RH_Byte2 = DHT11ReadByte();
  59. T_Byte1 = DHT11ReadByte();
  60. T_Byte2 = DHT11ReadByte();
  61. DHT11ReadByte(); /* Checksum */
  62.  
  63. // Set temp
  64. temp[15] = T_Byte1/10 + 48;
  65. temp[16] = T_Byte1%10 + 48;
  66. temp[18] = T_Byte2/10 + 48;
  67. UART1_Write_Text(temp);
  68. UART1_Write(10);
  69. UART1_Write(13);
  70.  
  71. // Set hum
  72. hum[12] = RH_Byte1/10 + 48;
  73. hum[13] = RH_Byte1%10 + 48;
  74. hum[15] = RH_Byte2/10 + 48;
  75. UART1_Write_Text(hum);
  76. UART1_Write(10);
  77. UART1_Write(13);
  78. UART1_Write(10);
  79. UART1_Write(13);
  80.  
  81. PORTC.RC0 = (RH_Byte1) >= 20;
  82. PORTC.RC1 = (RH_Byte1) >= 30;
  83. PORTC.RC2 = (RH_Byte1) >= 40;
  84.  
  85. // Wait
  86. Delay_ms(1000);
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement