Can not get PIC18F14K50 Special Event Trigger to work
I have a Low Pin Count USB Development Kit with a PIC18F14K50 microcontroller on it, and I am programming it using the MPLAB REAL ICE.
I am trying to take AD conversions at regular intervals using the "Special Event Trigger" described in Chapter 14 of the PIC18F14K50 datasheet. The Special Event Trigger is a feature of the Enhanced Capture/Compare/PWM (ECCP) module, where during compare mode, the ECCP is supposed to trigger an AD conversion every time the comparison succeeds (i.e. the comparison registers match the selected timer).
Here is the C code I wrote, that I expect to work but doesn't:
// Configuration bits for the Low Pin Count Development Kit
#pragma config CPUDIV=NOCLKDIV, USBDIV=OFF
#pragma config IESO=OFF, FCMEN=OFF, PCLKEN=ON, PLLEN=ON, FOSC=HS
#pragma config PWRTEN=OFF, BOREN=OFF, BORV=30
#pragma config WDTEN=OFF
#pragma config LVP=ON, XINST=OFF
// Hardware on the Low Pin Count Development Kit
#define LED0 LATCbits.LATC0
#define LED1 LATCbits.LATC1
#define TRIS_LED0 TRISCbits.TRISC0
#define TRIS_LED1 TRISCbits.TRISC1
// LED0 and LED1 start off
TRIS_LED0 = 0;
TRIS_LED1 = 0;
LED0 = 0;
LED1 = 0;
// Configure RC5/CCP1/P1A to be an output, as it says to do in Section 14.3.1 of the data sheet
TRISCbits.TRISC5 = 0;
// Configure RB4/AN10 to be an analog input (that's the trimpot)
ANSELHbits.ANS10 = 1;
TRISBbits.TRISB4 = 1;
// Configure the ADC
ADCON1 = 0; // reference voltages are VDD and VSS
ADCON2 = 0b00111110; // FOSC/64, 20 TAD
ADCON0 = 0b00101001; // Enable ADC, select channel 10
// Configure Timers
T1CON = 0b10110001; // timer 1 enabled, prescaler 1:8, 16-bit read/writes
T3CON = 0; // timer 1 is the source for ECCP1
// Configure the ECCP
CCPR1H = 0xFF;
CCPR1L = 0xFF;
CCP1CON = 0b00001011; // start AD conversion on successful compare (TMR1L==CCPR1L and TMR1H==CCPR1H)
// If I uncomment this line, then one AD conversion will be
// done, so the LED1 will turn on once and stay on.
//ADCON0bits.GO = 1;
// The compare module triggers every .044 seconds.
// When that happens, it sets PIR1bits.CCP1IF. So you will see LED0
// toggle 23 times per second.
PIR1bits.CCP1IF = 0;
LED0 = !LED0;
// Also, when the compare module triggers, it SHOULD start an AD conversion,
// and when that is done, the ADC module should set PIR1bits.ADIF.
// So LED1 should be toggling also, but it does not.
PIR1bits.ADIF = 0;
LED1 = !LED1;
When I load the program above on to my PIC18F14K50, I expect to see LED0 and LED1 toggling 23 times for second. But I only see LED1 toggling.
I have attached the MPLAB Project, C source code, and precompiled HEX file for this program. It would be great if someone can look at it and tell me what I'm doing wrong, or tell me if this is a confirmed problem with the PIC18F14K50!