Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

problem using a interrupt to count pulses in a second

Status
Not open for further replies.

vacant34

Newbie level 3
Newbie level 3
Joined
Nov 25, 2012
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,316
hi all iv been working on anemometer using a lpc936. my anemometer sends two pulses to the controller for every rotation.
im trying to use the external interrupt to count the amount of pulses in a second and use that variable to calculate the wind-speed
i would like someone to point out where im going wrong with the interrupt of if there a better method to calculate the wind-speed
thank you.

#include <REG936.H>
void delay(unsigned char no_loops);
//Level trigger external interrupt
sbit led =P2^0;
sbit IR = P1^3;
int count=0;
int speed=0;
void main()
{

EA=1;
//IE = 0x81;
IT0=1;
EX0=1;
led=1;
//count=0;

while(1);
//speed=speed/2;
}
void ISR_ex0(void) interrupt 0
{
int n;
//delay(50);

for(n=0;n<=9;n++)
{
if(IR==1); // if IR on port 1.3 goes 1 i.e. detects 5 volts
{

count++;
for(;IR ==1;); // wait for getting P1.3 to go low.
delay(50);
}



}
count=0;
}
//function to generate a delay of x * 50msec
void delay(unsigned char no_loops)
{
unsigned char x;
TR0 = 0;
TF0 = 0;
TMOD = 0x01;
TH0 = 0x87;
TL0 = 0xFF;
TR0 = 1;
for(x=0;x<no_loops;x++)
{
while(!TF0);
TF0 = 0;
TH0 = 0x87;
TL0 = 0xFF;
}
TR0 = 0;
}
 

If possible configure your external interrupt pin to Edge triggered rather than level triggered. If you can do that, then you can safely skip checking for IR in a loop in your ISR.


Pseudo code

void main()
{

do {
delay(1 sec);
speed = count /2; turns
count = 0;
}while(1);

}


void ISR_ex0(void) interrupt 0
{
if(1 == IR)
{
count++;
//reset the IR if needed
IR=0
}
}

implement the delay function


So in your main function you are waiting for a second and check how many interrupts are fired in that interval by means of count. And after reading counts you are resetting it for next second. So you get a speed value every second.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top