kwjy

持续进军之五一节日-----独立按键触发状态机LED显示

0
阅读(2050)

先上代码,不多解释,按键状态机耽误了我很多时间啊!!!o^^o

请高手明示


/*
网名:坎悟积验
功能:独立按键状态机检测
现象:简单的单个循环与连续循环
Fosc:11059200Hz
MCU :STC89C52RC
Day :2014-05-04
*/
#include <reg52.h>
typedef unsigned char Uint8;
typedef unsigned int Uint16;
Uint8 code tab[] = {0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
#define FSM_Initial    0
#define FSM_Confirm    1
#define FSM_Single     2
#define FSM_Continuous 3
//#define FSM_Reverse    4
sbit Key_IN = P3^4;
void Time0_10ms();
void main()
{
P2 = 0xff;
TMOD = 0x01;
TH0 = 0xdc;
TL0 = 0;
EA = 1;
ET0 = 1;
TR0 = 1;
while(1)
{
Time0_10ms(); 
}
}
Uint8 KeyScan()
{
Uint8 state_KeyIn;
static Uint8 state_keyscan = 0;
static Uint8 Conter_Num = 0;
static Uint8 Temp_Num = 0;
state_KeyIn = Key_IN;
switch(state_keyscan)
{
case FSM_Initial: if(!state_KeyIn)
  state_keyscan = FSM_Confirm;
     break;
case FSM_Confirm: if(!state_KeyIn)
  state_keyscan = FSM_Single;
 else 
  state_keyscan = FSM_Initial;
     break;
case FSM_Single:
if(++Conter_Num > 100)
{
Conter_Num = 0; 
state_keyscan = FSM_Continuous;  
}
//  if(++Conter_Num > 200)
//     {  
// Conter_Num = 0; 
// state_keyscan = FSM_Reverse;
//     }
 
else
{
  if(state_KeyIn)
{
state_keyscan = FSM_Initial;
    if(++Temp_Num == 9)Temp_Num = 0;


     break;
case FSM_Continuous: if(++Conter_Num == 10)
{
Conter_Num = 0;
if(++Temp_Num == 9)Temp_Num = 0;
}
else
if(state_KeyIn)
state_keyscan = FSM_Initial; 
     break;
// case FSM_Reverse:   if(++Conter_Num == 10)
// {
// Conter_Num = 0;
// if(--Temp_Num == 0)Temp_Num = 9;
// }
// else 
// if(state_KeyIn)
//
// state_keyscan = FSM_Initial;
// else
// {
// if(state_KeyIn)
// {
// state_keyscan = FSM_Initial;
//     if(--Temp_Num == 0)Temp_Num = 9;
// } 
// }  
//  break;
default: state_keyscan = FSM_Initial;
 break;
}
return Temp_Num;
}
void Time0_10ms()
{
if(TF0)
{
TF0 = 0;
TH0 = 0xdc;
   TL0 = 0;
P2 = tab[KeyScan()];
}
}


实验现象展示。。。。。。。。。

单次触发

连续触发


下一步:矩阵键盘状态机