DS18B20 alarm

General discussion on mikroBasic PRO for PIC.
Post Reply
Author
Message
rexp
Posts: 8
Joined: 11 Dec 2007 05:57

DS18B20 alarm

#1 Post by rexp » 28 Mar 2011 02:24

Does anyone know how to read the alarm from a DS18B20 after the "EC" command is sent.

The data sheet refers to the alarm but have not been able to find how to read it.

Thanks in advance

Rex

rexp
Posts: 8
Joined: 11 Dec 2007 05:57

Re: DS18B20 alarm

#2 Post by rexp » 03 Apr 2011 00:38

Hi

Finally found out how to read an alarm. Below is the code used for the DS18B20 to set up an alarm between 25 and 85 degrees C.

When the read alarm command (ECh) is sent and no alarm is present the data read looks like:
01111111
but when an alarm is present is data read looks like:
11111111

So if there is no alarm the byte will be (FEh) or if an alarm is present it will be (FFh)

Hope this helps save some time for others.

Rex

Code: Select all

                   'Set TH, TL and config values to scratchpad

      Ow_Reset(PORTE, 2)                           'Reset
      Ow_Write(PORTE, 2, 0xCC)                   'Skip ROM
      Ow_Write(PORTE, 2, 0x4E)                   'Write to scratchpad
      Ow_Write(PORTE, 2, 0x55)                   'TH register value  (85 degrees C)
      Ow_Write(PORTE, 2, 0x19)                   'TL register value  (25 degrees C)
      Ow_Write(PORTE, 2, 0x7F)                   'Config register value (12bit)

                             'Do temp conversion

     Ow_Reset(PORTE, 2)                            ' Reset 
     Ow_Write(PORTE, 2, 0xCC)                    'SKIP_ROM
     Ow_Write(PORTE, 2, 0x44)                    ' Issue command CONVERT_T
     Delay_us(120)

                            'Read Temp

     Ow_Reset(PORTE, 2)
     Ow_Write(PORTE, 2, 0xCC)                    'SKIP_ROM
     Ow_Write(PORTE, 2, 0xBE)                    'READ_SCRATCHPAD

     temp =  Ow_Read(PORTE, 2)
     temp = (Ow_Read(PORTE, 2) << 8) + temp

                           'Read Alarm

     Ow_Reset(PORTE, 2)                             'Reset
     Ow_Write(PORTE, 2, 0xEC)                     'Read alarm
     temp5 =  Ow_Read(PORTE, 2)                 'Alarm value
     Delay_us(120)




lorenz2828
Posts: 32
Joined: 29 Sep 2012 17:25

Re: DS18B20 alarm

#3 Post by lorenz2828 » 09 Oct 2012 17:58

sir can U insert this to this code

Code: Select all

// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections

//  Set TEMP_RESOLUTION to the corresponding resolution of used DS18x20 sensor:
//  18S20: 9  (default setting; can be 9,10,11,or 12)
//  18B20: 12
const unsigned short TEMP_RESOLUTION = 12;

char *text = "000.0000";
unsigned temp;

}void Display_Temperature(unsigned int temp2write) {
  const unsigned short RES_SHIFT = TEMP_RESOLUTION - 8;
  char temp_whole;
  unsigned int temp_fraction;

  // Check if temperature is negative
  if (temp2write & 0x8000) {
    text[0] = '-';
    temp2write = ~temp2write + 1;
  }

  // Extract temp_whole
  temp_whole = temp2write >> RES_SHIFT ;

  // Convert temp_whole to characters
  if (text[0] != '-') {
    if (temp_whole/100)
      text[0] = temp_whole/100  + 48;
    else
      text[0] = '0';
  }

  text[1] = (temp_whole/10)%10 + 48;             // Extract tens digit
  text[2] =  temp_whole%10     + 48;             // Extract ones digit

  // Extract temp_fraction and convert it to unsigned int
  temp_fraction  = temp2write << (4-RES_SHIFT);
  temp_fraction &= 0x000F;
  temp_fraction *= 625;

  // Convert temp_fraction to characters
  text[4] =  temp_fraction/1000    + 48;         // Extract thousands digit
  text[5] = (temp_fraction/100)%10 + 48;         // Extract hundreds digit
  text[6] = (temp_fraction/10)%10  + 48;         // Extract tens digit
  text[7] =  temp_fraction%10      + 48;         // Extract ones digit

  // Print temperature on LCD
  Lcd_Out(2, 5, text);
}


void main() {
  ADCON1 = 0b00000110;                            // all ADC ports digital
  TRISE.B2 = 1;                                    // Configure RE2 pin as input


  Lcd_Init();                                    // Initialize LCD
  Lcd_Cmd(_LCD_CLEAR);                           // Clear LCD
  Lcd_Cmd(_LCD_CURSOR_OFF);                      // Turn cursor off
  Lcd_Out(1, 1, " Temperature:   ");
  Lcd_Chr(2,13,223);                             // Print degree character, 'C' for Centigrades
                                                 // Different LCD displays have different char code for degree
                                                 // If you see greek alpha letter try typing 178 instead of 223
  Lcd_Chr(2,14,'C');


  // Main loop
  do {
    // Perform temperature reading
    Ow_Reset(&PORTE, 2);                         // Onewire reset signal
    Ow_Write(&PORTE, 2, 0xCC);                   // Issue command SKIP_ROM
    Ow_Write(&PORTE, 2, 0x44);                   // Issue command CONVERT_T
    Delay_us(120);

    Ow_Reset(&PORTE, 2);
    Ow_Write(&PORTE, 2, 0xCC);                   // Issue command SKIP_ROM
    Ow_Write(&PORTE, 2, 0xBE);                   // Issue command READ_SCRATCHPAD

    temp =  Ow_Read(&PORTE, 2);
    temp = ((Ow_Read(&PORTE, 2)) << 8) + temp;

    // Format and display result on Lcd
    Display_Temperature(temp);
    Delay_ms(500);

  }  while (1);
}
coz i do have a problem setting an alarm...
i really wanted it to alarm on 38 degrees.... pls help me.....
Last edited by lorenz2828 on 10 Oct 2012 01:51, edited 1 time in total.

Fakir
Posts: 102
Joined: 13 Jul 2012 08:33
Location: Czech Republic

Re: DS18B20 alarm

#4 Post by Fakir » 09 Oct 2012 23:23

Hi guys,
I appologize for my post being a bit off topic, but it is relevant and this topic reminded me to ask a question I carry in my mind for some time now:

What is the practical use for these on-sensor alarms? I've built several devices using 18B20s, but never found out, actually. Since you still have to poll the sensor to find out if the alarm treshold has been reached, you might as well read the temperature and check it in software.

I'd appreciate any idea, because I really don't get it this time. :)

Thanks.

Tom

lorenz2828
Posts: 32
Joined: 29 Sep 2012 17:25

Re: DS18B20 alarm

#5 Post by lorenz2828 » 10 Oct 2012 01:53

sir did you make a temperature sensor with alarm without using the alarm system in the sensor???
if u did can u help me???

dario-ri
Posts: 30
Joined: 23 Oct 2010 22:09

Re: DS18B20 alarm

#6 Post by dario-ri » 10 Oct 2012 12:14

Code: Select all

  if (temp >=38{
                   your code what to do 
      }

Fakir
Posts: 102
Joined: 13 Jul 2012 08:33
Location: Czech Republic

Re: DS18B20 alarm

#7 Post by Fakir » 10 Oct 2012 21:06

Exactly.

It's basically just comparing two numbers. Read the temperature value and then compare the value to any "alarm" value you have set in your software, either constant or variable.


An example in BASIC:

Code: Select all

if Temperature >= Alarm then

    ... your code ...

end if

Post Reply

Return to “mikroBasic PRO for PIC General”