蓝桥杯数码管和按键的应用

主函数:

/*Í·ÎļþÉùÃ÷ÇøÓò*/
#include <STC15F2K60S2.H>
#include <Init.h>
#include <Seg.h>
sbit s7=P3^0;
sbit s6=P3^1;
sbit s5=P3^2;
sbit s4=P3^3;


/*±äÁ¿ÉùÃ÷ÇøÓò*/
unsigned char Key_Slow_Down;
unsigned int Seg_Slow_Down;//

unsigned char Key_Val, Key_Down, Key_Old, Key_Up;			 // 
unsigned char Seg_Buf[8] = {10, 10, 10, 10, 10, 10, 10, 10}; // 
unsigned char Seg_Point[8] = {0, 0, 0, 0, 0, 0, 0, 0};		 // 
unsigned char Seg_Pos;
unsigned int sec=0; //
unsigned int cnt=9999; 
unsigned int sec1;
unsigned int cnt1;
/*º¯ÊýÉùÃ÷ÇøÓò*/
void Key_Proc();
void Seg_Proc();



void Delay15ms(void)	
{
	unsigned char data i, j;

	i = 176;
	j = 21;
	do
	{
		while (--j);
	} while (--i);
}





void Key_Proc()
{
	
	sec1=sec;
	cnt1=cnt;
		if(s4==0)
	{	
         Delay15ms();
         while(s4==0);			
		  sec++;
		if(++sec1 == 9999) 
		{
			sec=0;
		}
	}
	 if(s5==0)
	 {	 
		 Delay15ms();
         while(s5==0);		 
		 cnt--;
		if(--cnt1 == 65535) 
		
			cnt1=9999;
		 
	 }	
		
	if(s6==0)
	{
		Delay15ms();
         while(s6==0);	
		sec=0;
	}	
	
    if(s7==0)
	{	
         Delay15ms();
         while(s7==0);			
		  sec1=9999;
	}	

}



void Seg_Proc()
{
		
	if (Seg_Slow_Down) return;
	Seg_Slow_Down = 1;

	
	Seg_Buf[7] = cnt1 %10;                  
    Seg_Buf[6] = cnt1 /10%10;               
    Seg_Buf[5] = cnt1 /100%10;              
    Seg_Buf[4] = cnt1 /1000%10;             
    Seg_Buf[3] = sec1 %10;       
    Seg_Buf[2] = sec1 /10%10;    
	Seg_Buf[1] = sec1 /100%10;   
	Seg_Buf[0] = sec1 /1000%10;  
	 
	
}

 /* ¶¨Ê±Æ÷0µÄ³õʼ»¯º¯Êý */
void Timer0Init(void)		
{
	AUXR &= 0x7F;	
	TMOD &= 0xF0;   
	TL0 = 0x18;		
	TH0 = 0xFC;	
	TF0 = 0;		
	TR0 = 1;		
	ET0 = 1;        
	EA = 1;        
}

/*ÖжϷþÎñº¯Êý*/
void Timer0Server() interrupt 1
{  

	
	if(++Seg_Slow_Down ==500) Seg_Slow_Down = 0;//  
	if(++Seg_Pos == 8) Seg_Pos = 0;//ÊýÂë¹ÜÏÔʾרÓÃ
	Seg_Disp(Seg_Pos,Seg_Buf[Seg_Pos],Seg_Point[Seg_Pos]); 
	
}


 void main()
 {
	 
     Sys_Init(); 
     Timer0Init();
     
   
    while (1)
	{	
      Key_Proc();		
	  Seg_Proc(); 
	}
 }
 

数码管的底层驱动:

#include <Seg.h>

unsigned char Seg_Dula[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xc6,0x8c,0xbf};//0-9 10-熄灭
unsigned char Seg_Wela[] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};

void Seg_Disp(unsigned char wela,dula,point)
{
	P0 = 0xff;//消影
	P2 = P2 & 0x1f | 0xe0;
	P2 &= 0x1f;
	P0 = Seg_Wela[wela];
	P2 = P2 & 0x1f | 0xc0;
	P2 &= 0x1f;	
	if(point)
		P0 = Seg_Dula[dula] & 0x7f;
	else
		P0 = Seg_Dula[dula];
	P2 = P2 & 0x1f | 0xe0;
	P2 &= 0x1f;	
}

蓝桥杯初始化的底层:

#include <Init.h>

void Sys_Init()//关闭外设子函数
{
	P0=0xFF;//LED全灭
	P2=P2&0x1f|0x80; //选中LED通道
	P2&=0x1F;  //关闭LED通道
	
	
	P0=0x00;//关闭继电器和蜂鸣器
	P2=P2&0x1f|0xA0; //选中外设通道
	P2&=0x1F; //关闭通道
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值