First, the principle of the encoder
A and B correspond to two photosensitive receiving tubes. The distance S2 between AB is smaller than S0 and S1. Therefore, when rotating clockwise, the initial values of A and B are 1, 1. A will first enter the S0 area on the left side of the initial position, that is, the values of A and B are 0, 1. Then AB are in the S0 area at the same time, that is, 0, 0. Then A enters the S1 area first, and B remains in the S0 area. At this time, the values are 1, 0. This cycle represents clockwise, and the corresponding counterclockwise sequence can also be obtained. In summary:
The direction of the knob can be determined by judging the value of the previous moment and the value of the next moment. The drive is roughly as follows, which has not been verified:
#include"xx.h"//extern PWM;
#define PIN_A GPIO_X
#define PIN_B GPIO_Y
unsigned char flag = 0; // indicates the knob status
unsigned char Flag = 0; // indicates forward or reverse rotation
void Encoder_Scan()
{
if(PIN_A == PIN_B)
{
if(Flag == 1)
{
if(PWM<100)
PWM++;
else
{
PWM = 0;
}
}
else if (Flag == 2)
{
if(PWM>0)
PWM--;
else
{
PWM = 100;
}
}
else
{
/* code */
}
if (PIN_A == 0)
{ //Clockwise Counterclockwise
flag = 1; //A == 0 and B == 0 //A B A B
} //1 1 1 1
else //0 1 1 0
{ //0 0 0 0
flag = 2; //A == 1 and B == 1 //1 0 0 1
}
}
else if (PIN_A == 1 && PIN_B == 0)
{
if(flag = 1) // The last time A == 0 and B == 0
{
Flag = 1; // Clockwise
}
else // that is, the last time A == 1 and B == 1
{
Flag = 2; //Counterclockwise
}
}
else //(PIN_A == 0 && PIN_B == 1)
{
if(flag = 1) // The last time A == 0 and B == 0
{
Flag = 2; //Counterclockwise
}
else
{
Flag = 1; // Clockwise
}
}
}
Here we take MC9S12G64 as an example, connect the encoder's photosensitive pin to the GPIO (input) of the main chip, and control the PWM duty cycle by judging the value of GPIO.
The encoder's scanning function can be called in the chip's real-time interrupt function RTI timeout interrupt, and then the PWM duty cycle is sent in the main function. For specific PWM driver information, see the chip manual.
Previous article:Freescale MC9S12X CAN driver
Next article:Freescale MC9S12X: Application of CAN driver function in main function
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- EEWORLD University ---- Intel SoC FPGA Development and Application Training Course by Xiaomei
- [GD32E231 DIY Contest] 04. The difficult assembly process of the robotic arm
- A little new
- Introducing the multi-device C2000 programmer C2000-GANG
- Indoor positioning summary
- In a DC-DC circuit, if the current is less than 100uA after the output voltage, will it still pull down the input voltage? How to avoid this...
- [Shanghai Hangxin ACM32F070 development board + touch function evaluation board] 04.CAN communication test
- Professional questions and answers about laser distance sensors
- The Linux driver LCD screen is distorted or not bright at all. Where should I start debugging?
- Traditional silicon components, silicon carbide (SiC) and gallium nitride (GaN)