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.
main.cpp@0:e3161ef4ffd9, 2019-09-16 (annotated)
- Committer:
- Ferryy
- Date:
- Mon Sep 16 10:17:12 2019 +0000
- Revision:
- 0:e3161ef4ffd9
Working version of rotary encoder readout software.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Ferryy | 0:e3161ef4ffd9 | 1 | /* |
| Ferryy | 0:e3161ef4ffd9 | 2 | Software to read out rotary encoder through serial terminal readout |
| Ferryy | 0:e3161ef4ffd9 | 3 | Rotary encoder: YUMO E6B2-CWZ3E |
| Ferryy | 0:e3161ef4ffd9 | 4 | Connections: Brown to 5V supply on board |
| Ferryy | 0:e3161ef4ffd9 | 5 | Blue to Ground (0V) on board |
| Ferryy | 0:e3161ef4ffd9 | 6 | Data lines White and black to A0 and A1 (not sure which to which) |
| Ferryy | 0:e3161ef4ffd9 | 7 | Orange not connected (open) |
| Ferryy | 0:e3161ef4ffd9 | 8 | |
| Ferryy | 0:e3161ef4ffd9 | 9 | Code based on "Nucleo_Hello_Encoder" on mbed by David Lowe: |
| Ferryy | 0:e3161ef4ffd9 | 10 | https://os.mbed.com/users/gregeric/code/Nucleo_Hello_Encoder/ |
| Ferryy | 0:e3161ef4ffd9 | 11 | |
| Ferryy | 0:e3161ef4ffd9 | 12 | Odacon B.V. - Ferry Musters - 2019 |
| Ferryy | 0:e3161ef4ffd9 | 13 | */ |
| Ferryy | 0:e3161ef4ffd9 | 14 | |
| Ferryy | 0:e3161ef4ffd9 | 15 | #include "mbed.h" |
| Ferryy | 0:e3161ef4ffd9 | 16 | #include "Encoder.h" |
| Ferryy | 0:e3161ef4ffd9 | 17 | |
| Ferryy | 0:e3161ef4ffd9 | 18 | //STM mbed bug: these macros are MISSING from stm32f3xx_hal_tim.h |
| Ferryy | 0:e3161ef4ffd9 | 19 | #ifdef TARGET_STM32F3 |
| Ferryy | 0:e3161ef4ffd9 | 20 | #define __HAL_TIM_GET_COUNTER(__HANDLE__) ((__HANDLE__)->Instance->CNT) |
| Ferryy | 0:e3161ef4ffd9 | 21 | #define __HAL_TIM_IS_TIM_COUNTING_DOWN(__HANDLE__) (((__HANDLE__)->Instance->CR1 &(TIM_CR1_DIR)) == (TIM_CR1_DIR)) |
| Ferryy | 0:e3161ef4ffd9 | 22 | #endif |
| Ferryy | 0:e3161ef4ffd9 | 23 | |
| Ferryy | 0:e3161ef4ffd9 | 24 | Serial pc(USBTX, USBRX); // tx, rx |
| Ferryy | 0:e3161ef4ffd9 | 25 | |
| Ferryy | 0:e3161ef4ffd9 | 26 | TIM_Encoder_InitTypeDef encoder1, encoder2, encoder3, encoder4; |
| Ferryy | 0:e3161ef4ffd9 | 27 | TIM_HandleTypeDef timer1, timer2, timer3, timer4; |
| Ferryy | 0:e3161ef4ffd9 | 28 | |
| Ferryy | 0:e3161ef4ffd9 | 29 | int main() |
| Ferryy | 0:e3161ef4ffd9 | 30 | { |
| Ferryy | 0:e3161ef4ffd9 | 31 | //counting on both A&B inputs, 4 ticks per cycle, full 32-bit count |
| Ferryy | 0:e3161ef4ffd9 | 32 | EncoderInit(&encoder2, &timer2, TIM2, 0xffffffff, TIM_ENCODERMODE_TI12); |
| Ferryy | 0:e3161ef4ffd9 | 33 | |
| Ferryy | 0:e3161ef4ffd9 | 34 | uint32_t lastvalue = __HAL_TIM_GET_COUNTER(&timer2); |
| Ferryy | 0:e3161ef4ffd9 | 35 | int overflowcount = 0; |
| Ferryy | 0:e3161ef4ffd9 | 36 | while(1) { |
| Ferryy | 0:e3161ef4ffd9 | 37 | uint32_t currentvalue =__HAL_TIM_GET_COUNTER(&timer2); |
| Ferryy | 0:e3161ef4ffd9 | 38 | int8_t dir = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2); |
| Ferryy | 0:e3161ef4ffd9 | 39 | |
| Ferryy | 0:e3161ef4ffd9 | 40 | float pulsesperrevolution = 1024*4; |
| Ferryy | 0:e3161ef4ffd9 | 41 | //keep track of a possible overflow |
| Ferryy | 0:e3161ef4ffd9 | 42 | if((float)lastvalue - (float)currentvalue > 30000) { //with this huge value we must have an overflow |
| Ferryy | 0:e3161ef4ffd9 | 43 | //pc.printf("Overflow happened, curval: %d lastval: %d, overflowcount: %d\r\n", currentvalue, lastvalue, overflowcount); |
| Ferryy | 0:e3161ef4ffd9 | 44 | overflowcount++; |
| Ferryy | 0:e3161ef4ffd9 | 45 | } |
| Ferryy | 0:e3161ef4ffd9 | 46 | if((float)currentvalue - (float)lastvalue > 30000) { //underflow |
| Ferryy | 0:e3161ef4ffd9 | 47 | //pc.printf("Underflow happened, curval: %d lastval: %d, overflowcount: %d\r\n", currentvalue, lastvalue, overflowcount); |
| Ferryy | 0:e3161ef4ffd9 | 48 | overflowcount--; |
| Ferryy | 0:e3161ef4ffd9 | 49 | } |
| Ferryy | 0:e3161ef4ffd9 | 50 | float pulsevalue = (float) currentvalue + (float) overflowcount * 0x10000; |
| Ferryy | 0:e3161ef4ffd9 | 51 | float fullrotations = (currentvalue/pulsesperrevolution) + overflowcount * 0x10000/pulsesperrevolution; |
| Ferryy | 0:e3161ef4ffd9 | 52 | |
| Ferryy | 0:e3161ef4ffd9 | 53 | char inchar = ' '; |
| Ferryy | 0:e3161ef4ffd9 | 54 | scanf("%c",&inchar); |
| Ferryy | 0:e3161ef4ffd9 | 55 | if(inchar == 'i' || inchar == 'I') { |
| Ferryy | 0:e3161ef4ffd9 | 56 | pc.printf("Rotary\r\n"); |
| Ferryy | 0:e3161ef4ffd9 | 57 | } else if(inchar == 'm' || inchar == 'M') { |
| Ferryy | 0:e3161ef4ffd9 | 58 | pc.printf("%f\r\n", fullrotations); |
| Ferryy | 0:e3161ef4ffd9 | 59 | //pc.printf("%d, %f%s \r\n", overflowcount, fullrotations, dir==0 ? "+":"-"); |
| Ferryy | 0:e3161ef4ffd9 | 60 | //pc.printf("%d: %f%s \r\n", i++, (currentvalue/pulsesperrevolution), dir2==0 ? "+":"-"); |
| Ferryy | 0:e3161ef4ffd9 | 61 | } else if(inchar == 'p' || inchar == 'P') { |
| Ferryy | 0:e3161ef4ffd9 | 62 | pc.printf("%f\r\n", pulsevalue); |
| Ferryy | 0:e3161ef4ffd9 | 63 | } |
| Ferryy | 0:e3161ef4ffd9 | 64 | lastvalue = currentvalue; |
| Ferryy | 0:e3161ef4ffd9 | 65 | } |
| Ferryy | 0:e3161ef4ffd9 | 66 | } |
| Ferryy | 0:e3161ef4ffd9 | 67 |