Vous êtes sur la page 1sur 6

// Includes + Defines #include <inttypes.h> #include <avr/io.h> #include <avr/interrupt.h> #include <stdio.h> #include <avr/eeprom.h> #include <math.

h> #include <avr/pgmspace.h> #include <stdlib.h> #include <string.h> //#define F_CPU 16000000 UL #include <util/delay.h> // needed for lcd_lib #include <avr/sleep.h> #include "uart.h" #define begin { #define end } #define t1 15 //every 0.25 seconds #define t2 500 //every 8.2 seconds #define bat_tol 101 //tolerance value of battery code #define tol 8 //tolerance value of determine if patient is breathing code FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW); / /setup uart

//*************Macros************************************************ #define READ(U, N) ((U) >> (N) & 1u) #define SET(U, N) ((void)((U) |= 1u << (N))) #define CLR(U, N) ((void)((U) &= ~(1u << (N)))) #define FLIP(U, N) ((void)((U) ^= 1u << (N))) //*************Functions********************************************* void thermistor(void); //determine if patient is breathing void battery(void); //measure battery voltage void display(void); //display breathing rate (bpm) on LCD void measure(void); //measure breathing rate (bpm) void initialize (void); //**************Variables*************************************** //LCD variables const int8_t LCD_initialize[] PROGMEM = "LCD Initialized\0"; const int8_t LCD_line[] PROGMEM = "line 1\0"; const int8_t LCD_number[] PROGMEM = "Number=\0"; int8_t lcd_buffer[20]; // LCD display buffer volatile unsigned int time1, time2; //time out variables volatile int count; //keeps running count of clk cycles float breathrate; //breathing rate bpm char sample_old; //old time sample measured by ADC char sample; //new time sample measured by ADC volatile int tau1; //time 1 for respiration rate measurement volatile int tau2; //time 2 for respiratio n rate measurement int c; //counter for n umber of "no breaths" volatile char measure_flag; //set to one if AC ISR goes off

volatile int period; volatile char flag; float avg_period; ycles char bat_samp;

//period of one breath in clk cycles //set to one when AC ISR goes off //average period of one breath in clk c //battery voltage sample

//************ISR************************************************ //***********Timeout Counter ISR******************************** // timer 0 compare ISR ISR (TIMER0_COMPA_vect) begin //Decrement the three times if they are not already zero if (time1>0) --time1; //Used to time the thermistor measurements if (time2>0) --time2; //Used to time the battery voltage measurements if (count == 20000) //reset count so it doesnt overflow begin count = 0; tau1 = 0; end count++; end //**************AC ISR************************************ ISR (ANALOG_COMP_vect) begin CLR(ACSR, ACIE); flag = 1; pt tau2 = count; period = tau2 - tau1; tau1 = tau2; measure_flag = 1; end //*************Main********************************************* //Main int main (void) begin //charge capacitors DDRB = 0b00110000; //set B5,B4 to output SET(PORTB,PORTB4); //set B4 to 5V _delay_ms(5000); //wait for 5 seconds while charging CLR(PORTB,PORTB4); //turn B4 off DDRB = 0b00000000; //turn DDRB to all inputs initialize(); //for uart stdout = stdin = stderr = &uart_str; fprintf(stdout,"Starting...\n\r"); //main task scheduler loop //store new count value //calculate period //store old count value //activate measure function //turn off AC inturrupt //flag for turning on AC inturru

