I'm after a little help or pointers regarding the Timer 1 interrupt using a PIC16f1847, MikroC Pro and 8MHZ MCU clock.
I've added the code to my project which was created by the Timer Calculator.
Code: Select all
//Timer1
//Prescaler 1:4; TMR1 Preload = 15536; Actual Interrupt Time : 100 ms
//Place/Copy this part in declaration section
void InitTimer1(){
T1CON = 0x21;
TMR1IF_bit = 0;
TMR1H = 0x3C;
TMR1L = 0xB0;
TMR1IE_bit = 1;
INTCON = 0xC0;
}
void Interrupt(){
if (TMR1IF_bit){
TMR1IF_bit = 0;
TMR1H = 0x3C;
TMR1L = 0xB0;
//Enter your code here
}
}
I'm obviously missing something because I was expecting this interrupt to occur every 100ms but I am seeing this happen at just less than 2 seconds and not the 100ms that I was expecting.
This is me. I'm trying to learn and understand this so any help would be appreciated.
Ian
The actual current code is;
Code: Select all
sbit Relay at RB3_bit;
sbit Led at RB2_bit;
unsigned short cnt = 0;
//Timer1
//Prescaler 1:4; TMR1 Preload = 15536; Actual Interrupt Time : 100 ms
//Place/Copy this part in declaration section
void InitTimer1(){
T1CON = 0x21;
TMR1IF_bit = 0;
TMR1H = 0x3C;
TMR1L = 0xB0;
TMR1IE_bit = 1;
INTCON = 0xC0;
}
void Interrupt(){
if (TMR1IF_bit){
TMR1IF_bit = 0;
TMR1H = 0x3C;
TMR1L = 0xB0;
cnt++;
}
}
void main() {
//CMCON = 0x07; // turn off comparators
PORTA = 0; // All PORTA pins are cleared
TRISA = 0b00011111; //
PORTB = 0; // All PORTB pins are cleared
TRISB = 0b11110011; //
OSCCON = 0x72;
InitTimer1(); // Initiate Timer 1
LED = 1; // Switch the LED On
RELAY = 0; // Switch the RELAY Off
do {
if( cnt == 10 ){ LED = 0; } // cnt is incremented by the Intrrupt
if( cnt == 20 ){ LED = 1; cnt = 0;}
} while (1);
}