A program to monitor some parameters for a motor

Dependencies:   mbed-dev BufferSerial

Thanks to David Lowe for https://developer.mbed.org/users/gregeric/code/Nucleo_Hello_Encoder/ which I adapted for the use of TIM2 32bit timer as an encoder reader on the Nucleo L432KC board.

Revision:
7:7f59b69d8895
Parent:
6:5d4c09973041
Child:
8:28ad0ba5a673
--- a/main.cpp	Sun May 14 21:44:05 2017 +0000
+++ b/main.cpp	Thu May 18 00:08:20 2017 +0000
@@ -14,13 +14,15 @@
  * http://www.st.com/st-web-ui/static/active/en/resource/technical/document/reference_manual/DM00096844.pdf
  * http://www.st.com/web/en/resource/technical/document/application_note/DM00042534.pdf
  * http://www.st.com/web/en/resource/technical/document/datasheet/DM00102166.pdf
- * 
+ *
  * David Lowe Jan 2015
  */
 
 #include "mbed.h"
 #include "Encoder.h"
 #include "inttypes.h"
+#include <string.h>   // strstr()
+#include <stdbool.h>  // bool, true, false
 
 TIM_Encoder_InitTypeDef encoder1;
 TIM_HandleTypeDef timer2;
@@ -30,31 +32,37 @@
 AnalogIn adc2(PA_5); //ADC1_IN10
 AnalogIn adc3(PA_6); //ADC1_IN11
 AnalogOut dac1(PA_4); // DAC1_OUT1
+//Serial pc(SERIAL_TX, SERIAL_RX);
+Serial pc(USBTX, USBRX);
+//Serial& pc = get_stdio_serial();
+
+//Serial debug(PB_6,PA_10);    // Serial1 tx rx
+//Serial configBT(PA_2,PA_3);  // Serial2 tx rx
+//Serial slave(PC_10,PC_11);   // Serial3 tx rx
+//Serial modem(PA_0,PA_1);     // Serial4 tx rx
+
 uint32_t count1=0;
 int16_t count2=0;
 int16_t count3=0;
 int16_t adjust=0;
+volatile bool adc3_en = false;
 Timer timer1;
 
-void atint() {
+void atint()
+{
     timer1.start();
-    if (__HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2))
-    {
-    adjust--;
-    if (adjust == -3)
-        {
+    if (__HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2)) {
+        adjust--;
+        if (adjust == -3) {
             count2--;
             count1 +=__HAL_TIM_GET_COUNTER(&timer2);
             TIM2->CNT = 0x0000;
             adjust = 0;
             led =!led;
         }
-    }
-    else
-    {
+    } else {
         adjust++;
-        if (adjust == 3)
-        {
+        if (adjust == 3) {
             count2++;
             count1 +=__HAL_TIM_GET_COUNTER(&timer2);
             TIM2->CNT = 0x0000;
@@ -64,23 +72,40 @@
     }
 }
 
+void readData(void)
+{
+    char message[125];
+    if(pc.readable()) {
+        pc.scanf("%s", message);
+        printf("%d %s\r\n", strlen(message), message);
+        if (strcmp(message, "startAdc") == 0) {
+            adc3_en = true;
+            printf("true\r\n");
+        } else {
+            adc3_en = false;
+            printf("false\r\n");
+        }
+
+    }
+}
 int main()
 {
 
-    //counting on both A&B inputs (A at PA0, B at PA1), 4 ticks per cycle, 
+    //counting on both A&B inputs (A at PA0, B at PA1), 4 ticks per cycle,
     //full 32-bit count
-    //For L432KC to work with TIM2 one needs to reassign the system ticker 
+    //For L432KC to work with TIM2 one needs to reassign the system ticker
     //from TIM2 to TIM7 in TARGET/../device/hal_tick.h
     EncoderInit(&encoder1, &timer2, TIM2, 0xffffffff, TIM_ENCODERMODE_TI12);
     event.rise(&atint);
-    
+    pc.attach(&readData);
+
     printf("\n\rSTM HAL encoder with ADC and DAC\n\r");
-    
+
     //printf("%" PRIu32 "\n", UINT32_MAX);
-    
+
     while(1) {
         uint32_t count3=0;
-        
+
         int8_t dir1;
         uint16_t voltage1=0;
 
@@ -92,11 +117,14 @@
         dir1 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2);
         voltage1=adc1.read_u16();
         dac1.write_u16(count3*2);
-        
+
         printf("%ld%s passes=%d short=%ld\r\n", count1+count3, dir1==0 ? "+":"-", count2, count3);
         printf("Voltage= %d, DAC=%f\r\n", voltage1, dac1.read()*3.3);
         printf("percentage: %3.3f%%\r\n", adc2.read()*100.0f);
-        printf("percentage: %3.3f%%\r\n", adc3.read()*100.0f);
+        if (adc3_en) {
+            printf("percentage: %3.3f%V\r\n", adc3.read()*3.3f);
+        }
+
 
         wait(2.1);
     }