while(1) begin //testing fprintf statements //fprintf(stdout,"while\n\r"); //fprintf(stdout,"second = %d\n\r ",second); //fprintf(stdout,"tau 1 = %d tau2 = %d \n\r",tau1, tau2); //fprintf(stdout,"count = %d \n\r", count); //fprintf(stdout,"tau2 = %d \n\r",tau2); //fprintf(stdout,"period = %d\n\r ",period); //fprintf(stdout,"tau1 = %d \n\r",tau1); if (flag == 1) //for turning back on AC inturrupt begin _delay_ms(10); SET(ACSR,ACI); SET(ACSR, ACIE); //turn on AC inturrupt flag = 0; //reset flag end if (time1==0){ time1=t1; thermistor();} //determines if patient is //breathing or not if (time2==0){ time2=t2; battery();} g rate if (PINB & 0b00000010){display();} //if display button is pushed //activate the display function end end //main //**************Thermistor**************************** void thermistor(void) begin ADMUX = (1<<REFS0) | (1<<ADLAR) ; //turn on left adjust result, turn on external reference while(ADCSRA & (1<<ADSC)) ; // wait for conversion sample_old = sample; sample = ADCH; ADCSRA |= (1<<ADSC); // take sample from ADC // shift left //measures battery life

if (measure_flag == 1){measure_flag = 0; measure();} //measures breathin

if(abs(sample_old - sample) <= tol) begin c++; "no breath" detected if(c >= 10) { // increment counter if

//turn on pwm SET(DDRD,DDD5); TCCR1B = (1<<WGM12) | (1<< CS11); TCCR1A = (1<<COM1A0); OCR1A = 255; breathrate = 0; ate is 0 } end else begin c = 0; nt TCCR1A = 0; end end //*************Battery******************************* void battery(void) begin ADMUX = (1<<REFS0) | (1<<ADLAR) + 1 ; //turn on left adjust res ult, turn on second external reference ADCSRA |= (1<<ADSC); while(ADCSRA & (1<<ADSC)) ; version bat_samp = ADCH; variable to ADC sample //for testing fprintf(stdout, "bat samp = %d\n\r", bat_samp); if(bat_samp <= bat_tol) begin //turn on pwm SET(DDRD,DDD5); TCCR1B = (1<<WGM12) | (1<<CS11) | (1<<CS10); TCCR1A = (1<<COM1A0); OCR1A = 255; _delay_ms(1000); for 5 seconds TCCR1A = 0; end //pwm on //set //wait for con // keep PWM off // reset "no breath" cou //since patient isn't breathing, breathr

end //*************Display******************************* void display(void) begin LCDinit(); LCDcursorOFF(); LCDclr(); //initialize the display //clear the display

//sprintf(lcd_buffer, "win"); for testing sprintf(lcd_buffer, "%f Bpm", breathrate); LCDGotoXY(0,0); LCDstring(lcd_buffer, strlen(lcd_buffer)); //fprintf(stdout,"display \n\r"); for testing end //*************Measure******************************* void measure(void) begin avg_period = 0.75*avg_period + 0.25*period; //calculate avg period breathrate = 3/(avg_period*0.00164); //convert to bpm //for testing //fprintf(stdout,"breathrate: %f B/min\n\r", breathrate); end // measure task //****************Initialize***************************** void initialize(void) begin DDRA = 0b00000000; PORTA = 0b00000000; DDRB = 0b01000000; SET(PORTB,PORTB6); //Make PortA all inputs //turn off pull up resistors //set B6 to output //set B6 to 5V output

ADMUX = (1<<REFS0) | (1<<ADLAR) ; //turn on left adjust result, turn on external reference ADCSRA = (1<<ADEN) | (1<<ADSC) + 7; //turn on ADC, start conversion and set sample rate to 1Mhz //set up timer 0 for 1 mSec timebase TIMSK0= (1<<OCIE0A); atch ISR OCR0A = 255; re reg to 256 time ticks //turn on AC SET(ACSR, ACIE); nput capture on SET(ACSR, ACIS1); the comparater trigger on the rising edge //set prescalar to divide by 1024 //turn on timer 0 cmp m //set the compa

//turn analog i //ACIS1 and ACIS0 make

TCCR0B= 5; // turn on clear-on-match TCCR0A= (1<<WGM01) ; //init variables flag = 0; avg_period = 0; time1=t1; time2=t2; uart_init(); //crank up the ISRs sei(); end

Vous aimerez peut-être aussi