[Solved: Faulty connection] PIC 12F1840 CCP/PWM Woes - Page 1
Forum section: electronics / general

author: Jamie_A
author_rank: Contributor
timestamp: August 02, 2016, 09:02:57 pm
content: I'm new to the PIC ecosystem, and over the past few days have been experimenting with MPLAB X and the 12F1840, using the XC8 compiler. I'm able to leverage many of the features of the chip quite easily (interrupts, timer interrupts, ADC, GPIO, pull-ups, etc) and the documentation is beautiful.With that said, I'm currently bashing my head against the wall trying to get the CCP module working in PWM mode. I've followed the datasheet steps precisely, and even tried some sample code. No pulse width values work, and I get zero output.The Init sample I've provided is what I'm working with currently, after I've stripped all the other features out of my project. I can confirm it's being run on Main, and that the chip works fine otherwise.Frequency -> 8MHzVoltage -> 5.0VProbing -> RA2 & RA5Code:void InitApp(void){WPUA = 0b00000000; // no Pull-upsTRISA = 0b00000000; // IOANSELA = 0x00; //disable analogINTCONbits.GIE=1;// Global interrupt enable/*PWM Init*/TRISA |= (1<<2); //Disable output 2PR2 = 0xFF; // PWM PeriodCCP1CONbits.P1M = 0b00; // Single output, P1A modulated onlyCCP1CONbits.CCP1M = 0b1100; // PWM mode, P1A active-high, P1B active-highCCPR1L = 0xDF; //PWM pulse width (tried many combinations)CCP1CONbits.DC1B = 0b00; //Least significant PWM pulse width bits/* Initialize Timer 2 */PIR1bits.TMR2IF = 0; // Clear the interrupt flag for Timer 2T2CONbits.T2CKPS = 0b00; // Set a pre-scaler of 1:1T2CONbits.TMR2ON = 1; // Enable the timerwhile (!PIR1bits.TMR2IF); //wait for timer overflowTRISA &= ~(1<<2); //Enable output 2}Configuration bits:Code:#pragma config FOSC = INTOSC // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)#pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled)#pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)#pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled)#pragma config BOREN = OFF // Brown-out Reset Enable (Brown-out Reset disabled)#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is enabled on the CLKOUT pin)#pragma config IESO = ON // Internal/External Switchover (Internal/External Switchover mode is enabled)#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)// CONFIG2#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)#pragma config PLLEN = ON // PLL Enable (4x PLL enabled)#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)#pragma config LVP = ON // Low-Voltage Programming Enable (Low-voltage programming enabled)From what I gather, after first run the TMR2IF gets cleared after each overflow, and I don't need to deal with it in my ISR (which I can confirm is not being triggered anyhow).I've also tried the alternate CCP1 pin (setting the APFCON->CCPISEL register) and still have zero signal.Any input would be much appreciated.EDIT: I performed an idiot check, my probing was the culprit. Now preforming self shame

author: igendel
author_rank: Frequent Contributor
timestamp: August 02, 2016, 09:32:02 pm
content: I think that, in addition to setting the GIE bit, you need to set the PEIE bit too (Bit 6 of INTCON, Peripheral interrupt Enable - for timer2).

author: Jamie_A
author_rank: Contributor
timestamp: August 02, 2016, 10:09:44 pm
content: Faulty connection! I'm a fool. Sorry for any wasted time. All works fine nowMods feel free to delete.

author: igendel
author_rank: Frequent Contributor
timestamp: August 02, 2016, 10:18:40 pm
content: Hmmmm. Turns out I was mistaken too - you didn't need the interrupt at all. It was simply the only difference I found between your code and something I have written in the past...