fork the master

Dependencies:   TextLCD mbed-rtos mbed

Fork of Pacemaker by pacemaker team

Committer:
mfrede
Date:
Mon Dec 01 00:54:03 2014 +0000
Revision:
10:19a2ce5660ea
Parent:
9:5021d78ed4a0
Child:
11:dd278ffcb31f
changing LRI for pacemaker mode switch

Who changed what in which revision?

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