Understanding timers/interrupts

Status
Not open for further replies.

johnjameson

Junior Member level 3
Joined
Jan 30, 2012
Messages
31
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Visit site
Activity points
1,516
Hey guys,
I'm looking over some code here for a pulse sensor and are just looking for some clarification on some points,from I can see the interrupt of the mirco is being used to detect the pulse when a finger is placed on a sensor,I'm just gonna post the interrupts rather than post the whole code.
Variables used are all ints
Code:
void extrint (void) interrupt 0 // external Interrupt to detect the pulse
{
bt=tick; // number of ticks are picked
tick=0; // reset for next counting
}
so is bt or tick equal to the number of pulses the sensor reads when a finger is placed over it?
And resets back when the finger is removed?

Code:
void timer0 (void) interrupt 1 using 1 // Timer 0 for one second time
{
TH0 = 0xdc; //The value is taken for Ssc/100 at crystal 11.0592MHz
sec100++; // It is incremented every Ssc/100 at crystal 11.0592MHz
tick++; // This variable counts the time period of incoming pulse in Sec/100
if(tick>=3500){tick=0;} // tick are limited to less than 255 for valid calculation
if(sec100 >=100) // 1 sec = sec100 * 100
{
sec++;
sec100=0;
}
}
Not sure on this one,is this a timing reference for the pulses? or something to work out a 1 second timer as it says in comment,I'm a bit lost here
 

As of this code, what it does is that as long as u place ur finger, an external interrupt is sensed and it inititates a timer as per code 2.. mind it code 2 is for ext interrupt 1 ie., it is responsible when the ext pin is hig or ur finger is placed.. what it does is it counts number of seconds, you have put your finger on. Now as soon as u remove the finger, the port goes low and the program counter jumps to code 1 and the value of count is assigned to bt and tick is reset to 0.

Thus in short it outputs the time (in seconds) you have placed ur finger (i.e., at logic 1 of ext pin) when you remove it (ie., logic 0 of ext pin)

Hope this helps..
Utkarsh Mathur
 

I think so,so code 2,first it generates 1 second timer,that then counts how many seconds your finger is placed until it removed,those seconds are saved in bt which is then passed to this main
Code:
void main()
{
P0=0xff;
P1=0xff;
P2=0xff;
P3=0xff;
rw=0;
EA =1;
TMOD =0x21;
IT0 =1;
EX0 =1;
ET0 =1;
TR0 =1;

msdelay(1000);
lcdinit();
msdelay(1000);
send_string("Heart beat ");
msdelay(1500);

msdelay(500);

//delay(15000);
bpm=0;bt=0;

while(1)
{

if(sec >=1)
{
sec=0;
/*

The sampling time is fixed 1 sec.
A variable "tick" is incremented with one tick per 100mSc in the timer 0 interrupt routine.
Each on occurring of external interrupt the value in the "tick" is picked up
and it is set to zero for recounting.
The process continues till next external interrupt.
Formula for calculating beats per minutes (microcontroller based heartbeat monitor ) is

as tick is the time period in Sec/100. so extract the frequency of pulses at external interrupt
Frequency = (1/tick)* 100 i.e pulses /sec
Then
bpm = frequency * 60 for one minutes i.e pulses per minute
in short we can do it as
bpm = 6000/ bt

*/
lcdcmd(0x02);
if(bt >=7){
bpm =6000/bt;// for valid output bt is limited so that it should be greater than 6
msdelay(500);
send_string("Pulse. ");
lcddata((bpm/100)+0x30);
r=bpm%100;
lcddata((r/10)+0x30);
lcddata((r%10)+0x30);
send_string(" bpm ");
}
else{
send_string("out of range");}// otherwise bpm will be shown zero, 
}
}
}
I left out the lcd functions
 

Are you still facing some issues??
Just can't seem to work it out in my head.
If all the first two codes are doing,the interrupt and timer,are counting how long a finger is placed on the sensor in seconds? Then I dont see how the heartbeat can be worked out in the main() from that.
 

c the main point on having interrupts in a program is that they break the normal flow of program and the program jumps to a special function which we have defined it the interrupt is generated.. how it happens is like this:

1. An interrupt is generated (can be an edge triggered or a level triggered)
2. At this event, the system stores the address of current operation it was performing onto the stack and then the system checks which interrupt has occoured. It then checks its interrupt vector table for that particular interrupt.
3. From the table, it gets an address in the code space.
4. The program then jumps to that address.
5. At that address, if the program is too long, we jump to some other address where the entire interrupt service routine is written.
6. After it has processed the entire thing, it pops the address from the stack andthe program returns back to its normal functioning..

thus although in the main, you might not have called ur heartbeat function, but as soon as an interrupt occours, it is automatically called by itself and then the program functions normally...

hope this helps..
 

Ok so the interrupt takes priority over everything else.
Maybe I should backtrack to the timer code
Code:
void timer0 (void) interrupt 1 using 1 // Timer 0 for one second time
{
TH0 = 0xdc; //The value is taken for Ssc/100 at crystal 11.0592MHz
sec100++; // It is incremented every Ssc/100 at crystal 11.0592MHz
tick++; // This variable counts the time period of incoming pulse in Sec/100
if(tick>=3500){tick=0;} // tick are limited to less than 255 for valid calculation
if(sec100 >=100) // 1 sec = sec100 * 100
{
sec++;
sec100=0;
}
}
So when the interrupt is sensed this timer here start counting in seconds.
And it counts in seconds as thats how it has been defined withing that code
 

yes.. it breaks the normal routine of the code.. when the external pin goes high, the interrupt for timer is initiate and evertime the timer ticks, this timer0 function is automatically called and the value of tick is incremented by one.. as soon as the ext pin goes low, the program jumps to the other code and then it functions normally in the main code untill we get the interrupt again...
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…