Using encoder (control) to output PWM based on MC9S12G

Publisher:小九分析仪Latest update time:2021-07-08 Source: eefocusKeywords:MC9S12G Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

First, the principle of the encoder

insert image description here

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:

insert image description here

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.

insert image description here

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.


Keywords:MC9S12G Reference address:Using encoder (control) to output PWM based on MC9S12G

Previous article:Freescale MC9S12X CAN driver
Next article:Freescale MC9S12X: Application of CAN driver function in main function

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号