I'm learning PIC C programming from Gooligum Enhanced C lessons. In lesson 14 CCP module there is example 4a which flashes LED every 500ms, see below: Using 16F1824 PIC
I can compile in MPLAB X and when I run it in simulation mode CCP2 which is shared with RC3 doesn't change state when TMR1 reaches CCPR2 value. I also loaded this in Proteus but LED doesn't flash also. Can anyone perhaps quickly compile this and see if it works? Or explain to me what is going on?
#include <xc.h>
#include <stdint.h>
/***** CONFIGURATION *****/ // ext reset, internal oscillator (no clock out), 4xPLL off
#pragma config MCLRE = ON, FOSC = INTOSC, CLKOUTEN = OFF, PLLEN = OFF // no watchdog timer, brownout resets enabled, low brownout voltage
#pragma config WDTE = OFF, BOREN = ON, BORV = LO // no power-up timer, no failsafe clock monitor, two-speed start-up disabled
#pragma config PWRTE = OFF, FCMEN = OFF, IESO = OFF // no code or data protect, no write protection
#pragma config CP = OFF, CPD = OFF, WRT = OFF // stack resets on, high-voltage programming
#pragma config STVREN = ON, LVP = OFF
/***** MAIN PROGRAM *****/
void main()
{
/*** Initialisation ***/
// configure ports
TRISC = ~(1<<3); // configure PORTC as all inputs
// except RC3 (CCP2 output)
// configure oscillator
OSCCONbits.SCS1 = 1; // select internal clock
OSCCONbits.IRCF = 0b0111; // internal oscillator = 500 kHz
// -> 8 us / instruction cycle
// initialise Timer1
TMR1 = 0; // clear timer
T1CONbits.TMR1CS = 0b00; // use instruction clock
T1CONbits.T1CKPS = 0b00; // no prescaler
T1CONbits.TMR1ON = 1; // enable timer
// -> increment TMR1 every 8 us
// configure ECCP2 module
CCPR2 = 500000/8; // initial compare time = 0.5 s /8 us/count
CCP2CONbits.CCP2M = 0b0010; // compare mode, toggle CCP2 on match
// (CCP2 initially low)
/*** Main loop ***/
for (;;)
{
// Toggle CCP2 output every 0.5 sec
// wait for CCP match
PIR2bits.CCP2IF = 0; // clear CCP2 interrupt flag
while (!PIR2bits.CCP2IF) // wait for flag to go high
;
// add 0.5 sec to last compare time
CCPR2 += 500000/8; // add 0.5 sec / 8 us/count
}
}