# Setting up a 16F87x for PWMOutput

Tony Nixon [sales at picnpoke.com] of ICmicro's  says:

Assume PIC runs at 4MHz 1000Hz wanted at 50% duty cycle

These PWM enable steps are taken from the 16F87X data sheet

1. Set the PWM period (1000Hz = 0.001S) by writing to the PR2 register.
```PWM Period = [PR2 + 1]*4*Tosc*TMR2 PreScale value
```

Use 4 for TMR2 prescale. You can try other values but make sure it works with the duty cycle calcs as well.

```Tosc = 1/4000000 = 2.5*10-7
```

Rearranging the equation gives...

```PR2 = (Period/(4 * Tosc * TMR2 Prescale)) - 1
```

Therefore...

```PR2 = (0.001/(4 * 2.5 * 10-7 * 4)) - 1 = 249

bsf STATUS,RP0
movlw d'249'    ; when TMR2 = 249 = end of Period
movwf PR2
bcf STATUS,RP0

```
2. Set the PWM Duty Cycle by writing to the CCPR1L register and CCP1CON<5:4> bits.

CCPR1L contains the upper 8 bits of the 10 bit Duty Cycle value CCP1CON<5:4> contain the lower 2 bits

```PWM Duty Cycle = (CCPR1L:CCP1CON<5:4>)*Tosc*TMR2 Prescale Value

CCPR1L:CCP1Con<5:4> = PWM Duty Cycle / (Tosc * TMR2 Prescale)

PWM Duty Cycle = 50% of Period

PWM Duty Cycle = 50% of 0.001 = 0.0005

CCPR1L:CCP1Con<5:4> = 0.0005 / (2.5 * 10-7 * 4)

CCPR1L:CCP1Con<5:4> = 500

500 in 10 bit binary = 0111110100

CCPR1L = 01111101    CCP1Con<5:4> = 00
```

Write the 10 bit value

```   movlw b'01111101'  ; set bits 9 - 2
movwf CCPR1L
bcf CCP1CON,CCP1X  ; set bit 1
bcf CCP1CON,CCP1Y  ; set bit 0

```
3. Make the CCP1 pin an output by clearing the TRISC<2> bit
```   bsf STATUS,RP0
movlw b'11111011'
andwf TRISC
bcf STATUS,RP0
```
4. Set The TMR2 prescale value and enable TMR2 by writing to T2CON.

We decided previously to set the prescale value to 4.

TMR2 prescale are bits 1 and 0 in T2CON
TMR2 enable is bit 2

```   movlw b'00000101'   ; TMR2 = on, prescale = 1:4
movwf T2CON
```
5. Configure the CCP1 module for PWM
```   movf CCP1CON,W
andlw b'00110000'  ; mask all but previously set Duty Cycle bits
iorlw b'00001111'  ; and enable PWM mode
movwf CCP1CON
```

A 1000 Hz square wave with a 50% duty cycle should now be coming out of RC2.

Liew Danny Jiefu says:

how do you use the pwm after setting it up, for example like on the leds? could u write a sample program?

Comments:

• Thnk yuo very much for the explanation. I have red a lot of papers, including Micfochips Datasheet (pic16F877. But no ones explain as clear as you.

Regards
Jaime+
• thanks for pwm answers..+
• hi my name is sylvester, an elect/elect student. i'm presently working on a school mini project using the hitech picc compiler & microcontrollers. Really appreciate this doc, helped me alot. thankz+
• Quite possible that my advice is thoroughly outdated, but as of Liew's question, first you have to decide an application for the PWM output (like driving a switch in a Switch Mode converter)...as far as using it on LED's is concerned, if you connect the LED, with suitable series resistence, to output RC2, you would indeed notice the difference in light intensity with duty ratio; however, the switching would be impossible to notice by naked eye at a frequnecy of 1kHz, but you can always look at the waveform on O-scope.+
• KILLspamrseaf~NOSPAM~ at dsamail.com.sg
Searching for so long on internet on how to program PWM for 16f877 and finally I found this! Thanks alot Tony. The datasheet should have consider adding your tutorial to it in the furture. Btw...
```movf CCP1CON,W
andlw b'00110000'  ; mask all but previously set Duty Cycle bits
iorlw b'00001111'  ; and enable PWM mode
movwf CCP1CON
```
What is the rationale behind this? I know it work but how?

See also:

 file: /Techref/microchip/16F877/pwm.htm, 5KB, , updated: 2012/10/1 11:36, local time: 2022/8/11 17:21, TOP NEW HELP FIND:  44.197.230.180:LOG IN

 ©2022 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?Please DO link to this page! Digg it! / MAKE! Setting up a 16F87x for PWM output

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.

Link? Put it here:
if you want a response, please enter your email address:
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
 Did you find what you needed? "No. I'm looking for: " "No. Take me to the search page." "No. Take me to the top so I can drill down by catagory" "No. I'm willing to pay for help, please refer me to a qualified consultant"

.