08m2 how to generate 2 different continuous clock frequency

jackberg

New Member
Hi everyone,

I have a simple project consisting of one 08M2, and I have to generate 2 different continuous clock frequency on pin c.4,c.0 while pin c.1, c.2 are use for led.

the main problem I've got is using a do,loop with toggle work, but the 2 different frequency have to run shifted, eg: first frequency is 200Hz, and the second is 225Hz.

the second frequency has to be slower than the fist one for few Hertz.

any suggestions will help.

Thanks again

Code:
#picaxe 08m2
#NO_DATA
;#Terminal 4800
setfreq m16 ;32
b0 = 0: b1 = 0 : b2 = 80 : b3 = 86

DO
b0 = b0 + 1
b1 = b1 + 1

if b0 = b2 then
TOGGLE C.0
b0 = 0
endif

if b1 = b3 then
TOGGLE C.4
b1 = 0
endif

LOOP
 
Last edited:

Buzby

Senior Member
Try this ....

EDIT : I've just tried your code in the simulator, and it does seem to be doing what I think you're asking for. What do you think is wrong with your code ?

Code:
#picaxe 08M2

symbol freqpin1 = C.0
symbol freqpin2 = C.4

symbol count1 = w1
symbol count2 = w2

symbol Freq1set  = 80
symbol Freq2set  = 86

do
    if count1 > 0 then
        dec count1
    else
       count1 = Freq1set
       toggle freqpin1
    endif

    if count2 > 0 then
        dec count2
    else
       count2 = Freq2set
       toggle freqpin2
    endif
loop
 
Last edited:

jackberg

New Member
Hello Buzby, and thank's for your reply,
about my code I wonder if this is the best and simplest way to acheive it.
I know for fact that from this forum, there's always a way to simplify the coding.
 

Buzby

Senior Member
Both sets of code, yours and mine, seem to generate the independant frequencies as you want.
Both are already quite small, each is less than 45 bytes.

I'm fairly certain the code could be made even smaller, but maybe at the loss of clarity and flexibility.
I wouldn't bother trying to 'optimise' the frequency generator code further until the rest of the code is completed.
 

hippy

Ex-Staff (retired)
How accurate do the frequencies need to be, and are those the actual frequencies you want ?

With 200 Hz having a period of 5000 us and 225 Hz a period of 4444.44 us it might be challenging. I did think about ..,
Code:
Do
  count1 = count1 + 1 // 5000 : If count1 = 0 Then : Toggle OUT1 : End If
  count2 = count2 + 1 // 4444 : If count2 = 0 Then : Toggle OUT2 : End If
  PauseUs ...
Loop
But not sure how well that would perform, and might be worse than Buzby's solution.
 
Top