1
\$\begingroup\$

PIC16(L)F1786 has 3 CCP modules. I need a PWM signal on RB5 pin which belongs to CCP3 module, as well as RC6 pin. BUT, when I am setting up the PWM according to the datasheet (page 277), I am getting PWM signal on RC6 instead of RB5, even though in APFCON2 register CCP3SEL bit is set to 1. Is it even possible to get PWM on RB5 instead of RC6. And one more "BUT": I am using Proteus to test the firmware, and I am do not know is it a bug in Proteus or I am missing something. I am 99% sure I am doing something wrong since this is my 2nd day of PIC micro controller programming.

Thanks.

Below I am listing configuration and my code in MPLAB X IDE using RC8 compiler:

#pragma config FOSC = INTOSC
#pragma config WDTE = OFF       
#pragma config PWRTE = OFF    
#pragma config MCLRE = ON   
#pragma config CP = OFF    
#pragma config CPD = OFF     
#pragma config BOREN = ON    
#pragma config CLKOUTEN = OFF   
#pragma config IESO = ON      
#pragma config FCMEN = ON       

#pragma config WRT = OFF        
#pragma config VCAPEN = OFF     
#pragma config PLLEN = OFF      
#pragma config STVREN = ON     
#pragma config BORV = LO      
#pragma config LPBOR = OFF      
#pragma config LVP = ON      

#include <xc.h>

void main(void) {
    TRISA = 0x00;
    TRISB = 0x00;
    TRISC = 0x00;
    TRISE = 0x04;
    TRISBbits.TRISB5 = 0x01;
    TRISCbits.TRISC6 = 0x01;


    PR2 = 0x63; // PWMPeriod = (PR2(99)+1)*4*TMR2_Prescale_Value(1)/(f(8 000 000)) = 20kHz
    CCP3CONbits.CCP3M = 0x00;

    // CCP3M Mode Select bits 11xx = PWM mode
    CCP3CONbits.CCP3M3 = 0x01;
    CCP3CONbits.CCP3M2 = 0x01;

    CCPR3L = 0x32;
    CCP3CONbits.DC3B = 0x00; // CCP3L:DC3B = 0x32:0x00 = 00110010 00 = 200 DEC

    PIR1bits.TMR2IF = 0x00;

    APFCON2bits.CCP3SEL = 0x01; // CCP3SEL: CCP3 Input/Output Pin Selection bit, if 1 then CCP3 is on pin RB5

    //Timer configuration
    T2CONbits.T2CKPS = 0x00; // Timer prescale value 1:1
    T2CONbits.T2OUTPS = 0x00; // Timer postscale value 1:1
    T2CONbits.TMR2ON = 0x01; // Enable the Timer

    while(!PIR1bits.TMR2IF) // Wait until the Timer overflows and set the TMR2IF bit of the PIR1 register
    { 
        TRISBbits.TRISB5 = 0x00; // Enable the CCP3 pin output driver
    }

    while(1)
    {
    }

    return;
}
\$\endgroup\$
8
  • 1
    \$\begingroup\$ Your code seems correct, it could be a Proteus bug. Do you have the actual chip, to test on the real HW, just to be sure? \$\endgroup\$
    – anrieff
    Commented Apr 29, 2018 at 20:11
  • \$\begingroup\$ It could also be a hardware bug, check for chip errata. \$\endgroup\$ Commented Apr 29, 2018 at 20:14
  • \$\begingroup\$ @anrieff no, unfortunately. \$\endgroup\$
    – Arttu
    Commented Apr 29, 2018 at 20:37
  • \$\begingroup\$ @RichardtheSpacecat in ERRATA there is an issue with ccp3 but only for Capture mode and only for PIC16(L)F1784/7 \$\endgroup\$
    – Arttu
    Commented Apr 29, 2018 at 23:48
  • \$\begingroup\$ Hmm. Looks like there's multiple things potentially muxed to the RB5: inputs to some analog comparator MSSP's SDO output and other stuff. \$\endgroup\$ Commented Apr 30, 2018 at 0:03

