STM32 Learning Summary 4---Encoder knob type Incremental Encoder

Source: Internet
Author: User

This I really looked for a long time on the internet to find a usable program, most of you search the Encoder program comes out of the relevant program of the motor, and I want to be knob-type, pec11-4020f-s0018 model of the encoder.

This code is very well written, is not write the main function, so call the question, or the cost of a lot of God to get out, the key it inside some definitions, I have a long time without some procedures, are not clear, such as:

typedef struct TAGSPIN
{
Rotatestatus Rotatestatus; Rotation state
Keystatus Keystatus; Key state
}struspin;

I know struct is a struct, but typedef really forget, Baidu a bit, it is clear. In the main function, just

Struspin Left_spin;
Struspin Right_spin;

In the call function void Init_spin_status (Struspin *left_spin, Struspin *right_spin) and void Read_spin (Struspin *left_spin, Struspin * Right_spin), and then forget the *left_spin this is the pointer, so:

Init_spin_status (&left_spin, &right_spin); To add &

Read_spin (&left_spin, &right_spin);

Modify the relevant configuration, in the main function to add a few calls, you can run, but there are still a few questions:

1, the current implementation of the program is Rotary encoder two times only add 1 or minus 1;

2, pec11-4020f-s0018 Code Editor, a circle has 30 positioning, the physical self-turn out number. Data in 0018 = pulses per 360°rotation My understanding is a circle of 18 pulses, a lap with 30 positioning 18 pulses, that one should have a few pulses? Further study of the next procedure, how can be transferred two positioning plus 1 operation at a time;

3, I use the way of circulating scanning, the encoder has a button function, when only the scanning encoder does not display the digital tube, the key program is to light the light, but the actual is flashing no, but when I add the main program of digital tube display, key lights can always be lit;

4, in the main function while I added another four independent button scanning, keys and encoders can be normal operation, there is a problem is every time the key, the digital tube will flash, as if the button stopped a bit of digital tube display, it is obvious that the naked eye can be observed.

The main part of the program:

Initialize the state of the left and right rotary encoders
void Init_spin_status (Struspin *left_spin, Struspin *right_spin)
{
L eft_spin->rotatestatus = spin_no_rotate;
Left_spin->keystatus = key_no_pressed;
Right_spin->rotatestatus = spin_no_rotate;
Right_spin->keystatus = key_no_pressed;
}

////////////////////////////////////////////////////////////
Read the status of the 2 Rotary encoder
Static level last_leftspinkey_level= H, last_rightspinkey_level= H; The level of the last key saved
Static Spinabstatus last_leftspinabst=spin_ab_st3, Last_rightspinabst=spin_ab_st3; The level condition of the last AB Pin saved
void Read_spin (Struspin *left_spin, Struspin *right_spin)
{
Level now_leftspinkey_level, Now_rightspinkey_level;
Level now_leftspina_level, Now_leftspinb_level;
Level now_rightspina_level, Now_rightspinb_level;
Spinabstatus Now_leftspinabst, Now_rightspinabst;
Read the level values of each pin of the rotary encoder first
if (Read_spin_l_key ()) {now_leftspinkey_level = H;}//reads the key level value of the left-hand rotary Encoder
else {now_leftspinkey_level = L;}

if (Read_spin_r_key ()) {now_rightspinkey_level = H;}//Read the key level value of the right rotary encoder
else {now_rightspinkey_level = L;}

if (Read_spin_l_a ()) {now_leftspina_level = H;}//Read the A signal level value of the left Rotary encoder
else {now_leftspina_level = L;}

if (Read_spin_l_b ()) {now_leftspinb_level = H;}//Read the B signal level value of the left Rotary encoder
else {now_leftspinb_level = L;}

if (Read_spin_r_a ()) {now_rightspina_level = H;}//Read the A signal level value of the right rotary encoder
else {now_rightspina_level = L;}

if (Read_spin_r_b ()) {now_rightspinb_level = H;}//Read the B signal level value of the right rotary encoder
else {now_rightspinb_level = L;}

The key state is determined according to the level value two times before and after the key of the left Rotary encoder.
if ((Last_leftspinkey_level = = H) && (now_leftspinkey_level = = L)) {left_spin->keystatus = key_just_pressed; }
else if ((Last_leftspinkey_level = = h) && (Now_leftspinkey_level = = h)) {left_spin->keystatus = key_no_pressed ; }
else if ((Last_leftspinkey_level = = L) && (now_leftspinkey_level = = H)) {left_spin->keystatus = Key_just_popup ; }
else {left_spin->keystatus = key_already_pressed;}

The key state is determined according to the level value two times before and after the key of the right rotary encoder.
if ((Last_rightspinkey_level = = H) && (now_rightspinkey_level = = L)) {right_spin->keystatus = Key_just_presse D }
else if ((Last_rightspinkey_level = = h) && (Now_rightspinkey_level = = h)) {right_spin->keystatus = Key_no_pres SED; }
else if ((Last_rightspinkey_level = = L) && (now_rightspinkey_level = = H)) {right_spin->keystatus = Key_just_po PUP; }
else {right_spin->keystatus = key_already_pressed;}

Determine the AB status based on the level value of a B on the left-hand rotary Encoder
if ((Now_leftspina_level = = h) && (Now_leftspinb_level = = h)) {now_leftspinabst = SPIN_AB_ST3;}//A=1 b=1;
else if ((Now_leftspina_level = = H) && (now_leftspinb_level = = L)) {now_leftspinabst = Spin_ab_st2;}//A=1 b=0;
else if ((Now_leftspina_level = = L) && (now_leftspinb_level = = L)) {now_leftspinabst = spin_ab_st0;}//A=0 b=0;
else {now_leftspinabst = Spin_ab_st1;}//A=0 b=1;

if (Now_rightspina_level = = H) && (now_r Ightspinb_level = = H)) {now_rightspinabst = SPIN_AB_ST3;}//A=1 b=1;
Else if (now_rightspina_level = = H) && (now_rightspinb_level = = L)) {now_rightspinabst = Spin_ab_st2;} /A=1 b=0;
Else if (now_rightspina_level = = L) && (now_rightspinb_level = = L)) {now_rightspinabst = spin_ab_st0;} /A=0 b=0;
Else {now_rightspinabst = Spin_ab_st1;}//A=0 b=1;

