Sami Kanderian / Mbed 2 deprecated TestAnalogInPins

Dependencies:   mbed

Revision:
3:3b45e830ebe1
Parent:
2:f2583b56777e
Child:
4:9e93bf4863cb
--- a/main.cpp	Wed May 11 16:13:49 2016 +0000
+++ b/main.cpp	Wed May 11 20:20:13 2016 +0000
@@ -1,42 +1,59 @@
-//Chesapeake Embedded code by Sami Kanderian. Date of last update (version) shown below.
+/*TestAnalogInPins: written by Sami Kanderian, last updated on 11 May 2016. It continuously reads
+and prints  16 Analog Input Pin voltages on the NPX Freescale FRDM-KL25Z microprocessor board via
+Serial inputs registered via an RX Interrupt trigger that changes the value of charCCIn, where
+charCCIn goes from '00' to '16'. charCCIn should be preceeded by a '#'. Serial baud rate is 9600.
+The data acquisition rate is defined by the variable updatePeriodMs.
+
+Serial inputs followed by Carriage Return:
+'#00': print analog input voltages on all pins (deafault printout on startup)
+'#01': print analog input voltage on pin PTE20
+'#02': print analog input voltage on pin PTB0
+'#03': print analog input voltage on pin PTE21
+'#04': print analog input voltage on pin PTB1
+'#05': print analog input voltage on pin PTE22
+'#06': print analog input voltage on pin PTB2
+'#07': print analog input voltage on pin PTE23
+'#08': print analog input voltage on pin PTB3
+'#09': print analog input voltage on pin PTE29
+'#10': print analog input voltage on pin PTC2
+'#11': print analog input voltage on pin PTE30
+'#12': print analog input voltage on pin PTC1
+'#13': print analog input voltage on pin PTC0
+'#14': print analog input voltage on pin PTD1
+'#15': print analog input voltage on pin PTD5
+'#16': print analog input voltage on pin PTD6
+*/
+
 #include "mbed.h"
-#include "string"
-#include "iostream"
-#include "math.h"
-//#include "stdio.h"
-//#include "rtos.h"
 
 //Declare hardware inputs
+Serial serial(USBTX, USBRX);
 Timer timer;
-Serial serial(USBTX, USBRX);
-//IMPORTANT NOTE: PTA1 and PTA2 ARE RESERVED FOR SERIAL COMMUNICATION!!! DO NOT USE FOR SENSOR/ACTUATOR I/O!!!
 
+//IMPORTANT NOTE: PTA1 and PTA2 ARE RESERVED FOR SERIAL COMMUNICATION VIA SDA USB!!! DO NOT USE FOR SENSOR/ACTUATOR I/O!!!
 //Analog inputs : 0-3.3V
-AnalogIn pin1(PTE20);
-AnalogIn pin2(PTB0);
-AnalogIn pin3(PTE21);//should be 16bit but doesnt work on mbed
-AnalogIn pin4(PTB1);
-AnalogIn pin5(PTE22);
-AnalogIn pin6(PTB2);
-AnalogIn pin7(PTE23);
-AnalogIn pin8(PTB3);
-AnalogIn pin9(PTE29);
-AnalogIn pin10(PTC2);
-AnalogIn pin11(PTE30);
-AnalogIn pin12(PTC1);
-AnalogIn pin13(PTC0);
-AnalogIn pin14(PTD1);// doesnt work. Seems to be stuck at 3.3
-AnalogIn pin15(PTD5);
-AnalogIn pin16(PTD6);
+AnalogIn pin1(PTE20);//works as 16 bit ADC
+AnalogIn pin2(PTB0); //works as 12 bit ADC
+AnalogIn pin3(PTE21);//should be 16 bit ADC but doesnt work as ADC on mbed. Instead PTE21 reads whatever analog reading is input to PTE29
+AnalogIn pin4(PTB1); //works as 12 bit ADC
+AnalogIn pin5(PTE22);//works as 12 bit ADC
+AnalogIn pin6(PTB2);//works as 12 bit ADC
+AnalogIn pin7(PTE23);//works as 12 bit ADC
+AnalogIn pin8(PTB3); //works as 12 bit ADC
+AnalogIn pin9(PTE29);//works as 12 bit ADC
+AnalogIn pin10(PTC2);//works as 12 bit ADC
+AnalogIn pin11(PTE30);//works as 12 bit ADC
+AnalogIn pin12(PTC1);//works as 12 bit ADC
+AnalogIn pin13(PTC0);//works as 12 bit ADC
+AnalogIn pin14(PTD1);// doesnt work. Seems to be stuck at 3.3Vz
+AnalogIn pin15(PTD5);//works as 12 bit ADC
+AnalogIn pin16(PTD6);//works as 12 bit ADC
 
 //Built in LEDs
 PwmOut rLed(LED_RED);
 PwmOut gLed(LED_GREEN);
 PwmOut bLed(LED_BLUE);
 
-//Date version as global
-string dateVersion = "#Microcontroller Detected! uC code ver.: 04.12.2016";
-
 //New globals for RxInterrupt routine
 const int bufferSize = 256;
 char rxBuffer[bufferSize];
@@ -45,12 +62,10 @@
 volatile int rxOut = 0;
 bool rxFlag = 0;
 
-//Declare globals
-char hexCCIn[2];
-char hexDDDDIn[4];
+//other globals
+char charCCIn[2];
 int decCCIn;
-int decDDDDIn;
-
+int updatePeriodMs = 1000;
 
 void ledConfirmSent()//Light up blue LED 10%
 {
@@ -66,153 +81,71 @@
     bLed = 1;
 }
 
-void sendAnalogIn(int pinNum) //send Analog input in V
-{
-    
-    //serial.printf("\r\n");
+void sendAnalogIn(int pinNum) //send Analog input in V.
+//Pin inputs are normalized to have a max value of 1 so a 3.3 multiplier is used to convert back to voltage
+{    
     if (pinNum == 0 || pinNum == 1) {
-        serial.printf("%s%03.1f%s\r\n", "#PTE20: ", 3.3f*pin1.read(), "V^");
+        serial.printf("%s%03.1f%s\r\n", "#PTE20: ", 3.3f*pin1.read(), "V");
     }
     if (pinNum == 0 || pinNum == 2) {
-        serial.printf("%s%03.1f%s\r\n", "#PTB0:  ", 3.3f*pin2.read(), "V^");
+        serial.printf("%s%03.1f%s\r\n", "#PTB0: ", 3.3f*pin2.read(), "V");
     }
     if (pinNum == 0 || pinNum == 3) {
-        serial.printf("%s%03.1f%s\r\n", "#PTE21: ", 3.3f*pin3.read(), "V^");
+        serial.printf("%s%03.1f%s\r\n", "#PTE21: ", 3.3f*pin3.read(), "V");
     }
     if (pinNum == 0 || pinNum == 4) {
-        serial.printf("%s%03.1f%s\r\n", "#PTB1:  ", 3.3f*pin4.read(), "V^");
+        serial.printf("%s%03.1f%s\r\n", "#PTB1: ", 3.3f*pin4.read(), "V");
     }
     if (pinNum == 0 || pinNum == 5) {
-        serial.printf("%s%03.1f%s\r\n", "#PTE22: ", 3.3f*pin5.read(), "V^");
+        serial.printf("%s%03.1f%s\r\n", "#PTE22: ", 3.3f*pin5.read(), "V");
     }
     if (pinNum == 0 || pinNum == 6) {
-        serial.printf("%s%03.1f%s\r\n", "#PTB2:  ", 3.3f*pin6.read(), "V^");
+        serial.printf("%s%03.1f%s\r\n", "#PTB2: ", 3.3f*pin6.read(), "V");
     }
     if (pinNum == 0 || pinNum == 7) {
-        serial.printf("%s%03.1f%s\r\n", "#PTE23: ", 3.3f*pin7.read(), "V^");
+        serial.printf("%s%03.1f%s\r\n", "#PTE23: ", 3.3f*pin7.read(), "V");
     }
     if (pinNum == 0 || pinNum == 8) {
-        serial.printf("%s%03.1f%s\r\n", "#PTB3:  ", 3.3f*pin8.read(), "V^");
+        serial.printf("%s%03.1f%s\r\n", "#PTB3: ", 3.3f*pin8.read(), "V");
     }
     if (pinNum == 0 || pinNum == 9) {
-        serial.printf("%s%03.1f%s\r\n", "#PTE29: ", 3.3f*pin9.read(), "V^");
+        serial.printf("%s%03.1f%s\r\n", "#PTE29: ", 3.3f*pin9.read(), "V");
     }
     if (pinNum == 0 || pinNum == 10) {
-        serial.printf("%s%03.1f%s\r\n", "#PTC2:  ", 3.3f*pin10.read(), "V^");
+        serial.printf("%s%03.1f%s\r\n", "#PTC2: ", 3.3f*pin10.read(), "V");
     }
     if (pinNum == 0 || pinNum == 11) {
-        serial.printf("%s%03.1f%s\r\n", "#PTE30: ", 3.3f*pin11.read(), "V^");
+        serial.printf("%s%03.1f%s\r\n", "#PTE30: ", 3.3f*pin11.read(), "V");
     }
     if (pinNum == 0 || pinNum == 12) {
-        serial.printf("%s%03.1f%s\r\n", "#PTC1:  ", 3.3f*pin12.read(), "V^");
+        serial.printf("%s%03.1f%s\r\n", "#PTC1: ", 3.3f*pin12.read(), "V");
     }
     if (pinNum == 0 || pinNum == 13) {
-        serial.printf("%s%03.1f%s\r\n", "#PTC0:  ", 3.3f*pin13.read(), "V^");
+        serial.printf("%s%03.1f%s\r\n", "#PTC0: ", 3.3f*pin13.read(), "V");
     }
     if (pinNum == 0 || pinNum == 14) {
-        serial.printf("%s%03.1f%s\r\n", "#PTD1:  ", 3.3f*pin14.read(), "V^");
+        serial.printf("%s%03.1f%s\r\n", "#PTD1: ", 3.3f*pin14.read(), "V");
     }
     if (pinNum == 0 || pinNum == 15) {
-        serial.printf("%s%03.1f%s\r\n", "#PTD5:  ", 3.3f*pin15.read(), "V^");
+        serial.printf("%s%03.1f%s\r\n", "#PTD5:  ", 3.3f*pin15.read(), "V");
     }
     if (pinNum == 0 || pinNum == 16) {
-        serial.printf("%s%03.1f%s\r\n", "#PTD6:  ", 3.3f*pin16.read(), "V^");
+        serial.printf("%s%03.1f%s\r\n", "#PTD6:  ", 3.3f*pin16.read(), "V");
     }
-
     ledConfirmSent();
 }
 
-string decToHexDDDD(int i) {
-    //Do twos complement to handle negative numbers
-    if (i < 0) {
-        i = 32767 - i;
-    }
-    char hex_string[10];
-    sprintf(hex_string, "0x%02X", i);
-    return string(hex_string);
-}
-
-int hexDDDDToDec(char hex[]) {
-    int decValue = strtol(hex, NULL, 16);
-    if (decValue >= 32768) { //Do twos complement to handle negative numbers
-        decValue = 32767 - decValue;
-    }
-    return decValue;
-}
-
-int hexToDec(char hex[]) {
-    int decValue = strtol(hex, NULL, 16);
-    return decValue;
-}
-
-int hexToDecSub(char hex[], int st, int ed) {
-    int n = ed - st + 1;
-    char hexSubset[n];
-    for (int i = st; i < ed + 1; i++) {
-        hexSubset[i - st] = hex[i];
-    }
-    int decValue = strtol(hexSubset, NULL, 16);
-    return decValue;
-}
-
-void sendHexDDDDValTimes100(float val) {
-    int valTimes100 = (int) ((100 * val));
-    string hexDDDD = decToHexDDDD(valTimes100);
-    int nLeadingZeros = 4 - hexDDDD.length();
-
-    char hexDDDDout[hexDDDD.length()];
-    for (int i = 0; i < hexDDDD.length(); i++) {
-        hexDDDDout[i] = hexDDDD[i];
-    }
-    char leadingZeros[nLeadingZeros];
-    for (int i = 0; i < nLeadingZeros; i++) {
-        leadingZeros[i] = '0';
+void runWhenNewSerialIn() {
+    if (rxBuffer[0] !='#') {
+        serial.printf("%s\r\n", "Input format should be '#XX'. First input character should be '#'");
+        serial.printf("%s\r\n", "followed by XX where XX goes from '00' to '16'");
+    }    
+    for (int i = 0; i < 2; i++) {
+        charCCIn[i] = rxBuffer[i+1];        
     }
-    //serial.printf("#%s%saa^\r\n", leadingZeros, hexDDDDout);
-    serial.printf("#%s%saa", leadingZeros, hexDDDDout);
-    //ledConfirmSent();
-}
-
-void runSingleCommand(int comInd) {
-    int endBuffer = (comInd + 1)*8;
-    int startBuffer = endBuffer - 7;
-
-    //get hexCCIn
-    for (int i = 0; i < 2; i++) {
-        hexCCIn[i] = rxBuffer[i + startBuffer];
-    }
-    //serial.printf("hexCCIn= is %s\r\n", hexCCIn);
-
-    for (int i = 0; i < 4; i++) {
-        hexDDDDIn[i] = rxBuffer[i + startBuffer + 2];
-    }
-
-    decCCIn = hexToDec(hexCCIn);
-    decDDDDIn = hexDDDDToDec(hexDDDDIn);
-
-    //Make sure incoming checksum pans out: DO THIS LATER
-
-    if (comInd == 0) { //include # if first command
-        startBuffer = startBuffer - 1;
-    }
-    int i2 = 0;
-    for (int i = startBuffer; i <= endBuffer; i++) { // duplicate bytes 0 to 9 for now;
-        txBuffer[i2] = rxBuffer[i];
-        i2++;
-    }
-
-    //Do action based on CC: already in while loop
-   
-
-}
-
-void runWhenNewSerialIn() {
-    int nCommands = (rxIn - 1) / 8; //find out how many commands are sent in one serial message
-    //serial.printf("%s%d%s\r\n", "#nCommands: ", nCommands, "commands");
-    for (int comInd = 0; comInd < nCommands; comInd++) { // duplicate bytes 0 to 9 for now;
-        runSingleCommand(comInd);
-    }
-    serial.printf("^\r\n"); //end line after responses to all commands returned
+    decCCIn=strtol(charCCIn,NULL,10);// this line converts char to int    
+    //decCCIn= 10*((int)(charCCIn[0])-48)+ ((int)(charCCIn[1])-48);// equivalent to line above
+    serial.printf("%s%d\r\n", "decCCIn=  ",decCCIn);
 }
 
 void Rx_interrupt() {
@@ -237,26 +170,19 @@
 }
 
 int main() {
-
     serial.baud(9600);
     serial.attach(&Rx_interrupt, Serial::RxIrq);
-
-    //rLed.period(0.001f);
-    //gLed.period(0.001f);
-    //bLed.period(0.001f);
-   
-
-    //flash LED blue then green on startup or system reset
-    //serial.printf("Before Blue on Startup^\r\n");
+    timer.start();        
+    //flash LED blue then green on startup or system reset    
     ledConfirmSent();
-    ledConfirmReceive();
-    //serial.printf("After Green on Startup^\r\n");
-    timer.start();
+    wait(0.2);
+    ledConfirmReceive();    
 
     while (1) {
-        //scanf("%10s", rxBuffer);// NO! Use Rx_interrupt instead!
-        //Always keep running computePressureCommand when pump is enabled (runPump=true)
-        sendAnalogIn(decDDDDIn);
-        wait(1);
+        timer.reset();
+        //Run sendAnalogIn in a loop. New decCCIn is registered with new serial input
+        sendAnalogIn(decCCIn);
+        //wait for updatePeriodMs to go by before printing analog voltage(s) again
+        while(timer.read_ms()<updatePeriodMs);
     }
 }
\ No newline at end of file