Advertisement
Guest User

dht22_pic24hj

a guest
Oct 28th, 2012
2,371
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Delay function for up to 4552ms at 7.37Mhz FOSC */
  2. void wait_ms(unsigned int wait) {
  3.     float tick = 0;
  4.     unsigned int loopValue = 0;
  5.     T1CON = 0x0030;     // Set prescaler for T1 to 256
  6.     tick = 69*1e-6;      // Calculate tick for given FOSC
  7.     wait = wait * 1e-3;
  8.     loopValue = (unsigned int) ( wait / tick );
  9.     TMR1 = 0;           // Clear T1 registers
  10.     _TON = 1;           // Start T1
  11.     while (TMR1 < loopValue);   // Delay for @wait
  12.     _TON = 0;           // Stop T1
  13. }
  14.  
  15. /* Delay function for up to 142276us at 7.37Mhz FOSC*/
  16. void wait_us(unsigned int wait) {
  17.     float tick = 0;
  18.     unsigned int loopValue = 0;
  19.     T1CON = 0x0010;     // Set prescaler for T1 to 8
  20.     tick = 2*1e-6;      // Calculate tick for given FOSC
  21.     wait = wait * 1e-6;
  22.     loopValue = (unsigned int) ( wait / tick );
  23.     TMR1 = 0;           // Clear T1 registers
  24.     _TON = 1;           // Start T1
  25.     while (TMR1 < loopValue);   // Delay for @wait
  26.     _TON = 0;           // Stop T1
  27. }
  28.  
  29. /* Function to start T1 */
  30. void startTimer() {
  31.     TMR1 = 0;           // Clear T1 registers
  32.     _T1IP = 0x01;       // Set T1 interrupt priority lvl
  33.     _T1IF = 0;          // Clear T1 interrupt flag
  34.     _T1IE = 1;          // Enable T1 interrupt
  35.     T1Flag = 0;         // Flag for T1 overflow
  36.     T1CON = 0x8010;     // Set prescaler for T1 to 8 and start T1
  37. }
  38.  
  39. /* Stop T1 and calculate the signal time. Returns signal time in us. */
  40. unsigned int stopTimer() {
  41.     unsigned int sigTime = 0, TMRValue = 0;
  42.     float tick;
  43.     TMRValue = TMR1;       // Save current T1 register value
  44.     _TON = 0;               // Stop T1
  45.     T1Flag = 0;             // Flag for T1 overflow
  46.     tick = 2;               // Calculate tick for given FOSC
  47.     sigTime = (unsigned int) (TMRValue * tick);
  48.     return sigTime;
  49. }
  50.  
  51. /* Initalises communication with the sensor */
  52. void sendStartSignal() {
  53.     BUSHIGH;             // Set logical bus state to high
  54.     WRITE;              // Bus pin as output
  55.     BUSLOW;              // Set logical bus state to low
  56.     wait_ms(20);        // Wait for at least 18ms
  57.     BUSHIGH;            // Set logical bus state to high
  58.     wait_us(39);        // Wait for at least 20us
  59.     READ;               // Bus pin as input
  60. }
  61.  
  62. /* Listen on the bus for sensor start signal */
  63. unsigned char waitForSensor() {
  64.     startTimer();       // Start T1 to determine sensor start signal time
  65.     while (!DATADHT && !T1Flag);   // Sensor pulls bus low for ~80us
  66.     if (T1Flag) {
  67.         stopTimer();
  68.         return 0;
  69.     }
  70.     else {
  71.         while (DATADHT && !T1Flag);    // Sensor pulls bus high for ~80us
  72.         if (T1Flag) {
  73.             stopTimer();
  74.             PutsXLCD("hier");
  75.             return 0;
  76.         }
  77.         else {
  78.             unsigned int time = stopTimer();    // Stop timer
  79.             if (time >= 160 && !T1Flag)
  80.                 return 1;
  81.             else
  82.                 return 0;
  83.         }
  84.     }
  85. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement