XBEE Project for FRDM-K64F - program number 1 of 2.
Dependencies: C12832 LM75B SDFileSystem XBee mbed
main.cpp@0:add6ccfe6b49, 2017-01-11 (annotated)
- Committer:
- darrenf87
- Date:
- Wed Jan 11 20:18:18 2017 +0000
- Revision:
- 0:add6ccfe6b49
XBEE project for FRDM-K64F device. This project is number 1 of 2 programs for the final real time embedded systems report.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
darrenf87 | 0:add6ccfe6b49 | 1 | #include "mbed.h" /*Include standard mbed library*/ |
darrenf87 | 0:add6ccfe6b49 | 2 | #include "XBee.h" /*Include library for XBee transmitter/reciever*/ |
darrenf87 | 0:add6ccfe6b49 | 3 | #include "C12832.h" /*Include library for temperature sensor*/ |
darrenf87 | 0:add6ccfe6b49 | 4 | #include "LM75B.h" /*Include library for LCD display*/ |
darrenf87 | 0:add6ccfe6b49 | 5 | #include "SDFileSystem.h" /*Include library for SD card read/write*/ |
darrenf87 | 0:add6ccfe6b49 | 6 | |
darrenf87 | 0:add6ccfe6b49 | 7 | Serial xbee1(D1,D0); /*Declare XBee transmit/receive as pins 1 and 0*/ |
darrenf87 | 0:add6ccfe6b49 | 8 | DigitalOut rst1(D3); /*Declare pin 3 as the reset pin for the XBee*/ |
darrenf87 | 0:add6ccfe6b49 | 9 | LM75B temp(PTE25,PTE24); /*Declare temperature sensor transmit/recieve as pins 24 and 25*/ |
darrenf87 | 0:add6ccfe6b49 | 10 | DigitalOut red(PTB22); /*Declare pin 22 as the red LED*/ |
darrenf87 | 0:add6ccfe6b49 | 11 | DigitalOut green(PTE26); /*Declare pin 26 as the green LED*/ |
darrenf87 | 0:add6ccfe6b49 | 12 | DigitalOut blue(PTB21); /*Declare pin 21 as the blue LED*/ |
darrenf87 | 0:add6ccfe6b49 | 13 | DigitalOut relay(D4); /*Declare pin 4 as the output to an external relay*/ |
darrenf87 | 0:add6ccfe6b49 | 14 | |
darrenf87 | 0:add6ccfe6b49 | 15 | C12832 lcd(D11, D13, D12, D7, D10); /*Assign pins for LCD display (MOSI, SCK, Reset, A0 and nCS)*/ |
darrenf87 | 0:add6ccfe6b49 | 16 | SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); /*Assign pins for SD card us (MOSI, MISO, sclk and cs*/ |
darrenf87 | 0:add6ccfe6b49 | 17 | |
darrenf87 | 0:add6ccfe6b49 | 18 | int main() { /*Start of main function*/ |
darrenf87 | 0:add6ccfe6b49 | 19 | rst1 = 0; /*Set XBee reset pin to 0*/ |
darrenf87 | 0:add6ccfe6b49 | 20 | wait_ms(1); /*Wait 1 millisecond*/ |
darrenf87 | 0:add6ccfe6b49 | 21 | rst1 = 1; /*Set XBee reset pin to 1*/ |
darrenf87 | 0:add6ccfe6b49 | 22 | wait_ms(1); /*Wait 1 millisecond*/ |
darrenf87 | 0:add6ccfe6b49 | 23 | FILE *File = fopen("/sd/BTLDirectory.txt", "w"); /*Open file*/ |
darrenf87 | 0:add6ccfe6b49 | 24 | |
darrenf87 | 0:add6ccfe6b49 | 25 | |
darrenf87 | 0:add6ccfe6b49 | 26 | lcd.locate(20,0); /*Locate the area to print in on the LCD*/ |
darrenf87 | 0:add6ccfe6b49 | 27 | lcd.printf("Battery Temperature"); /*Output to LCD screen at position 20,0*/ |
darrenf87 | 0:add6ccfe6b49 | 28 | lcd.locate(18,10); /*Locate the area to print in on the LCD*/ |
darrenf87 | 0:add6ccfe6b49 | 29 | lcd.printf("Monitoring - DMOS A"); /*Output to LCD screen at position 18,10*/ |
darrenf87 | 0:add6ccfe6b49 | 30 | lcd.locate(15,20); /*Locate the area to print in on the LCD*/ |
darrenf87 | 0:add6ccfe6b49 | 31 | lcd.printf("(C) D.Fitzgerald - 2016"); /*Output to the LCD screen at position 15,20*/ |
darrenf87 | 0:add6ccfe6b49 | 32 | wait(5); /*Wait 5 seconds (displays the above message for 5 seconds*/ |
darrenf87 | 0:add6ccfe6b49 | 33 | lcd.cls(); /*Clear all messages from LCD screen*/ |
darrenf87 | 0:add6ccfe6b49 | 34 | |
darrenf87 | 0:add6ccfe6b49 | 35 | int a; /*Declare 'a' as an integer value*/ |
darrenf87 | 0:add6ccfe6b49 | 36 | int b; /*Declare 'b' as an integer value*/ |
darrenf87 | 0:add6ccfe6b49 | 37 | int c; /*Declare 'c' as an integer value*/ |
darrenf87 | 0:add6ccfe6b49 | 38 | int d; /*Declare 'd' as an integer value*/ |
darrenf87 | 0:add6ccfe6b49 | 39 | |
darrenf87 | 0:add6ccfe6b49 | 40 | lcd.locate(6,0); /*Locate the area to print on the LCD*/ |
darrenf87 | 0:add6ccfe6b49 | 41 | lcd.printf("DMOS A Batteries = "); /*Output to LCD screen at position 6,0*/ |
darrenf87 | 0:add6ccfe6b49 | 42 | lcd.locate(6,16); /*Locate the area to print on the LCD*/ |
darrenf87 | 0:add6ccfe6b49 | 43 | lcd.printf("DMOS B Batteries = "); /*Output to LCD screen at position 6,16*/ |
darrenf87 | 0:add6ccfe6b49 | 44 | |
darrenf87 | 0:add6ccfe6b49 | 45 | d=1; /*Set value of 'd' to be '1' permanently*/ |
darrenf87 | 0:add6ccfe6b49 | 46 | |
darrenf87 | 0:add6ccfe6b49 | 47 | while (d>0) { /*Infinite while loop whilst d is greater than 0, which is always*/ |
darrenf87 | 0:add6ccfe6b49 | 48 | /*The three LED's must be 'logically notted' in order to force them off before use*/ |
darrenf87 | 0:add6ccfe6b49 | 49 | green=!green; /*Logically not the green LED*/ |
darrenf87 | 0:add6ccfe6b49 | 50 | red=!red; /*Logically not the red LED*/ |
darrenf87 | 0:add6ccfe6b49 | 51 | blue=!blue; /*Logically not the blue LED (not used but still needs to be turned off to prevent mixing with other colours*/ |
darrenf87 | 0:add6ccfe6b49 | 52 | a = temp.read(); /*Read the temperature from the LM75 temperature sensor*/ |
darrenf87 | 0:add6ccfe6b49 | 53 | /*XBee devices are capable of duplex communications, which is utilised in the next two steps*/ |
darrenf87 | 0:add6ccfe6b49 | 54 | xbee1.putc(a); /*Put the read temperature into the XBee output port*/ |
darrenf87 | 0:add6ccfe6b49 | 55 | b = xbee1.getc(); /*Retrieve the temperature that is being sent from the other ARM MBED device from the XBee*/ |
darrenf87 | 0:add6ccfe6b49 | 56 | lcd.locate(100,0); /*Locate the area to print the temperature to on the LCD screen*/ |
darrenf87 | 0:add6ccfe6b49 | 57 | lcd.printf("%d C\n",a); /*Print the temperature from the other ARM MBED device at location 100,0*/ |
darrenf87 | 0:add6ccfe6b49 | 58 | lcd.locate(100,16); /*Locate the area to print the temperature to on the LCD screen*/ |
darrenf87 | 0:add6ccfe6b49 | 59 | lcd.printf("%d C\n",b); /*Print the temperature from the local ARM MBED device at location 100,16*/ |
darrenf87 | 0:add6ccfe6b49 | 60 | wait_us(10); /*Wait 10 microseconds*/ |
darrenf87 | 0:add6ccfe6b49 | 61 | c = abs(a-b); /*Subtract the local temperature from the remote temperature and create this as an absolute (positive) only value*/ |
darrenf87 | 0:add6ccfe6b49 | 62 | if(c<3){ /*Start of If loop for if the temperature difference calculated in the previous step is equal to or less than 3 degrees*/ |
darrenf87 | 0:add6ccfe6b49 | 63 | /*The LED's have to be forced off to prevent the RGB LED mixing it's colours*/ |
darrenf87 | 0:add6ccfe6b49 | 64 | green=1; /*Turn the green LED on*/ |
darrenf87 | 0:add6ccfe6b49 | 65 | blue=0; /*Force the red LED off*/ |
darrenf87 | 0:add6ccfe6b49 | 66 | red=0; /*Force the blue LED off*/ |
darrenf87 | 0:add6ccfe6b49 | 67 | } /*End of If loop*/ |
darrenf87 | 0:add6ccfe6b49 | 68 | else{ /*If the temperature difference calculated as 'c' is anything other than the parameter set in the previous if loop, do the following...*/ |
darrenf87 | 0:add6ccfe6b49 | 69 | red=1; /*Turn the red LED on*/ |
darrenf87 | 0:add6ccfe6b49 | 70 | green=0; /*Force the green LED off*/ |
darrenf87 | 0:add6ccfe6b49 | 71 | blue=0; /*Force the blue LED off*/ |
darrenf87 | 0:add6ccfe6b49 | 72 | relay=1; /*Apply a logic value of '1' to pin 8, to activate an external relay in order to turn off battery charging supply*/ |
darrenf87 | 0:add6ccfe6b49 | 73 | fprintf(File, "The battery box temperatures on this train have differed by\nat least 3 degrees during the course of the day"); /*Store number of events to SD Card*/ |
darrenf87 | 0:add6ccfe6b49 | 74 | fclose(File); /*Close SD Card File*/ |
darrenf87 | 0:add6ccfe6b49 | 75 | } /*End of If loop*/ |
darrenf87 | 0:add6ccfe6b49 | 76 | } /*End of infinite while loop*/ |
darrenf87 | 0:add6ccfe6b49 | 77 | } /*End of main function*/ |