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
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- Another technical solution for power-type plug-in hybrid: A brief discussion on Volvo T8 plug-in hybrid technology
- The driving voltage of LCD TV reaches more than 1000V. I want to know whether the LCD is connected in series with diodes? Instead of parallel connection...
- EEWORLD University - SimpleLink Wi-Fi CC3120 Project Introduction
- LMZ31710EVM test input voltage rebound research
- How to communicate between ARM and DSP (non-multi-core SOC)
- OC5501-OC5502-LED automotive high and low beam solutions (no diode design, applied to automotive lights, MR16, architectural lighting)
- Share: About the working principle of fast charging
- Xiaomi 22.5W 10000 mAh fast charging power bank disassembly
- I would like to ask you guys, do I need to draw PCB in electronic competitions?
- How to configure random generation of parameters (Get request with parameters) when using jmeter tool for interface stress testing?
- There are BUGs in smart meters. What do you think?