1 Answer 1

1
\$\begingroup\$

I don't use Proteus but here is an example that works in the MPLABX simulator:

enter image description here

This is the whole code:

/*
 * file: main.c
 * author: dan1138
 * target: PIC16F1786
 * compiler: XC8 v1.35
 * IDE: MPLABX v4.05
 * 
 * Description:
 *  Show PWM3 output on RB5 in MPLABX simulator.
 */
#pragma config FOSC = INTOSC, WDTE = OFF, PWRTE = OFF, MCLRE = ON
#pragma config CP = OFF, CPD = OFF, BOREN = OFF, CLKOUTEN = OFF
#pragma config IESO = ON, FCMEN = OFF
#pragma config WRT = OFF, VCAPEN = OFF, PLLEN = OFF, STVREN = ON
#pragma config BORV = LO, LPBOR = OFF, LVP = OFF

#include <xc.h>

/*
 * Define what we expect the system oscillator frequency will be.
 */
#define FOSC (32000000ul)
#define FCYC (FOSC/4ul)
/*
 * Initialize this PIC hardware
 */
void PIC_Init( void ) {
    /* disable global interrupt sources */
    INTCON =  0;

    /* select 8MHz internal oscillator*/
    OSCCONbits.IRCF = 0b1110;
    /* Enable PLL for a 32MHZ system clock */
    OSCCONbits.SPLLEN = 1;

    /* disable all interrupt sources */
    PIE1 = 0;
    PIE2 = 0;
    PIE3 = 0;
    PIE4 = 0;

    /* Make all GPIOs inputs */
    TRISA = 0xFF;
    TRISB = 0xFF;
    TRISC = 0xFF;
    TRISE = 0xFF;

    /* set OPTION_REG to defaults */
    OPTION_REG = 0xFF;
}
/*
 * Initialize PWM3 on RB5
 */
void PWM3_Init( void ) {
    PIE3bits.CCP3IE = 0;            /* Disable CCP3 interrupts */
    PIE1bits.TMR2IE = 0;            /* Disable TIMER2 interrupts */
    TRISBbits.TRISB5 = 1;           /* Tri-state PWM3 output */
    CCP3CON = 0;                    /* Disable PWM3 */
    T2CON = 0;                      /* Stop TIMER2  */
    APFCON2bits.CCP3SEL = 1;        /* Select RB5 for CCP3 output */
    T2CONbits.T2CKPS = 1;           /* Select TIMER2 prescale as 1:4 */
    PR2 = (FCYC/(4ul*20000ul))-1;
    CCP3CONbits.CCP3M = 0b1100;     /* enable PWM3 */
    CCPR3L = (PR2+1u)>>1u;          /* Set for 50% duty cycle */
    PIR1bits.TMR2IF = 0;
    T2CONbits.TMR2ON = 1;           /* Start PWM clock source */
    while(!PIR1bits.TMR2IF);        /* wait for PWM to cycle once */
    PIR1bits.TMR2IF = 0;
    TRISBbits.TRISB5 = 0;           /* Turn on PWM3 output */
}
/*
 * Main application
 */
void main( void ) {
    unsigned char PWM_cycle_count = 9;

    PIC_Init();
    PWM3_Init();
    /* Process loop */
    for(;;) {
        if(PWM_cycle_count) {
            if(PIR1bits.TMR2IF) {
                PWM_cycle_count--;
                PIR1bits.TMR2IF = 0;
            }
            if(PWM_cycle_count == 5) {
                CCPR3L = 0; /* set duty cycle tp 0% */
            }
        } else {
             NOP(); /* put simulator breakpoint here */
        }
    }
}

The MPLAB v8.92 simulator does not fully support the PIC16F1786 so the PWM output is on RC6 only. No settings in the APFCON1 or APFCON2 registers changes this.

\$\endgroup\$

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.