I am just starting to learn how to program a pic. I'm pretty much teaching myself this. I'm using the pic16f1829. I'm playing around with the examples and trying to do PWM. Obviously, it works and the code is fine. But I'd like it(just for future use and figuring it out how) for the PWM signal to come out of any of pin. Currently, it comes out of RC3. I've tried changing the TRISC and LATC to different (I don't know the actual terms, yet) values, but I haven't had any results. Is there something in the datasheet I'm missing on assigning the pins?

void main(void) {
OSCCON = 0b00111000;                            //500KHz clock speed
TRISC = 0;                                      //all LED pins are outputs
LATC = 0;

                                                //setup ADC
TRISAbits.TRISA4 = 1;                           //Potentiamtor is connected to RA4...set as input
ANSELAbits.ANSA4 = 1;                           //analog
ADCON0 = 0b00001101;                            //select RA4 as source of ADC and enable the module (AN3)
ADCON1 = 0b00010000;                            //left justified - FOSC/8 speed - Vref is Vdd

                                                //setup the PWM
CCP2CON = 0b00001100;                           //PWM mode single output
PR2 = 255;                                      //Frequency at 486Hz. Anything over ~60Hz will get rid of any flicker
                                                //PWM Period = [PR2 + 1]*4*Tosc*T2CKPS = [255 + 1] * 4 * (1 / 500KHz) * 1
CCPTMRSbits.C2TSEL = 0b00;                      //select timer2 as PWM source
T2CONbits.T2CKPS = 0b00;                        //1:1 prescaler
T2CONbits.TMR2ON = 1;                           //start the PWM

while (1) {
    __delay_us(5);                              //wait for ADC charging cap to settle
    GO = 1;                                     //start the ADC conversion
    while (GO) continue;                        //wait for conversion to be finished
    CCPR2L = ADRESH;                            //put the top 8 MSbs into CCPR2L
    CCP2CONbits.DC2B = (ADRESL>>6);             //put the 2 LSbs into DC2B register to complete the 10bit resolution


Like I said, the code is straight example. Would someone be willing to just point me in the right direction on how to change the output pin?

I'm on this site multiple times everyday, and rather than down voting me(I'm trying to be able to comment on things) just say "here, in the comments is your answer! now delete this question!" and I will.


3 Answers 3


You can do a software PWM on any pin (not recommended), but you must use a pin that has a CCP timer output in order to use the timer for the PWM source.

For instance, if you want PWM on pin 11, then you need to use CCP3, not CCP2 as you are currently configured. If you want to use pin 10, I see no CCP output assigned to it, so you would have to do a PWM in software (set pin, wait, clear pin, wait). Pin 9 has a CCP4 output, so you could use that, but you would probably have to change your timer to timer 4. And so on...

This is something to pay attention to when you are making your schematic.

  • \$\begingroup\$ Okay, I see exactly what you are saying. Nice little chart in the datasheet that spells it all out. \$\endgroup\$
    – Rayray
    Commented Apr 13, 2016 at 3:05
  • \$\begingroup\$ Awesome, welcome to microcontrollers, they are a lot of fun! \$\endgroup\$ Commented Apr 13, 2016 at 3:05
  • \$\begingroup\$ Yes, thank you. I've been racking my brain on this for a couple hours. I'm trying to move over from hardware. Let me tell you, for me, hardware is a million times easier to understand. \$\endgroup\$
    – Rayray
    Commented Apr 13, 2016 at 3:07
  • \$\begingroup\$ You get better at it. And all microcontroller series have their quirks... and different ones from each other. The PIC16F series (in assembly, never did C) is what I learned on, so I remember (some of) the quirks. Once you get past your first project or two, it gets a lot easier. Keep at it! \$\endgroup\$ Commented Apr 13, 2016 at 3:12
  • \$\begingroup\$ @Rayray, this is going to sound like I'm muddying the waters, but, (and this goes for other embedded controllers too) you can also (occasionally) move features to different pins. The pic16f1829 is fairly limited (you can only move up to 16 features to 1 of 2 pins using the ADCON0 & ADCON1 registers). But larger Microchip processors have many more options. Frankly, it can get really confusing fast. Especially if you make use of multiplexing features on the same pin in your final design! \$\endgroup\$
    – st2000
    Commented Apr 13, 2016 at 3:25

This MCU has PWM steering, so you have to have the PWM in single-output mode, clear the appropriate bit in the TRIS register, and set the PSTR1CON/PSTR2CON registers to steer the output to whatever pins Microchip in their wisdom have allowed it to be routed to.

It's all in the datasheet, which is worth reading. See page 244 for example.


Not all features of the PIC are available on all pins. And some features can be move to different pins. The APFCON1 register, bit 0 controls which pin the CCP2 feature is connected to. If this bit is 0 CCP2 is connected to RC3. If 1 CCP2 is connected to RA5.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.