PDA

View Full Version : pwm



pic beginner
- 8th July 2004, 05:01
Hi anyone
my question about pwm
I know that pic16f877 has 10 bit resolution pwm module.
Suppose that we have a word variable for example: Y
maximum value of Y is 1023. We want put Y in pwm module and convert digital
value of Y to analog with RC netwok.(PWM as DAC)
pwm freq is 1500.
How write this program with PBP.
Please help me
Thanks alot.

Melanie
- 8th July 2004, 07:39
A fine example of 8-bit PWM is in the manual, if you show us an attempt at getting that to run using your variable Y, then we can show you how to get the remaining two Least Significant Bits activated to turn PBP's 8-bit into a full 10-bit operation.

pic beginner
- 8th July 2004, 12:05
Hi Melanie
I thank you for reading and answering my question.
I think taht below program shows 8-bit hardware PWM in pbp :
Y VAR WORD
HPWM 1,Y,1000
This program produces pwm that y specifies duty cucle . The frequency of pwm is 1khz.
For example for Y=127 duty cycle is 50%
But HPWM command only considers low byte of Y.
I want to know about 10 bit pwm in pbp
thanks alot.

Melanie
- 8th July 2004, 12:41
That's a start, but as you correctly say PBP will only look at the Lower 8-bits... so your example will lose the upper two, which are far more important.

First, let's grab the UPPER eight bits (variable TempA is a BYTE)...

TempA=Y >> 2

Now let's set the 8-bit PWM with the eight most significant bits...

HPWM 1,Temp,1000

For the two least significant bits, if you looked in the 'Capture/Compare/PWM' section of your PIC's Datasheet you would have found that bits 5 and 4 of the CCP1CON Register handle those... so, simply just load them...

CCP1CON.5=Y.1
CCP1CON.4=Y.0

That's all there is to it... simple? Sure it is... like I keep telling everyone - Read the Datasheet. Actually, it's so simple to fire up the PWM module, you can dispense with PBP's HPWM and manipulate the registers yourself (detailed instructions in Datasheet)... ends up using heaps less program code.

Lastly, don't forget to set TRIS appropriately for output on the HPWM channel you are using. For a 16F628 this will be TRISB.3=0, For a 16F877 this would be TRISC.2=0.

Melanie