Author Topic: rotary encoder in c (stm32)  (Read 1762 times)

0 Members and 1 Guest are viewing this topic.

Offline m0p

  • Newbie
  • Posts: 1
  • Country: se
rotary encoder in c (stm32)
« on: July 03, 2017, 07:51:26 am »
Hi!
Im trying to get a rotary encoder working but its just to advanced for me...
This is the code i tried to write but its not the right way to do it...
And i just dont know how to think...
Can someone help me in the right direction here?  :)

Code: [Select]
          int rot = (0x0000);
 
while(1)
{

       if (!GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1))
       {
       rot = 1;
       rot<<=1;

       }

       if (!GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_2))
       {
              rot = rot + 1;
              rot<<=1;
       }

       if (!GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1))
               {
              rot = rot + 1;
              rot<<=1;

               }

            if (!GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_2))
               {
                    rot = rot + 1;
                    rot<<=1;
               }

           if (rot == 1 || rot == 7 || rot == 8 || rot == 14)
           {
           a[0] = a[0] + 34360;
           }

           if (rot == 2 || rot == 7 || rot == 8 || rot == 14)
                      {
                       a[0] = a[0] - 34360;
                      }
           
           rot = 0;
}
           
       
 

Offline voltsandjolts

  • Supporter
  • ****
  • Posts: 768
  • Country: gb
Re: rotary encoder in c (stm32)
« Reply #1 on: July 03, 2017, 07:55:46 am »
Welcome to the forum.

This topic has been discussed quite a lot. You can use the google search term "rotary encoder site:eevblog.com" if you don't like the forum built-in search.

Example:
https://www.eevblog.com/forum/projects/easy-rotary-encoder-implementation-good-easy-to-implement-code/
 

Offline MarkF

  • Super Contributor
  • ***
  • Posts: 1477
  • Country: us
Re: rotary encoder in c (stm32)
« Reply #2 on: July 03, 2017, 09:13:58 am »
I used the attached application note for PEC11 encoders with their suggested pull-up and filter shown in the datasheet. The assembly code in the app note can be accomplished with just a few lines of C code.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf