prayami
Joined: 22 Dec 2004 Posts: 78
|
CCP1 and Time1 Interrupt : calculate RPM |
Posted: Thu Jul 13, 2006 8:39 pm |
|
|
Hi...After having passed through very bad time I have solved this problem. As I got clue from this forum but that code was not working perfect for me. I think to post it so it can be useful for other like me.
I am calculating RPM using CCP1 and Timer1 interrupt. In the case when timer1 and CCP1 occur almost same time then it was calculating wrong RPM.
As this calculations are little bit different than http://www.ccsinfo.com/forum/viewtopic.php?t=22403 and http://www.ccsinfo.com/forum/viewtopic.php?t=906
I am posting my code here:
Code: |
#CASE
#include <18F452>
#fuses H4, NOWDT, NOLVP, BROWNOUT, BORV42, PUT, PROTECT, NOCPD, STVREN, NOWRT, NOWRTD, NOWRTC, NOWRTB, EBTR, NOCPB, EBTRB, NODEBUG
#use delay(clock=40000000)
#include <stdlib>
#byte PIR1 = 0x0F9E
//#bit TMR1IF = 0x0F9E.0
//#bit CCP1IF = 0x0F9E.2
unsigned int16 NewRPM=0;
unsigned int16 temptimer1=0,tempcount1=0;
int1 isCCP1=0;
unsigned int16 counttimer1=0;
unsigned int16 temptimer1_1=0,tempcount1_1=0;
unsigned int32 TimerCycle=0;
#int_ccp1
void CCP1_isr()
{
//When ever CCP1 interrupt occur it will copy the timer1 value to CCP_1 register.
//But in the situation when timer1 overflow happen at almost same time when CCP1
//interrupt occur.
if(bit_test(PIR1,0)) //TMR1IF
{
//Here there are two possibilities for CCP_1 value. One is CCP_1 value is
//achieved before timer1 overflow(example: CCP_1=65530 and get_timer1() = 5).
//Second is CCP_1 value achieved after timer1 overflow.(example: CCP_1 = 2 and
//get_timer1= 5)
//If we want high accuracy then use this code.
/*
if(get_timer1() < CCP_1)
{
temptimer1 = CCP_1;
}
else
{
temptimer1 = 0;
counttimer1++;
}
*/
//If we don't want high accuracy then use this code....Here we ignore some count of
//CCP_1. example: if CCP_1=65530 or CCP_1=4 in both the cases we are increamenting
//counttimer1 and take temptimer1=0;
temptimer1 = 0;
counttimer1++;
tempcount1 = counttimer1;
}
else
{
//If timer1 interrupt does not occur when CCP1 occur.
temptimer1 = CCP_1;
tempcount1 = counttimer1;
}
set_timer1(0);
counttimer1=0;
if(isCCP1==0)
{
temptimer1_1 = temptimer1;
tempcount1_1 = tempcount1;
}
isCCP1=1;
//This is because in the case when timer1 overflow occur after executing code from above
//else loop then clear TIMER1IF interrupt. We can do this because we are setting timer1 zero
//every time. i.e. set_timer1(0).
bit_clear(PIR1,0);
}//#int_ccp1
#INT_TIMER1
void timer1_isr() {
//Here again same like above:
//Here there are two possibilities for CCP_1 value.
//CCP1IF
if(bit_test(PIR1 ,2) && (get_timer1() < CCP_1))
{
/*
One is CCP_1 value is achieved before timer1 overflow(example: CCP_1=65530 and get_timer1() = 5).
Then we will use CCP_1 and counttimer1 will not be increamented.
*/
}
else
{
/*
There are two situations here:
(1) Second is CCP_1 value achieved after timer1 overflow.(example: CCP_1 = 2 and get_timer1= 5)
(2) Normal or regular timer1 overflow interrupt when no ccp1 interrupt occur.
*/
counttimer1++;
}
}//#INT_TIMER1
|
As my code does not displaying proper, I have slpit in two parts. |
|