蓝桥杯-单片机竞赛-数码管

一、原理图

蓝桥杯-单片机竞赛-数码管_第1张图片
蓝桥杯-单片机竞赛-数码管_第2张图片
由P25、P26、P27经过138译码器,控制D触发器的选通信号,选通不同的D触发器,从而达到控制不同的器件的目的。

二、代码实现

思路:通过定时器2定时1ms,每1ms刷新数码管显示。
1、数码管选中某一位

void LED_Bit(unsigned char k)
{
	P0 = k;
	P2 = 0xc0;
	_nop_();
	P2 = 0x00;
}

2、数码管段选

void LED_Segment(unsigned char k)
{
	P0 = ~k;
	P2 = 0xe0;
	_nop_();
	P2 = 0x00;
}

3、定时器中断初始化及中断服务函数
实现功能:在数码管上显示字符8,每秒钟右移1位。

//数码管段选和位选
unsigned char code smg_duan[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0x00,0x40,0x76};
unsigned char code smg_wei[8] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
unsigned char menu[8] = {8, 20, 20, 20, 20, 20, 20, 20};//用于数码管显示的数组

//定时器2初始化函数
void Timer2Init(void)		//1000微秒@11.0592MHz
{
	AUXR |= 0x04;		//定时器时钟1T模式
	T2L = 0xCD;		//设置定时初值
	T2H = 0xD4;		//设置定时初值
	AUXR |= 0x10;		//定时器2开始计时
	IE2 |= 0x04;                    //开定时器2中断
}
//中断服务程序
void t2int() interrupt 12           //中断入口
{
	static unsigned char i;
	static unsigned char bit_cnt;
	LED_Segment(0x00);
	LED_Bit(smg_wei[i]);
	LED_Segment(smg_duan[menu[i]]);
	if(++i == 8)
		i = 0;
	
	if(timer2_cnt % 20 == 0)
	{
		key_flag = 1;
		KeyScan();
	}
	
	if(timer2_cnt % 200 == 0)
		flag_200ms = 1;
  if(++timer2_cnt % 1000 == 0)
	{
		menu[bit_cnt] = 20;
		if(++bit_cnt == 8)
			bit_cnt = 0;
		menu[bit_cnt] = 8;
		timer2_cnt = 0;
	}
}

你可能感兴趣的:(蓝桥杯)