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