【蓝桥杯-单片机设计与开发】4.模板

希望不要鸽,继续更新(手动滑稽)
#include "stc15f2k60s2.h"


sbit S7=P3^0;
sbit S6=P3^1;
sbit S5=P3^2;
sbit S4=P3^3;


unsigned char POS=0;


unsigned char smgdat[8]={21,21,21,21,21,21,21,21};
unsigned char code SMG[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
			   0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,
			   0xbf,0xff};


void init0(void);		//上电初始化
void Timer2Init(void);	//定时器2初始化
void Key_scan(void);	//独立按键










void main(void)
{
    //初始化部分
    init0();
    Timer2Init();
    while(1)
    {
     	//用户执行任务   
        
    }
}

void hc138(unsigned char channel)
{
    switch(channel)
    {
        case 4:P2=(P2&0x1f)|0x80;break;
        case 5:P2=(P2&0x1f)|0xa0;break;
        case 6:P2=(P2&0x1f)|0xc0;break;
        case 7:P2=(P2&0x1f)|0xe0;break;
    }
    
}
void init0(void)
{
    hc138(4);P0=0xff;//关闭LED
    hc138(5);P0=0x00;//关闭蜂鸣器继电器
    hc138(6);P0=0x00;//关闭数码管
    hc138(7);P0=0xff;
    
}
void Timer2Init(void)		//2毫秒@12.000MHz
{
	AUXR |= 0x04;		//定时器时钟1T模式
	T2L = 0x40;		//设置定时初值
	T2H = 0xA2;		//设置定时初值
	AUXR |= 0x10;		//定时器2开始计时

	/*后加的部分*/
	EA=1;
	IE2|=0x04;
}

void display(unsigned char pos,dat){

	hc138(6);P0=0x01<<pos;P2&=0x1f;
	hc138(7);P0=dat; P2&=0x1f;
}


void T2_Display()interrupt 12
{
	hc138(6);P0=0x00;		//清除断码
	display(POS,SMG[smgdat[POS]]);
	if(++POS==8)POS=0;
	
}
void Delay5ms()		//@12.000MHz
{
	unsigned char i, j;

	i = 59;
	j = 90;
	do
	{
		while (--j);
	} while (--i);
}
void Key_scan(void)
{
    if(S4==0)
    {
        Delay5ms();//消抖
        if(S4==0)
        {
            //用户执行操作
        }
        while(!S4);//松手检测
    }
    else if(S5==0)
    {
        Delay5ms();
        if(S5==0)
        {
            
        }
        while(!S5);
    }
    else if(S6==0)
    {
        Delay5ms();
        if(S6==0)
        {
            
        }
        while(!S6);
    }
    else if(S7==0)
    {
        Delay5ms();
        if(S7==0)
        {
            
        }
        while(!S7);
    }
}
posted @ 2022-01-04 11:46  寥若辰星  阅读(85)  评论(0编辑  收藏  举报