Help I need to compare DS1820 with other values

Discussion in 'Embedded Systems and Microcontrollers' started by mido_ameer, Aug 13, 2012.

  1. mido_ameer

    Thread Starter New Member

    Jul 31, 2011
    3
    0
    I recently finished a program that uses DS1820 temperature sensor with PIC16F877 to measure its whole range -ve and +ve.
    But I don't know how to read a temp. in the code itself, as I just (copy & paste it) slightly modified it from the internet to be able to run on PIC16F877 (I tried the code in MikroC program's Help, but didn't work on Proteus ISIS) .
    I want (porta.b5 = 1) when the temp. is +5°C, else porta.b5=0
    The problem is I don't understand how the code is running, I tried (text[2] == 5) porta.b5=1, and it didn't work (as text is an array of char)

    Here is the code:

    Code ( (Unknown Language)):
    1.  
    2. [FONT=Fixedsys]sbit LCD_RS at RC4_bit;
    3. sbit LCD_EN at RC5_bit;
    4. sbit LCD_D4 at RC0_bit;
    5. sbit LCD_D5 at RC1_bit;
    6. sbit LCD_D6 at RC2_bit;
    7. sbit LCD_D7 at RC3_bit;
    8. sbit LCD_RS_Direction at TRISC4_bit;
    9. sbit LCD_EN_Direction at TRISC5_bit;
    10. sbit LCD_D4_Direction at TRISC0_bit;
    11. sbit LCD_D5_Direction at TRISC1_bit;
    12. sbit LCD_D6_Direction at TRISC2_bit;
    13. sbit LCD_D7_Direction at TRISC3_bit;
    14. unsigned short C_Neg=0;
    15. char *text = "000.0000";
    16. unsigned temp, templ, temph;
    17. unsigned int temp2write, tempinc, temp_fraction;
    18. char temp_whole;
    19. void main()
    20. {
    21.  trisc = 0;
    22.  portc = 0;
    23.  trisa = 1;
    24.  porta.b0 = 0;
    25.  ADCON1 = 7; // Configure AN pins as digital I/O
    26.  Lcd_Init(); // Lcd_Init_EP4, see Autocomplete
    27.  Lcd_Cmd(_LCD_CURSOR_OFF);
    28.  Lcd_Out(1, 1, " Temperature: ");
    29.  // Print degree character, 'C' for Centigrades
    30.  Lcd_Chr(2,13,223);
    31.  Lcd_Chr(2,14,'C');
    32.     OPTION_REG = 0x00;   // Clear INTEDG, External Interrupt on falling edge
    33.     INTCON.INTF = 0;     // Clear interrupt flag prior to enable
    34.     INTCON.INTE = 1;     // enable INT interrupt
    35.     INTCON.GIE  = 1;     // enable Global interrupts
    36.     //--- main loop
    37.     while (1)
    38.     {
    39.     //--- perform temperature reading
    40.     Ow_Reset(&PORTa,0); // Onewire reset signal
    41.     Ow_Write(&porta,0,0xCC); // Issue command SKIP_ROM
    42.     Ow_Write(&porta,0,0x44); // Issue command CONVERT_T
    43.     INTCON.GIE  = 1;     // 1-wire library disables interrpts
    44.     Delay_ms(750);
    45.     Ow_Reset(&porta,0);
    46.     Ow_Write(&porta,0,0xCC); // Issue command SKIP_ROM
    47.     Ow_Write(&porta,0,0xBE); // Issue command READ_SCRATCHPAD
    48.     templ = Ow_Read(&porta,0);
    49.     TempH = Ow_Read(&PORTA, 0);
    50.     temp = (temph << 8) + templ;   //temp(temp_value)
    51.      // check if temperature is negative
    52.     if (temp & 0x8000)
    53.     {
    54.       C_Neg = 1;
    55.       text[0] = '-';  //text(tempc)
    56.       temp = ~temp + 1;     //temp(temp_value)
    57.     }
    58.     else C_Neg = 0;
    59.     // extract temp_whole
    60.     temp_whole = temp >> 1;
    61.     if (temp & 0x0001)
    62.     {  // LSB is 0.5C
    63.        temp_fraction = 5;
    64.     }
    65.     else temp_fraction = 0;
    66.     tempinc = temp_whole*10+temp_fraction;
    67.     // convert temp_whole to characters
    68.    if (!C_Neg)
    69.    {
    70.     if (tempinc/1000) text[0] = tempinc/1000 +48;
    71.     else text[0] = ' ';
    72.    }
    73.    text[1] = (tempinc/100)%10 + 48; // extract tens digit
    74.    text[2] = (tempinc/10)%10 + 48; // extract ones digit
    75.    text[4] =  tempinC%10  + 48;
    76.    Lcd_Out(2, 5, text);
    77.  }
    78. }
    79. [/FONT]

     
    Last edited by a moderator: Aug 13, 2012
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    it looks like the variable temp_whole. Holds the value you are looking for. If this value is greater or equal to 5 turn on your LED.
     
  3. mido_ameer

    Thread Starter New Member

    Jul 31, 2011
    3
    0
    I tried that but nothing happens, and I tried to write it on the LCD and nothing appears.
    I need to covert array(text) to a variable with its sign, then convert it to string to be displayed on LCD.
     
Loading...