Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

waiting for input while checking temprature?

Status
Not open for further replies.

dawson

New Member
I currently use the following code to read the temp from a ds1820, I have it displaying the current temp all the time but I have a menu so if a button is pressed it brings up the menu instead of displaying the temp.

The problem is that the read_temp() function has delays so the switch has to be pressed at a certain time, here is the code:

Code:
void read_temp() {
   //--- perform temperature reading
    Ow_Reset(&PORTB, 5);      // Onewire reset signal
    Ow_Write(&PORTB, 5, 0xCC);   // Issue command SKIP_ROM
    Ow_Write(&PORTB, 5, 0x44);   // Issue command CONVERT_T
    Delay_ms(600);
    // If this delay is less than 500ms, you will see the first reading on LCD
    //85C which is (if you remember from my article on DS1820)
    //a power-on-reset value.

    Ow_Reset(&PORTB, 5);
    Ow_Write(&PORTB, 5, 0xCC);    // Issue command SKIP_ROM
    Ow_Write(&PORTB, 5, 0xBE);    // Issue command READ_SCRATCHPAD

    // Read Byte 0 from Scratchpad
    temp_value =  Ow_Read(&PORTB, 5);
    // Then read Byte 1 from Scratchpad and shift 8 bit left and add the Byte 0
    temp_value = (Ow_Read(&PORTB, 5) << 8) + temp_value;

    //--- Format and display result on Lcd
    Display_Temperature(temp_value);
}

void main() {
  CMCON  |= 7;                       // Disable Comparators
  Lcd_Init();                                    // Initialize LCD
  Lcd_Cmd(_LCD_CLEAR);                           // Clear LCD
  Lcd_Cmd(_LCD_CURSOR_OFF);                      // Turn cursor off
  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Out(1, 3, "Temperature:   ");
  // Print degree character, 'C' for Centigrades
  Lcd_Chr(2,11,223);
 // different LCD displays have different char code for degree
 // if you see greek alpha letter try typing 178 instead of 223

  Lcd_Chr(2,12,'C');
  //--- main loop
  do {
    if(Button(&PORTA,2, 1, 0)) {
             read_sw();
             }
 
            read_temp();
          
    } while (1);
}

How can I have it so that it will still loop through checking the temp but will goto the menu as soon as the switch is pressed?

Edit: Forgot to mention Im using the pic16f628
 
Last edited:

dawson

New Member
uC? Im using pic16f628. I have had a look for interrupts but cant really find much can anyone help?
 

Mr RB

Well-Known Member
Just remove (or shorten) the 600mS delay. If you read the scratchpad too soon (before conversion is completed) if just reads the previous scratchpad value. And if you tell it do a conversion while it is still doing one it will ignore the new request.
 

Diver300

Well-Known Member
Most Helpful Member
When reading the DS18B20, the simplest routine is something like this:-

Code:
[U]Microcontroller[/U]				[U]DS18B20[/U]
Reset 					Presence pulse
Skip Rom command (0xCC)      
Convert Temp (0x44)
Wait for 750 ms
Reset 					Presence pulse
Skip Rom command (0xCC)
Read scratchpad (0xBE) 			Temperature

(You don't have to read the rest of the scratchpad. The microcontroller can stop after it has read the temperature)

The point is that the routine can be split into 3 parts:-
1) The communication bit before the 750 ms pause
2) The 750 ms pause
3) The communication bit after the 750 ms pause

The two communication parts are very quick. They are so quick that it doesn't matter if you don't look at the pushbutton during that time.

The 750 ms pause is what makes the pushbutton not respond. If you make the pause code read the pushbutton lots of times, it will still be a perfectly good pause. It really doesn't matter if you pause a bit too long. If the pushbutton is pressed you can abandon the temperature reading and start a new reading when you have dealt with the pushbutton.

It does mean that you have to modify the read_temp() code.

Are you using the DS18B20 with parasite power? The suggestions from Mr RB will only work if there is a separate power connection.
 
Last edited:

AtomSoft

Well-Known Member
As i said a few times just check withing your code a few times. Interrupts are way better but since your LCD was on PORTB already it would require a change.

Your writing in MikroC (PRO) right? If you want... rearrange the LCD to another place and ill see if i can help you write some interrupt code.
free up At least RB0
 
Last edited:

dawson

New Member
Thanks for clearing that up diver i think I understand what you mean, I will give it a try. I am currentyl using the DS18S20 but may switch to the DS18B20 to use the 12 bit res

Atomsoft, thanks for the generous offer, I will see what I can do and might take you up on your offer.
 
Last edited:
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top