Shop OBEX P1 Docs P2 Docs Learn Events
Infinite loop within Programme — Parallax Forums

Infinite loop within Programme

Neillson01Neillson01 Posts: 10
edited 2013-03-10 10:23 in General Discussion
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

Comments

  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2013-03-08 09:08
    In PBasic, probably no.
    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,
  • Neillson01Neillson01 Posts: 10
    edited 2013-03-08 09:35
    Afraid it does have to be in BS2 and am using a 16 pin REV J microncontroller. It seems to be a lost cause in this case but thank you for your help
  • Mike GreenMike Green Posts: 23,101
    edited 2013-03-08 09:55
    Normally this is done by using an external RTC. It's possible to use a timed event loop to get times on the order of seconds. Each time through the loop occupies close to one second. Sometimes, the loop just PAUSEs. Sometimes, the capacitor charges for 5 successive times through the loop. Sometimes, the Stamp measures the discharge time (assumed to take less than one second). An external RTC is better and simpler.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2013-03-09 21:18
    Well, Mike Green is pointing the way... it all has to be in one loop without interrupts. You just have to fiddle with creating a loop that is accurately timed.
  • Mike GreenMike Green Posts: 23,101
    edited 2013-03-09 22:47
    You're making what's called a "state machine" where each time through the loop represents one of a series of states. Typically you'd have a variable that holds the current state number and that's used in a SELECT statement with each CASE handling one state. Each state routine sets the next state if it's different from the current one. State 0 would be the default state that would check a pushbutton if you wanted to start a cycle that way, then it would start charging the capacitor and move to state 1 the next cycle. States 1-5 might each be a 1 second PAUSE for charging the capacitor and each increments the current state number. State 6 might use RCTIME to measure the capacitance, then PAUSE for 1 second minus the time it takes for the RCTIME. You'd also add a PAUSE to each other state to make that state take as close to 1 second as possible.
  • Neillson01Neillson01 Posts: 10
    edited 2013-03-10 10:23
    OK, thank you. I'm going to coding this tomorrow with the hope of getting it done, using the state machine seems to be a good option and as I am not an advanced programmer it does not seem to be to challenging.
Sign In or Register to comment.