石头

基于 残奕悟恩(刘平老师)的状态机按键扫描方法控制小灯程序2

0
阅读(2903)
/******************************************************
基于 残奕悟恩(刘平老师)的状态机按键扫描方法控制小灯程序2
实现功能:四个独立按键Key1、Key2、Key3、Key4、
              Key1按下,小灯开始流动,P1——P7;
              Key2按下,小灯停止流动。停止后不响应Key3、Key4.
              Key3按下,小灯流动方向:P1——P7
              Key4按下,小灯流动方向:P7——P1
硬件连线:Led小灯接P1口
               Key1接P2.0
               Key2接P2.1
               Key3接P2.2
               Key4接P2.3
晶振频率:11.0592Mhz

*******************************************************/

#include<STC12C5A60S2.H>
#include <intrins.h>   //循环左移右移函数必须

typedef unsigned char uint8;
typedef unsigned int  uint16;

#define Key_Initial 0 //按键初始状态
#define Key_Affirm  1    //按键确认状态
#define Key_Singel  2 //按键松手状态

#define Led  P1

uint16 Led_Time_Cnt = 0;

uint8 temp = 0xfe;

sbit Key1 = P2^0;
sbit Key2 = P2^1;
sbit Key3 = P2^2;
sbit Key4 = P2^3;

bit UpDown = 0; //定义Led小灯流动方向
bit StartEnd = 0; //定义小灯开始、停止状态

void initial_myself();
void Delayms(uint16 z);
void initial_peripheral();
char Key_Scan();
void Key_Sever();

void main()
{
    initial_myself();
    Delayms(10);
    initial_peripheral();
    Led = temp;
    while(1)
    {
        Key_Sever();
    }
}

char Key_Scan()  //按键扫描函数
{
    static uint8 Key_Step = 0;
    uint16 Key_Value;        
    Key_Value = (Key1&Key2&Key3&Key4);

    switch (Key_Step)
    {
        case Key_Initial:

        if(Key_Value != 1)
        {
            Key_Step = Key_Affirm;
        }                
        break;
  
        case Key_Affirm:
        if(Key_Value != 1)
        {
            if(Key1 == 0)
            {
                StartEnd = 1;
            }
            if(Key2 == 0)  
            {
                StartEnd = 0;
            }
             if(Key3 == 0)  
             {
                UpDown = 0;
             }
            if(Key4 == 0)
            {
                 UpDown = 1;
            }
            Key_Step = Key_Singel;
        } 
         else
        {
            Key_Step = Key_Initial;    
        }                
        break;

        case Key_Singel:
        if(Key_Value == 1)
        {
            Key_Step = Key_Initial;
        }
        break;    
    } 
    return 0 ;   
}


void Key_Sever()   //按键服务函数
{
    if(1 == StartEnd)
    {
        if(1 == UpDown)
        {
            if(Led_Time_Cnt > 50)
           {
                Led_Time_Cnt = 0;
                Led = _cror_(Led,1);
           }   
        }
            else 
            {
                if(Led_Time_Cnt > 50)
                {
                     Led_Time_Cnt = 0;
                     Led = _crol_(Led,1);
                 }
           }
    }
}

void initial_myself()  //上电初始化
{
    Led = 0xff;
    Key1 = 1;
    Key2 = 1;
    Key3 = 1;
    Key4 = 1;

    TMOD |= 0x01;
    TH0 = 0xdc;
    TL0 = 0x00;
}

void Delayms(uint16 z)
{
    uint16 x,y;
    for(x = 0 ; x < z; x++)
         for(y = 0 ; y < 100 ; y++);
}

void initial_peripheral() //外围设备初始化
{
    EA = 1;
    ET0 = 1;
    TR0 = 1;
}

void interruptT0() interrupt 1
{
    TF0 = 0;
    TH0 = 0xdc;
    TL0 = 0x00;
    Key_Scan();
if(1 == StartEnd)
    {
        Led_Time_Cnt++;
    }
}