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.

Committer:
tonnyleonard
Date:
Sat Jun 03 18:59:04 2017 +0000
Revision:
14:e5f5b345b2fe
Parent:
13:e08e3540c30b
Child:
15:8adff67fe707
Correct ADC & DAC factors for DCPS load conditions, add table with these corrective additions

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tonnyleonard 0:789510d98ade 1 /*
tonnyleonard 10:8862c8779b71 2 * Nucleo STM32F4(or L4) quadrature decoder, ADCs and DACs
tonnyleonard 8:28ad0ba5a673 3 * with serial communication over the USB virtual serial port
tonnyleonard 10:8862c8779b71 4
tonnyleonard 8:28ad0ba5a673 5 * Developed for Elliptec X15 piezoelectric motor control, on a L432KC board
tonnyleonard 8:28ad0ba5a673 6 *
tonnyleonard 0:789510d98ade 7 * Using STM32's counter peripherals to interface rotary encoders.
tonnyleonard 10:8862c8779b71 8 * Encoders are supported on F4xx's TIM1,2,3,4,5. TIM2 & TIM5 have 32bit count,
tonnyleonard 8:28ad0ba5a673 9 * others 16bit.
tonnyleonard 10:8862c8779b71 10 * Take into account that on F4xx Mbed uses TIM5 for system timer, SPI needs TIM1,
tonnyleonard 8:28ad0ba5a673 11 * others are used for PWM.
tonnyleonard 10:8862c8779b71 12 * Check your platform's PeripheralPins.c & PeripheralNames.h if you need
tonnyleonard 8:28ad0ba5a673 13 * both PWM & encoders. This project does not use PWM.
tonnyleonard 0:789510d98ade 14 *
tonnyleonard 10:8862c8779b71 15 * On L432KC, for example, only TIM2 has 32bit count, others have 16bit.
tonnyleonard 8:28ad0ba5a673 16 * However, mbed has TIM2 assigned by default to the system ticker
tonnyleonard 10:8862c8779b71 17 * For L432KC to work with TIM2 encoder input one needs to reassign
tonnyleonard 10:8862c8779b71 18 * the system ticker from TIM2 to TIM7, for example,
tonnyleonard 8:28ad0ba5a673 19 * in mbed-dev/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L432xC/device/hal_tick.h
tonnyleonard 8:28ad0ba5a673 20 *
tonnyleonard 8:28ad0ba5a673 21 * Edit HAL_TIM_Encoder_MspInitFx(Lx).cpp to suit your mcu & board's available
tonnyleonard 8:28ad0ba5a673 22 * pinouts & pullups/downs.
tonnyleonard 0:789510d98ade 23 *
tonnyleonard 0:789510d98ade 24 * Thanks to:
tonnyleonard 8:28ad0ba5a673 25 * David Lowe (for the quadrature encoder code)
tonnyleonard 8:28ad0ba5a673 26 * https://developer.mbed.org/users/gregeric/code/Nucleo_Hello_Encoder/
tonnyleonard 10:8862c8779b71 27 *
tonnyleonard 9:4d736d29ce19 28 * And Frederic Blanc
tonnyleonard 9:4d736d29ce19 29 * https://developer.mbed.org/users/fblanc/code/AnalogIn_Diff/
tonnyleonard 9:4d736d29ce19 30 *
tonnyleonard 9:4d736d29ce19 31 * And Eric Lewiston / STM32L4xx_HAL_Driver
tonnyleonard 9:4d736d29ce19 32 * https://developer.mbed.org/users/EricLew/code/STM32L4xx_HAL_Driver/docs/tip/group__ADC__LL__EF__Configuration__Channels.html
tonnyleonard 0:789510d98ade 33 *
tonnyleonard 0:789510d98ade 34 * References:
tonnyleonard 8:28ad0ba5a673 35 * http://www.st.com/resource/en/datasheet/stm32l432kc.pdf
tonnyleonard 8:28ad0ba5a673 36 * https://developer.mbed.org/platforms/ST-Nucleo-L432KC/
tonnyleonard 0:789510d98ade 37 * http://www.st.com/web/en/resource/technical/document/application_note/DM00042534.pdf
tonnyleonard 0:789510d98ade 38 * http://www.st.com/web/en/resource/technical/document/datasheet/DM00102166.pdf
tonnyleonard 7:7f59b69d8895 39 *
tonnyleonard 8:28ad0ba5a673 40 * Tonny-Leonard Farauanu, 2017
tonnyleonard 0:789510d98ade 41 */
tonnyleonard 0:789510d98ade 42
tonnyleonard 0:789510d98ade 43 #include "mbed.h"
tonnyleonard 0:789510d98ade 44 #include "Encoder.h"
tonnyleonard 8:28ad0ba5a673 45 #include "inttypes.h" // for PRIu32 encoding
tonnyleonard 8:28ad0ba5a673 46
tonnyleonard 8:28ad0ba5a673 47 //Defining the timer and its coresponding encoder
tonnyleonard 8:28ad0ba5a673 48 TIM_HandleTypeDef timer2;
tonnyleonard 4:4f115819171f 49 TIM_Encoder_InitTypeDef encoder1;
tonnyleonard 8:28ad0ba5a673 50
tonnyleonard 8:28ad0ba5a673 51 //The input for the encoder's index channel
tonnyleonard 5:29372f6cb533 52 InterruptIn event(PA_8);
tonnyleonard 8:28ad0ba5a673 53
tonnyleonard 8:28ad0ba5a673 54 //LED1 to signal USB serial RX interrupt reading
tonnyleonard 8:28ad0ba5a673 55 DigitalOut led1(LED1);
tonnyleonard 8:28ad0ba5a673 56
tonnyleonard 8:28ad0ba5a673 57 //LED2 to signal the encoder's index impulses
tonnyleonard 10:8862c8779b71 58 //only for feedback, to calibrate the position
tonnyleonard 8:28ad0ba5a673 59 //of the AVAGO encoder with respect to the shaft
tonnyleonard 8:28ad0ba5a673 60 DigitalOut led2(PB_4);
tonnyleonard 8:28ad0ba5a673 61
tonnyleonard 12:8c355d78e081 62 //Relay to power on and off the DCPS
tonnyleonard 12:8c355d78e081 63 DigitalOut relay1(PB_5);
tonnyleonard 12:8c355d78e081 64
tonnyleonard 9:4d736d29ce19 65 //AnalogIn vref(ADC_VREF);
tonnyleonard 9:4d736d29ce19 66 //AnalogIn tempint(ADC_TEMP);
tonnyleonard 8:28ad0ba5a673 67
tonnyleonard 8:28ad0ba5a673 68 //Defining the ADCs
tonnyleonard 6:5d4c09973041 69 AnalogIn adc1(PA_3); //ADC1_IN8
tonnyleonard 10:8862c8779b71 70 AnalogIn adc2(PA_7); //ADC1_IN9
tonnyleonard 6:5d4c09973041 71 AnalogIn adc3(PA_6); //ADC1_IN11
tonnyleonard 8:28ad0ba5a673 72
tonnyleonard 10:8862c8779b71 73 //Defining the DAC for setting the half of the current amplitude
tonnyleonard 10:8862c8779b71 74 //and generate a quasi square signal for the PLL current phase input
tonnyleonard 10:8862c8779b71 75 AnalogOut dac1(PA_4); // DAC1_OUT1
tonnyleonard 10:8862c8779b71 76
tonnyleonard 10:8862c8779b71 77 //Defining the DAC for the Power Source
tonnyleonard 8:28ad0ba5a673 78 //(DCPS - Digitally Controlled Power Source)
tonnyleonard 8:28ad0ba5a673 79 //DAC1_OUT2 on pin PA_5 is at least twices as fast as DAC1_OUT1 on pin PA_4
tonnyleonard 10:8862c8779b71 80 AnalogOut dac2(PA_5); // DAC1_OUT2
tonnyleonard 8:28ad0ba5a673 81
tonnyleonard 8:28ad0ba5a673 82 //Defining the serial object to be used for communicating with Raspi
tonnyleonard 8:28ad0ba5a673 83 Serial raspi(USBTX, USBRX);
tonnyleonard 10:8862c8779b71 84
tonnyleonard 8:28ad0ba5a673 85 //Serial& raspi = get_stdio_serial();
tonnyleonard 7:7f59b69d8895 86 //Serial debug(PB_6,PA_10); // Serial1 tx rx
tonnyleonard 7:7f59b69d8895 87
tonnyleonard 14:e5f5b345b2fe 88 int ADC_Count = 0;
tonnyleonard 14:e5f5b345b2fe 89 float Voltage;
tonnyleonard 14:e5f5b345b2fe 90 float Voltage_total;
tonnyleonard 14:e5f5b345b2fe 91 float samples[1024];
tonnyleonard 14:e5f5b345b2fe 92
tonnyleonard 8:28ad0ba5a673 93 uint16_t count1=0;
tonnyleonard 5:29372f6cb533 94 int16_t count2=0;
tonnyleonard 8:28ad0ba5a673 95 int32_t count3=0;
tonnyleonard 5:29372f6cb533 96 int16_t adjust=0;
tonnyleonard 8:28ad0ba5a673 97
tonnyleonard 10:8862c8779b71 98 float speedLast=0;
tonnyleonard 10:8862c8779b71 99
tonnyleonard 8:28ad0ba5a673 100 float dac_val=0;
tonnyleonard 13:e08e3540c30b 101 float dac2_val=0;
tonnyleonard 10:8862c8779b71 102 float adc3_val=0;
tonnyleonard 8:28ad0ba5a673 103
tonnyleonard 14:e5f5b345b2fe 104 //Array with adc1, adc2 & adc3 correction addition,
tonnyleonard 14:e5f5b345b2fe 105 //experimentally determined
tonnyleonard 14:e5f5b345b2fe 106 const float adc_corr[] = { 0, 0.022f, 0.2995, 0.0267};
tonnyleonard 14:e5f5b345b2fe 107
tonnyleonard 14:e5f5b345b2fe 108 int main()
tonnyleonard 14:e5f5b345b2fe 109 {
tonnyleonard 14:e5f5b345b2fe 110 printf("%d\n", lookup("X0"));
tonnyleonard 14:e5f5b345b2fe 111 return 0;
tonnyleonard 14:e5f5b345b2fe 112 }
tonnyleonard 14:e5f5b345b2fe 113
tonnyleonard 13:e08e3540c30b 114 volatile bool adc3_en = true;
tonnyleonard 8:28ad0ba5a673 115 int16_t lines = 4;
tonnyleonard 8:28ad0ba5a673 116
tonnyleonard 8:28ad0ba5a673 117 //TIM1 to be used with the interrupt for the encoder's index pulses
tonnyleonard 5:29372f6cb533 118 Timer timer1;
tonnyleonard 10:8862c8779b71 119 Timer timer3;
tonnyleonard 8:28ad0ba5a673 120
tonnyleonard 8:28ad0ba5a673 121 //Function invoked by the encoder's index interrupt pulses
tonnyleonard 7:7f59b69d8895 122 void atint()
tonnyleonard 7:7f59b69d8895 123 {
tonnyleonard 5:29372f6cb533 124 timer1.start();
tonnyleonard 7:7f59b69d8895 125 if (__HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2)) {
tonnyleonard 7:7f59b69d8895 126 adjust--;
tonnyleonard 10:8862c8779b71 127 if (adjust == -1) {
tonnyleonard 5:29372f6cb533 128 count2--;
tonnyleonard 5:29372f6cb533 129 count1 +=__HAL_TIM_GET_COUNTER(&timer2);
tonnyleonard 9:4d736d29ce19 130 //TIM2->CNT = 0x0000;
tonnyleonard 5:29372f6cb533 131 adjust = 0;
tonnyleonard 8:28ad0ba5a673 132 led2 =!led2;
tonnyleonard 5:29372f6cb533 133 }
tonnyleonard 7:7f59b69d8895 134 } else {
tonnyleonard 5:29372f6cb533 135 adjust++;
tonnyleonard 10:8862c8779b71 136 if (adjust == 1) {
tonnyleonard 5:29372f6cb533 137 count2++;
tonnyleonard 5:29372f6cb533 138 count1 +=__HAL_TIM_GET_COUNTER(&timer2);
tonnyleonard 9:4d736d29ce19 139 //TIM2->CNT = 0x0000;
tonnyleonard 5:29372f6cb533 140 adjust = 0;
tonnyleonard 8:28ad0ba5a673 141 led2 =!led2;
tonnyleonard 5:29372f6cb533 142 }
tonnyleonard 5:29372f6cb533 143 }
tonnyleonard 5:29372f6cb533 144 }
tonnyleonard 14:e5f5b345b2fe 145 float speedRead()
tonnyleonard 14:e5f5b345b2fe 146 {
tonnyleonard 10:8862c8779b71 147 uint16_t i = 0;
tonnyleonard 10:8862c8779b71 148 uint32_t deltaT;
tonnyleonard 10:8862c8779b71 149 float speed;
tonnyleonard 10:8862c8779b71 150 uint32_t pos1;
tonnyleonard 10:8862c8779b71 151 uint32_t pos2;
tonnyleonard 10:8862c8779b71 152 int32_t pos;
tonnyleonard 10:8862c8779b71 153 int8_t sens1;
tonnyleonard 10:8862c8779b71 154 int8_t sens2;
tonnyleonard 10:8862c8779b71 155 printf("Encoder speed reading!\r\n");
tonnyleonard 10:8862c8779b71 156 timer3.start();
tonnyleonard 10:8862c8779b71 157 pos1=__HAL_TIM_GET_COUNTER(&timer2);
tonnyleonard 10:8862c8779b71 158 sens1 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2);
tonnyleonard 10:8862c8779b71 159 wait_ms(5);
tonnyleonard 10:8862c8779b71 160 pos2=__HAL_TIM_GET_COUNTER(&timer2);
tonnyleonard 10:8862c8779b71 161 sens2 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2);
tonnyleonard 14:e5f5b345b2fe 162
tonnyleonard 10:8862c8779b71 163 //The speed computation method adapts to slow/fast speeds, in order to
tonnyleonard 10:8862c8779b71 164 //optimize the rapport between computation precision and time windows size
tonnyleonard 10:8862c8779b71 165 while (pos2 == pos1 && i<99) { // The accepted max delay time is 0.5 seconds
tonnyleonard 10:8862c8779b71 166 wait_ms(5);
tonnyleonard 10:8862c8779b71 167 i++;
tonnyleonard 10:8862c8779b71 168 pos2=__HAL_TIM_GET_COUNTER(&timer2);
tonnyleonard 10:8862c8779b71 169 sens2 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2);
tonnyleonard 14:e5f5b345b2fe 170 }
tonnyleonard 10:8862c8779b71 171 pos2=__HAL_TIM_GET_COUNTER(&timer2);
tonnyleonard 10:8862c8779b71 172 sens2 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2);
tonnyleonard 10:8862c8779b71 173 timer3.stop();
tonnyleonard 10:8862c8779b71 174 deltaT = timer3.read_us();
tonnyleonard 10:8862c8779b71 175 timer3.reset();
tonnyleonard 10:8862c8779b71 176 pos = (int32_t) pos2 - (int32_t) pos1;
tonnyleonard 14:e5f5b345b2fe 177
tonnyleonard 10:8862c8779b71 178 printf("Time is %lu microseconds, position modified %ld %lu %lu\r\n", deltaT, pos, pos1, pos2);
tonnyleonard 14:e5f5b345b2fe 179 if (deltaT > 0) {
tonnyleonard 11:7a9837acbea0 180 speed = ((float) pos)*125.f/((float) deltaT); // (pulses/us)*1000000/8000 -> rot/s
tonnyleonard 14:e5f5b345b2fe 181 } else {
tonnyleonard 10:8862c8779b71 182 printf("Error, time interval not greater than zero, speed not calculated!\r\n");
tonnyleonard 14:e5f5b345b2fe 183 }
tonnyleonard 14:e5f5b345b2fe 184 printf("The encoder speed is %f rot/s\r\n", speed);
tonnyleonard 10:8862c8779b71 185 return speed;
tonnyleonard 14:e5f5b345b2fe 186 }
tonnyleonard 0:789510d98ade 187
tonnyleonard 8:28ad0ba5a673 188 //Function attached to the serial RX interrupt event
tonnyleonard 7:7f59b69d8895 189 void readData(void)
tonnyleonard 7:7f59b69d8895 190 {
tonnyleonard 7:7f59b69d8895 191 char message[125];
tonnyleonard 8:28ad0ba5a673 192 if(raspi.readable()) {
tonnyleonard 8:28ad0ba5a673 193 int p1=0;
tonnyleonard 8:28ad0ba5a673 194 led1 = 1;
tonnyleonard 8:28ad0ba5a673 195 raspi.scanf("%s", message);
tonnyleonard 7:7f59b69d8895 196 printf("%d %s\r\n", strlen(message), message);
tonnyleonard 7:7f59b69d8895 197 if (strcmp(message, "startAdc") == 0) {
tonnyleonard 7:7f59b69d8895 198 adc3_en = true;
tonnyleonard 8:28ad0ba5a673 199 lines = 6;
tonnyleonard 7:7f59b69d8895 200 printf("true\r\n");
tonnyleonard 8:28ad0ba5a673 201 } else if (strcmp(message, "stopAdc") == 0) {
tonnyleonard 7:7f59b69d8895 202 adc3_en = false;
tonnyleonard 8:28ad0ba5a673 203 lines = 4;
tonnyleonard 7:7f59b69d8895 204 printf("false\r\n");
tonnyleonard 10:8862c8779b71 205 } else if (p1=strstr(message, "DAC=") != NULL) {
tonnyleonard 14:e5f5b345b2fe 206 //The DCPS has 1V offset, so we have to remove it
tonnyleonard 14:e5f5b345b2fe 207 dac_val = (atof(message+p1+3)-1.0f)/15.61;
tonnyleonard 10:8862c8779b71 208 dac2.write(dac_val);
tonnyleonard 8:28ad0ba5a673 209 printf("Value to write to DAC: %f\r\n", dac_val*3.3f);
tonnyleonard 10:8862c8779b71 210 } else if (strcmp(message, "reset") == 0) {
tonnyleonard 10:8862c8779b71 211 TIM2->CNT = 0x0000;
tonnyleonard 10:8862c8779b71 212 printf("Encoder reset!\r\n");
tonnyleonard 12:8c355d78e081 213 } else if (strcmp(message, "poweron") == 0) {
tonnyleonard 12:8c355d78e081 214 relay1.write(1);
tonnyleonard 12:8c355d78e081 215 printf("DCPS on\r\n");
tonnyleonard 12:8c355d78e081 216 } else if (strcmp(message, "poweroff") == 0) {
tonnyleonard 12:8c355d78e081 217 relay1.write(0);
tonnyleonard 12:8c355d78e081 218 printf("DCPS off\r\n");
tonnyleonard 10:8862c8779b71 219 }
tonnyleonard 10:8862c8779b71 220
tonnyleonard 8:28ad0ba5a673 221 }
tonnyleonard 8:28ad0ba5a673 222 led1 = 0;
tonnyleonard 8:28ad0ba5a673 223 }
tonnyleonard 7:7f59b69d8895 224
tonnyleonard 14:e5f5b345b2fe 225 void ADC_read(AnalogIn adc)
tonnyleonard 14:e5f5b345b2fe 226 {
tonnyleonard 14:e5f5b345b2fe 227 ADC_Count++;
tonnyleonard 14:e5f5b345b2fe 228 Voltage_total =0;
tonnyleonard 14:e5f5b345b2fe 229 for (int i=0; i<100; i++) { // do 25 readings
tonnyleonard 14:e5f5b345b2fe 230 Voltage = adc.read();
tonnyleonard 14:e5f5b345b2fe 231
tonnyleonard 14:e5f5b345b2fe 232 Voltage_total = Voltage_total+ Voltage; //Note Vinput.read can be summed then averaged
tonnyleonard 14:e5f5b345b2fe 233 //wait_us(10);
tonnyleonard 14:e5f5b345b2fe 234 }
tonnyleonard 14:e5f5b345b2fe 235 Voltage=Voltage_total/100.f;
tonnyleonard 14:e5f5b345b2fe 236 samples[ADC_Count] = Voltage; //Save averaged reading within an array
tonnyleonard 14:e5f5b345b2fe 237 if (ADC_Count == 1023){
tonnyleonard 14:e5f5b345b2fe 238 ADC_Count = 0;
tonnyleonard 14:e5f5b345b2fe 239 }
tonnyleonard 14:e5f5b345b2fe 240
tonnyleonard 14:e5f5b345b2fe 241 }
tonnyleonard 14:e5f5b345b2fe 242
tonnyleonard 8:28ad0ba5a673 243 //The main function
tonnyleonard 0:789510d98ade 244 int main()
tonnyleonard 0:789510d98ade 245 {
tonnyleonard 12:8c355d78e081 246 //Power onn the DCPS
tonnyleonard 12:8c355d78e081 247 relay1.write(1);
tonnyleonard 14:e5f5b345b2fe 248
tonnyleonard 7:7f59b69d8895 249 //counting on both A&B inputs (A at PA0, B at PA1), 4 ticks per cycle,
tonnyleonard 5:29372f6cb533 250 //full 32-bit count
tonnyleonard 7:7f59b69d8895 251 //For L432KC to work with TIM2 one needs to reassign the system ticker
tonnyleonard 10:8862c8779b71 252 //from TIM2 to TIM7 in TARGET/../device/hal_tick.h\
tonnyleonard 8:28ad0ba5a673 253 //Initialise encoder
tonnyleonard 4:4f115819171f 254 EncoderInit(&encoder1, &timer2, TIM2, 0xffffffff, TIM_ENCODERMODE_TI12);
tonnyleonard 10:8862c8779b71 255
tonnyleonard 10:8862c8779b71 256 //Define function to call for interrupt event rise
tonnyleonard 8:28ad0ba5a673 257 //This is triggered by the encoder's index pulses
tonnyleonard 8:28ad0ba5a673 258 //and it resets the encoder counter
tonnyleonard 5:29372f6cb533 259 event.rise(&atint);
tonnyleonard 14:e5f5b345b2fe 260
tonnyleonard 12:8c355d78e081 261 //Set serial baud rate
tonnyleonard 12:8c355d78e081 262 raspi.baud(115200);
tonnyleonard 14:e5f5b345b2fe 263
tonnyleonard 8:28ad0ba5a673 264 //Attach functin to call for serial interrupt event
tonnyleonard 8:28ad0ba5a673 265 raspi.attach(&readData);
tonnyleonard 7:7f59b69d8895 266
tonnyleonard 8:28ad0ba5a673 267 //Message to mark the initialisation of the program
tonnyleonard 4:4f115819171f 268 printf("\n\rSTM HAL encoder with ADC and DAC\n\r");
tonnyleonard 9:4d736d29ce19 269 printf("Running at %u MHz\r\n\n", HAL_RCC_GetSysClockFreq()/1000000);
tonnyleonard 7:7f59b69d8895 270
tonnyleonard 6:5d4c09973041 271 //printf("%" PRIu32 "\n", UINT32_MAX);
tonnyleonard 7:7f59b69d8895 272
tonnyleonard 10:8862c8779b71 273 int i = 0;
tonnyleonard 8:28ad0ba5a673 274 //The main loop
tonnyleonard 0:789510d98ade 275 while(1) {
tonnyleonard 10:8862c8779b71 276
tonnyleonard 8:28ad0ba5a673 277 //Variable for the one loop encoder counter
tonnyleonard 5:29372f6cb533 278 uint32_t count3=0;
tonnyleonard 7:7f59b69d8895 279
tonnyleonard 8:28ad0ba5a673 280 //Variable for the direction of the counter
tonnyleonard 4:4f115819171f 281 int8_t dir1;
tonnyleonard 10:8862c8779b71 282
tonnyleonard 9:4d736d29ce19 283 //uint16_t voltage1=0;
tonnyleonard 0:789510d98ade 284
tonnyleonard 0:789510d98ade 285
tonnyleonard 0:789510d98ade 286 //OK 401 411 446 NOK 030
tonnyleonard 5:29372f6cb533 287 //count1=TIM2->CNT;
tonnyleonard 5:29372f6cb533 288 //dir1=TIM2->CR1&TIM_CR1_DIR;
tonnyleonard 10:8862c8779b71 289
tonnyleonard 8:28ad0ba5a673 290 //It reads the ADC1 value converted from 12bit to 16bit resolution
tonnyleonard 9:4d736d29ce19 291 //voltage1=adc1.read_u16();
tonnyleonard 10:8862c8779b71 292
tonnyleonard 8:28ad0ba5a673 293 //It resets the DAC1
tonnyleonard 8:28ad0ba5a673 294 //dac1.free();
tonnyleonard 13:e08e3540c30b 295 dac2_val = dac2.read();
tonnyleonard 10:8862c8779b71 296
tonnyleonard 8:28ad0ba5a673 297 //It writes the DAC1 value as a 16bit number
tonnyleonard 8:28ad0ba5a673 298 //dac1.write_u16(count3*2);
tonnyleonard 10:8862c8779b71 299
tonnyleonard 8:28ad0ba5a673 300 //It writes the DAC1 value as a subunitary float number
tonnyleonard 8:28ad0ba5a673 301 //to be multiplied with the max DAC_RANGE
tonnyleonard 14:e5f5b345b2fe 302
tonnyleonard 10:8862c8779b71 303
tonnyleonard 8:28ad0ba5a673 304 //It gets the one loop position and the direction of the encoder
tonnyleonard 5:29372f6cb533 305 count3=__HAL_TIM_GET_COUNTER(&timer2);
tonnyleonard 4:4f115819171f 306 dir1 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2);
tonnyleonard 10:8862c8779b71 307
tonnyleonard 10:8862c8779b71 308 if (i >= 100) {
tonnyleonard 10:8862c8779b71 309 adc3_val = adc3.read();
tonnyleonard 14:e5f5b345b2fe 310 ADC_read(adc1);
tonnyleonard 14:e5f5b345b2fe 311 ADC_read(adc2);
tonnyleonard 14:e5f5b345b2fe 312 ADC_read(adc3);
tonnyleonard 14:e5f5b345b2fe 313 //dac1.write(adc3_val);
tonnyleonard 14:e5f5b345b2fe 314
tonnyleonard 10:8862c8779b71 315 printf("%ld%s passes=%d\r\n", count3, dir1==0 ? "+":"-", count2);
tonnyleonard 14:e5f5b345b2fe 316 printf("Voltage ADC1= %3.3f%V, DAC=%f\r\n", (3.3f*samples[ADC_Count-2]+0.022f), dac2_val*3.3f);
tonnyleonard 14:e5f5b345b2fe 317 printf("VOUT: %f\r\n", dac2_val*15.61f+1.0f);
tonnyleonard 14:e5f5b345b2fe 318 printf("Voltage ADC2: %3.3f%V\r\n", (3.3f*samples[ADC_Count-1]+0.062f)/0.207048458f);
tonnyleonard 6:5d4c09973041 319
tonnyleonard 10:8862c8779b71 320 //printf("Vref(f): %f, Vref : %u, Temperature : %u\r\n",
tonnyleonard 10:8862c8779b71 321 // vref.read(), vref.read_u16(), tempint.read_u16());
tonnyleonard 10:8862c8779b71 322 if (adc3_en) {
tonnyleonard 10:8862c8779b71 323 printf("Voltage ADC3: %3.3f%V\r\n", adc3_val*3.3f);
tonnyleonard 14:e5f5b345b2fe 324 printf("Average ADC3: %3.3f%V\r\n", (3.3f*samples[ADC_Count]+0.022f)/0.8245614f);
tonnyleonard 10:8862c8779b71 325 printf("DAC1 read: %3.3f%V\r\n", dac1.read()*3.3f);
tonnyleonard 10:8862c8779b71 326 printf("DAC2 read: %3.3f%V\r\n", dac2.read()*3.3f);
tonnyleonard 10:8862c8779b71 327 }
tonnyleonard 10:8862c8779b71 328 speedLast = speedRead();
tonnyleonard 10:8862c8779b71 329 //printf("\033[%dA", lines); // Moves cursor up of #lines
tonnyleonard 14:e5f5b345b2fe 330
tonnyleonard 10:8862c8779b71 331 i=0;
tonnyleonard 10:8862c8779b71 332 }
tonnyleonard 10:8862c8779b71 333 i++;
tonnyleonard 10:8862c8779b71 334
tonnyleonard 10:8862c8779b71 335 wait(0.04);
tonnyleonard 0:789510d98ade 336 }
tonnyleonard 8:28ad0ba5a673 337
tonnyleonard 8:28ad0ba5a673 338 }