serial binding protocol control
Dependencies: C12832 LM75B mbed
main.cpp@0:399e24bbd6c2, 2015-11-07 (annotated)
- Committer:
- dwijaybane
- Date:
- Sat Nov 07 05:00:41 2015 +0000
- Revision:
- 0:399e24bbd6c2
Initial version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dwijaybane | 0:399e24bbd6c2 | 1 | #include "mbed.h" // Basic Library required for onchip peripherals |
dwijaybane | 0:399e24bbd6c2 | 2 | #include "LM75B.h" // Library for LM75B I2C based Temperature sensor |
dwijaybane | 0:399e24bbd6c2 | 3 | #include "C12832.h" // Library for SPI based LCD |
dwijaybane | 0:399e24bbd6c2 | 4 | #include <cstdlib> |
dwijaybane | 0:399e24bbd6c2 | 5 | //#include "USBSerial.h" |
dwijaybane | 0:399e24bbd6c2 | 6 | |
dwijaybane | 0:399e24bbd6c2 | 7 | /* Create Objects */ |
dwijaybane | 0:399e24bbd6c2 | 8 | LM75B tmp(p28,p27); // Initialize I2C pins for Temperature Sensor |
dwijaybane | 0:399e24bbd6c2 | 9 | Serial serial(USBTX,USBRX); |
dwijaybane | 0:399e24bbd6c2 | 10 | //USBSerial serial; //use this for a virtual com port on D+ & D- |
dwijaybane | 0:399e24bbd6c2 | 11 | AnalogIn Pot1(p19); // Initialize Pot1 object with Analog input |
dwijaybane | 0:399e24bbd6c2 | 12 | C12832 lcd(p5, p7, p6, p8, p11); // Initialize lcd object with SPI pins |
dwijaybane | 0:399e24bbd6c2 | 13 | DigitalOut myled(LED1); |
dwijaybane | 0:399e24bbd6c2 | 14 | DigitalOut myled1(LED2); |
dwijaybane | 0:399e24bbd6c2 | 15 | PwmOut r (p23); // Create PWM object r for Red LED pin p23 |
dwijaybane | 0:399e24bbd6c2 | 16 | PwmOut g (p24); // Create PWM object g for Green LED pin p24 |
dwijaybane | 0:399e24bbd6c2 | 17 | PwmOut b (p25); // Create PWM object b for Blue LED pin p25 |
dwijaybane | 0:399e24bbd6c2 | 18 | |
dwijaybane | 0:399e24bbd6c2 | 19 | char ser_buf[50],i,data_OK; |
dwijaybane | 0:399e24bbd6c2 | 20 | |
dwijaybane | 0:399e24bbd6c2 | 21 | void Serial_parser(); |
dwijaybane | 0:399e24bbd6c2 | 22 | void Serial_get_data(); |
dwijaybane | 0:399e24bbd6c2 | 23 | |
dwijaybane | 0:399e24bbd6c2 | 24 | /* Main Program */ |
dwijaybane | 0:399e24bbd6c2 | 25 | int main() { |
dwijaybane | 0:399e24bbd6c2 | 26 | float board_temp,cpot,fpot; |
dwijaybane | 0:399e24bbd6c2 | 27 | |
dwijaybane | 0:399e24bbd6c2 | 28 | r.period(0.001); // Periods in seconds which is 1KHz PWM freq |
dwijaybane | 0:399e24bbd6c2 | 29 | r = 1; |
dwijaybane | 0:399e24bbd6c2 | 30 | g = 1; |
dwijaybane | 0:399e24bbd6c2 | 31 | b = 1; |
dwijaybane | 0:399e24bbd6c2 | 32 | |
dwijaybane | 0:399e24bbd6c2 | 33 | lcd.cls(); // Clear LCD Screen |
dwijaybane | 0:399e24bbd6c2 | 34 | lcd.locate(0,3); // Start from x=0 and y=3 pixels |
dwijaybane | 0:399e24bbd6c2 | 35 | lcd.printf("mbed application board!"); // print string on LCD |
dwijaybane | 0:399e24bbd6c2 | 36 | |
dwijaybane | 0:399e24bbd6c2 | 37 | serial.attach(&Serial_get_data); |
dwijaybane | 0:399e24bbd6c2 | 38 | /** |
dwijaybane | 0:399e24bbd6c2 | 39 | * Configure Terminal at 9600 8N1 configuration |
dwijaybane | 0:399e24bbd6c2 | 40 | * In linux COM port would be /dev/ttyACM0 |
dwijaybane | 0:399e24bbd6c2 | 41 | * or to find out use "dmesg" command |
dwijaybane | 0:399e24bbd6c2 | 42 | */ |
dwijaybane | 0:399e24bbd6c2 | 43 | while (1) { |
dwijaybane | 0:399e24bbd6c2 | 44 | board_temp = tmp; // read temperature |
dwijaybane | 0:399e24bbd6c2 | 45 | fpot = (float)Pot1.read_u16(); |
dwijaybane | 0:399e24bbd6c2 | 46 | cpot = 3300*(fpot/65535); |
dwijaybane | 0:399e24bbd6c2 | 47 | serial.printf("pot1:%.2fmV_temperature:%.2fC;",cpot,board_temp); // Display temperature on Terminal |
dwijaybane | 0:399e24bbd6c2 | 48 | wait(1.0); // 1 sec delay |
dwijaybane | 0:399e24bbd6c2 | 49 | } |
dwijaybane | 0:399e24bbd6c2 | 50 | } |
dwijaybane | 0:399e24bbd6c2 | 51 | |
dwijaybane | 0:399e24bbd6c2 | 52 | void Serial_parser() // Parser function for Data in the Serial buffer |
dwijaybane | 0:399e24bbd6c2 | 53 | { |
dwijaybane | 0:399e24bbd6c2 | 54 | float Rcnv,Gcnv,Bcnv; |
dwijaybane | 0:399e24bbd6c2 | 55 | |
dwijaybane | 0:399e24bbd6c2 | 56 | if(data_OK) { //if everything is ok, data are available |
dwijaybane | 0:399e24bbd6c2 | 57 | switch (ser_buf[0]) { |
dwijaybane | 0:399e24bbd6c2 | 58 | case 'L': |
dwijaybane | 0:399e24bbd6c2 | 59 | if((ser_buf[1]=='O') && (ser_buf[2]=='N')) { //The command for turning led on is "LON |
dwijaybane | 0:399e24bbd6c2 | 60 | myled = 1; |
dwijaybane | 0:399e24bbd6c2 | 61 | } |
dwijaybane | 0:399e24bbd6c2 | 62 | if((ser_buf[1]=='O') && (ser_buf[2]=='F') && (ser_buf[3]=='F')) { //the command for turning led off is "LOFF" |
dwijaybane | 0:399e24bbd6c2 | 63 | myled = 0; |
dwijaybane | 0:399e24bbd6c2 | 64 | } |
dwijaybane | 0:399e24bbd6c2 | 65 | break; |
dwijaybane | 0:399e24bbd6c2 | 66 | |
dwijaybane | 0:399e24bbd6c2 | 67 | case 'R': |
dwijaybane | 0:399e24bbd6c2 | 68 | Rcnv = atof(&ser_buf[1]); |
dwijaybane | 0:399e24bbd6c2 | 69 | r = 1.0 - (Rcnv/100); |
dwijaybane | 0:399e24bbd6c2 | 70 | break; |
dwijaybane | 0:399e24bbd6c2 | 71 | |
dwijaybane | 0:399e24bbd6c2 | 72 | case 'G': |
dwijaybane | 0:399e24bbd6c2 | 73 | Gcnv = atof(&ser_buf[1]); |
dwijaybane | 0:399e24bbd6c2 | 74 | g = 1.0 - (Gcnv/100); |
dwijaybane | 0:399e24bbd6c2 | 75 | break; |
dwijaybane | 0:399e24bbd6c2 | 76 | |
dwijaybane | 0:399e24bbd6c2 | 77 | case 'B': |
dwijaybane | 0:399e24bbd6c2 | 78 | Bcnv = atof(&ser_buf[1]); |
dwijaybane | 0:399e24bbd6c2 | 79 | b = 1.0 - (Bcnv/100); |
dwijaybane | 0:399e24bbd6c2 | 80 | break; |
dwijaybane | 0:399e24bbd6c2 | 81 | |
dwijaybane | 0:399e24bbd6c2 | 82 | default: |
dwijaybane | 0:399e24bbd6c2 | 83 | serial.printf("Command unkonwn \r\n"); |
dwijaybane | 0:399e24bbd6c2 | 84 | } |
dwijaybane | 0:399e24bbd6c2 | 85 | data_OK = 0; // we did what we have to do |
dwijaybane | 0:399e24bbd6c2 | 86 | for(int x=0; x<50; x++) ser_buf[x] = 0; //clear buffer |
dwijaybane | 0:399e24bbd6c2 | 87 | } |
dwijaybane | 0:399e24bbd6c2 | 88 | } |
dwijaybane | 0:399e24bbd6c2 | 89 | |
dwijaybane | 0:399e24bbd6c2 | 90 | |
dwijaybane | 0:399e24bbd6c2 | 91 | void Serial_get_data() // Aquiring serial data |
dwijaybane | 0:399e24bbd6c2 | 92 | { |
dwijaybane | 0:399e24bbd6c2 | 93 | if(serial.readable()) { //if data are available - uncomment this for mini usb MBED |
dwijaybane | 0:399e24bbd6c2 | 94 | // if(serial.available()) { //use this for virtual com port on D+ & D- |
dwijaybane | 0:399e24bbd6c2 | 95 | |
dwijaybane | 0:399e24bbd6c2 | 96 | ser_buf[i]=serial.getc(); |
dwijaybane | 0:399e24bbd6c2 | 97 | |
dwijaybane | 0:399e24bbd6c2 | 98 | if (ser_buf[i] == '\r') { //if CR is recieved, we ended the sentence |
dwijaybane | 0:399e24bbd6c2 | 99 | data_OK = 1; //we have something to parse |
dwijaybane | 0:399e24bbd6c2 | 100 | i=0; //reset the counter |
dwijaybane | 0:399e24bbd6c2 | 101 | |
dwijaybane | 0:399e24bbd6c2 | 102 | Serial_parser(); //call the parser function |
dwijaybane | 0:399e24bbd6c2 | 103 | } else { |
dwijaybane | 0:399e24bbd6c2 | 104 | serial.putc(ser_buf[i]); |
dwijaybane | 0:399e24bbd6c2 | 105 | i++; |
dwijaybane | 0:399e24bbd6c2 | 106 | if(i>50) i=0; //we need only a few characters to turn a led on or off |
dwijaybane | 0:399e24bbd6c2 | 107 | } |
dwijaybane | 0:399e24bbd6c2 | 108 | } |
dwijaybane | 0:399e24bbd6c2 | 109 | } |
dwijaybane | 0:399e24bbd6c2 | 110 |