PIC - pic16F877A
Clock - 8MHz
Problem - beginner in PIC programming
I wan't to simulate a clock. Somebody suggest me to use timer2.
But the time count is not correct. Some one says i need to set the PR2.
Q - how to calculate the value for prescaler, postscaler and PR2 ???
my code for Timer2 as below :
/**************** timer2 ***********************/
void timer2_init() {
// Setting Timer2 - postscaler rate = 1:16 ? what does this mean
// Prescale rate = 1:16 ? what does this mean.
T2CON = 0xFF;
TMR2 = 0; // Timer2 initial value
PIE1.TMR2IE = 1; // Timer 2 interrupt enable
INTCON = 0xC0; // Set GIE, PEIE
PR2 = 0xFF; // how to calculate to get the right value -
}
void interrupt() { // Interrupt occurs on every timer register TMR0 overflow.
if (PIR1.TMR2IF) {
cnt++; // Interrupt causes cnt to be incremented by 1
PIR1.TMR2IF = 0; // reset timer 2 interrupt flag
TMR2 = 0; // set TMR2 to initial value
if (cnt > 100) { // actually i trying to get 100 count = 1 sec or may be less ??
cnt = 0; ss++;
}
}
}
/**************** timer0 ***********************/
Clock - 8MHz
Problem - beginner in PIC programming
I wan't to simulate a clock. Somebody suggest me to use timer2.
But the time count is not correct. Some one says i need to set the PR2.
Q - how to calculate the value for prescaler, postscaler and PR2 ???
my code for Timer2 as below :
/**************** timer2 ***********************/
void timer2_init() {
// Setting Timer2 - postscaler rate = 1:16 ? what does this mean
// Prescale rate = 1:16 ? what does this mean.
T2CON = 0xFF;
TMR2 = 0; // Timer2 initial value
PIE1.TMR2IE = 1; // Timer 2 interrupt enable
INTCON = 0xC0; // Set GIE, PEIE
PR2 = 0xFF; // how to calculate to get the right value -
}
void interrupt() { // Interrupt occurs on every timer register TMR0 overflow.
if (PIR1.TMR2IF) {
cnt++; // Interrupt causes cnt to be incremented by 1
PIR1.TMR2IF = 0; // reset timer 2 interrupt flag
TMR2 = 0; // set TMR2 to initial value
if (cnt > 100) { // actually i trying to get 100 count = 1 sec or may be less ??
cnt = 0; ss++;
}
}
}
/**************** timer0 ***********************/