1kHz, 50% duty with 08M...help!

ccdubs

Member
Hi all,

I need to make an 08m output a 1kHz square wave with a 50% duty cycle. I have looked at the pwmout function and tried to use the wizard in the programming software, but it tells me "value outside range".

Any help on how to achieve this signal is greatly appreciated.
 

Jeremy Leach

Senior Member
The PWMout command can't go that low with a 4MHz clock. There may be a way of doing it by lowering the clock speed of the PICAXE (I've never tried this but there are tricks people have done), or I think you might have to just do what's called 'bit-banging' - meaning generating the output signal manually in code.

A 1KHz signal has a period of 1/1000 seconds = 1 millisecond. We want to cange the level of the signal twice per period, ie every 0.5 milliseconds.

So the basic code would be :

<code><pre><font size=2 face='Courier'>
GenerateSignal:
Toggle SignalOutPin
Pause HalfPeriod
Goto GenerateSignal
</font></pre></code>

However the Pause command only works with millisecond values at 4MHz. BUT if we increase the clock speed to 8MHz then each unit of Pause represents half a millisecond !

So :
<code><pre><font size=2 face='Courier'>
Symbol
SetFreq M8

GenerateSignal:
Toggle SignalOutPin
Pause 1 ' 0.5ms
Goto GenerateSignal
</font></pre></code>

This should do it. However the PICAXE won't be able to do anything else while it's generating this signal. Not sure if you want that.
 

eclectic

Moderator
Or, the slow way.

Using the information provided in this thread,

http://www.rev-ed.co.uk/picaxe/forum/Topic.asp?topic_id=2262

main:
&#8216;
pwmout 2, 249,500 ' 4kHz pwmout
&#8216;
poke $8f, %01000000 ' 1 MHz processor
&#8216;
' produces 1kHz pwmout
&#8216; may need tweaking with calibfreq
&#8216;
stop

BUT, all the rest of the program then runs at quarter speed.

Possibly use an 08M as a dedicated oscillator.

e.
 

Dippy

Moderator
As a slight aside J. with your experiments in poking CCP and Timer, is there any mileage with experimenting with CCPR1L register and CCP1CON&lt;5:4&gt; and Timer 2 prescale to extend range of pwm?
 

Jeremy Leach

Senior Member
My experiments have only used Timer1. I'm wanting to get into the other discoveries that Hippy's come up with, but haven't had time. I've been thinking along the same line too though.

I didn't want to complicate this thread with ideas of using Timer1, as I think we've hopefully solved this particular problem in a simple way.
 

ccdubs

Member
Thanks for the help.

I have come up with the following code and would appreciate verification. I am not too sure if the pause operator is the best option here and if it is, from what I read I need to factor in a divisor of 4 on the pause time to account for the clock speed change.

Simply I am trying to drive a piezo on and off with the on period 3 x that of the off.


**********************************************
main:

poke $8f, %01000000 ' Set processor speed to 1 MHz

goto loop


loop:

pwmout 4, 249,500 ' Turn ON 4kHz pwmout
pause 750

pwmout 4, 0,0 ' Turb OFF 4kHz pwmout
pause 250

goto loop
 

ccdubs

Member
Hi all.

I have managed to verify the code myself after finding that pin 2 is the PWMOUT pin.

Thanks for your help.
 
Top