Understanding timers/interrupts

Thread Starter

johnjameson2011

Joined Feb 17, 2011
12
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
Rich (BB 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?

Rich (BB 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
 

THE_RB

Joined Feb 11, 2008
5,438
The second code look like they are using TMR0 to make 1/100th second period, ie 10mS, then every 100 times it makes a second.

I don't like that method using TMR0 because they are writing to the timer in an interrupt which is inaccurate for a number of reasons. One is the TMR0 write latency which is 2 Tcy, another reason is because you can variable interrupt entry time (if you have multiple interrupts with selection).

At the very least if you MUST write to TMR0 do it as a += which advances TMR0 from where it IS, not where it should be. Write latency will be 3 Tcy.

Or better still, use TMR2 and PR2 to make a fixed interval timer interrupt at 10mS, or use one of the zero-error code examples from this page;
http://www.romanblack.com/one_sec.htm

Which can make a perfect average 10mS period from ANY xtal value in ANY timer interrupt. :)
 

Thread Starter

johnjameson2011

Joined Feb 17, 2011
12
Actually here's the rest of it to show the purpose of bt,I left out the lcd functions though
Rich (BB 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, 
}
}
}
 

Thread Starter

johnjameson2011

Joined Feb 17, 2011
12
Actually just a question on a bit of the heartbeat calculation
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

How is this shortcut working? or what I mean is where did the 6000 figure come from?
 
Top