//To determine whether the left or right-handed
if ((Last_leftspinabst = = spin_ab_st3) && (Now_leftspina) is based on the state of the AB signal line two times before and after rotating the encoder Bst = = Spin_ab_st2)) {left_spin->rotatestatus = spin_unticlockwise;}
Else if (Last_leftspinabst = = spin_ab_st3) && (Now_leftspinabst = = Spin_ab_st1)) {Left_spin->rotatestat US = spin_clockwise; }
Else {left_spin->rotatestatus = spin_no_rotate;}


Then, according to the state of the AB signal line two times before and after the right Rotary encoder, determine whether the left or right rotation
if ((last_rightspinabst== spin_ab_st3) && (now_rightspinabst== spin_ab_st2)) {right_spin->rotatestatus = Spin_unticlockwise;}
else if ((last_rightspinabst== spin_ab_st3) && (now_rightspinabst== spin_ab_st1)) {right_spin-> Rotatestatus = spin_clockwise; }
else {right_spin->rotatestatus = spin_no_rotate;}

Last_leftspinkey_level = Now_leftspinkey_level; Save the current state as the basis for the next state
Last_rightspinkey_level = Now_rightspinkey_level;
Last_leftspinabst = Now_leftspinabst;
Last_rightspinabst = Now_rightspinabst;


}

Right Rotary encoder, user's own code
void Process_right_spin (Struspin *rightspin)
{
if (Rightspin->keystatus = = key_just_pressed)//Rotary Encoder button Just press the handler, the user fills in
{
led5=! LED5;

}
else if (rightspin->rotatestatus = = spin_clockwise)//Rotary encoder Clockwise rotation of the handler, the user fills in
{
right_num++;
}
else if (rightspin->rotatestatus = = spin_unticlockwise)//Rotary Encoder counter-clockwise handler, the user fills in
{
right_num--;
}
}

//////////////////////////////////////////////////////
Rotary encoder on the left///////////////
#define Rcc_left_spin RCC_APB2PERIPH_GPIOC//left rotary encoder with Gpioe clock
#define Gpio_spin_l_port GPIOC//left side of the Rotary Encoder drive pin located on the port

#define Spin_l_key_pin gpio_pin_9//Left ROTARY Encoder key PIN number
#define Pinspinleftkey PIN9
Rotary Encoder A PIN number #define Spin_l_a_pin gpio_pin_7//Left
#define PINSPINLEFTA PIN7
Rotary Encoder B PIN number #define Spin_l_b_pin gpio_pin_8//Left
#define PINSPINLEFTB PIN8

#define READ_SPIN_L_KEY () pcin (Pinspinleftkey)//PC9
#define READ_SPIN_L_A () pcin (PINSPINLEFTA)//PC7 read in the A pin level
#define READ_SPIN_L_B () pcin (PINSPINLEFTB)//PC8 read in the level of the B pin

///////////////////////////////////////////////////

typedef enum
{
key_no_pressed = 0,
Key_just_pressed,
Key_already_pressed,
Key_just_popup,
}keystatus;

typedef enum
{
L = 0,//low level
H,//High level
}level;

typedef enum
{
Spin_no_rotate = 0,//no rotation
Spin_clockwise,//clockwise rotation
Spin_unticlockwise,//counter-clockwise rotation
}rotatestatus;

typedef struct TAGSPIN
{
Rotatestatus Rotatestatus; Rotation state
Keystatus Keystatus; Key state
}struspin;

typedef enum
{
Spin_ab_st0 = 0,//a=0 b=0;
Spin_ab_st1,//A=0 b=1;
Spin_ab_st2,//A=1 b=0;
SPIN_AB_ST3,//A=1 b=1;
}spinabstatus; Rotary Encoder Signal Line A-B level

Init_spin_status (&left_spin, &right_spin);
left_num=0;
right_num=0;
led5=0;
while (1)
{
Read_spin (&left_spin, &right_spin);
Process_left_spin (&left_spin);
Process_right_spin (&right_spin);
Display (0,left_num);
Display (1,right_num);
}

STM32 Learning Summary 4---Encoder knob type Incremental Encoder

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.