Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // goebish realtime heart beat monitor
- // video at http://www.youtube.com/watch?v=A5Pk9o-pu90
- volatile uint8_t count,rate;
- uint32_t start=millis();
- void setup()
- {
- DDRB=0xFF; // 7 segments cathodes
- PORTB=0xFF; // 7 segments cathodes
- PORTC |=0b111; // 7 segments anodes
- DDRD |= 0b10000; // LED
- DDRD &= ~0b10000000; // mode switch
- PORTD |= 0b10000000; // mode switch pullup
- attachInterrupt(0, onBeat, RISING ); // beat counter interrupt
- Serial.begin(9600);
- }
- void loop()
- {
- if( PIND & 0b100)
- PORTD |= 0b10000;
- else
- PORTD &=~0b10000;
- if(millis()-start > 20000)
- {
- rate=count*3;
- count=0;
- start=millis();
- }
- display();
- }
- uint8_t digitMask(uint8_t num) {
- switch (num) {
- case 0 : return 0xC0;
- case 1 : return 0xF9;
- case 2 : return 0xA4;
- case 3 : return 0xB0;
- case 4 : return 0x99;
- case 5 : return 0x92;
- case 6 : return 0x82;
- case 7 : return 0xF8;
- case 8 : return 0x80;
- case 9 : return 0x90;
- }
- }
- void onBeat()
- {
- static uint32_t lastBeat,periodBuffer[3];
- static uint8_t bufferPos=0,looped=false;
- if(!(PIND&0b10000000) && lastBeat)
- {
- uint32_t beatPeriod = millis()-lastBeat;
- lastBeat = millis();
- periodBuffer[bufferPos] = beatPeriod;
- if(++bufferPos==3)
- {
- looped=true;
- bufferPos=0;
- }
- if(!looped)
- rate = 60000/beatPeriod;
- else
- rate = (60000/periodBuffer[0]+60000/periodBuffer[1]+60000/periodBuffer[2])/3;
- Serial.println(rate,DEC);
- }
- count++;
- }
- void display()
- {
- static uint8_t curDigit;
- PORTB = 0xFF;
- if(++curDigit==3)
- curDigit=0;
- if(curDigit==0)
- {
- DDRC &= ~0b110;
- PORTC&= ~0b110;
- DDRC |= 0b001;
- PORTC|= 0b001;
- PORTB = digitMask(rate%10);
- }
- else if(curDigit==1)
- {
- DDRC &= ~0b101;
- PORTC&= ~0b101;
- DDRC |= 0b010;
- PORTC|= 0b010;
- if(rate>9)
- PORTB = digitMask((rate/10)%10);
- }
- else
- {
- DDRC &= ~0b011;
- PORTC&= ~0b011;
- DDRC |= 0b100;
- PORTC|= 0b100;
- if(rate>99)
- PORTB = digitMask((rate/100)%10);
- }
- delay(5);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement