Hi All,
trying to get Timer1 working with a 32.678kHz crystal. Using a PIC16LF1937 on a board which one of my co workers has used before.
Crystal is on the T1OSCIN and OUT pins.
What happens: RE2 goes high for 0.5sec and goes low again as expected in a loop. RD1 goes high and stays high....
Code:
#include <pic.h>
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
//#define _XTAL_FREQ 4000000UL
#define _XTAL_FREQ 32000000UL
//#define _XTAL_FREQ 4000000
#pragma config CPD=OFF, BOREN=OFF, IESO=OFF, FOSC=INTOSC, FCMEN=OFF, MCLRE=OFF, WDTE=OFF, CP=OFF, PWRTE=ON, CLKOUTEN=OFF, LVP=ON, PLLEN=ON
unsigned int wat = 0;
int main(void) {
TRISD1 = 0;
TRISE2 = 0;
//internal 32kHz clock
OSCCONbits.SCS = 0b00;
OSCCONbits.IRCF = 0b1110;
OSCCONbits.SPLLEN = 1;
// configure Timer1
T1CONbits.TMR1CS = 2; //clock source as 10, external crystal
T1CONbits.T1OSCEN = 1; // enable external oscillator
T1CONbits.nT1SYNC = 0; // sync external clock with system clock
T1CONbits.TMR1ON = 1; // enable Timer1
// setup Timer1 interrupt flags to default
PIR1bits.TMR1IF = 0; //Timer1 Overflow Interrupt not pending
PIE1bits.TMR1IE = 1; //Timer1 Overflow interrupt enable
INTCONbits.PEIE = 1; // peripheral interrupt enable
ei();
// main loop
while (1) {
PORTEbits.RE2 = 0;
__delay_ms(500);
PORTEbits.RE2 = 1;
__delay_ms(500);
}
return 0;
}
void interrupt isr()
{
if (TMR1IF && TMR1IE) {
TMR1IF = 0; // clear flag
INTCONbits.GIE = 1;
wat++;
if (wat > 1) {
PORTDbits.RD1 = ~PORTDbits.RD1;
wat = 0;
}
return;
}
}
Any help would be appreciated, Ive been trying to get this going all day with help from the datasheet...