Rotary encoder with STM32F401CCU

What are you developing?
Post Reply
hitachii
Posts: 22
Joined: Sat Jan 29, 2022 8:59 pm

Rotary encoder with STM32F401CCU

Post by hitachii »

Does anybody have a recommendation for either a library, or direct code for using a rotary encoder with the STM32F401? I have tried several libraries with no success, including Encoder.h which would not compile and seems incompatible, RotaryEncoder which doesn't seem to work at all.

The example code in this tutorial includes reading the GPIO register of an Arduino: https://www.instructables.com/Improved- ... r-Reading/

Using the following code from the next linked page, I tried to do the same for the STM32 but I got a flag that PORTB has no member named "regs" (I get the same for PORTA).

Code: Select all

PORTA->regs->CRL = (PORTA->regs->CRL & 0x00F00F00) | 0x88000080 |0x00033003;
https://gist.github.com/iwalpola/6c36c9 ... 0a118571ca

So now I'm a little stuck. Any suggestions for libraries, code, or reading PORTB is welcome. Thank you.
ozcar
Posts: 143
Joined: Wed Apr 29, 2020 9:07 pm
Answers: 5

Re: Rotary encoder with STM32F401CCU

Post by ozcar »

hitachii wrote: Tue Apr 26, 2022 5:48 am ...
Using the following code from the next linked page, I tried to do the same for the STM32 but I got a flag that PORTB has no member named "regs" (I get the same for PORTA).

Code: Select all

PORTA->regs->CRL = (PORTA->regs->CRL & 0x00F00F00) | 0x88000080 |0x00033003;
https://gist.github.com/iwalpola/6c36c9 ... 0a118571ca

So now I'm a little stuck. Any suggestions for libraries, code, or reading PORTB is welcome. Thank you.
I think you have two problems there.

Firstly, I guess you are using the official ST core (good choice), while what you found there was was written for core derived from Libmaple (the "Roger" core and cousins). In other cores they did their own thing to define the processor registers. So, for example for the GPIO input data register you would need something like PORTA->IDR instead of GPIOA->regs->IDR as shown on the page you found.

Secondly, when you are working at that level, you are exposed to differences between the different STM32 processor families. As far as I know, STM32F4 processors don't have CRL/CRH registers for GPIO, but instead have other registers which hold the equivalent settings.
hitachii
Posts: 22
Joined: Sat Jan 29, 2022 8:59 pm

Re: Rotary encoder with STM32F401CCU

Post by hitachii »

ozcar wrote: Tue Apr 26, 2022 8:04 am something like PORTA->IDR
Worked like a charm :)

ozcar wrote: Tue Apr 26, 2022 8:04 am Secondly, when you are working at that level, you are exposed to differences between the different STM32 processor families. As far as I know, STM32F4 processors don't have CRL/CRH registers for GPIO, but instead have other registers which hold the equivalent settings.
Luckily for this application it only requires reading the register, but this is good to know for the future!
BennehBoy
Posts: 135
Joined: Sat Jan 04, 2020 2:38 am
Answers: 1

Re: Rotary encoder with STM32F401CCU

Post by BennehBoy »

Hi,

I'm using a slightly modified version of the ClickEncoder library.

You can find the modified library in the 'encoder' folder in this repo -> https://github.com/BennehBoy/LRDuinoTD5

You'll also find the setup code and event code dotted in the code of that sketch, up to you to dig out the relevant bits however....
ozcar
Posts: 143
Joined: Wed Apr 29, 2020 9:07 pm
Answers: 5

Re: Rotary encoder with STM32F401CCU

Post by ozcar »

hitachii wrote: Tue Apr 26, 2022 9:06 am
ozcar wrote: Tue Apr 26, 2022 8:04 am something like PORTA->IDR
Worked like a charm :)
Well, I have to say that is a bit amazing, because I see now I was a bit crazy when I wrote that. What I meant to write was to use GPIOA->IDR instead of PORTA->regs->IDR. Maybe you were smart enough to see through my botched cut/paste/edit!
hitachii wrote: Tue Apr 26, 2022 9:06 am
ozcar wrote: Tue Apr 26, 2022 8:04 am Secondly, when you are working at that level, you are exposed to differences between the different STM32 processor families. As far as I know, STM32F4 processors don't have CRL/CRH registers for GPIO, but instead have other registers which hold the equivalent settings.
Luckily for this application it only requires reading the register, but this is good to know for the future!
The code you showed originally was trying to set something in CRL, so did you get rid of that? You could probably have just used pinMode(...) instead.

But then if BennehBoy has something that works just go with that.
henreylouice69
Posts: 1
Joined: Wed Jun 08, 2022 6:48 pm

Re: Rotary encoder with STM32F401CCU

Post by henreylouice69 »

I guess you are using the official ST core good choice while what you found there was was written for core derived from Libmaple the "Roger" core and cousins.In other cores they did their own thing to define the processor registers.

Secondly, when you are working at that level, you are exposed to differences between the different STM32 processor families.
As far as I know, STM32F4 processors don't have CRL CRH registers for GPIO, but instead have other registers which hold the equivalent settings.
_Northstrix_
Posts: 13
Joined: Sun Sep 03, 2023 4:11 am

Re: Rotary encoder with STM32F401CCU

Post by _Northstrix_ »

Hi, I know It's a bit too late, but still, here's the library.
https://github.com/GyverLibs/EncButton
I've actually used it in two of my projects involving STM32F401CCU6.
Post Reply

Return to “Projects”