This program displays heart rate and time between heart beats on LCD, prints it to a USB serial port, print it to a bluetooth serial port and store it on a USB mass storage device. The program has two interrupt routines: 1.Every 1ms a counter is increased with one, 2. On every heart beat the counter is value copied. In the main loop the beats per minute are calculated. Ext.Modules:- Polar RMCM-01 heart rate module connected to pin8. - 2x16 LCD - a RF-BT0417CB bluetooth serial device connected to p27 and p28 - an USB mass storage device
main.cpp@1:8b001f936bb0, 2010-11-27 (annotated)
- Committer:
- jrsikken
- Date:
- Sat Nov 27 11:13:20 2010 +0000
- Revision:
- 1:8b001f936bb0
- Parent:
- 0:939617e180e8
- Child:
- 2:e660e68a91fa
Now heart rate is stored on USB mass storage device.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jrsikken | 1:8b001f936bb0 | 1 | //This program displays heart rate and time between heart beats on LCD, serial port and on a USB mass storage device. |
jrsikken | 0:939617e180e8 | 2 | //The program has two interrupt routines: 1.Every 1ms a counter is increased with one, 2. On every heart beat the counter is value copied. |
jrsikken | 0:939617e180e8 | 3 | //In the main loop the beats per minute are calculated. |
jrsikken | 0:939617e180e8 | 4 | //Ext.Modules: -Polar RMCM-01 heart rate module connected to pin8. |
jrsikken | 1:8b001f936bb0 | 5 | // -2x16 LCD |
jrsikken | 0:939617e180e8 | 6 | |
jrsikken | 0:939617e180e8 | 7 | #include "mbed.h" |
jrsikken | 0:939617e180e8 | 8 | #include "TextLCD.h" |
jrsikken | 1:8b001f936bb0 | 9 | #include "MSCFileSystem.h" |
jrsikken | 1:8b001f936bb0 | 10 | #define FSNAME "msc" |
jrsikken | 1:8b001f936bb0 | 11 | MSCFileSystem msc(FSNAME); |
jrsikken | 0:939617e180e8 | 12 | |
jrsikken | 1:8b001f936bb0 | 13 | Timer t; // counts the time from beginning of main loop |
jrsikken | 0:939617e180e8 | 14 | TextLCD lcd(p10, p12, p15, p16, p29, p30); // rs, e, d0-d3 |
jrsikken | 1:8b001f936bb0 | 15 | InterruptIn beat(p8); // beat is the name for a inteerupt on pin 8 |
jrsikken | 1:8b001f936bb0 | 16 | DigitalOut led(LED1); // this led is toggled on every heart beat |
jrsikken | 1:8b001f936bb0 | 17 | Ticker mscnt; // this is used to create a ms counter |
jrsikken | 0:939617e180e8 | 18 | |
jrsikken | 0:939617e180e8 | 19 | //initialize |
jrsikken | 0:939617e180e8 | 20 | int count, CNT,displayBPMFlag ; |
jrsikken | 0:939617e180e8 | 21 | |
jrsikken | 1:8b001f936bb0 | 22 | void ms_counter() { //this interrupt routine starts every ms |
jrsikken | 1:8b001f936bb0 | 23 | count++; //the counter is increased with 1 |
jrsikken | 1:8b001f936bb0 | 24 | if (count>1999) { //when no heart beat is detected for >2ms, then display "-" in the main loop |
jrsikken | 0:939617e180e8 | 25 | CNT=count; // copy counter value to CNT |
jrsikken | 0:939617e180e8 | 26 | displayBPMFlag = 1; // set flag that the BPM can be put to LCD in the main loop |
jrsikken | 0:939617e180e8 | 27 | count=0; // reset counter value |
jrsikken | 0:939617e180e8 | 28 | } |
jrsikken | 0:939617e180e8 | 29 | } |
jrsikken | 0:939617e180e8 | 30 | |
jrsikken | 0:939617e180e8 | 31 | void flip() { //this interrupt routine starts on every heart beat |
jrsikken | 0:939617e180e8 | 32 | CNT = count; // copy counter value to CNT |
jrsikken | 0:939617e180e8 | 33 | count = 0 ; // reset counter value |
jrsikken | 0:939617e180e8 | 34 | displayBPMFlag = 1; // set flag that the BPM can be put to LCD in the main loop |
jrsikken | 0:939617e180e8 | 35 | led = !led; // toggle the led on/off |
jrsikken | 0:939617e180e8 | 36 | } |
jrsikken | 0:939617e180e8 | 37 | |
jrsikken | 0:939617e180e8 | 38 | int main() { |
jrsikken | 1:8b001f936bb0 | 39 | t.start(); // The ti |
jrsikken | 0:939617e180e8 | 40 | int BPM; //initialize BPM |
jrsikken | 0:939617e180e8 | 41 | lcd.cls(); //clear the lcd display |
jrsikken | 0:939617e180e8 | 42 | mscnt.attach_us(&ms_counter, 1000); // the address of the function to be attached (ms_counter) and the interval (1ms) |
jrsikken | 0:939617e180e8 | 43 | beat.rise(&flip); // attach the address of the flip function to the rising edge |
jrsikken | 0:939617e180e8 | 44 | while (1) { |
jrsikken | 0:939617e180e8 | 45 | if (displayBPMFlag == 1) { // program loops around in here |
jrsikken | 0:939617e180e8 | 46 | displayBPMFlag = 0; // clear displayBPMflag |
jrsikken | 0:939617e180e8 | 47 | if (CNT>250&CNT<2000) { //when heart rate is within 30-240BPM |
jrsikken | 0:939617e180e8 | 48 | BPM = 60000/CNT; //calculate BPM |
jrsikken | 0:939617e180e8 | 49 | lcd.cls(); //clear the lcd display |
jrsikken | 0:939617e180e8 | 50 | lcd.locate(0,0); //set cursor to first character and first line |
jrsikken | 0:939617e180e8 | 51 | lcd.printf("%i ms", CNT); //display the time in ms on the lcd |
jrsikken | 0:939617e180e8 | 52 | lcd.locate(0,1); //set cursor to first character and second line |
jrsikken | 0:939617e180e8 | 53 | lcd.printf("%i BPM",BPM); //display the beats per minute on the lcd |
jrsikken | 1:8b001f936bb0 | 54 | printf("%i\r\n",CNT); //print time between heart beats to the serial port |
jrsikken | 1:8b001f936bb0 | 55 | FILE *fp = fopen( "/msc/HR.txt", "a+");//Open file on USB Mass Storage device, 8.3 filesystem, open for write and append |
jrsikken | 1:8b001f936bb0 | 56 | if ( fp == NULL )error("Could not open file for write\n"); // error code |
jrsikken | 1:8b001f936bb0 | 57 | fprintf(fp, "%f %i\r\n",t.read(),BPM);//write time-since-start-main-loop and BPM to file |
jrsikken | 1:8b001f936bb0 | 58 | fclose(fp); // close file |
jrsikken | 0:939617e180e8 | 59 | } else {//when heart rate is NOT within 30-240BPM |
jrsikken | 0:939617e180e8 | 60 | lcd.cls(); //clear the lcd display |
jrsikken | 0:939617e180e8 | 61 | lcd.locate(0,0); //set cursor to first character and first line |
jrsikken | 0:939617e180e8 | 62 | lcd.printf("- ms"); //display empty time in ms on the lcd |
jrsikken | 0:939617e180e8 | 63 | lcd.locate(0,1); //set cursor to first character and second line |
jrsikken | 0:939617e180e8 | 64 | lcd.printf("- BPM"); //display empty beats per minute on the lcd |
jrsikken | 0:939617e180e8 | 65 | printf("- \r\n", CNT); //print the time in ms to the serial port |
jrsikken | 0:939617e180e8 | 66 | } |
jrsikken | 0:939617e180e8 | 67 | |
jrsikken | 0:939617e180e8 | 68 | } |
jrsikken | 0:939617e180e8 | 69 | |
jrsikken | 0:939617e180e8 | 70 | } |
jrsikken | 1:8b001f936bb0 | 71 | |
jrsikken | 1:8b001f936bb0 | 72 | } |