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:
9:4d736d29ce19
Parent:
8:28ad0ba5a673
Child:
10:8862c8779b71
diff -r 28ad0ba5a673 -r 4d736d29ce19 main.cpp
--- a/main.cpp	Fri May 19 23:01:01 2017 +0000
+++ b/main.cpp	Sat May 27 01:26:41 2017 +0000
@@ -24,6 +24,12 @@
  * Thanks to:
  * David Lowe (for the quadrature encoder code)
  * https://developer.mbed.org/users/gregeric/code/Nucleo_Hello_Encoder/
+ * 
+ * And Frederic Blanc
+ * https://developer.mbed.org/users/fblanc/code/AnalogIn_Diff/
+ *
+ * And Eric Lewiston /  STM32L4xx_HAL_Driver
+ * https://developer.mbed.org/users/EricLew/code/STM32L4xx_HAL_Driver/docs/tip/group__ADC__LL__EF__Configuration__Channels.html
  *
  * References:
  * http://www.st.com/resource/en/datasheet/stm32l432kc.pdf
@@ -58,8 +64,8 @@
 //of the AVAGO encoder with respect to the shaft
 DigitalOut led2(PB_4);
 
-AnalogIn    vref(ADC_VREF);
-AnalogIn   tempint(ADC_TEMP);
+//AnalogIn    vref(ADC_VREF);
+//AnalogIn   tempint(ADC_TEMP);
 
 //Defining the ADCs
 AnalogIn adc1(PA_3); //ADC1_IN8
@@ -124,7 +130,7 @@
         if (adjust == -3) {
             count2--;
             count1 +=__HAL_TIM_GET_COUNTER(&timer2);
-            TIM2->CNT = 0x0000;
+            //TIM2->CNT = 0x0000;
             adjust = 0;
             led2 =!led2;
         }
@@ -133,7 +139,7 @@
         if (adjust == 3) {
             count2++;
             count1 +=__HAL_TIM_GET_COUNTER(&timer2);
-            TIM2->CNT = 0x0000;
+            //TIM2->CNT = 0x0000;
             adjust = 0;
             led2 =!led2;
         }
@@ -188,6 +194,7 @@
 
     //Message to mark the initialisation of the program
     printf("\n\rSTM HAL encoder with ADC and DAC\n\r");
+    printf("Running at %u MHz\r\n\n", HAL_RCC_GetSysClockFreq()/1000000);
 
     //printf("%" PRIu32 "\n", UINT32_MAX);
 
@@ -202,7 +209,7 @@
         int8_t dir1;
         
         
-        uint16_t voltage1=0;
+        //uint16_t voltage1=0;
 
 
         //OK 401 411 446 NOK 030
@@ -210,7 +217,7 @@
         //dir1=TIM2->CR1&TIM_CR1_DIR;
         
         //It reads the ADC1 value converted from 12bit to 16bit resolution
-        voltage1=adc1.read_u16();
+        //voltage1=adc1.read_u16();
         
         //It resets the DAC1
         //dac1.free();
@@ -226,20 +233,21 @@
         count3=__HAL_TIM_GET_COUNTER(&timer2);
         dir1 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2);
         
-        printf("%ld%s passes=%d short=%ld\r\n", count1+count3, dir1==0 ? "+":"-", count2, count3);
-        printf("Voltage ADC1= %u, DAC=%f\r\n", voltage1, dac1.read()*3.3);
-        printf("Vref: %f\r\n", (3.3f)*vref.read());
-        printf("percentage: %3.3f%%\r\n", adc2.read()*100.0f);
+        printf("%ld%s passes=%d\r\n", count3, dir1==0 ? "+":"-", count2);
+        printf("Voltage ADC1= %3.3f%V, DAC=%f\r\n", adc1.read()*3.3f, dac1.read()*3.3);
+        //printf("Vref: %f\r\n", vref.read());
+        printf("Voltage ADC2: %3.3f%V\r\n", adc2.read()*3.3f);
         
         //printf("Vref(f): %f, Vref : %u, Temperature : %u\r\n",
         //         vref.read(), vref.read_u16(), tempint.read_u16());
         if (adc3_en) {
             printf("Voltage ADC3: %3.3f%V\r\n", adc3.read()*3.3f);
-            printf("Difference: %f\r\n", dac1.read()*3.3 - adc3.read()*3.3f);
+            printf("Voltage ADC3: %u\r\n", adc3.read_u16());
+            printf("DAC read: %3.3f%V\r\n", dac1.read()*3.3f);
         } 
-        printf("\033[%dA", lines); // Moves cursor up of #lines
+        //printf("\033[%dA", lines); // Moves cursor up of #lines
 
-        wait(0.04);
+        wait(3.04);
     }
 
 }
\ No newline at end of file