PDA

View Full Version : 10 Bit HPWM?



Desterline
- 14th December 2003, 16:59
Hello,
According to the datasheet the PIC16f87x chips have 10 bit hardware PWM. But the manual for PBP (2.43) seems to only show 8 bit resolution.

What's the best (read easiest) way to get 10 bit resolution out of them?

Thanks
-Denny

Melanie
- 15th December 2003, 10:25
Yes this is true. PBP only supports 8 bits with HPWM.

You could do it the old and trusted way by setting the PIC's registers directly... this will get you started...

Initialisation (at the start of your program)...

PR2=$FF ' Timer2 Period Register
T2CON=%00000100 ' Values for PWM period
CCP1CON=%00001100 ' Set PWM Mode

Now in your program... the WORD variable TempWord contains the 10-bit value to set...

TempWord=TempWord<<6
CCPR1L=TempWord.highbyte
CCP1CON.5=TempWord.7
CCP1CON.4=TempWord.6

You'll notice I put 8 bits into CCPR1L in one hit and the remaining two bits into CCP1CON.

Refer back to the PIC Datasheet to see what bits I've manipulated.

There is an alternative you could try... that is set the 8-bits with HPWM, and load the remaining two bits into CCP1CON manually (always assuming you're running channel 1 with HPWM) - I've not tried this myself but you might be able to simply pull it off that way.

Melanie

Desterline
- 15th December 2003, 16:19
Thank you Melanie. You know one of these days were going to have to make good on our threats and actualy send you some flowers. :-)


Clarify somthing else for me: The datasheet gives the max resolution of the HPM as a function of Fosc. Asuming a 4MHz resonator, is Fosc 4Mhz, or the internal divided by 4 - 1Mhz?


As I read the formula:


LOG(fosc/Fpwm)
--------------------- = Resolution (in bits)
LOG(2)

That works out to be just under 4KHz (assuming Fosc is 4Mhz) for 10 bit resolution, is that right?

Thanks
-Denny

Melanie
- 15th December 2003, 20:03
The thing I like about computers is that thery're pretty idiot proof if you think about it - especially the mnemonics...

FOSC = Frequency OSCillator

Namely it's the frequency of your xtal/resonator - ie the Fequency of the Oscillator.

Whereas Fosc/4 is the instruction speed.

btw... I calculate 10 bits to be precisely 3906.25 at 4.000000MHz assuming you have a xtal that's spot on *smiles*...

Desterline
- 15th December 2003, 20:13
Thanks,
That's what I thought, but I couldn't pin it down definitively.

-Denny