Cortex Challenge Team / Mbed 2 deprecated Nucleo_read_analog_set

Dependencies:   mbed

Fork of Nucleo-Analog_read by Cortex Challenge Team

Revision:
2:eb5ee8191175
Parent:
1:dc5a274adc7d
Child:
3:a87ea8280bcf
--- a/main.cpp	Fri Mar 13 01:37:57 2015 +0000
+++ b/main.cpp	Sat Mar 21 14:32:22 2015 +0000
@@ -1,37 +1,82 @@
+/**********************************************************************************
+* @file    main.cpp
+* @author  Name
+* @version V0.1
+* @date    09-March-2015
+* @brief   Read analog value and write it to serial.
+*          It could be set some settings through serial.
+*          Serial speed is set to 115200.
+***********************************************************************************/
+
+/**********************************************************************************/
+/*                 Table of A/D pins on Nucleo F303 (LQFP64)                      */
+/**********************************************************************************/
+/*  LQFP64 pin   |   Nucleo pin   |   ST Pin   |   AD Number      |    Channel    */
+/*       8       |      A5        |    PC_0    |      ADC12       |       6       */
+/*       9       |      A4        |    PC_1    |      ADC12       |       7       */
+/*      14       |      A0        |    PA_0    |      ADC1        |       1       */
+/*      15       |      A1        |    PA_1    |      ADC1        |       2       */
+/*      16       |      D1        |    PA_2    |      ADC1        |       3       */
+/*      17       |      D0        |    PA_3    |      ADC1        |       4       */
+/*      20       |      A2        |    PA_4    |      ADC2        |       1       */
+/*      21       |      D13       |    PA_5    |      ADC2        |       2       */
+/*      22       |      D12       |    PA_6    |      ADC2        |       3       */
+/*      23       |      D11       |    PA_7    |      ADC2        |       4       */
+/*      26       |      A3        |    PB_0    |      ADC3        |       12      */
+/**********************************************************************************/
+
+
+/* Includes ----------------------------------------------------------------------*/
 #include "mbed.h"
 
-#define voltage 3300
-#define mv(x)       ((x*voltage)/0xFFFF)
+/* Defines -----------------------------------------------------------------------*/
+#define voltage 3300                        //voltage in mV, which is used in Nucleo kits
+#define mv(x)       ((x*voltage)/0xFFFF)    // raw value to value in mili volts
 
-AnalogIn analog_value(A0);
-
-Serial pc(SERIAL_TX, SERIAL_RX);
+/* Variables ---------------------------------------------------------------------*/
+int voltages =0;            //voltage to compare and start print
+bool start = true;          // if true it print value to serial
 
-DigitalOut led(LED1);
+//mbed - initialization of peripherals
+AnalogIn analog_value(A0);      // inicialize analog input A0
+Serial pc(SERIAL_TX, SERIAL_RX);// inicialize Serial to connect to PC
+DigitalOut led(LED1);           // inicialize LED
+Ticker toggle_ticker;           // inicialize ticker
+
+/* Functions----------------------------------------------------------------------*/
 
