Infinite loop within Programme
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 5 seconds, then measures the capacitor discharge time using RCTIME function within PBasic. this value is then compared to a set values of discharge to vs temperature and output the value on an LCD screen.
For the project I have decided to implement a real-time clock into the program. A problem as arisen while implementing the program as for the real-time clock to function it requires an infinite loop. therefore the program will never the reach the code to calculate discharge time/output temperature.
My question is; can both these subprograms possibly be implemented into the same program or can 2 programs be operating in conjunction with each other.
I have C+P the code below:
'Project: Temperature sensor using semi conductor diode
'Main project
'{$STAMP BS2}
'{$PBASIC 2.5}
Main:
GOSUB time 'go to time sub
GOSUB Chdis 'go to chdis sub
GOSUB compare 'go to compare sub
GOSUB alarm 'go to alarm sub
Time:
timecounter VAR Byte 'set counter as variable
hrs VAR Byte 'set hrs variable
mins VAR Byte 'set mins variable
sec VAR Byte 'set sec variable
hrs = 0
mins = 0 'initialize hrs and mins
second:
sec = 0 'reset seconds to 0
FOR timecounter = 1 TO 60 'set counter for 60 seconds
PAUSE 980 'pause for > 1 second
sec = sec + 1 'add second
DEBUG CLS 'clear screen
DEBUG "the time is ", DEC hrs, ":", DEC mins, ":", DEC sec 'output time to screen
NEXT 'next second on counter
GOTO minute 'go to minute sub
minute:
mins = mins + 1 'add 1 to minute
IF mins = 60 THEN hours 'go to hours sub if minutes = 60
GOSUB second
hours:
mins = 0
hrs = hrs + 1 'add 1 to hours vairable
IF hrs = 24 THEN hrsreset 'reset hours if hrs = 24
GOTO second
hrsreset:
hrs = 0 'reset hour to 0
GOTO second
counter VAR Nib
timecounter = 0 'reset counter to zero for ease of measurement
PAUSE 1000 'initial pause of 1s
DEBUG CLS 'clear the debug screen for new reading
HIGH 7 'sets pin 7 to start charging the capacitor
DEBUG "Capacitor Charging...", CR 'shows the capacitor on debug screen
FOR counter = 5 TO 0 'sets the pin high for 5 seconds
PAUSE 1000 'pause for 1s
DEBUG DEC2 counter, CR, CRSRUP
NEXT
DEBUG CR, CR, "Measure decay time now!", CR, CR 'shows on debug screen it is taking a reading of RC time
INPUT 7 'sets the output pin as an input to discharge the capacitor
DO
PAUSE 10
timeCounter = timeCounter + 1 'sets readings to be taken as 10ms intervals
DEBUG ? IN7
DEBUG DEC5 timeCounter, CR, CRSRUP, CRSRUP 'shows a reading 1
LOOP UNTIL IN7 = 0 'loops until the value is equal to 0
DEBUG CR, CR, CR, "The RC decay time was ", 'shows output message on debug screen,
DEC timeCounter, CR, 'followed by the amount of cycles the microproccessor -
"hundredths of a second.", CR, CR 'counted until the capacitor has discharged
PAUSE 1000
GOTO compare 'move to next programme
The system works by charging a capacitor through the leakage current of the diode for 5 seconds, then measures the capacitor discharge time using RCTIME function within PBasic. this value is then compared to a set values of discharge to vs temperature and output the value on an LCD screen.
For the project I have decided to implement a real-time clock into the program. A problem as arisen while implementing the program as for the real-time clock to function it requires an infinite loop. therefore the program will never the reach the code to calculate discharge time/output temperature.
My question is; can both these subprograms possibly be implemented into the same program or can 2 programs be operating in conjunction with each other.
I have C+P the code below:
'Project: Temperature sensor using semi conductor diode
'Main project
'{$STAMP BS2}
'{$PBASIC 2.5}
Main:
GOSUB time 'go to time sub
GOSUB Chdis 'go to chdis sub
GOSUB compare 'go to compare sub
GOSUB alarm 'go to alarm sub
Time:
timecounter VAR Byte 'set counter as variable
hrs VAR Byte 'set hrs variable
mins VAR Byte 'set mins variable
sec VAR Byte 'set sec variable
hrs = 0
mins = 0 'initialize hrs and mins
second:
sec = 0 'reset seconds to 0
FOR timecounter = 1 TO 60 'set counter for 60 seconds
PAUSE 980 'pause for > 1 second
sec = sec + 1 'add second
DEBUG CLS 'clear screen
DEBUG "the time is ", DEC hrs, ":", DEC mins, ":", DEC sec 'output time to screen
NEXT 'next second on counter
GOTO minute 'go to minute sub
minute:
mins = mins + 1 'add 1 to minute
IF mins = 60 THEN hours 'go to hours sub if minutes = 60
GOSUB second
hours:
mins = 0
hrs = hrs + 1 'add 1 to hours vairable
IF hrs = 24 THEN hrsreset 'reset hours if hrs = 24
GOTO second
hrsreset:
hrs = 0 'reset hour to 0
GOTO second
counter VAR Nib
timecounter = 0 'reset counter to zero for ease of measurement
PAUSE 1000 'initial pause of 1s
DEBUG CLS 'clear the debug screen for new reading
HIGH 7 'sets pin 7 to start charging the capacitor
DEBUG "Capacitor Charging...", CR 'shows the capacitor on debug screen
FOR counter = 5 TO 0 'sets the pin high for 5 seconds
PAUSE 1000 'pause for 1s
DEBUG DEC2 counter, CR, CRSRUP
NEXT
DEBUG CR, CR, "Measure decay time now!", CR, CR 'shows on debug screen it is taking a reading of RC time
INPUT 7 'sets the output pin as an input to discharge the capacitor
DO
PAUSE 10
timeCounter = timeCounter + 1 'sets readings to be taken as 10ms intervals
DEBUG ? IN7
DEBUG DEC5 timeCounter, CR, CRSRUP, CRSRUP 'shows a reading 1
LOOP UNTIL IN7 = 0 'loops until the value is equal to 0
DEBUG CR, CR, CR, "The RC decay time was ", 'shows output message on debug screen,
DEC timeCounter, CR, 'followed by the amount of cycles the microproccessor -
"hundredths of a second.", CR, CR 'counted until the capacitor has discharged
PAUSE 1000
GOTO compare 'move to next programme
Comments
The actual chip that is the microprocessor does have an Interrupt Service Routine that might be coded successfully. But PBasic uses a tokenized setup and an interpreter within the chip that never really allowed for PBasic to accomodate a software solution for a Real Time Clock.
You might consider using an add on RTC.
What an interrupt service routine does is to allow two loops to share defined time slots. In that way, you might have what appears to be two independent tasks looping infinitely.
You could do it in Assembly code for an SX28 chip or a PIC16F84. Take a look at PIClist.com or SXlist.com for a lot of discussion about such things.
If you really need to use a BasicStamp type of 24 pin hardware and a form of Basic, the ZBasic ZX24 will provide you with software and hardware that will do it.
www.zbasic.net
But it would be a lot cheaper to just use a Parallax Propeller and Spin Objects,