- #ifndef _KEY_H_
- #define _KEY_H_
- //---包含头文件---//
- #include "Config.h"
- #define KEY_TIME_SHORT 20 //短按的按键去抖动延时的时间
- //---加入全局变量--//
- extern uchar KeyCode;
- extern uint KeyTimeCnt[5]; //按键去抖动延时计数器,分别对应KEY1/2/3/4/5
- extern uchar KeyLock[5]; //按键触发后自锁的变量标志,分别对应KEY1/2/3/4/5
- //-----------------------------------------------------------------
- // 相关函数声明
- //-----------------------------------------------------------------
- void KeyScan(); //按键扫描函数 放在定时中断里
- void Delay10ms(); //延时函数
-
- #endif
- #include "Key.h"
- uchar KeyCode = 0; //按键编码值
- //共5个独立机械按键,通过短按实现5种按键编码值
- uint KeyTimeCnt[5] = {0,0,0,0,0}; //按键去抖动延时计数器,分别对应KEY1/2/3/4/5
- uchar KeyLock[5] = {0,0,0,0,0}; //按键触发后自锁的变量标志,分别对应KEY1/2/3/4/5
- //-----------------------------------------------------------------
- //函数名称: void KeyScan()
- //函数功能: 按键扫描程序,放在定时中断里,可较快实现按键响应
- //调用子函数: 无
- //输入参数: 无
- //返回值: 无,说明:返回的按键编码值保存在KeyCode变量里
- //-----------------------------------------------------------------
- void KeyScan()
- {
- //------KEY1实现-------------------------------------------------
- if(KEY1 == 1) //IO是高电平,说明按键没有被按下,这时要及时清零一些标志位
- { KeyLock[0] = 0; //按键自锁变量标志清零
- KeyTimeCnt[0] = 0; //按键去抖动延时计数器清零
- }
- else if(KeyLock[0] == 0)
- {
- KeyTimeCnt[0]++;
- if(KeyTimeCnt[0] > KEY_TIME_SHORT)
- {
- KeyTimeCnt[0] = 0;
- KeyLock[0] = 1; //自锁按键置位,避免一直触发
- KeyCode = 1;
- }
- }
-
- //------KEY2实现-----------------------------------
- if(KEY2 == 1)
- { KeyLock[1] = 0;
- KeyTimeCnt[1] = 0;
- }
- else if(KeyLock[1] == 0)
- {
- KeyTimeCnt[1]++;
- if(KeyTimeCnt[1] > KEY_TIME_SHORT) //消抖
- {
- KeyTimeCnt[1] = 0; //按键去抖动延时计数器清零
- KeyLock[1] = 1; //自锁按键置位,避免一直触发
- KeyCode = 2;
- }
- }
- //------KEY3实现-----------------------------------
- if(KEY3 == 1)
- { KeyLock[2] = 0;
- KeyTimeCnt[2] = 0;
- }
- else if(KeyLock[2] == 0)
- {
- KeyTimeCnt[2]++;
- if(KeyTimeCnt[2] > KEY_TIME_SHORT)
- {
- KeyTimeCnt[2] = 0;
- KeyLock[2] = 1;
- KeyCode = 3;
- }
- }
- //------KEY4实现-----------------------------------
- if(KEY4 == 1)
- { KeyLock[3] = 0;
- KeyTimeCnt[3] = 0;
- }
- else if(KeyLock[3] == 0)
- {
- KeyTimeCnt[3]++;
- if(KeyTimeCnt[3] > KEY_TIME_SHORT)
- {
- KeyTimeCnt[3] = 0;
- KeyLock[3] = 1;
- KeyCode = 4;
- }
- }
-
- //------KEY5实现-----------------------------------
- if(KEY5 == 1)
- { KeyLock[4] = 0;
- KeyTimeCnt[4] = 0;
- }
- else if(KeyLock[4] == 0)
- {
- KeyTimeCnt[4]++;
- if(KeyTimeCnt[4] > KEY_TIME_SHORT)
- {
- KeyTimeCnt[4] = 0;
- KeyLock[4] = 1;
- KeyCode = 5;
- }
- }
- }
- #include "Key.h"
- //0~9 的共阳数码管段码,最后一个是黑屏
- const uchar SEG_CODE[] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xFF};
- //输出励磁序列的频率参数{TH1,TL1}
- const uchar Timer[9][2]={0xDE,0xE4};//,{0xE1,0xEC},{0xE5,0xD4},{0xE9,0xBC}, //8.476 ~ 1ms
- // {0xEd,0xA4},{0xF1,0x8C},{0xF5,0x74},{0xF9,0x5C},{0xFC,0x18}};
- //步进电机正转的励磁序列
- const uchar FFW[] = {0x1F,0x3F,0x2F,0x6F,0x4F,0xCF,0x8F,0x9F}; //DCBAXXXX
- //步进电机反转的励磁序列
- const uchar REV[] = {0x9F,0x8F,0xCF,0x4F,0x6F,0x2F,0x3F,0x1F}; //DCBAXXXX
- //枚举变量--正反转标志
- typedef enum
- {FwdRun, RevRun} RunFlag;
- RunFlag flag1 = FwdRun;
- uchar speed = 0;
- //函数声明
- void T0_Init(); //定时器0初始化函数声明
- void T1_Init(); //定时器1初始化函数声明
- void KeyService(); //按键服务函数声明
- void delay_ms(uint t); //毫秒延时函数声明
-
- //-----------------------------------------------------------------
- // 主程序
- //-----------------------------------------------------------------
- void main()
- {
- T0_Init(); //定时器0初始化
- T1_Init(); //定时器1初始化
- FWD_LED = 0; //正转指示灯亮,默认初始化是正转
-
- delay_ms(500); //延时500ms,待各个模块上电稳定
- EA = 1; //开启中断
- while(1)
- {
- KeyService(); //按键服务程序
- }
- }
- //-----------------------------------------------------------------
- // Timer0 初始化,模式1,允许中断,1ms定时
- // Timer0 用于按键扫描
- //-----------------------------------------------------------------
- void T0_Init()
- {
- TMOD &= 0xF0;
- TMOD |= 0x01; //T0工作于模式1,16位定时器
- TH0 = (0xFA)/2;//(65536 - 1000) >> 8; //1ms定时
- TL0 = (0xFA)/2;//(65536 - 1000) & 0xFF;
- ET0 = 1; //允许T0中断
- TR0 = 1; //启动T0
- }
- //-----------------------------------------------------------------
- // Timer1 初始化,模式1,允许中断
- // Timer1 用于改变步进电机转速
- //-----------------------------------------------------------------
- void T1_Init()
- {
- TMOD &= 0x0F;
- TMOD |= 0x10; //定时器1工作于模式1,16位定时器
- TH1 = Timer[speed][0];
- TL1 = Timer[speed][1]; //定时器1,定时用于步进电机转速控制
- ET1 = 1; //允许T1中断
- PT1 = 1; //定时器1优先级高
- TR1 = 0; //关闭定时器1
- }
- //-----------------------------------------------------------------
- // Timer0 中断服务程序
- //-----------------------------------------------------------------
- void InterruptTime0() interrupt 1
- {
- TH0 = (65536 - 1000) >> 8; //1ms
- TL0 = (65536 - 1000) & 0xFF;
- KeyScan(); //按键扫描
- }
- //-----------------------------------------------------------------
- // Timer1 中断服务程序
- //-----------------------------------------------------------------
- void InterruptTime1() interrupt 3
- {
- static step1 = 0;
- static step2 = 0;
- TH1 = Timer[speed - 1][0]; //根据不同的转速档位,给定时器1赋定时器值
- TL1 = Timer[speed - 1][1];
-
- //按照定时器1的频率循环发送正转励磁序列的脉冲给步进电机,使其不停地转动
- if(flag1 == FwdRun)//正转
- {
- MotorDriver = FFW[step1++];
- if(step1 == 8) step1 = 0;
- }
-
- //按照定时器1的频率循环发送反转励磁序列的脉冲给步进电机,使其不停地转动
- else if(flag1 == RevRun)
- {
- MotorDriver = REV[step2++];
- if(step2 == 8) step2 = 0;
- }
- }
- //-----------------------------------------------------------------
- // 按键服务程序
- //-----------------------------------------------------------------
- void KeyService()
- {
- switch (KeyCode)
- {
- case 1: flag1 = FwdRun; //正转
- FWD_LED = 0; REV_LED = 1; //正转指示灯亮
- KeyCode = 0;
- break;
- delay_ms(500);
- case 2: flag1 = RevRun; //反转
- FWD_LED = 1; REV_LED = 0; //反转指示灯亮
- KeyCode = 0;
- break;
- delay_ms(500);
- case 3: speed++; //速度加
- if(speed == 3) speed = 1;
- SpeedShow = SEG_CODE[speed];
- if(TR1 == 0) TR1 = 1;
- KeyCode = 0;
- break;
- delay_ms(500);
- // case 4: speed--; //速度减
- // if(speed == 0 || speed == -1 ) speed = 2;
- // SpeedShow = SEG_CODE[speed];
- // if(TR1 == 0) TR1 = 1;
- // KeyCode = 0;
- // break;
- case 5: TR1 = 0; //停止
- SpeedShow = 0xFF;
- MotorDriver = 0x00;
- KeyCode = 0;
- break;
- delay_ms(500);
- default: ;
- }
- }
- //-----------------------------------------------------------------
- //函数名称: void delay_ms(uint t)
- //函数功能: 延时ms程序(粗略)
- //调用子函数: 无
- //输入参数: t
- //返回值: 无
- //-----------------------------------------------------------------
- void delay_ms(uint t)
- {
- uchar i; while(t--) for(i = 0; i < 125; i++);
- }
复制代码 |