-Ticker toggle_ticker;
-int voltages =0;
-
-bool start = true;
-
+/***********************************************************************************
+* Function Name  : toggle.
+* Description    : Read analog value, compare, blink with LED and send value to serial.
+* Input          : None.
+* Output         : None.
+* Return         : None.
+***********************************************************************************/
 void toggle()
 {
     int meas;
 
     meas = analog_value.read_u16(); // Converts and read the analog input value (value from 0.0 to 0xFFFF)
-    meas = mv(meas); // Change the value to be in the 0 to 3300 range
-    while(!pc.writeable());
+    meas = mv(meas);                // Change the value to be in the 0 to 3300 range
 
 
     led= !led;
-    if (meas > voltages ) { // If the value is greater than 2V then switch the LED on
-        start = true;
+    if (meas > voltages ) { // If the value is greater than set voltage
+        start = true;       //start send data to Serial
     }
     if(start) {
-        printf("%d\n", meas);
+        while(!pc.writeable());     // wait to be serial available for sending data
+        pc.printf("%d\n", meas);    // send data to Serial
     }
 }
 
+/***********************************************************************************
+* Function Name  : flushSerialPort.
+* Description    : Serial flush rountine.
+* Input          : None.
+* Output         : None.
+* Return         : None.
+***********************************************************************************/
 void flushSerialPort()
 {
     while(pc.readable())
@@ -39,48 +84,86 @@
     return;
 }
 
+/***********************************************************************************
+* Function Name  : menu.
+* Description    : Print menu to serial.
+* Input          : None.
+* Output         : None.
+* Return         : None.
+***********************************************************************************/
+void menu()
+{
+    while(!pc.writeable()); // wait to be serial available for sending data
+    pc.printf("HELP - MENU\n"); // send text to Serial
+    while(!pc.writeable());
+    pc.printf("Data send to PC (numbers) are in mV.\n");
+    while(!pc.writeable());
+    pc.printf("Set data exactly.\n");
+    while(!pc.writeable());
+    pc.printf("Write to console: \"xx yy\", where xx is a code of seting and yy his value.\n");
+    while(!pc.writeable());
+    pc.printf("01 y.yyy - set period[s] to send data to PC and start reading value, example:01 0.01\n");
+    while(!pc.writeable());
+    pc.printf("02 0 - stop reading value example:02 0\n");
+    while(!pc.writeable());
+    pc.printf("03 yyyy - Wait for minimum voltage[mV] (to 3299) and send data example:03 2000\n");
+    while(!pc.writeable());
+    pc.printf("end HELP\n");
+}
+
+/***********************************************************************************
+* Function Name  : main.
+* Description    : Main routine.
+* Input          : None.
+* Output         : None.
+* Return         : None.
+***********************************************************************************/
 int main()
 {
-    int prijData=0;
-    int Data1=0;
-    float Data2=0;
-    pc.baud(115200);
-    toggle_ticker.detach();
-    toggle_ticker.attach(&toggle, 0.01);
-    printf("\nAnalogIn example, data (numbers) are in mV.\n");
+    // variables to read from serials
+    int prijData=0;     // check if data are well set  
+    int Data1=0;        // first data from serial
+    float Data2=0;      // second data from serial
+
+    pc.baud(115200);    //set speed of serial to 115200
+
+    toggle_ticker.detach();             // do NOT call function
+    toggle_ticker.attach(&toggle, 1);   // 1 second was passed, call function toggle
+
+    pc.printf("\nAnalogIn example, data (numbers) are in mV.\n");
+    menu(); //print menu
 
     while(1) {
-        prijData=pc.scanf("%d %f",&Data1,&Data2);
+
+        //accepted data from serial
+        prijData=pc.scanf("%d",&Data1);             // read number from serial
+        if(prijData==1 && (Data1>=1 && Data1<=3)) { // test if number was read and it is between 1 and 3
+            prijData=pc.scanf("%f",&Data2);         // read float number from serial
+            if(prijData==1) {
+                prijData=2;         // set variable to set measure of values
+            } else {
+                flushSerialPort();  // iscard data from serial
+                prijData=0;         // set variable to print menu
+            }
+        } else {
+            flushSerialPort();      // discard data from serial
+            prijData=0;             // set variable to print menu
+        }
+
         if(prijData==2) {
-            //pc.printf()
-            if(Data1==1) {
-                toggle_ticker.detach();
-                toggle_ticker.attach(&toggle, Data2);
-            } else if(Data1==2) {
-                toggle_ticker.detach();
-            } else if(Data1==3) {
+            if(Data1==1) {                              // change time to call function toggle
+                toggle_ticker.detach();                 // do NOT call function
+                toggle_ticker.attach(&toggle, Data2);   // Data2 seconds was passed, call function toggle
+            } else if(Data1==2) {                       // stop call function toggle
+                toggle_ticker.detach();                 // do NOT call function
+            } else if(Data1==3) {                       //set variables, which are use in toggle
                 voltages=(int)Data2;
                 start = false;
             }
         } else {
-            while(!pc.writeable());
-            printf("HELP - MENU\n");
-            while(!pc.writeable());
-            printf("Data send to PC (numbers) are in mV.\n");
-            while(!pc.writeable());
-            printf("Set data exactly.\n");
-            while(!pc.writeable());
-            printf("Write to console: \"xx yy\", where xx is a code of seting and yy his value.\n");
-            while(!pc.writeable());
-            printf("01 y.yyy - set period[s] to send data to PC and start reading value, example:01 0.01\n");
-            while(!pc.writeable());
-            printf("02 0 - stop reading value example:02 0\n");
-            while(!pc.writeable());
-            printf("03 yyyy - Wait for minimum voltage[mV] (to 3299) and send data example:03 2000\n");
-        while(!pc.writeable());
-            printf("end HELP\n");
-        flushSerialPort();
+            menu();             // print menu
+            flushSerialPort();  // discard data from serial
+
         }
-        // wait(0.2); // 200 ms
     }
 }