蓝桥杯单片机独立按键(状态机)

完成任务
1、按键S7,在数码管上显示5201314
2、按键S6,LED灯完成一系列操作。
3、按键S5,蜂鸣器开。
4、按键S4,蜂鸣器关。

#include"stc12c5a60s2.h"
#include"intrins.h"

#define Y4 P2=(P2&0x1f)|0x80
#define Y5 P2=(P2&0x1f)|0xa0
#define Y6 P2=(P2&0x1f)|0xc0
#define Y7 P2=(P2&0x1f)|0xe0
#define Y0 P2=(P2&0x1f)|0x00

void SMG_set();
void SMG_diaplay(unsigned char d0,d1,d2,d3,d4,d5,d6,d7);
void Timer0Init();
unsigned char key_read();
void buzzeropen();
void buzzerclose();

unsigned char key_flag;
unsigned char key_value;
unsigned char key_return;

//延时函数
//***************************************************************************
//***************************************************************************
void Delay1ms(unsigned int n)  //@11.0592MHz
{
 unsigned char i,j;
  while(n--)
 {
 _nop_(); 
 _nop_();
 _nop_();
 i = 11;
 j = 190;
 do
 {
  while (--j);
 } while (--i);
}
}

//蜂鸣器
//***************************************************************************
//***************************************************************************
void buzzerclose()
{
Y0;
Y5;
P0=0X00;
}
void buzzeropen()
{
Y0;
Y5;
P0=0XFF;
}

//LED灯
//***************************************************************************
//***************************************************************************
void LED()
{
 unsigned char code LED_change[]={~0x81,~0x42,~0x24,~0x18,0x81,0x42,0x24,0x18};
 unsigned char i;
 for(i=0;i<9;i++)
 {
 Y4;
 P0=~(0X01<<i);
  Delay1ms(500);
 }
 for(i=0;i<8;i++)
 {
  Y4;
 P0=LED_change[i];
 Delay1ms(500);
 }
 Y4;
  P0=0XFF;
}

//数码管
//***************************************************************************
//***************************************************************************
unsigned char code SMG_duan[]={~0x3F,~0x06,~0x5B,~0x4F,~0x66,~0x6D,~0x7D,~0x07,~0x7F,~0x6F,0xff};
unsigned char code SMG_wei[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
unsigned char SMG_init[8]={0,0,0,0,0,0,0,0};`
void SMG_set()
{
  static unsigned char num=0;
 Y7;
 P0=0X00;
 Y6;
 P0=SMG_wei[num];
 Y7;
 P0=SMG_duan[SMG_init[num]];
 num++;
 if(num==8)
  num=0;
}

void SMG_display(unsigned char d0,d1,d2,d3,d4,d5,d6,d7)
{
  SMG_init[0]=d0;
 SMG_init[1]=d1;
 SMG_init[2]=d2;
 SMG_init[3]=d3;
 SMG_init[4]=d4;
 SMG_init[5]=d5;
 SMG_init[6]=d6;
 SMG_init[7]=d7;
} 

//定时器
//***************************************************************************
//***************************************************************************
void Timer0Init()  //1毫秒@11.0592MHz
{
 AUXR |= 0x80;  //定时器时钟1T模式
 TMOD &= 0xF0;  //设置定时器模式
 TL0 = 0xCD;  //设置定时初值
 TH0 = 0xD4;  //设置定时初值
 TF0 = 0;  //清除TF0标志
 TR0 = 1;  //定时器0开始计时
 EA=1;
 ET0=1;
}
static unsigned char count=0;
void Timer0() interrupt 1
{
  SMG_set();
 count++;
 if(count==10)
 {
 count=0;
  key_flag=1;
 }
}

//独立按键状态机
//***************************************************************************
//***************************************************************************
#define key_state_0 0
#define key_state_1 1
#define key_state_2 2
unsigned char key_read()
{
  static unsigned char key_state=0;
 unsigned char key_press,key_value=0;
 key_press=P3&0X0f;
 switch(key_state)
 {
case key_state_0: if(key_press!=0x0f) key_state=1; break;
case key_state_1: 
   if(key_press!=0x0f) 
   {
      if(key_press==0x0e) key_value=7;/key_press而不是P3
      if(key_press==0x0d) key_value=6;
      if(key_press==0x0b) key_value=5;
      if(key_press==0x07) key_value=4;
              key_state=2;
   }
   else 
    key_state=0;
    break;
case key_state_2: 
     if(key_press==0x0f)
    key_state=0;
   break;
}
  return key_value;
}

//主函数
//***************************************************************************
//***************************************************************************
void main()
 {
 Timer0Init();
while(1)
{
 if(key_flag==1)
 {
  key_flag=0;
  key_value=key_read();
  switch(key_value)
  {
   case 4:  buzzerclose();  break;
   case 5:  buzzeropen();    break;
   case 6:  LED();          break;
   case 7:  SMG_display(5,2,0,1,3,1,4,10);  break;
  }
 }
}
}
  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_44828988

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值