Entire Forum This board This topic Members Entire Site
 Welcome, Guest. Please login or register.Did you miss your activation email? April 26, 2018, 02:39:21 02:39 1 Hour 1 Day 1 Week 1 Month Forever Login with username, password and session length
 Pages: [1]
 Author Topic: PLS HELP, PIC16F877 TIMER0 PROBLEM  (Read 2372 times) 0 Members and 1 Guest are viewing this topic.
microtech
Newbie

Offline

Posts: 29

Thank You
-Given: 13

 « on: September 30, 2010, 01:28:54 13:28 »

Hi,
I am using pic17f877 to measure motor speed in RPM.
I connect sensor output(pulse) to RB0 interrupt.
Turn ON Timer0 and check the RB0 pin for every 1 sec to count the pulse and then multiply the data with 60 to get RPM -- per 1 min.
Here is my code, Pls help me to check and advice me , is this is the correct way of calculate the RPM. tHANKS.

CRYSTAL FREQ: 4 MHZ

if (TMR0 ==250 ){
TMR0 = 0;
SEC ++;

if(SEC == 1000 ){ //
RPM = (count/*60;
SEC = 0;

}
}
/////////////////////////////////////

static void interrupt
isr()
{

if(INTF==1)
{
INTF=0;
count++;

}
}
 Logged
TomJackson69
Active Member

Offline

Posts: 221

Thank You
-Given: 23

 « Reply #1 on: September 30, 2010, 02:48:23 14:48 »

Assume that each RB0 int is one revolution.

How do you get TMR0 = 250 equal ONE second? I think at 4MHz, 250 count is 62.5uS that is:
1/4000000 = 0.000000250
So 1000000uS/ 62.5uS = 16000

You will need few variables to keep track of overall operation. Let say we have:
-   Num_Of_62_5uS
-   Num_Of_Sec _

if (TMR0 ==250 )
{
TMR0 = 0;
Num_Of_62_5uS++;

if(Num_Of_62_5uS == 16000)
{
Num_Of_Sec += 1;   // increase number of second count (do you need to wait this long to update???)
RPM = count * 60;   // you have got the RPM
// Now print out or do whatever with it…
}
}

Other way to do is use TMR0 interrupt. Since it count from 0 to 255 and roll back to 0 and generate INT. You can calculate how long it takes for TMR0 INT. Than find how many TMR0_INT to get one second. When you get to one second than get the count value. This count value multiply to 60 you get the RPM.

Please check to make sure the PIC16F877 has one instruction per clock cycle.

This is just an idea, you must test your program to see if it is correct.

Tom
 « Last Edit: September 30, 2010, 02:51:55 14:51 by TomJackson69 » Logged

Con Rong Chau Tien
Top
Senior Member

Offline

Posts: 333

Thank You
-Given: 43

 « Reply #2 on: September 30, 2010, 05:50:37 17:50 »

Maybe easier to connect sensor to RC2/CCP1 pin and use Timer1 Capture Mode to measure time between pulses. Then RPM = 60/time.