Don't like ads? PRO users don't see any ads ;-)
Guest

pulsemeter

By: a guest on May 28th, 2012  |  syntax: C  |  size: 1.90 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*
  2.   Project: Measuring heart rate through fingertip
  3.   Copyright @ Rajendra Bhatt
  4.   January 18, 2011
  5.   PIC16F628A at 4.0 MHz external clock, MCLR enabled
  6. */
  7.  
  8. sbit IR_Tx at RA3_bit;
  9. sbit DD0_Set at RA2_bit;
  10. sbit DD1_Set at RA1_bit;
  11. sbit DD2_Set at RA0_bit;
  12. sbit start at RB7_bit;
  13. unsigned short j, DD0, DD1, DD2, DD3;
  14. unsigned short pulserate, pulsecount;
  15. unsigned int i;
  16. //-------------- Function to Return mask for common anode 7-seg. display
  17. unsigned short mask(unsigned short num) {
  18.  switch (num) {
  19.  case 0 : return 0xC0;
  20.  case 1 : return 0xF9;
  21.  case 2 : return 0xA4;
  22.  case 3 : return 0xB0;
  23.  case 4 : return 0x99;
  24.  case 5 : return 0x92;
  25.  case 6 : return 0x82;
  26.  case 7 : return 0xF8;
  27.  case 8 : return 0x80;
  28.  case 9 : return 0x90;
  29.  } //case end
  30. }
  31.  
  32. void delay_debounce(){
  33.  Delay_ms(300);
  34. }
  35.  
  36. void delay_refresh(){
  37.  Delay_ms(5);
  38. }
  39.  
  40. void countpulse(){
  41.  IR_Tx = 1;
  42.  delay_debounce();
  43.  delay_debounce();
  44.  TMR0=0;
  45.  Delay_ms(15000);  // Delay 1 Sec
  46.  IR_Tx = 0;
  47.  pulsecount = TMR0;
  48.  pulserate = pulsecount*4;
  49. }
  50.  
  51. void display(){
  52.   DD0 = pulserate%10;
  53.   DD0 = mask(DD0);
  54.   DD1 = (pulserate/10)%10;
  55.   DD1 = mask(DD1);
  56.   DD2 = pulserate/100;
  57.   DD2 = mask(DD2);
  58.   for (i = 0; i<=180*j; i++) {
  59.     DD0_Set = 0;
  60.     DD1_Set = 1;
  61.     DD2_Set = 1;
  62.     PORTB = DD0;
  63.     delay_refresh();
  64.     DD0_Set = 1;
  65.     DD1_Set = 0;
  66.     DD2_Set = 1;
  67.     PORTB = DD1;
  68.     delay_refresh();
  69.     DD0_Set = 1;
  70.     DD1_Set = 1;
  71.     DD2_Set = 0;
  72.     PORTB = DD2;
  73.     delay_refresh();
  74.     }
  75.   DD2_Set = 1;
  76. }
  77.  
  78. void main() {
  79.  CMCON = 0x07;    // Disable Comparators
  80.  TRISA = 0b00110000; // RA4/T0CKI input, RA5 is I/P only
  81.  TRISB = 0b10000000; // RB7 input, rest output
  82.  OPTION_REG = 0b00101000; // Prescaler (1:1), TOCS =1 for counter mode
  83.  pulserate = 0;
  84.  j = 1;
  85.  display();
  86.  do {
  87.   if(!start){
  88.    delay_debounce();
  89.    countpulse();
  90.    j= 3;
  91.    display();
  92.   }
  93.  } while(1);  // Infinite loop
  94. }