![]() | ![]() | ![]() |
| |||||||
| Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc. |
![]() |
| | Tools |
| | #1 |
|
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);
}
Edit: Forgot to mention Im using the pic16f628 Last edited by dawson; 27th October 2009 at 06:37 PM. | |
| |
| | #2 |
| | |
| |
| | #3 |
|
thanks, will have a search for using interupts. Anyone have any good resources?
| |
| |
| | #4 |
|
It would help if you said which uC. Most, if not all? pic16f's have PORTB interrupts on change, look into that | |
| |
| | #5 |
|
uC? Im using pic16f628. I have had a look for interrupts but cant really find much can anyone help?
| |
| |
| | #6 |
|
Port B interrupts, the datasheet provides plenty of info about how to set them up!
| |
| |
| | #7 |
|
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.
| |
| |
| | #8 |
|
I will give that a try since I am not getting very far with interrupts lol, thanks
| |
| |
| | #9 |
| | |
| |
| | #10 |
|
thanks ill take a look
| |
| |
| | #11 |
|
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.
__________________ http://nottheboss.wordpress.com/ | |
| |
| | #12 |
|
diver300, can you explain that please i dont really understand
| |
| |
| | #13 |
|
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 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.
__________________ http://nottheboss.wordpress.com/ Last edited by Diver300; 30th October 2009 at 09:07 AM. | |
| |
| | #14 |
|
Great advice diver! Do you happen to have corresponding code though hanging around somewhere? ![]() Thanks | |
| |
| | #15 |
|
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.
| |
| |
|
| 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 |