Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Tools
Old 27th October 2009, 06:24 PM   #1
Default waiting for input while checking temprature?

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 by dawson; 27th October 2009 at 06:37 PM.
dawson is offline  
Old 27th October 2009, 07:26 PM   #2
Default

Use interrupts.

- Raj
Experiments with PIC16F628A
rajbex is offline  
Old 27th October 2009, 07:36 PM   #3
Default

thanks, will have a search for using interupts. Anyone have any good resources?
dawson is offline  
Old 27th October 2009, 09:53 PM   #4
Default

It would help if you said which uC. Most, if not all? pic16f's have PORTB interrupts on change, look into that
__________________
Mike
My website: www.ElectroBird.net
birdman0_o is online now  
Old 27th October 2009, 10:58 PM   #5
Default

uC? Im using pic16f628. I have had a look for interrupts but cant really find much can anyone help?
dawson is offline  
Old 27th October 2009, 11:21 PM   #6
Default

Port B interrupts, the datasheet provides plenty of info about how to set them up!
__________________
Mike
My website: www.ElectroBird.net
birdman0_o is online now  
Old 28th October 2009, 03:34 PM   #7
Default

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.
Mr RB is offline  
Old 28th October 2009, 08:25 PM   #8
Default

I will give that a try since I am not getting very far with interrupts lol, thanks
dawson is offline  
Old 29th October 2009, 02:03 AM   #9
Default

A Tutorial on PIC interrupts using BoostC including Example Programs - Open Circuits
Russ Hensel is offline  
Old 29th October 2009, 06:20 PM   #10
Default

thanks ill take a look
dawson is offline  
Old 29th October 2009, 10:22 PM   #11
Default

You can get the delay routine to read the button.

All the other operations of a DS1820 are very quick, so they won't introduce too big a lag.
Diver300 is online now  
Old 30th October 2009, 12:16 AM   #12
Default

diver300, can you explain that please i dont really understand
dawson is offline  
Old 30th October 2009, 09:05 AM   #13
Default

When reading the DS18B20, the simplest routine is something like this:-

Code:
Microcontroller DS18B20
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 by Diver300; 30th October 2009 at 09:07 AM.
Diver300 is online now  
Old 30th October 2009, 11:17 AM   #14
Default

Great advice diver! Do you happen to have corresponding code though hanging around somewhere?

Thanks
__________________
Mike
My website: www.ElectroBird.net
birdman0_o is online now  
Old 30th October 2009, 12:23 PM   #15
Default

As diver said set an push button press flag bit inside the 750mS routine if the button was pressed & check the flag bit before updating or reading the temperature.
__________________
Gayan

My Website
http://gsmicro.blogspot.com/
Gayan Soyza is offline  
Reply

Tags
checking, input, temprature, waiting

Thread Tools
Display Modes


Similar
Title Starter Forum Replies Latest
Temprature Electronman Chit-Chat 6 9th October 2009 07:52 PM
Checking short!!! Badar General Electronics Chat 4 17th April 2007 08:47 PM
Accident waiting to happen... Marks256 Chit-Chat 8 15th February 2007 01:27 PM
Dallas DS1620 Temprature Sensor abees81 General Electronics Chat 4 8th December 2006 12:33 PM
call waiting haerifar General Electronics Chat 1 28th March 2004 08:16 PM



All times are GMT. The time now is 10:59 PM.


Electronic Circuits  |  Learning Electronics
eXTReMe Tracker