-
Code trouble
Can anyone spot my mistake in this simple program. The program run but it will never go into the wakeup mode. thank you much see below:
' this is for pic12f675
ANSEL=0 ' change pin into digital Input
CMCON=7
pulsewidth var word
counter var word
looptime con 10000
pulsin 4,1,pulsewidth ' read the pulse width at pin 4
for counter=0 to looptime 'loop
if pulsewidth >150 then gosub switch ' read pulse width again
if pulsewidth=0 then gosub blink
pulsin 4,1,pulsewidth
pause 1
if counter=looptime-1 then gosub wakeup
next counter
switch:
low 2 'pin 2 becomes ground =20 ohms resistance w ground
pause 1000
input 2 ' pin 2 becomes open collector means high impedance
pause 1000
counter=0 ' reset the counter
return
' : wake up will do the same thing as the switch function above but only after
' the loop times out
wakeup:
low 2 'pin 2 becomes ground =20 ohms resistance w ground
pause 1000
input 2 ' pin 2 becomes open collector means high impedance
pause 1000
counter=0
return
blink: ' blink the led
high 0
pause 80
low 0
pause 80
counter=0
return
-
I would have to say your problem is using an "IF" statement inside the "FOR...NEXT" loop. Also you do realize the PAUSE 1 = 1mS and not 1s. I also noticed there is not main program loop, do you in fact what the program to stop once it has read the pulse width?
Code:
start:
counter = 0
loop:
counter = counter + 1
pulsin 4,1,pulsewidth
if pulsewidth >150 then
gosub switch
endif
if pulsewidth=0 then
gosub blink
endif
pulsin 4,1,pulsewidth
pause 1
if counter = (looptime - 1 ) then
gosub wakeup
endif
goto start
switch:
blah, blah, blah...
return
blink:
blah, blah, blah...
return
wakeup:
blah, blah, blah...
return
-
still doesn't work... i don't see what's the problem with mine. the program never gets to wakeup.
-
I got it to work...initially i though that the 1ms pause and i am doing a 10000 loop i would get about 10 seconds delay...but it turned out that this is not correct i would get about 56 seconds delay for 2700 loop not sure why it took so long to execute all these functions
-
All the other commands require processing time which can certainly add up over these kinds of execution times. I'm sure if you calculate how long each command take to process you will find your time is pretty close if not bang on. Especially is say one command take 1ms to process then you would have added 2.7s to the time right off the bat.