Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 0:e3161ef4ffd9, committed 2019-09-16
- Comitter:
- Ferryy
- Date:
- Mon Sep 16 10:17:12 2019 +0000
- Commit message:
- Working version of rotary encoder readout software.
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Encoder/Encoder.cpp Mon Sep 16 10:17:12 2019 +0000
@@ -0,0 +1,36 @@
+#include "mbed.h"
+
+void EncoderInit(TIM_Encoder_InitTypeDef * encoder, TIM_HandleTypeDef * timer, TIM_TypeDef * TIMx, uint32_t maxcount, uint32_t encmode)
+{
+
+ timer->Instance = TIMx;
+ timer->Init.Period = maxcount;
+ timer->Init.CounterMode = TIM_COUNTERMODE_UP;
+ timer->Init.Prescaler = 0;
+ timer->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
+
+ encoder->EncoderMode = encmode;
+
+ encoder->IC1Filter = 0x0F;
+ encoder->IC1Polarity = TIM_INPUTCHANNELPOLARITY_RISING;
+ encoder->IC1Prescaler = TIM_ICPSC_DIV4;
+ encoder->IC1Selection = TIM_ICSELECTION_DIRECTTI;
+
+ encoder->IC2Filter = 0x0F;
+ encoder->IC2Polarity = TIM_INPUTCHANNELPOLARITY_FALLING;
+ encoder->IC2Prescaler = TIM_ICPSC_DIV4;
+ encoder->IC2Selection = TIM_ICSELECTION_DIRECTTI;
+
+
+ if (HAL_TIM_Encoder_Init(timer, encoder) != HAL_OK) {
+ printf("Couldn't Init Encoder\r\n");
+ while (1) {}
+ }
+
+ if(HAL_TIM_Encoder_Start(timer,TIM_CHANNEL_1)!=HAL_OK) {
+ printf("Couldn't Start Encoder\r\n");
+ while (1) {}
+ }
+}
+
+
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Encoder/Encoder.h Mon Sep 16 10:17:12 2019 +0000 @@ -0,0 +1,7 @@ +#ifndef ENCODER_H +#define ENCODER_H + +void EncoderInit(TIM_Encoder_InitTypeDef * encoder, TIM_HandleTypeDef * timer, TIM_TypeDef * TIMx, uint32_t maxcount, uint32_t encmode); + +#endif + \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Encoder/EncoderMspInitL1.cpp Mon Sep 16 10:17:12 2019 +0000
@@ -0,0 +1,63 @@
+#include "mbed.h"
+/*
+ * HAL_TIM_Encoder_MspInit()
+ * Overrides the __weak function stub in stm32l4xx_hal_tim.h
+ *
+ * Edit the below for your preferred pin wiring & pullup/down
+ * I have encoder common at 3V3, using GPIO_PULLDOWN on inputs.
+ * Encoder A&B outputs connected directly to GPIOs.
+ *
+ * http://www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/DM00098321.pdf
+ * Table 9 has GPIO alternate function pinout mappings.
+ *
+ * TIM2_CH1: AF1 @ PA_0, PA_5, PA_15, PE_9
+ * TIM2_CH2: AF1 @ PA_1, PB_3, PE_10
+ *
+ * TIM3_CH1: AF2 @ PA_6, PB_4, PC_6, PE_3
+ * TIM3_CH2: AF2 @ PA_7, PB_5, PC_7, PE_4
+ *
+ * TIM4_CH1: AF2 @ PB_6, PD_12
+ * TIM4_CH2: AF2 @ PB_7, PD_13
+ *
+ * TIM5_CH1: AF2 @ PA_0* *TIM5 used by mbed system ticker so unavailable
+ * TIM5_CH2: AF2 @ PA_1*
+ *
+ */
+
+#ifdef TARGET_STM32L1
+void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef *htim)
+{
+ GPIO_InitTypeDef GPIO_InitStruct;
+
+ if (htim->Instance == TIM2) { //PA0 PA1 = Nucleo A0 A1
+ __TIM2_CLK_ENABLE();
+ __GPIOA_CLK_ENABLE();
+ GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1;
+ GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
+ GPIO_InitStruct.Pull = GPIO_NOPULL;
+ GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
+ GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;
+ HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
+ }
+// else if (htim->Instance == TIM3) { //PB4 PB5 = Nucleo D5 D4
+// __TIM3_CLK_ENABLE();
+// __GPIOB_CLK_ENABLE();
+// GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5;
+// GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
+// GPIO_InitStruct.Pull = GPIO_PULLDOWN;
+// GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
+// GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;
+// HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
+// }
+// else if (htim->Instance == TIM4) { // PB6 PB7 = Nucleo D10 MORPHO_PB7
+// __TIM4_CLK_ENABLE();
+// __GPIOB_CLK_ENABLE();
+// GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7;
+// GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
+// GPIO_InitStruct.Pull = GPIO_PULLDOWN;
+// GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
+// GPIO_InitStruct.Alternate = GPIO_AF2_TIM4;
+// HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
+// }
+}
+#endif
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Mon Sep 16 10:17:12 2019 +0000
@@ -0,0 +1,67 @@
+/*
+ Software to read out rotary encoder through serial terminal readout
+ Rotary encoder: YUMO E6B2-CWZ3E
+ Connections: Brown to 5V supply on board
+ Blue to Ground (0V) on board
+ Data lines White and black to A0 and A1 (not sure which to which)
+ Orange not connected (open)
+
+ Code based on "Nucleo_Hello_Encoder" on mbed by David Lowe:
+ https://os.mbed.com/users/gregeric/code/Nucleo_Hello_Encoder/
+
+ Odacon B.V. - Ferry Musters - 2019
+*/
+
+#include "mbed.h"
+#include "Encoder.h"
+
+//STM mbed bug: these macros are MISSING from stm32f3xx_hal_tim.h
+#ifdef TARGET_STM32F3
+#define __HAL_TIM_GET_COUNTER(__HANDLE__) ((__HANDLE__)->Instance->CNT)
+#define __HAL_TIM_IS_TIM_COUNTING_DOWN(__HANDLE__) (((__HANDLE__)->Instance->CR1 &(TIM_CR1_DIR)) == (TIM_CR1_DIR))
+#endif
+
+Serial pc(USBTX, USBRX); // tx, rx
+
+TIM_Encoder_InitTypeDef encoder1, encoder2, encoder3, encoder4;
+TIM_HandleTypeDef timer1, timer2, timer3, timer4;
+
+int main()
+{
+ //counting on both A&B inputs, 4 ticks per cycle, full 32-bit count
+ EncoderInit(&encoder2, &timer2, TIM2, 0xffffffff, TIM_ENCODERMODE_TI12);
+
+ uint32_t lastvalue = __HAL_TIM_GET_COUNTER(&timer2);
+ int overflowcount = 0;
+ while(1) {
+ uint32_t currentvalue =__HAL_TIM_GET_COUNTER(&timer2);
+ int8_t dir = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2);
+
+ float pulsesperrevolution = 1024*4;
+ //keep track of a possible overflow
+ if((float)lastvalue - (float)currentvalue > 30000) { //with this huge value we must have an overflow
+ //pc.printf("Overflow happened, curval: %d lastval: %d, overflowcount: %d\r\n", currentvalue, lastvalue, overflowcount);
+ overflowcount++;
+ }
+ if((float)currentvalue - (float)lastvalue > 30000) { //underflow
+ //pc.printf("Underflow happened, curval: %d lastval: %d, overflowcount: %d\r\n", currentvalue, lastvalue, overflowcount);
+ overflowcount--;
+ }
+ float pulsevalue = (float) currentvalue + (float) overflowcount * 0x10000;
+ float fullrotations = (currentvalue/pulsesperrevolution) + overflowcount * 0x10000/pulsesperrevolution;
+
+ char inchar = ' ';
+ scanf("%c",&inchar);
+ if(inchar == 'i' || inchar == 'I') {
+ pc.printf("Rotary\r\n");
+ } else if(inchar == 'm' || inchar == 'M') {
+ pc.printf("%f\r\n", fullrotations);
+ //pc.printf("%d, %f%s \r\n", overflowcount, fullrotations, dir==0 ? "+":"-");
+ //pc.printf("%d: %f%s \r\n", i++, (currentvalue/pulsesperrevolution), dir2==0 ? "+":"-");
+ } else if(inchar == 'p' || inchar == 'P') {
+ pc.printf("%f\r\n", pulsevalue);
+ }
+ lastvalue = currentvalue;
+ }
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main_original.cpp.txt Mon Sep 16 10:17:12 2019 +0000
@@ -0,0 +1,81 @@
+
+#include "mbed.h"
+#include "Encoder.h"
+
+//STM mbed bug: these macros are MISSING from stm32f3xx_hal_tim.h
+#ifdef TARGET_STM32F3
+#define __HAL_TIM_GET_COUNTER(__HANDLE__) ((__HANDLE__)->Instance->CNT)
+#define __HAL_TIM_IS_TIM_COUNTING_DOWN(__HANDLE__) (((__HANDLE__)->Instance->CR1 &(TIM_CR1_DIR)) == (TIM_CR1_DIR))
+#endif
+
+TIM_Encoder_InitTypeDef encoder1, encoder2, encoder3, encoder4;
+TIM_HandleTypeDef timer1, timer2, timer3, timer4;
+
+int main()
+{
+ //examples
+
+ //counting on A-input only, 2 ticks per cycle, rolls over at 100
+ //EncoderInit(&encoder1, &timer1, TIM1, 99, TIM_ENCODERMODE_TI1);
+
+ //counting on both A&B inputs, 4 ticks per cycle, full 32-bit count
+ EncoderInit(&encoder2, &timer2, TIM2, 0xffffffff, TIM_ENCODERMODE_TI12);
+
+ //counting on B-input only, 2 ticks per cycle, full 16-bit count
+ //EncoderInit(&encoder3, &timer3, TIM3, 0xffff, TIM_ENCODERMODE_TI2);
+
+ //counting on both A&B inputs, 4 ticks per cycle, full 16-bit count
+ //EncoderInit(&encoder4, &timer4, TIM4, 0xffff, TIM_ENCODERMODE_TI12);
+
+ //TIM5 is used by mbed for systick
+ //EncoderInit(encoder2, timer2, TIM5, 0xffffffff, TIM_ENCODERMODE_TI12);
+
+ printf("STM HAL encoder demo\n\r");
+ int i = 0;
+ while(1) {
+ uint16_t count1=0, count3=0, count4=0;
+ uint32_t count2=0;
+ int8_t dir1, dir2, dir3, dir4;
+
+
+ //OK 401 411 446 TICKER 030
+ //count1=TIM1->CNT;
+ //dir1=TIM1->CR1&TIM_CR1_DIR;
+ //count1=__HAL_TIM_GET_COUNTER(&timer1);
+ //dir1 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer1);
+
+ //OK 401 411 446 NOK 030
+ //count2=TIM2->CNT;
+ //dir2=TIM2->CR1&TIM_CR1_DIR;
+ count2=__HAL_TIM_GET_COUNTER(&timer2);
+ dir2 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2);
+
+ //OK 401 411 446 030
+ //count3=TIM3->CNT;
+ //dir3=TIM3->CR1&TIM_CR1_DIR;
+ //count3=__HAL_TIM_GET_COUNTER(&timer3);
+ //dir3 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer3);
+
+ //OK 401 411 446 N/A 030
+ //count4=TIM4->CNT;
+ //dir4=TIM4->CR1&TIM_CR1_DIR;
+ //count4=__HAL_TIM_GET_COUNTER(&timer4);
+ //dir4 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer4);
+
+ //TICKER 401 411 446 N/A 030
+// count5=__HAL_TIM_GET_COUNTER(&timer5);
+// dir5 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer5);
+
+ //printf("%d%s %d%s %d%s %d%s\r\n", count1, dir1==0 ? "+":"-",
+ // count2, dir2==0 ? "+":"-",
+ // count3, dir3==0 ? "+":"-",
+ // count4, dir4==0 ? "+":"-" );
+ float pulsesperrevolution = 1024*4;
+ float fullrotations = (count2/pulsesperrevolution);
+ printf("%f%s \r\n", fullrotations, dir2==0 ? "+":"-");
+ //printf("%d: %f%s \r\n", i++, (count2/pulsesperrevolution), dir2==0 ? "+":"-");
+ wait(0.1);
+
+ }
+}
+
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon Sep 16 10:17:12 2019 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/3a7713b1edbc \ No newline at end of file