Continue to Site

# 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() {
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

// 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)) {
}

} 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
thanks, will have a search for using interupts. Anyone have any good resources?

#### birdman0_o

##### Active Member
It would help if you said which uC. Most, if not all? pic16f's have PORTB interrupts on change, look into that

#### dawson

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

#### birdman0_o

##### Active Member
Port B interrupts, the datasheet provides plenty of info about how to set them up!

#### 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.

#### dawson

##### New Member
I will give that a try since I am not getting very far with interrupts lol, thanks

#### dawson

##### New Member
thanks ill take a look

#### Diver300

##### Well-Known Member
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.

#### dawson

##### New Member
diver300, can you explain that please i dont really understand

#### Diver300

##### Well-Known 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:

#### birdman0_o

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

Thanks

#### Gayan Soyza

##### Active Member
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.

#### 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:

#### AtomSoft

##### Well-Known Member
ok Ill be here if anything

Status
Not open for further replies.

Replies
4
Views
1K
Replies
10
Views
8K
Replies
30
Views
3K
Replies
5
Views
803
Replies
9
Views
3K