Advertisement
goebish

Realtime heart beat rate source code

Apr 26th, 2012
1,350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.10 KB | None | 0 0
  1. // goebish realtime heart beat monitor
  2. // video at http://www.youtube.com/watch?v=A5Pk9o-pu90
  3. volatile uint8_t count,rate;
  4. uint32_t start=millis();
  5.  
  6. void setup()
  7. {
  8.   DDRB=0xFF; // 7 segments cathodes
  9.   PORTB=0xFF; // 7 segments cathodes
  10.   PORTC |=0b111; // 7 segments anodes
  11.   DDRD |= 0b10000; // LED
  12.   DDRD &= ~0b10000000; // mode switch
  13.   PORTD |= 0b10000000; // mode switch pullup
  14.   attachInterrupt(0, onBeat, RISING ); // beat counter interrupt
  15.   Serial.begin(9600);
  16. }
  17.  
  18. void loop()
  19. {
  20.   if( PIND & 0b100)
  21.     PORTD |= 0b10000;  
  22.   else
  23.     PORTD &=~0b10000;
  24.   if(millis()-start > 20000)
  25.   {
  26.     rate=count*3;
  27.     count=0;
  28.     start=millis();
  29.   }
  30.   display();
  31. }
  32.  
  33. uint8_t digitMask(uint8_t num) {
  34.  switch (num) {
  35.  case 0 : return 0xC0;
  36.  case 1 : return 0xF9;
  37.  case 2 : return 0xA4;
  38.  case 3 : return 0xB0;  
  39.  case 4 : return 0x99;
  40.  case 5 : return 0x92;
  41.  case 6 : return 0x82;
  42.  case 7 : return 0xF8;
  43.  case 8 : return 0x80;
  44.  case 9 : return 0x90;
  45.  }
  46. }
  47.  
  48. void onBeat()
  49. {
  50.   static uint32_t lastBeat,periodBuffer[3];
  51.   static uint8_t bufferPos=0,looped=false;
  52.   if(!(PIND&0b10000000) && lastBeat)
  53.   {
  54.     uint32_t beatPeriod = millis()-lastBeat;
  55.     lastBeat = millis();
  56.     periodBuffer[bufferPos] = beatPeriod;
  57.     if(++bufferPos==3)
  58.     {
  59.       looped=true;
  60.       bufferPos=0;
  61.     }
  62.     if(!looped)
  63.       rate = 60000/beatPeriod;
  64.     else
  65.       rate = (60000/periodBuffer[0]+60000/periodBuffer[1]+60000/periodBuffer[2])/3;
  66.     Serial.println(rate,DEC);
  67.   }
  68.   count++;
  69. }
  70.  
  71. void display()
  72. {
  73.   static uint8_t curDigit;
  74.   PORTB = 0xFF;
  75.   if(++curDigit==3)
  76.     curDigit=0;  
  77.  
  78.   if(curDigit==0)
  79.   {
  80.     DDRC &= ~0b110;
  81.     PORTC&= ~0b110;
  82.     DDRC |=  0b001;
  83.     PORTC|=  0b001;
  84.     PORTB = digitMask(rate%10);
  85.   }
  86.   else if(curDigit==1)
  87.   {
  88.     DDRC &= ~0b101;
  89.     PORTC&= ~0b101;
  90.     DDRC |=  0b010;
  91.     PORTC|=  0b010;
  92.     if(rate>9)
  93.       PORTB = digitMask((rate/10)%10);
  94.   }  
  95.   else
  96.   {
  97.     DDRC &= ~0b011;
  98.     PORTC&= ~0b011;
  99.     DDRC |=  0b100;
  100.     PORTC|=  0b100;
  101.     if(rate>99)
  102.       PORTB = digitMask((rate/100)%10);
  103.   }
  104.   delay(5);
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement