|
|
View previous topic :: View next topic |
Author |
Message |
Guest
|
Making buttons more responsive..... |
Posted: Fri Oct 30, 2009 2:56 pm |
|
|
Hi All,
I've got a project that reads two Dallas DS18B20 temperatures in a continuous loop. I'm powering each sensor in 'parasite' power mode, so each conversion takes about .75 seconds due to the requirement to pull the 1-wire bus high for that long during conversions. I've got a couple of pushbuttons that I'd like to poll while this is going on. The problem is that the pushbuttons aren't very responsive due to the delays associated with reading the temperature sensors. My buttons are currently working, but you've got to hold them a while until the program has time to take a look at them. What is the proper way to handle such a situation? I considered using the Ext. Int. interrupt, but I have two buttons. Could I wire-OR them to the B0 input as well as to a dedicated pin for reading? Could I use a timer in some way?
Joe |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Oct 30, 2009 3:12 pm |
|
|
Quote: |
I've got a couple of pushbuttons that I'd like to poll while this is going on. |
Post your loop code, that calls the ds18b20 conversion routines, and
that calls the pushbutton routines. Are you currently using interrupts ? |
|
|
Guest
|
|
Posted: Fri Oct 30, 2009 3:31 pm |
|
|
PCM,
Here is the loop code:
Code: |
while(1)
{
// Here we check the status of the pushbutton switches.....
if(button(Inc_Btn, 0, 50, 10, SW1, 1))
SetPoint++;
if (button(Dec_Btn, 0, 50, 10, SW2, 1))
SetPoint--;
//Here we have a 10 ms delay for the button routines
delay_ms(10);
for(iSensor = 0 ; iSensor < 2 ; iSensor++)
{
if (iSensor == 0)
{
ow_reset_sensor1();
write_byte_sensor1(0xCC, 0); // Skip Rom command
write_byte_sensor1(0x44, 1); // Temperature Convert command, and signal Temp1 to remain hi
output_high(Temp1); // Strong Pull-up on Temp1 to power the DS18B20 during the temp conversion.
delay_ms(750); // Max. time for conversion is 750mS
ow_reset_sensor1();
write_byte_sensor1(0xCC, 0); // Skip Rom command
write_byte_sensor1(0xBE, 0); // Read scratch pad command
}
else
{
ow_reset_sensor2();
write_byte_sensor2(0xCC, 0); // Skip Rom command
write_byte_sensor2(0x44, 1); // Temperature Convert command, and signal Temp1 to remain hi
output_high(Temp2); // Strong Pull-up on Temp2 to power the DS18B20 during the temp conversion.
delay_ms(750); // Max. time for conversion is 750mS
ow_reset_sensor2();
write_byte_sensor2(0xCC, 0); // Skip Rom command
write_byte_sensor2(0xBE, 0); // Read scratch pad command
}
// Here we read 9 data bytes from the DS18B20
for (iIndex = 0 ; iIndex < 9 ; iIndex++)
{
if (iSensor == 0)
scratch[iIndex] = read_byte_sensor1();
else
scratch[iIndex] = read_byte_sensor2();
}
if (iSensor == 0)
ow_reset_sensor1();
else
ow_reset_sensor2();
CRC[iSensor] = scratch[8];
// Here we combine byte[0] and byte[1] to get the 16 bit temperature word.
TempC[iSensor] = (signed int16) make16(scratch[1],scratch[0]);
// Here we convert the 16 bit word to decrees celcius
if (TempC[iSensor] >= 0)
TempC[iSensor] = (TempC[iSensor] + 8)/16;
else
TempC[iSensor] = (TempC[iSensor] - 8)/16;
// Here we convert to decrees fahrenheit
TempF[iSensor] = ((TempC[iSensor] * 9) / 5) +32;
}
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Oct 30, 2009 3:52 pm |
|
|
The cheap, quick way to do it would be to replace your 750ms delays
with something like this:
Code: |
// Delay for 750 ms. Poll the buttons during that time.
for(i = 0; i < 75; i++)
{
if(button(Inc_Btn, 0, 50, 10, SW1, 1))
SetPoint++;
if(button(Dec_Btn, 0, 50, 10, SW2, 1))
SetPoint--;
delay_ms(10);
}
|
Just drop that in and replace the two delay_ms(750) lines.
You could make it into a routine and call it, if you wanted to.
Since you're constantly doing conversions, you could delete the existing
code that polls the pushbuttons, and just use the code above. |
|
|
Guest
|
|
Posted: Tue Nov 03, 2009 8:22 pm |
|
|
Hi PCM,
Yes, that works like a champ! I made a subroutine for the delay function, and all is well!
I'm kicking myself that I didn't see this myself. I guess it should have been obvious that since the 3/4 second delays were the bulk of the problem, that is where I should have focused my attention. Thanks for the great tip!
Joe |
|
|
DEsterline
Joined: 25 Aug 2009 Posts: 6
|
|
Posted: Fri Nov 06, 2009 2:40 pm |
|
|
There's one other change I would recommend.
your code has it in a loop, but effectively your code:
Start conversion on 1
wait
read 1
start conversion in 2
wait
read 2
Since your sensors are on different pins, you can do this:
start conversion on 1
start conversion on 2
wait
read 1
read 2
That way you only have one wait period. Since you're using parasite powered sensors, this only works if they are on different pins. |
|
|
Guest
|
|
Posted: Fri Nov 06, 2009 3:05 pm |
|
|
DEsterline,
Ah, that is a great suggestion as well! I will implement that idea this weekend! This will allow me to get temperature updates twice as often!
Thanks!
Joe |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Fri Nov 06, 2009 4:54 pm |
|
|
Ok, well is seems kinda silly to have bus based sensors on different pins. They could be on the same pins with a proper strong pullup during conversions... (make sure enough current is available).
My first project in PIC C (about 1997) used 4 sensors on 1 bus. For current usage, I did a conversion at a time... saved pins.
Ahhh, the options with today's fun Micro's.
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
DEsterline
Joined: 25 Aug 2009 Posts: 6
|
|
Posted: Fri Nov 06, 2009 7:31 pm |
|
|
There's a lot of valid reasons to have a bus based sensor on multiple busses. My current design has 5 separate DS1822 on separate IO lines. Busses are great, but the discovery process is quite complicated. Not only the one-wire address arbitration process, but having the software figure the physical location of the different sensors can present significant problems. |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Fri Nov 06, 2009 9:01 pm |
|
|
That's True...
I suppose one could say that's what user configuration is for. but for something more turnkey -- I'd go with that. _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|