DS18b20 problems.

Discussion in 'Embedded Systems and Microcontrollers' started by tryingtolearn, Feb 4, 2011.

  1. tryingtolearn

    Thread Starter New Member

    Apr 4, 2010
    17
    0
    Hello, I am having problems with setting the alarm function for a ds18b20 on my 16f887. I kinda understand how to write the code to write to the scratch pad and search for an alarm. I don't however understand the format of what I am to enter. Say I want the high at 26C and the low at 18C.

    So I tried writing an if statement for the pic to do the comparison but can't get it to actually compare and send port pin high/low.

    Any help in either of these methods would be much appreciated.

    Code ( (Unknown Language)):
    1.  
    2. sbit LCD_RS at RC2_bit;
    3. sbit LCD_EN at RC7_bit;
    4. sbit LCD_D4 at RD4_bit;
    5. sbit LCD_D5 at RD5_bit;
    6. sbit LCD_D6 at RD6_bit;
    7. sbit LCD_D7 at RD7_bit;
    8.  
    9. sbit LCD_RS_Direction at TRISC2_bit;
    10. sbit LCD_EN_Direction at TRISC7_bit;
    11. sbit LCD_D4_Direction at TRISD4_bit;
    12. sbit LCD_D5_Direction at TRISD5_bit;
    13. sbit LCD_D6_Direction at TRISD6_bit;
    14. sbit LCD_D7_Direction at TRISD7_bit;
    15. // End LCD module connections
    16.  
    17. //  Set TEMP_RESOLUTION to the corresponding resolution of used DS18x20 sensor:
    18. //  18S20: 9  (default setting; can be 9,10,11,or 12)
    19. //  18B20: 12
    20. const unsigned short TEMP_RESOLUTION = 12;
    21.  
    22. char *text = "000.0000";
    23. unsigned temp;
    24.  
    25.  
    26. void Relay () {
    27. if (*text <= 18)
    28. RA6_bit = 0xff;
    29. else
    30. RA6_bit = 0x00;
    31. }
    32.  
    33. void Display_Temperature(unsigned int temp2write) {
    34.   const unsigned short RES_SHIFT = TEMP_RESOLUTION - 8;
    35.   char temp_whole;
    36.   unsigned int temp_fraction;
    37.  
    38.  
    39.  
    40.   // Check if temperature is negative
    41.   if (temp2write & 0x8000) {
    42.      text[0] = '-';
    43.      temp2write = ~temp2write + 1;
    44.      }
    45.  
    46.   // Extract temp_whole
    47.   temp_whole = temp2write >> RES_SHIFT ;
    48.  
    49.   // Convert temp_whole to characters
    50.   if (temp_whole/100)
    51.      text[0] = (temp_whole/100  + 48);
    52.   else
    53.      text[0] = '0';
    54.  
    55.   text[1] = (temp_whole/10)%10 + 48;             // Extract tens digit
    56.   text[2] =  temp_whole%10     + 48;             // Extract ones digit
    57.  
    58.   // Extract temp_fraction and convert it to unsigned int
    59.   temp_fraction  = temp2write << (4-RES_SHIFT);
    60.   temp_fraction &= 0x000F;
    61.   temp_fraction *= 625;
    62.  
    63.   // Convert temp_fraction to characters
    64.   text[4] =  temp_fraction/1000    + 48;         // Extract thousands digit
    65.   text[5] = (temp_fraction/100)%10 + 48;         // Extract hundreds digit
    66.   text[6] = (temp_fraction/10)%10  + 48;         // Extract tens digit
    67.   text[7] =  temp_fraction%10      + 48;         // Extract ones digit
    68.  
    69.  
    70.   // Print temperature on LCD
    71.   Lcd_Out(2, 5, text);
    72.  
    73.  
    74.  
    75. }
    76.  
    77.  
    78.  
    79.  
    80.  
    81. void main() {
    82.   ANSEL  = 0;                                    // Configure AN pins as digital I/O
    83.   ANSELH = 0;
    84.   C1ON_bit = 0;                                  // Disable comparators
    85.   C2ON_bit = 0;
    86.  
    87.   TRISA = 0x00;
    88.   PORTA = 0x00;
    89.   Lcd_Init();                                    // Initialize LCD
    90.   Lcd_Cmd(_LCD_CLEAR);                           // Clear LCD
    91.   Lcd_Cmd(_LCD_CURSOR_OFF);                      // Turn cursor off
    92.   Lcd_Out(1, 1," Temperature:   ");              //" Temperature:   "
    93.   // Print degree character, 'C' for Centigrades
    94.   Lcd_Chr(2,13,223);                             // Different LCD displays have different char code for degree
    95.                                                  // If you see greek alpha letter try typing 178 instead of 223
    96. //  Lcd_Chr(2,16,'F');
    97.   Lcd_Chr(2,14,'C');
    98.  
    99.   //--- Main loop
    100.   do {
    101.     //--- Perform temperature reading
    102.     Ow_Reset(&PORTE, 2);                         // Onewire reset signal
    103.     Ow_Write(&PORTE, 2, 0xCC);                   // Issue command SKIP_ROM
    104.     Ow_Write(&PORTE, 2, 0x44);                   // Issue command CONVERT_T
    105.     Delay_us(120);
    106.  
    107.     Ow_Reset(&PORTE, 2);
    108.     Ow_Write(&PORTE, 2, 0xCC);                   // Issue command SKIP_ROM
    109.     Ow_Write(&PORTE, 2, 0xBE);                   // Issue command READ_SCRATCHPAD
    110.  
    111.     temp =  Ow_Read(&PORTE, 2);
    112.     temp = (Ow_Read(&PORTE, 2) << 8) + temp;
    113.  
    114.     //--- Format and display result on Lcd
    115.     Display_Temperature(temp);
    116.     Relay;
    117.  
    118.     Delay_ms(500);
    119.  
    120.   } while (1);
    121. }
    122.  
     
  2. tryingtolearn

    Thread Starter New Member

    Apr 4, 2010
    17
    0
    I figured it out. Its probably a lot more work than what is needed but I got it working and thats what I was after. It turns relay on in between 23.5c and 27c. Now to figure Fahrenheit conversion out.

    Added the following code to make it work:

    Code ( (Unknown Language)):
    1.  
    2. unsigned text100;
    3. unsigned text200;
    4. unsigned text300;
    5. unsigned text400;
    6. unsigned text500;
    7.  
    8. ////existing/////
    9. text[4] =  temp_fraction/1000    + 48;         // Extract thousands digit
    10.   text[5] = (temp_fraction/100)%10 + 48;         // Extract hundreds digit
    11.   text[6] = (temp_fraction/10)%10  + 48;         // Extract tens digit
    12.   text[7] =  temp_fraction%10      + 48;         // Extract ones digit
    13. ////end of existing/////
    14.  
    15. if ((temp_whole/23) )
    16. text100 = 1;
    17. else
    18. text100=0;
    19.  
    20. if ((temp_fraction/5000))
    21. text200 = 1;
    22. else
    23. text200 = 0;
    24.  
    25. if (temp_whole/24)
    26. text300 =2;
    27. else
    28. text300=0;
    29.  
    30. if (temp_whole/27)
    31. text500=-3;
    32. else
    33. text500=0;
    34.  
    35. text400= ((text100+text300+text200+text500));
    36.  
    37. if (text400/2)
    38. RA6_bit = 0xff;
    39. else
    40. RA6_bit = 0x00;
    41.  
     
Loading...