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.
Diff: main.cpp
- Revision:
- 5:29372f6cb533
- Parent:
- 4:4f115819171f
- Child:
- 6:5d4c09973041
--- a/main.cpp Sat May 13 19:04:08 2017 +0000 +++ b/main.cpp Sat May 13 23:47:38 2017 +0000 @@ -22,32 +22,69 @@ #include "Encoder.h" TIM_Encoder_InitTypeDef encoder1; -TIM_HandleTypeDef timer1, timer2; +TIM_HandleTypeDef timer2; +InterruptIn event(PA_8); +DigitalOut led(LED1); +uint32_t count1=0; +int16_t count2=0; +int16_t count3=0; +int16_t adjust=0; +Timer timer1; + +void atint() { + timer1.start(); + 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 + { + adjust++; + if (adjust == 3) + { + count2++; + count1 +=__HAL_TIM_GET_COUNTER(&timer2); + TIM2->CNT = 0x0000; + adjust = 0; + led =!led; + } + } +} int main() { - //counting on both A&B inputs, 4 ticks per cycle, full 32-bit count + //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 //from TIM2 to TIM7 in TARGET/../device/hal_tick.h EncoderInit(&encoder1, &timer2, TIM2, 0xffffffff, TIM_ENCODERMODE_TI12); + event.rise(&atint); printf("\n\rSTM HAL encoder with ADC and DAC\n\r"); while(1) { - uint16_t count2=0; - uint32_t count1=0; + uint32_t count3=0; + int8_t dir1; //OK 401 411 446 NOK 030 - //count2=TIM2->CNT; - //dir2=TIM2->CR1&TIM_CR1_DIR; - count1=__HAL_TIM_GET_COUNTER(&timer2); + //count1=TIM2->CNT; + //dir1=TIM2->CR1&TIM_CR1_DIR; + count3=__HAL_TIM_GET_COUNTER(&timer2); dir1 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2); - printf("%d%s\r\n", count1, dir1==0 ? "+":"-"); + printf("%ld%s passes=%d short=%ld\r\n", count1+count3, dir1==0 ? "+":"-", count2, count3); wait(0.1); } }