Newbie Questions re pwmout

Mikler

Member
Help for a newbie please....

I am using a 14M

ONE:
I am trying to use pwmout to produce various precise frequencies around the 500hz range but the wizard says I can't produce a fequency below 3.9Khz - is there a workaround? or something I am missing?
(I need to use pwmout as I need an accurate frequency - and wish to do something else at the same time)

TWO:
I suspect this is a very stupid question, but if my code reads:
pwmout 2,x,x
why is the 'output' coming from pin5 which is an input pin....?

Heeeeelp.....
 

jglenn

Senior Member
I am new too, but one program I ran flashed a led. That is an easy way to test an output pin. Read the manual to determine when pins are in or out.

Using pwm to make a freq is overkill. Just turn on an output, have a delay, turn the output off, then loop. The delay will determine the freq. There may be other ways to do this. Counter-timers perhaps! :confused:
 

lbenson

Senior Member
According to Appendix C of Manual 1, "Configuring the PICAXE-14M Input-Output Pins" (page 79 in the latest version--6.7), PWM 2 is *leg* 5, which is also input 2 among other things.

If you run the chip at a slow enough speed to get 500Hz, will it be processing fast enough to do what else you want (approximately 1/10th the speed = around 200 instructions per second)?
 
The 14M's clock frequency can't be reduced below "m4" or 4Mhz. As such the pwmout command can't generate 500hz output.

What are you planning to do with 500hz?
How precise does it need to be?
 

jglenn

Senior Member
You know, I just noticed that my PICAXE proto board does not have a crystal or resonator. Which means they must be using the internal clock. Question for the guru's:

Is there a way to execute the OSCCAL command (assembly) that takes the factory oscillator correction factor, and loads it to trim the clock? I used to use crystals with raw pics, but went to RC, to save cost, and more rugged (my products often hit the ground at a couple hundred MPH, breaks xtal), then the int clk. Don't really need supreme accuracy, but do the trim anyway. Just wondering. Have not seen any utility to embed machine code.
 

lbenson

Senior Member
Underclocking

Stan.Swan AKA manuka showed how to slow down an 08M in this thread: http://www.picaxeforum.co.uk/showthread.php?t=2233

This works for other picaxes as well. As I understand, PWM is slowed proportionally. Slowing down to around 500kHz might enable close to the 500Hz desired.

'Internal clock frequency can be tinkered easily on an 08M as follows
'(Notes from Microchip documentation for 08M hardware)

'REGISTER 3-2: OSCCON - OSCILLATOR CONTROL REGISTER (ADDRESS: 8Fh)
'bit 6-4 IRCF<2:0>: Internal Oscillator Frequency Select bits
'

'%00000000 = 31 kHz ~19bps
'%00010000 = 125 kHz 75bps
'%00100000 = 250 kHz 150bps
'%00110000 = 500 kHz 300bps
'%01000000 = 1 MHz 600bps
'%01010000 = 2 MHz 1200bps
'%01100000 = 4 MHz 2400bps
'%01110000 = 8 MHz 4800bps

loop:

poke $8F,%01100000 'Set clock to 4 MHz
 

womai

Senior Member
Another option is to use the timer 2 prescaler. This is part of the timer 2 setup register (0x12) and can be used to slow down PWM by a factor of 4 and 16, respectively. I've used it on the 28X and the 28X1, and there is a good chance the register exists on the 14M as well. On the 28X/28X1 you access it with peek and poke to address 0x12 (hex) / 18 (dec.) On the 14M the address may be different (have a look at the data sheet of the underlying Microchip PIC. For the 28X1 that's the PIC16F886, for the 14M I don't know).

Read the register contents first, then modify the two prescaler bits, then write it back.

That way you can go down to approx. 250 Hz (3.9 kHz / 16) with a clock of 4 MHz.

Advantage over changing the clock frequency - Picaxe can continue running at full speed.

Here is my code:

Code:
symbol COUNTER2_SETUP_REG = 0x12

symbol FAST_CLK_PWM_CHAN = 1

symbol b_pwm_period      = b19
symbol w_pwm_duty        = w10
symbol b_pwm_prescaler   = b22

...
...

    ' set up PWM clock
    pwmout FAST_CLK_PWM_CHAN, b_pwm_period, w_pwm_duty

    ' slow down PWM if necessary, using counter 2 prescaler
    peek COUNTER2_SETUP_REG, b_dummy
    b_dummy = b_dummy & %11111100 | b_pwm_prescaler ' 00 = div 1, 01 = div 4, 10 or 11 = div 16
    poke COUNTER2_SETUP_REG, b_dummy
Wolfgang
 

Dippy

Moderator
Goodness knows how many times I and others have posted information on this subject.

This should all be in another PICAXE App Note along with the 'how to control a MOSFET' App Note which hippy is writing.
 

Mikler

Member
A newbie comment...

Hi!

Just to thank everyone for their help

Dippy >>Goodness knows how many times I and others have posted information on this subject...

My apologies but I am a beginner and had absolutely no idea what terms to search for - I have now searched for prescaler and similar terms and have found a wealth of info...

Michael
 
Top