fork the master
Dependencies: TextLCD mbed-rtos mbed
Fork of Pacemaker by
main.cpp@17:6c44d5317c49, 2014-12-02 (annotated)
- Committer:
- mfrede
- Date:
- Tue Dec 02 00:14:53 2014 +0000
- Revision:
- 17:6c44d5317c49
- Parent:
- 16:a1c97cd207eb
- Parent:
- 15:4cd550c14f97
- Child:
- 18:e0bc54c0dbeb
merge
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mfrede | 0:6d04b1860ecf | 1 | /* |
mfrede | 0:6d04b1860ecf | 2 | * Pacemaker MBED code |
mfrede | 0:6d04b1860ecf | 3 | * |
mfrede | 0:6d04b1860ecf | 4 | * CIS541 Embedded Systems for Life Critical Applications |
amlittle042 | 15:4cd550c14f97 | 5 | * Authors: Jing Qiu |
amlittle042 | 15:4cd550c14f97 | 6 | * Michael Frederick |
amlittle042 | 15:4cd550c14f97 | 7 | * Tim Hu |
amlittle042 | 15:4cd550c14f97 | 8 | * Alex Little |
mfrede | 0:6d04b1860ecf | 9 | * |
mfrede | 1:e37d0cad77e2 | 10 | */ |
Jing_Qiu | 3:34e9766539fe | 11 | |
mfrede | 1:e37d0cad77e2 | 12 | #include "mbed.h" |
mfrede | 1:e37d0cad77e2 | 13 | #include "LPC17xx.h" |
mfrede | 1:e37d0cad77e2 | 14 | #include "TextLCD.h" |
mfrede | 1:e37d0cad77e2 | 15 | #include "rtos.h" |
mfrede | 11:dd278ffcb31f | 16 | #include <ctype.h> |
mfrede | 1:e37d0cad77e2 | 17 | |
Jing_Qiu | 3:34e9766539fe | 18 | #define AVI_h 100 |
Jing_Qiu | 3:34e9766539fe | 19 | #define AVI_l 30 |
Jing_Qiu | 3:34e9766539fe | 20 | #define PVARP_h 500 |
Jing_Qiu | 3:34e9766539fe | 21 | #define PVARP_l 150 |
Jing_Qiu | 3:34e9766539fe | 22 | |
Jing_Qiu | 3:34e9766539fe | 23 | #define VRP_h 500 |
Jing_Qiu | 3:34e9766539fe | 24 | #define VRP_l 150 |
Jing_Qiu | 3:34e9766539fe | 25 | #define BM(x) (1<<(x)) |
Jing_Qiu | 3:34e9766539fe | 26 | #define PULSE_WIDTH_V 200 |
Jing_Qiu | 3:34e9766539fe | 27 | #define PULSE_WIDTH_A 100 |
Jing_Qiu | 3:34e9766539fe | 28 | |
amlittle042 | 15:4cd550c14f97 | 29 | TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD16x2); |
Jing_Qiu | 5:376358077dc8 | 30 | |
Jing_Qiu | 3:34e9766539fe | 31 | int LRI_h = 1666; |
Jing_Qiu | 3:34e9766539fe | 32 | int LRI_l = 666; |
Jing_Qiu | 5:376358077dc8 | 33 | int observation_interval=10000; //ms |
mfrede | 8:ce2565cfe709 | 34 | bool setObservation = false; |
Jing_Qiu | 3:34e9766539fe | 35 | |
mfrede | 1:e37d0cad77e2 | 36 | Serial pc(USBTX, USBRX); |
amlittle042 | 15:4cd550c14f97 | 37 | |
mfrede | 1:e37d0cad77e2 | 38 | char mode = 'N'; |
Jing_Qiu | 3:34e9766539fe | 39 | |
mfrede | 1:e37d0cad77e2 | 40 | int a_clock; |
mfrede | 1:e37d0cad77e2 | 41 | int v_clock; |
mfrede | 6:6bc5e65ada4e | 42 | bool setobservation = false; |
mfrede | 1:e37d0cad77e2 | 43 | |
Jing_Qiu | 5:376358077dc8 | 44 | int beat; |
Jing_Qiu | 5:376358077dc8 | 45 | |
amlittle042 | 15:4cd550c14f97 | 46 | InterruptIn atrial_int(p21); // double check me |
amlittle042 | 15:4cd550c14f97 | 47 | InterruptIn vent_int(p22); // me too |
Jing_Qiu | 3:34e9766539fe | 48 | |
mfrede | 16:a1c97cd207eb | 49 | const int a_pace = 3; //pin 23 a pace output |
mfrede | 16:a1c97cd207eb | 50 | const int v_pace = 2; //pin 24 v pace output |
Jing_Qiu | 3:34e9766539fe | 51 | |
Jing_Qiu | 3:34e9766539fe | 52 | bool aSensed = 0; // 0 means that we are expecting Apace or Asense next |
Jing_Qiu | 5:376358077dc8 | 53 | DigitalOut led1(LED1); //apace |
Jing_Qiu | 5:376358077dc8 | 54 | DigitalOut led2(LED2); //vpace |
Jing_Qiu | 5:376358077dc8 | 55 | DigitalOut led3(LED3); //asense |
Jing_Qiu | 5:376358077dc8 | 56 | DigitalOut led4(LED4); //vsense |
Jing_Qiu | 3:34e9766539fe | 57 | |
Jing_Qiu | 3:34e9766539fe | 58 | void initTimer(); |
Jing_Qiu | 3:34e9766539fe | 59 | void startTimer(); |
Jing_Qiu | 3:34e9766539fe | 60 | void initGPIO_outputs(); |
Jing_Qiu | 3:34e9766539fe | 61 | void setGPIO(const int pinName); |
Jing_Qiu | 3:34e9766539fe | 62 | void clearGPIO(const int pinName); |
Jing_Qiu | 3:34e9766539fe | 63 | |
Jing_Qiu | 3:34e9766539fe | 64 | void asense(); |
Jing_Qiu | 3:34e9766539fe | 65 | void vsense(); |
Jing_Qiu | 3:34e9766539fe | 66 | |
Jing_Qiu | 3:34e9766539fe | 67 | void apace(); |
Jing_Qiu | 3:34e9766539fe | 68 | void vpace(); |
Jing_Qiu | 3:34e9766539fe | 69 | |
Jing_Qiu | 3:34e9766539fe | 70 | |
Jing_Qiu | 3:34e9766539fe | 71 | void apace() |
Jing_Qiu | 3:34e9766539fe | 72 | { |
Jing_Qiu | 5:376358077dc8 | 73 | led1 = 1; |
Jing_Qiu | 3:34e9766539fe | 74 | setGPIO(a_pace); |
Jing_Qiu | 3:34e9766539fe | 75 | wait_us(PULSE_WIDTH_A); |
Jing_Qiu | 5:376358077dc8 | 76 | clearGPIO(a_pace); |
mfrede | 8:ce2565cfe709 | 77 | led1 = 0; |
mfrede | 13:0a430559b488 | 78 | beat++; |
Jing_Qiu | 5:376358077dc8 | 79 | |
Jing_Qiu | 3:34e9766539fe | 80 | } |
Jing_Qiu | 3:34e9766539fe | 81 | |
Jing_Qiu | 3:34e9766539fe | 82 | void vpace() |
Jing_Qiu | 3:34e9766539fe | 83 | { |
Jing_Qiu | 5:376358077dc8 | 84 | led2= 1; |
Jing_Qiu | 3:34e9766539fe | 85 | setGPIO(v_pace); |
Jing_Qiu | 3:34e9766539fe | 86 | wait_us(PULSE_WIDTH_V); |
Jing_Qiu | 3:34e9766539fe | 87 | clearGPIO(v_pace); |
Jing_Qiu | 5:376358077dc8 | 88 | led2 = 0; |
Jing_Qiu | 3:34e9766539fe | 89 | } |
Jing_Qiu | 3:34e9766539fe | 90 | |
Jing_Qiu | 3:34e9766539fe | 91 | void initGPIO_outputs() |
Jing_Qiu | 3:34e9766539fe | 92 | { |
Jing_Qiu | 3:34e9766539fe | 93 | LPC_SC->PCONP |= 1<<15; |
Jing_Qiu | 3:34e9766539fe | 94 | LPC_GPIO2->FIODIR |= 1<<5; |
Jing_Qiu | 3:34e9766539fe | 95 | LPC_GPIO2->FIODIR |= 1<<3; |
Jing_Qiu | 3:34e9766539fe | 96 | LPC_GPIO2->FIODIR &= ~(1<<1); |
Jing_Qiu | 3:34e9766539fe | 97 | LPC_GPIO2->FIODIR &= ~(1<<2); |
Jing_Qiu | 3:34e9766539fe | 98 | } |
Jing_Qiu | 3:34e9766539fe | 99 | |
Jing_Qiu | 3:34e9766539fe | 100 | void setGPIO(const int pinName) |
Jing_Qiu | 3:34e9766539fe | 101 | { |
Jing_Qiu | 3:34e9766539fe | 102 | LPC_GPIO2->FIOPIN |= (1<<pinName); |
Jing_Qiu | 3:34e9766539fe | 103 | } |
Jing_Qiu | 3:34e9766539fe | 104 | |
Jing_Qiu | 3:34e9766539fe | 105 | void clearGPIO(const int pinName) |
Jing_Qiu | 3:34e9766539fe | 106 | { |
Jing_Qiu | 3:34e9766539fe | 107 | LPC_GPIO2->FIOPIN &= ~(1<<pinName); |
Jing_Qiu | 3:34e9766539fe | 108 | } |
Jing_Qiu | 3:34e9766539fe | 109 | |
Jing_Qiu | 3:34e9766539fe | 110 | void initTimer() |
Jing_Qiu | 3:34e9766539fe | 111 | { |
Jing_Qiu | 3:34e9766539fe | 112 | // set up OS timer (timer0) |
Jing_Qiu | 3:34e9766539fe | 113 | LPC_SC->PCONP |= BM(1); //power up timer0 |
Jing_Qiu | 3:34e9766539fe | 114 | LPC_SC->PCLKSEL0 |= BM(2); // clock = CCLK (96 MHz) |
Jing_Qiu | 3:34e9766539fe | 115 | LPC_TIM0->PR = 48000; // set prescale to 48000 (2048 Hz timer) |
Jing_Qiu | 3:34e9766539fe | 116 | LPC_TIM0->MR0 = 1; // match0 compare value (32-bit) |
Jing_Qiu | 3:34e9766539fe | 117 | LPC_TIM0->MCR |= BM(0)|BM(1); // interrupt and reset on match0 compare |
Jing_Qiu | 3:34e9766539fe | 118 | NVIC_EnableIRQ(TIMER0_IRQn); // enable timer interrupt |
Jing_Qiu | 3:34e9766539fe | 119 | } |
Jing_Qiu | 3:34e9766539fe | 120 | |
Jing_Qiu | 3:34e9766539fe | 121 | void startTimer() |
Jing_Qiu | 3:34e9766539fe | 122 | { |
Jing_Qiu | 3:34e9766539fe | 123 | LPC_TIM0->TCR |= BM(1); // reset timer1 |
Jing_Qiu | 3:34e9766539fe | 124 | LPC_TIM0->TCR &= ~BM(1); // release reset |
Jing_Qiu | 3:34e9766539fe | 125 | LPC_TIM0->TCR |= BM(0); // start timer |
Jing_Qiu | 3:34e9766539fe | 126 | } |
Jing_Qiu | 3:34e9766539fe | 127 | |
Jing_Qiu | 3:34e9766539fe | 128 | void resetTimer() |
Jing_Qiu | 3:34e9766539fe | 129 | { |
Jing_Qiu | 3:34e9766539fe | 130 | LPC_TIM0->TCR |= BM(1); // reset timer0 |
Jing_Qiu | 3:34e9766539fe | 131 | LPC_TIM0->TCR &= ~BM(1); // release reset |
Jing_Qiu | 3:34e9766539fe | 132 | } |
Jing_Qiu | 3:34e9766539fe | 133 | |
Jing_Qiu | 3:34e9766539fe | 134 | |
Jing_Qiu | 3:34e9766539fe | 135 | |
mfrede | 1:e37d0cad77e2 | 136 | void display_thread_handler(void const *args) |
mfrede | 1:e37d0cad77e2 | 137 | { |
mfrede | 1:e37d0cad77e2 | 138 | while(1) |
mfrede | 1:e37d0cad77e2 | 139 | { |
Jing_Qiu | 5:376358077dc8 | 140 | wait_ms(observation_interval); |
amlittle042 | 15:4cd550c14f97 | 141 | pc.printf("BPM: %d\r\n",beat*60000/observation_interval); |
amlittle042 | 15:4cd550c14f97 | 142 | lcd.cls(); |
amlittle042 | 15:4cd550c14f97 | 143 | lcd.printf("BPM: %d\r\n", beat*60000/observation_interval); |
Jing_Qiu | 5:376358077dc8 | 144 | beat=0; |
mfrede | 1:e37d0cad77e2 | 145 | } |
mfrede | 1:e37d0cad77e2 | 146 | } |
mfrede | 1:e37d0cad77e2 | 147 | |
mfrede | 1:e37d0cad77e2 | 148 | |
mfrede | 1:e37d0cad77e2 | 149 | void asense() { |
Jing_Qiu | 3:34e9766539fe | 150 | if (v_clock >= VRP_l) { //Ignore vSense outside this time interval |
Jing_Qiu | 3:34e9766539fe | 151 | v_clock = 0; |
Jing_Qiu | 3:34e9766539fe | 152 | aSensed = 0; |
Jing_Qiu | 3:34e9766539fe | 153 | } |
Jing_Qiu | 5:376358077dc8 | 154 | led3 = 1; |
Jing_Qiu | 5:376358077dc8 | 155 | |
mfrede | 1:e37d0cad77e2 | 156 | } |
mfrede | 1:e37d0cad77e2 | 157 | |
mfrede | 1:e37d0cad77e2 | 158 | void vsense() { |
mfrede | 16:a1c97cd207eb | 159 | if ((a_clock >= PVARP_l) && aSensed == 0){ |
Jing_Qiu | 3:34e9766539fe | 160 | a_clock = 0; |
Jing_Qiu | 3:34e9766539fe | 161 | aSensed = 1; |
Jing_Qiu | 5:376358077dc8 | 162 | beat++; |
Jing_Qiu | 3:34e9766539fe | 163 | } |
Jing_Qiu | 5:376358077dc8 | 164 | led4 = 1; |
mfrede | 1:e37d0cad77e2 | 165 | } |
mfrede | 1:e37d0cad77e2 | 166 | |
mfrede | 1:e37d0cad77e2 | 167 | void button_handler(void const *args) |
mfrede | 1:e37d0cad77e2 | 168 | { |
mfrede | 8:ce2565cfe709 | 169 | int observation_temp=0; |
mfrede | 1:e37d0cad77e2 | 170 | while(1) |
mfrede | 1:e37d0cad77e2 | 171 | { |
mfrede | 1:e37d0cad77e2 | 172 | char buffer; |
mfrede | 1:e37d0cad77e2 | 173 | if(pc.readable()) { |
mfrede | 11:dd278ffcb31f | 174 | buffer = toupper(pc.getc()); |
mfrede | 1:e37d0cad77e2 | 175 | if (buffer == 'N') |
mfrede | 10:19a2ce5660ea | 176 | { |
mfrede | 1:e37d0cad77e2 | 177 | mode = buffer; |
mfrede | 10:19a2ce5660ea | 178 | LRI_h = 1500; |
mfrede | 10:19a2ce5660ea | 179 | LRI_l = 600; |
mfrede | 10:19a2ce5660ea | 180 | } |
mfrede | 1:e37d0cad77e2 | 181 | else if (buffer == 'S') |
mfrede | 10:19a2ce5660ea | 182 | { |
mfrede | 1:e37d0cad77e2 | 183 | mode = buffer; |
mfrede | 10:19a2ce5660ea | 184 | LRI_h = 2000; |
mfrede | 10:19a2ce5660ea | 185 | LRI_l = 1000; |
mfrede | 10:19a2ce5660ea | 186 | } |
mfrede | 1:e37d0cad77e2 | 187 | else if (buffer == 'E') |
mfrede | 10:19a2ce5660ea | 188 | { |
mfrede | 1:e37d0cad77e2 | 189 | mode = buffer; |
mfrede | 10:19a2ce5660ea | 190 | LRI_h = 600; |
mfrede | 10:19a2ce5660ea | 191 | LRI_l = 342; |
mfrede | 10:19a2ce5660ea | 192 | } |
mfrede | 1:e37d0cad77e2 | 193 | else if (buffer == 'M') |
mfrede | 10:19a2ce5660ea | 194 | { |
mfrede | 1:e37d0cad77e2 | 195 | mode = buffer; |
mfrede | 10:19a2ce5660ea | 196 | LRI_h = 2000; |
mfrede | 10:19a2ce5660ea | 197 | LRI_l = 342; |
mfrede | 10:19a2ce5660ea | 198 | } |
mfrede | 1:e37d0cad77e2 | 199 | else if (buffer == 'A' && mode == 'M') |
mfrede | 1:e37d0cad77e2 | 200 | apace(); |
mfrede | 1:e37d0cad77e2 | 201 | else if (buffer == 'V' && mode == 'M') |
mfrede | 1:e37d0cad77e2 | 202 | vpace(); |
mfrede | 8:ce2565cfe709 | 203 | else if (buffer == 'O') { |
mfrede | 8:ce2565cfe709 | 204 | setObservation = true; |
mfrede | 8:ce2565cfe709 | 205 | observation_temp = 0; |
mfrede | 8:ce2565cfe709 | 206 | } |
mfrede | 14:94f1b9e3eb43 | 207 | else if (buffer == 13 && setObservation) //newline |
mfrede | 6:6bc5e65ada4e | 208 | { |
mfrede | 8:ce2565cfe709 | 209 | observation_interval = observation_temp*1000; |
mfrede | 8:ce2565cfe709 | 210 | setObservation = false; |
mfrede | 14:94f1b9e3eb43 | 211 | // printf("observation interval now: %d\r\n",observation_interval); |
mfrede | 6:6bc5e65ada4e | 212 | } |
mfrede | 8:ce2565cfe709 | 213 | else if (setObservation) |
mfrede | 6:6bc5e65ada4e | 214 | { |
mfrede | 8:ce2565cfe709 | 215 | observation_temp *= 10; |
mfrede | 14:94f1b9e3eb43 | 216 | observation_temp += buffer-48; |
mfrede | 1:e37d0cad77e2 | 217 | |
mfrede | 6:6bc5e65ada4e | 218 | } |
mfrede | 1:e37d0cad77e2 | 219 | } |
mfrede | 1:e37d0cad77e2 | 220 | } |
mfrede | 1:e37d0cad77e2 | 221 | } |
mfrede | 1:e37d0cad77e2 | 222 | |
Jing_Qiu | 3:34e9766539fe | 223 | void pacemaker_thread_handler(void const *args) |
Jing_Qiu | 3:34e9766539fe | 224 | { |
Jing_Qiu | 3:34e9766539fe | 225 | initGPIO_outputs(); |
Jing_Qiu | 3:34e9766539fe | 226 | |
Jing_Qiu | 3:34e9766539fe | 227 | /********************************************************** |
Jing_Qiu | 3:34e9766539fe | 228 | ************Initialize timer to interrupt every 1 ms********* |
Jing_Qiu | 3:34e9766539fe | 229 | ***********************************************************/ |
Jing_Qiu | 3:34e9766539fe | 230 | |
Jing_Qiu | 3:34e9766539fe | 231 | initTimer(); |
Jing_Qiu | 3:34e9766539fe | 232 | startTimer(); |
Jing_Qiu | 3:34e9766539fe | 233 | |
Jing_Qiu | 3:34e9766539fe | 234 | atrial_int.rise(&asense); |
Jing_Qiu | 3:34e9766539fe | 235 | vent_int.rise(&vsense); |
Jing_Qiu | 3:34e9766539fe | 236 | while(1){} |
Jing_Qiu | 3:34e9766539fe | 237 | |
Jing_Qiu | 3:34e9766539fe | 238 | } |
Jing_Qiu | 3:34e9766539fe | 239 | |
Jing_Qiu | 3:34e9766539fe | 240 | |
Jing_Qiu | 3:34e9766539fe | 241 | /********************************************************** |
Jing_Qiu | 3:34e9766539fe | 242 | ************ timer interrupt every 1 ms********* |
Jing_Qiu | 3:34e9766539fe | 243 | ***********************************************************/ |
Jing_Qiu | 3:34e9766539fe | 244 | |
Jing_Qiu | 3:34e9766539fe | 245 | extern "C" void TIMER0_IRQHandler (void) { |
Jing_Qiu | 3:34e9766539fe | 246 | if((LPC_TIM0->IR & 0x01) == 0x01) // if MR0 interrupt |
Jing_Qiu | 3:34e9766539fe | 247 | { |
Jing_Qiu | 3:34e9766539fe | 248 | LPC_TIM0->IR |= (1 << 0); // Clear MR0 interrupt flag |
Jing_Qiu | 3:34e9766539fe | 249 | if (v_clock >= (LRI_h-AVI_l) && aSensed == 0) { |
Jing_Qiu | 3:34e9766539fe | 250 | a_clock = 0; |
Jing_Qiu | 3:34e9766539fe | 251 | aSensed = 1; |
Jing_Qiu | 3:34e9766539fe | 252 | //printf("Apace %d\r\n",v_clk); |
Jing_Qiu | 3:34e9766539fe | 253 | apace(); |
Jing_Qiu | 3:34e9766539fe | 254 | /* |
Jing_Qiu | 3:34e9766539fe | 255 | setGPIO(a_pace); |
Jing_Qiu | 3:34e9766539fe | 256 | wait_us(PULSE_WIDTH_A); |
Jing_Qiu | 3:34e9766539fe | 257 | clearGPIO(a_pace); |
Jing_Qiu | 3:34e9766539fe | 258 | */ |
Jing_Qiu | 3:34e9766539fe | 259 | } |
Jing_Qiu | 3:34e9766539fe | 260 | if ((a_clock >= AVI_h) && aSensed == 1) { |
Jing_Qiu | 3:34e9766539fe | 261 | v_clock = 0; |
Jing_Qiu | 3:34e9766539fe | 262 | aSensed = 0; |
Jing_Qiu | 5:376358077dc8 | 263 | //led3 = 0; |
Jing_Qiu | 3:34e9766539fe | 264 | //printf("Vpace %d\r\n",a_clk); |
Jing_Qiu | 3:34e9766539fe | 265 | /* |
Jing_Qiu | 3:34e9766539fe | 266 | setGPIO(v_pace); |
Jing_Qiu | 3:34e9766539fe | 267 | wait_us(PULSE_WIDTH_V); |
Jing_Qiu | 3:34e9766539fe | 268 | clearGPIO(v_pace); |
Jing_Qiu | 3:34e9766539fe | 269 | */ |
Jing_Qiu | 3:34e9766539fe | 270 | vpace(); |
Jing_Qiu | 3:34e9766539fe | 271 | } |
Jing_Qiu | 3:34e9766539fe | 272 | v_clock++; |
Jing_Qiu | 3:34e9766539fe | 273 | a_clock++; |
Jing_Qiu | 5:376358077dc8 | 274 | |
Jing_Qiu | 5:376358077dc8 | 275 | if(v_clock>500) led3 = 0; |
Jing_Qiu | 5:376358077dc8 | 276 | if(a_clock>500) led4 = 0; |
Jing_Qiu | 3:34e9766539fe | 277 | } |
Jing_Qiu | 3:34e9766539fe | 278 | } |
mfrede | 1:e37d0cad77e2 | 279 | |
mfrede | 1:e37d0cad77e2 | 280 | |
Jing_Qiu | 3:34e9766539fe | 281 | int main (void) { |
Jing_Qiu | 3:34e9766539fe | 282 | //TODO set parameters |
Jing_Qiu | 5:376358077dc8 | 283 | |
Jing_Qiu | 3:34e9766539fe | 284 | |
Jing_Qiu | 3:34e9766539fe | 285 | Thread display(display_thread_handler); |
Jing_Qiu | 3:34e9766539fe | 286 | Thread keyboard(button_handler); |
Jing_Qiu | 3:34e9766539fe | 287 | Thread pacemaker(pacemaker_thread_handler); |
Jing_Qiu | 3:34e9766539fe | 288 | |
Jing_Qiu | 3:34e9766539fe | 289 | while(1){} |
Jing_Qiu | 3:34e9766539fe | 290 | |
Jing_Qiu | 3:34e9766539fe | 291 | |
mfrede | 1:e37d0cad77e2 | 292 | } |