Sami Kanderian / Mbed 2 deprecated TestAnalogInPins

Dependencies:   mbed

Revision:
6:214e2180c586
Parent:
5:b98a0f60f700
Child:
7:78e7984909f4
--- a/main.cpp	Wed May 11 20:24:18 2016 +0000
+++ b/main.cpp	Wed Nov 09 17:43:31 2016 +0000
@@ -25,7 +25,7 @@
 */
 
 #include "mbed.h"
-
+#define MAXINT 2147483647
 //Declare hardware inputs
 Serial serial(USBTX, USBRX);
 Timer timer;
@@ -45,7 +45,7 @@
 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 pin14(PTD1);// doesnt work. Seems to be stuck at 3.3V
 AnalogIn pin15(PTD5);//works as 12 bit ADC
 AnalogIn pin16(PTD6);//works as 12 bit ADC
 
@@ -63,7 +63,7 @@
 bool rxFlag = 0;
 
 //other globals
-char charCCIn[2];
+char charCCIn[3];
 int decCCIn;
 int updatePeriodMs = 1000;
 
@@ -83,7 +83,7 @@
 
 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");
     }
@@ -127,28 +127,30 @@
         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();
 }
 
-void runWhenNewSerialIn() {
+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];        
+        charCCIn[i] = rxBuffer[i+1];
     }
-    decCCIn=strtol(charCCIn,NULL,10);// this line converts char to int    
+    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() {
+void Rx_interrupt()
+{
     // Loop just in case more than one character is in UART's receive FIFO buffer
     // Stop if buffer full
     //while ((serial.readable()) && (((rxIn + 1) % bufferSize) != 0)) {
@@ -158,31 +160,37 @@
             rxFlag = 1;
             //Turn built in LED blue (at half intensity) to confirm command recieved
             ledConfirmReceive();
-            //Execute runWhenNewSerialIn when new Rx recieved (ending with \r)
-            runWhenNewSerialIn();
-            rxFlag = 0; //reset flag to listen for next message
-            rxIn = 0; // reset position index to 0
-
         } else {
             rxIn = (rxIn + 1) % bufferSize;
         }
     }
 }
 
-int main() {
+int main()
+{
     serial.baud(9600);
     serial.attach(&Rx_interrupt, Serial::RxIrq);
-    timer.start();        
-    //flash LED blue then green on startup or system reset    
-    ledConfirmSent();
-    wait(0.2);
-    ledConfirmReceive();    
+    timer.start();
+    int startFnTimeUs;
+    int lastActionTimeUs=timer.read_us();
+
+    int updatePeriodUs = updatePeriodMs*1000;
 
     while (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);
+        startFnTimeUs = timer.read_us();
+        if (rxFlag==1) {
+            runWhenNewSerialIn();
+            rxFlag = 0; //reset flag to listen for next message
+            rxIn = 0;   //reset position index to 0
+        }
+        int timeChangeUs = (startFnTimeUs - lastActionTimeUs);
+        if (timeChangeUs<0) { //IMPOTRANT!! This handles wrapping of timer when it exceeds MAXINT and goes slightly above zero resulting in a negative timeChangeUs
+            timeChangeUs=timeChangeUs + MAXINT +1;
+        }
+        if (timeChangeUs >= updatePeriodUs) {//Prefer never to use wait in loop in priciple as it hangs processor from doing other tasks when necessary
+            //Run sendAnalogIn if elapsed time has passed. New decCCIn is registered with new serial input
+            sendAnalogIn(decCCIn);                     
+            lastActionTimeUs=startFnTimeUs;        
+        }
     }
 }
\ No newline at end of file