Veketti
Full Member level 3
Dear All,
I’m stumbled with Timer1 in PIC16F1847 as it seems the initialization does make the microcontroller stuck. Especially this line: T1CON = 0b00111101; So when I comment that it will initialize correctly. I’m using 4MHz crystal connected to RA6 & 7. I try to have wake on sleep at RB0 and interrupt at the same pin as well. RA4 pin reads ADC voltage. Timer1 is configured to interrupt every 0.5 seconds where I set flags for display update and after 60 seconds commence sleep. Compiler is mikroC.
Here is the initialization and interrupts:
I’d be very thankful if somebody could point me what is wrong with my initialization?
I’m stumbled with Timer1 in PIC16F1847 as it seems the initialization does make the microcontroller stuck. Especially this line: T1CON = 0b00111101; So when I comment that it will initialize correctly. I’m using 4MHz crystal connected to RA6 & 7. I try to have wake on sleep at RB0 and interrupt at the same pin as well. RA4 pin reads ADC voltage. Timer1 is configured to interrupt every 0.5 seconds where I set flags for display update and after 60 seconds commence sleep. Compiler is mikroC.
Here is the initialization and interrupts:
C:
void InitMain(void) {
DACCON0.b7 = 0; // DAC disabled
SRCON0.b7 = 0; // SR latch disabled
CM1CON0.b7 = 0; // Disable comparator 1
CM2CON0.b7 = 0; // Disable comparator 2
TRISA = 0b00011100; // Set RA2,3,4 as inputs, others are output 0 = output 1 = input
// RA3 is VREF+, RA4 is ADC, RA2 tied to ground VREF-, RA5 transistor pull
TRISB = 0b10010011; // Set all RB as output except RB0, RB1, RB4, RB7. RB0 button RB1 provision for button
ANSELA = 0b00010000; // Set all as digital except RA4 which is ADC
ANSELB = 0b00000000; // Set all B ports as digital
INTCON = 0b11011000; // Interrupt on change bit enabled
OPTION_REG = 0b10000000;
T1CON = 0b00111101; //Timer1 Registers Prescaler= 8 - TMR1 Preset = 3035 - Freq = 2.00 Hz - Period = 0.500008 seconds
TMR1H = 11; // preset for timer1 MSB register
TMR1L = 219; // preset for timer1 LSB register
PIE1 = 0b00000001; // Enable Timer1 interrupt
IOCBP = 0b00000000;
IOCBN = 0b00000011; // Negative edge detection on pin RB0 & RB1
ADCON0 = 0b00010001; // AN4 is ADC, ADC is enabled
ADCON1 = 0b10010010; // FOSC/8
FlagForHalfSecond = 0;
}
void Interrupt(){
if (TMR1IF_bit){ // Timer Interrupt happened
TMR1IF_bit = 0;
if (TimerRounds > 120 ) { // 60 seconds to sleep
TimerRounds = 0;
FlagForSleep = 1;
} else TimerRounds ++;
FlagForHalfSecond = 1;
}
if (INTF_bit){ // Pin Interrupt happened
INTF_bit = 0;
if (IOCBF0_bit)
{
FlagForBtnPress = 1;
IOCBF = 0b00000000;
}
delay_ms(5);
}
}
I’d be very thankful if somebody could point me what is wrong with my initialization?