Picaxe 40x2 Internal Clock Frequency Not Setting

With the following code downloaded to the chip it takes one minute for the light to toggle on and off!!! Which corresponds to a 900K clock frequency!!! I have no crystal for an external oscillator connected. Both supply points are connected to 5.0 v and 0v. This should be flashing every 3.25 seconds! What could I be doing wrong?


DirsB = %00100000

setfreq m16

Main:
for w0 = 1 to 65000
pauseus 100
next w0
toggle b.5
goto main
 
Last edited:

rq3

Senior Member
Ignore your frequency setting for now, since you'll be re-writing your code.

w0 starts at 1, there is a 1000 microsecond pause (1 millisecond, each pauseus is 10 microseconds), the code then immediately sets w0 to 2 and pauses for 1 millisecond, and repeats the entire process 65000 times (65000 milliseconds, or 65 seconds). Only when w0 gets to 65000 does B.5 actually toggle.

I do the same thing all the time! The code steps explicitly as you tell it to do, line by line. The simulator often catches "oops" like this one.

If you just want the LED on for 3.25 seconds, and then off for 3.25 seconds:

Code:
Main:
high B.5           ;turn LED on
pause 3250      ;wait  3250 milliseconds
toggle B.5         ;turn LED off
pause 3250       ;wait 3250 milliseconds
goto main          ;repeat
 
Last edited:

lbenson

Senior Member
rq3's code needs another PAUSE 3250 after toggle--otherwise the cycle back to main will immediately make B.5 high again. Or move HIGH B.5 above Main:.

He's right--the simulator will be your friend for this kind of problem.
 
  • Like
Reactions: rq3

rq3

Senior Member
rq3's code needs another PAUSE 3250 after toggle--otherwise the cycle back to main will immediately make B.5 high again.

He's right--the simulator will be your friend for this kind of problem.
Thanks, already caught that! I tend to stick to hardware design for obvious reasons.
 

AllyCat

Senior Member
Hi,

Yes, the "nominal" PAUSEUS delay is in tens of us so PAUSEUS 100 would be expected to be 1 ms (at the default 8 MHz of an X2), or 500 us with double the normal clock frequency . That suggests a delay of 32.5 seconds OFF and then 32.5 seconds ON for counts of 65,000.

BUT PICaxe is an interpreted languange and all instructions take a significant time to execute. I did all my measurements with M2 chips, but typically PAUSEUS 1 takes around 700 us to execute (at 4 MHz) and an empty FOR .. NEXT loop about 1500 us per pass (at 4 MHz),

That adds probably more than another 500 us per pass (at 16 MHz) , so I'd expect the toggling period to be a little more than one minute.

Cheers, Alan. .
 

hippy

Technical Support
Staff member
an empty FOR .. NEXT loop about 1500 us per pass (at 4 MHz)
That seems about right.

I ran the code on a 28X2 and took the PAUSEUS command out. That toggled the LED every 23 seconds, 23,000,000 us.

23,000,000 / 65,000 loops = 350 us per loop

So there's about 350 us overhead in a FOR-NEXT loop at 16MHz
 
Top