Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

How to monitor run time using timer2 in pic16F877A (time in minute and second)

Status
Not open for further replies.

bapak71

New Member
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 ***********************/
 
If you set the prescaler to 16, postscaler to 10 and PR2 to 125 you will get an interrupt 100 time per second (100Hz). Use a counter to count to 100 and then increment your seconds variable.

So,
T2CON=0b01001110;
PR2=125;

The way to work it out is,
Hz=Oscillator/4/prescaler/postscaler/pr2

Mike.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top