Mini Projectcode
Mini Projectcode
Int thresh = 512; // used to find instant moment of heart beat, seeded
Bit firstBeat; // used to seed rate array so we startup with reasonable BPM
Bit secondBeat; // used to seed rate array so we startup with reasonable BPM
// these variables are volatile because they are used during the interrupt service routine!
Unsigned int IBI = 600; // holds the time between beats, must be seeded!
Bit Pulse ; // true when pulse wave is high, false when it’s low
Bit QS;
Int I = 0;
Void InitTimer0(){ //timer0 is set to Interrupt every 2ms. PIC18F452 is running at 32MHz
T0CON = 0xC5;
TMR0L = 0x06;
GIE_bit = 1;
TMR0IE_bit = 1;
Void Interrupt(){
GIE_bit = 0;
Signal = ADC_Get_Sample(0);
sampleCounter += 2;
Trough = Signal;
P_cnt = Signal;
If ( (Signal > thresh) && (Pulse == FALSE) && (N_cnt > (IBI/5)*3) ){
Pulse = TRUE; // set the Pulse flag when we think there is a pulse
for(i=0; i<=9; i++){ // seed the running total to get a realisitic BPM at startup
rate[i] = IBI;
}
If(firstBeat){ // if it’s the first time we found a beat, if firstBeat == TRUE
BPM = 60000/runningTotal; // how many beats can fit into a minute? That’s BPM!
If (Signal < thresh && Pulse == TRUE){ // when the values are going down, the beat is over
Pulse = FALSE; // reset the Pulse flag so we can do it again
Trough = thresh;
TMR0IF_bit = 0;
TMR0L = 0x06;
GIE_bit =1;
Void main() {
Int g;
OSCCON.IRCF0=0;
OSCCON.IRCF1=1;
OSCCON.IRCF2=1;
ANSELA=0x01;
ANSELB=0x00;
ANSELC=0x00;
ANSELD=0x00;
ANSELE=0x00;
Pulse = FALSE;
QS = FALSE;
firstBeat = TRUE;
secondBeat = FALSE;
Delay_ms(200);
Lcd_Cmd(_LCD_CURSOR_OFF);
Delay_ms(200);
Lcd_Out(1,1, intro_msg);
Lcd_Cmd(_LCD_SHIFT_LEFT);
Delay_ms(250);
Delay_ms(200);
ADC_Init();
InitTimer0();
While(1){
Lcd_Out(2,1,” “);
IntToStr(BPM, temp_str);
Lcd_Out(2,8,temp_str);
Delay_ms(2000);