I am using: Curiosity LPC dev kit, PIC16F15325, MPLABX v5.35 on Mac, XC8 compiler. I am able to blink each LED on the Curiosity.
My goal is to generate a PWM signal on RC5 to drive an LED on the Curiosity board. I want to be able to vary the pulse width to adjust the brightness.
I originally was using the LFINTOSC at 31kHz, but I saw another post that said that you can't run the CCP module when TMR2 is using the LFINTOSC because it's too slow. I am happy to have the PWM at a frequency of <500Hz, so if it's possible to use the LFINTOSC, please let me know. For now I'm using the HFINTOSC at 1MHz.
I also noticed that the steps in the datasheet for configuring the PWM of the CCP module are slightly differently ordered from the steps for configuring the dedicated PWM module. Maybe this discrepancy is part of my issue.
For now I am just trying to enable the PWM in main and then loop on a blinking LED so that I know the MCU hasn't gotten stuck.
Nothing happens to the LED on RC5 when I run this code.
My code is as follows:
void main(void) {
ANSC4 = 0; //disable for analog input
TRISC4 = 1; //configure PORTC4 for digital input
TRISA = 0x00; //configure all PORTA to digital output
TRISC5 = 0; //configure PORTC5 to digital output
ANSA5 = 0; //disable analog inputs for LED pins
ANSA1 = 0;
ANSA2 = 0;
ANSC5 = 0;
LATA = 0x00; //Set all output low
LATC = 0x00;
IOCAP = 0x00; //Disable interrupts on PORTA
IOCAN = 0x00;
IOCAF = 0x00; //Clear PORTA interrupt flags
IOCCP = 0b00000000;
IOCCN = 0b00010000; //Set PORTC4 to interrupt on negative edge
IOCCF = 0x00; //Clear PORTC interrupt flags
GIE = 1; //Enable global interrupts
IOCIE = 1; //Enable interrupts on change
int duty_cycle_reg = 0b0000001100;
enable_pwm_ccp(duty_cycle_reg);
while(1){
LATA5 = 1;
delay_ms(100);
LATA5 = 0;
delay_ms(100);
}
}
Enable_PWM_CCP definition:
void enable_pwm_ccp(int duty_cycle_reg){
int lsb = 0b0000000011;
int msb = 0b1111111100;
lsb &= duty_cycle_reg;
msb &= duty_cycle_reg;
lsb = lsb << 6;
msb = msb >> 2;
RC5PPS = 0x09; //set CCP1 output to RC5 via PPS
TRISC5 = 1; //disable output
CCP1CONbits.MODE = 0xF; //set CCP1 to PWM mode
CCP1CONbits.FMT = 1; //set CCP1 format left-aligned
CCPR1H = msb; //set duty cycle
CCPR1L = lsb;
PIR4bits.TMR2IF = 0; //clear TMR2IF flag
T2CONbits.CKPS = 0b111; //set TMR2 prescale to 128
T2CONbits.OUTPS = 0b000; //set TMR2 postscaler to 1
T2CLKCON = 0x02;
PR2 = 0x26; //set PR2 to make PWM freq = 50Hz
T2CONbits.ON = 1; //enable TMR2
while(!PIR4bits.TMR2IF){} //wait for TMR2 to settle
TRISC5 = 0; //set RC5 for digital output
}