Understanding timers/interrupts

Discussion in 'Programmer's Corner' started by johnjameson2011, May 23, 2014.

  1. johnjameson2011

    Thread Starter New Member

    Feb 17, 2011
    12
    0
    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 ( (Unknown Language)):
    1. void extrint (void) interrupt 0 // external Interrupt to detect the pulse
    2. {
    3. bt=tick; // number of ticks are picked
    4. tick=0; // reset for next counting
    5. }
    6.  
    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 ( (Unknown Language)):
    1. void timer0 (void) interrupt 1 using 1 // Timer 0 for one second time
    2. {
    3. TH0 = 0xdc; //The value is taken for Ssc/100 at crystal 11.0592MHz
    4. sec100++; // It is incremented every Ssc/100 at crystal 11.0592MHz
    5. tick++; // This variable counts the time period of incoming pulse in Sec/100
    6. if(tick>=3500){tick=0;} // tick are limited to less than 255 for valid calculation
    7. if(sec100 >=100) // 1 sec = sec100 * 100
    8. {
    9. sec++;
    10. sec100=0;
    11. }
    12. }
    13.  
    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
     
  2. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    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. :)
     
  3. johnjameson2011

    Thread Starter New Member

    Feb 17, 2011
    12
    0
    Actually here's the rest of it to show the purpose of bt,I left out the lcd functions though
    Code ( (Unknown Language)):
    1. void main()
    2. {
    3. P0=0xff;
    4. P1=0xff;
    5. P2=0xff;
    6. P3=0xff;
    7. rw=0;
    8. EA =1;
    9. TMOD =0x21;
    10. IT0 =1;
    11. EX0 =1;
    12. ET0 =1;
    13. TR0 =1;
    14.  
    15. msdelay(1000);
    16. lcdinit();
    17. msdelay(1000);
    18. send_string("Heart beat ");
    19. msdelay(1500);
    20.  
    21. msdelay(500);
    22.  
    23. //delay(15000);
    24. bpm=0;bt=0;
    25.  
    26. while(1)
    27. {
    28.  
    29. if(sec >=1)
    30. {
    31. sec=0;
    32. /*
    33.  
    34. The sampling time is fixed 1 sec.
    35. A variable "tick" is incremented with one tick per 100mSc in the timer 0 interrupt routine.
    36. Each on occurring of external interrupt the value in the "tick" is picked up
    37. and it is set to zero for recounting.
    38. The process continues till next external interrupt.
    39. Formula for calculating beats per minutes (microcontroller based heartbeat monitor ) is
    40.  
    41. as tick is the time period in Sec/100. so extract the frequency of pulses at external interrupt
    42. Frequency = (1/tick)* 100 i.e pulses /sec
    43. Then
    44. bpm = frequency * 60 for one minutes i.e pulses per minute
    45. in short we can do it as
    46. bpm = 6000/ bt
    47.  
    48. */
    49. lcdcmd(0x02);
    50. if(bt >=7){
    51. bpm =6000/bt;// for valid output bt is limited so that it should be greater than 6
    52. msdelay(500);
    53. send_string("Pulse. ");
    54. lcddata((bpm/100)+0x30);
    55. r=bpm%100;
    56. lcddata((r/10)+0x30);
    57. lcddata((r%10)+0x30);
    58. send_string(" bpm ");
    59. }
    60. else{
    61. send_string("out of range");}// otherwise bpm will be shown zero,
    62. }
    63. }
    64. }
     
  4. johnjameson2011

    Thread Starter New Member

    Feb 17, 2011
    12
    0
    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?
     
Loading...