📄 code.c
字号:
}
}
//***********************************************
void deflatestate(void)
{
/*if(timedeflate >= 1900)
{
PORTD = 0;
if(timedeflate >= 2000)
{
PORTD = 0x02;
timedeflate = 0;
}
}*/
if((~PINB & 0x02) && (timepress1 > 30)) Maybe1 = 1;
if(Maybe1 && (PINB == 0xff))
{
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf("Emergency Stop");
sprintf(lcd_output,"%-i",(int)pressure);
lcd_gotoxy(0,1);
lcd_puts(lcd_output);
//turn off motor and open the valve
PORTD=0;
currentState = resetState;
Maybe1 = 0;
timepress1 = 0;
}
//if(done) --> Display state
if(currentState==deflateState) pressuremeasure(); //if still deflating, measure everything
}
//***********************************************
void display_state(void)
{
if(timedisplay<=1000)
{
if(timecount>=200)
{
lcd_clear();
timecount=0;
lcd_gotoxy(0,0);
lcd_putsf("Sys");
lcd_gotoxy(7,0);
lcd_putsf("Dias");
lcd_gotoxy(15,0);
lcd_putsf("HR");
sprintf(lcd_output,"%-i",(int)systolic);
lcd_gotoxy(0,1);
lcd_puts(lcd_output);
sprintf(lcd_output,"%-i",(int)diastolic);
lcd_gotoxy(7,1);
lcd_puts(lcd_output);
sprintf(lcd_output,"%-i",(int)pulse_per_min);
lcd_gotoxy(14,1);
lcd_puts(lcd_output);
}
}
else if (timedisplay>1000&&timedisplay<2000)
{
if(timecount>=200)
{ lcd_clear();
timecount=0;
lcd_gotoxy(0,0);
lcd_putsf("Black: Resume");
}
}
else
{
timedisplay=0;
}
if((~PINB & 0x04) && (timepress2 > 30)) Maybe2 = 1;
if(Maybe2 && (PINB == 0xff))
{
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf("White: Start");
lcd_gotoxy(0,1);
lcd_putsf("Grey: Stop");
currentState = startState;
timepress2 = 0;
Maybe2=0;
systolic=0;
diastolic=0;
pulse_per_min=0;
}
}
//***********************************************
void reset_state(void)
{
if(timedisplay<=1000)
{
if(timecount>=200)
{ timecount=0;
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf("Emergency Stop");
}
}
else if (timedisplay>1000&&timedisplay<2000)
{
if(timecount>=200)
{ lcd_clear();
timecount=0;
lcd_gotoxy(0,0);
lcd_putsf("Black: Resume");
}
}
else
{
timedisplay=0;
}
if((~PINB & 0x04) && (timepress2 > 30)) Maybe2 = 1;
if(Maybe2 && (PINB == 0xff))
{
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf("White: Start");
lcd_gotoxy(0,1);
lcd_putsf("Grey: Stop");
currentState = startState;
timepress2 = 0;
Maybe2=0;
}
}
//***********************************************
// Function to measure everything
//------------------------------------------------
void pressuremeasure(void)
{
switch (meas_state)
{
case Sys_Measure:
if(timing==0) sysmeasure(); //sampling signal at 40msec
break;
case Sys_Cal:
if(timing==0) syscal();
break;
case Rate_Measure:
if(timing==0) ratemeasure();
break;
case dias_Measure:
diasmeasure();
break;
case dias_Cal:
diascal();
break;
} //switch
}//pressuremeasure
//*********************************************************
void sysmeasure(void)
{
if(timing==0)
{ADMUX = 0b00100000; //choose ADC0 for reading AC
//enable ADC and set prescaler to 1/128*16MHz=125,000
//and set interupt enable
//and start a conversion
ADCSR = 0b11001111;
}
if(sys_count>=6)
{
meas_state = Sys_Cal;
timecount=0;
}
if(timecount>=200)
{
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf("Measuring");
timecount=0;
}
}
//***********************************************************
//this function is to calculate systolic pressure
void syscal(void)
{
ADMUX=0b00100001;//choose ADC1 for reading DC
//enable ADC and set prescaler to 1/128*16MHz=125,000
//and set interupt enable
//and start a conversion
ADCSR = 0b11001111;
if(timecount>=200)
{
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf("Sys Cal");
timecount=0;
}
}//syscal
//************************************************************
void ratemeasure(void)
{
ADMUX=0b00100000; //choose ADC0 for reading AC
//enable ADC and set prescaler to 1/128*16MHz=125,000
//and set interupt enable
//and start a conversion
ADCSR = 0b11001111;
//calculate the mean of pulse rate
if(count_average==5)
{
pulse_period = total_pulse_period/5000;
pulse_per_min= 60/pulse_period;
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf("Pulse Rate");
sprintf(lcd_output,"%-i",(int)pulse_per_min);
lcd_gotoxy(0,1);
lcd_puts(lcd_output);
meas_state = dias_Measure;
//then set timerun_dias=0
//also reset count_average for the next operation
count_average=0;
timerun_dias=0;
}
}
//************************************************************
void diasmeasure(void)
{
ADMUX=0b00100000;//choose ADC1 for reading AC
//enable ADC and set prescaler to 1/128*16MHz=125,000
//and set interupt enable
//and start a conversion
ADCSR = 0b11001111;
}//dias measure
//*************************************************************
void diascal(void)
{
ADMUX=0b00100001;//choose ADC1 for reading DC
//enable ADC and set prescaler to 1/128*16MHz=125,000
//and set interupt enable
//and start a conversion
ADCSR = 0b11001111;
if(timecount>=200)
{
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf("Dias_Cal");
timecount=0;
}
}
void initialize(void)
{
//Initialize LCD
timecount=0;
lcd_init(LCDwidth);
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf("White: Start");
lcd_gotoxy(0,1);
lcd_putsf("Grey: Stop");
//set up timer0
TIMSK =2; //turn on timer 0 comp match
OCR0 = 250; //set the compare register to 250
//prescaler to 64 and turn on clear-on-match
TCCR0 = 0b00001011;
timepress0 = 0;
timepress1 = 0;
DDRB=0x00; //PORT B is an input(2 buttons)
DDRD=0xff; //PORT D is an output(motor control);
PORTD=0x00;
PORTB=0xff;
PORTA=0x00;
maxpressure = 160;
meas_state = Sys_Measure;
former=TH_sys-0.01;
TH_sys=4.0;
TH_rate = 2.5;
TH_dias = 4.8;
timerun_dias=0;
time_pulse=0;
timerate=0;
timing=40;
timedisplay=0;
total_pulse_period=0;
systolic=0;
diastolic=0;
pulse_per_min=0;
Vref=5.0;
sys_count=0;
count_average=0;
countpulse=0;
DC_gain=213;
accum_data=0;
press_data=0;
count=0;
#asm
sei
#endasm
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -