RCTIME function outputting wrong data
Neillson01
Posts: 10
I am currently undertaking a University project involving Temperature sensing using a Zener diode. The system uses a parallax BS2 microcontroller.
The system works by charging a capacitor through the leakage current of the diode for 4 seconds, then measures the capacitor discharge time using RCTIME function within PBasic and output the discharge time value on an debug screen. There is an additional clock feature that has been added to the program, the discharge time measurement has been timed into the clock.
While using the RCTIME function, the only output value that is read from the debug screen is a 1 or 0, I need help to find out why the correct RCTIME is not being outputted, at room temperature the discharge time is found to be roughly 190ms by measuring it manually. I am using a 220 ohm resistor connected to a 1nF capacitor and zener diode in parallel.
I have C+P the code below:
'Project: Temperature sensor using semi conductor diode
'Main project
'{$STAMP BS2}
'{$PBASIC 2.5}
mins VAR Byte
hrs VAR Byte
mincounter VAR Byte
diff VAR Byte
result VAR Byte
main:
mins = 0
hrs = 0
minute:
FOR mincounter = 1 TO 60
DEBUG CLS
GOSUB chdis
DEBUG CLS
DEBUG DEC hrs, ":", DEC mins, CR, DEC result
SLEEP 56
mins = mins + 1 'add 1 to minute
IF mins = 60 THEN hours
NEXT
hours:
mins = 0
hrs = hrs + 1 'add 1 to hours vairable
IF hrs = 24 THEN hrsreset 'reset hours if hrs = 24
GOTO minute
hrsreset:
hrs = 0 'reset hour to 0
GOTO main
chdis:
HIGH 7 'sets pin 7 to start charging the capacitor
DEBUG CLS
DEBUG DEC hrs, ":", DEC mins, CR, "temp"
PAUSE 1000
DEBUG CLS
DEBUG DEC hrs, ":", DEC mins, CR, "temp."
PAUSE 1000
DEBUG CLS
DEBUG DEC hrs, ":", DEC mins, CR, "temp.."
PAUSE 1000
DEBUG CLS
DEBUG DEC hrs, ":", DEC mins, CR, "temp..."
PAUSE 1000
RCTIME 7, 1, result
diff = 1000 - result
PAUSE diff
RETURN
The system works by charging a capacitor through the leakage current of the diode for 4 seconds, then measures the capacitor discharge time using RCTIME function within PBasic and output the discharge time value on an debug screen. There is an additional clock feature that has been added to the program, the discharge time measurement has been timed into the clock.
While using the RCTIME function, the only output value that is read from the debug screen is a 1 or 0, I need help to find out why the correct RCTIME is not being outputted, at room temperature the discharge time is found to be roughly 190ms by measuring it manually. I am using a 220 ohm resistor connected to a 1nF capacitor and zener diode in parallel.
I have C+P the code below:
'Project: Temperature sensor using semi conductor diode
'Main project
'{$STAMP BS2}
'{$PBASIC 2.5}
mins VAR Byte
hrs VAR Byte
mincounter VAR Byte
diff VAR Byte
result VAR Byte
main:
mins = 0
hrs = 0
minute:
FOR mincounter = 1 TO 60
DEBUG CLS
GOSUB chdis
DEBUG CLS
DEBUG DEC hrs, ":", DEC mins, CR, DEC result
SLEEP 56
mins = mins + 1 'add 1 to minute
IF mins = 60 THEN hours
NEXT
hours:
mins = 0
hrs = hrs + 1 'add 1 to hours vairable
IF hrs = 24 THEN hrsreset 'reset hours if hrs = 24
GOTO minute
hrsreset:
hrs = 0 'reset hour to 0
GOTO main
chdis:
HIGH 7 'sets pin 7 to start charging the capacitor
DEBUG CLS
DEBUG DEC hrs, ":", DEC mins, CR, "temp"
PAUSE 1000
DEBUG CLS
DEBUG DEC hrs, ":", DEC mins, CR, "temp."
PAUSE 1000
DEBUG CLS
DEBUG DEC hrs, ":", DEC mins, CR, "temp.."
PAUSE 1000
DEBUG CLS
DEBUG DEC hrs, ":", DEC mins, CR, "temp..."
PAUSE 1000
RCTIME 7, 1, result
diff = 1000 - result
PAUSE diff
RETURN
Comments
If I remember correctly, the RCTIME result is not in milliseconds. I think it is in 10uSec increments.
Bean
Hal
Component values: The voltage rating of the zener will make quite a difference in how the discharge proceeds, particularly if it is around 5 volts or less. There might be a number printed on the diode, but perhaps not. You can probably read the value of the capacitor from lettering on one side, a number such as "104".
I'm sure the time to charge up the capacitor through the 220Ω will be much less than 4 seconds, probably less than a millisecond, judging from the appearance of the capacitor.
I suggest you simplify the code, to help resolve the problem. Leave out the clock stuff. Just have the RCTIME in a loop repeated at intervals of a second or two.
When RCTIME returns 1, that means the capacitor discharged instantly, less than 2µs. When RCTIME returns 0, that means it took too long to discharge, greater than 1.3107 second. (2µs * 65535).
Hal
As far as the RCTIME is concerned for the time being I have programmed the RCTIME function manually by using the following code:
chdis:
In terms of the discharge time it is measuring it at room temperature with the above code with the value 185 - 195. The problem is as the programme runs repeatedly this value fluctuates to random values, when the "reset" button is pressed on the microcontroller interface the values are constant so are assumed to be correct, is there a way of resetting the microcontroller in the code without physically pressing it?
Thanks
Neill
Remember that, with the PAUSE 10, timeCounter is in units of approximately 10ms, not milliseconds. Easiest way to fix this is to increment by 10 instead of by 1. Also remember that the other statements in the loop will contribute to the execution time. See this webpage and click on the "app-notes" link for a discussion of Stamp execution times. Along those lines, you may have noticed that the hours and minutes numbers are way off. That's also because all the statements in the program that execute contribute to the execution time, not just things like PAUSE and SLEEP. The DEBUG statements, for example, take about 1ms per character transmitted. Most statements take on the order of hundreds of microseconds.
Thanks
Neill
"The clock is one of the main parts of the project so would like to keep that in the programme." It is sooo easy to reprogram a Stamp. If one small section of a program is giving us trouble, it is standard practice to work on it in isolation.
Is the standard RCTIME function returning good values now?