stc12c5a60s 按键控制数码管显示

按键控制数码管显示

main.c

#include <reg52.h>
#include <intrins.h>

#define uchar unsigned char
typedef unsigned int uint;

//定义位选管脚
sbit ASEG = P0^2;
sbit BSEG = P0^3;
sbit CSEG = P0^4;
sbit DSEG = P0^5;

sbit BUT1 = P3^2;
sbit BUT2 = P3^3;

sbit LED = P0^1;

uchar code smgduan[17]=	 //共阴数码管显示0~F的值
{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

uchar showdata[4] = { 0 };

void delay(uint x)
{
	uchar i, j;
	while(x --) //1ms
	{
		_nop_();
		_nop_();
		_nop_();
		i = 11;
		j = 190;
		do
		{
			while (--j);
		} while (--i);
	}
}

void seg4show(uchar i)
{
	switch(i)
	{
		case 0:
			if(showdata[i] != 0)
			{
				ASEG = 0; BSEG = 1; CSEG = 1; DSEG = 1;
			}
			else
			{
				ASEG = 1; BSEG = 1; CSEG = 1; DSEG = 1;	
			}
			break;
		case 1:
			if(showdata[i] != 0 | showdata[0] != 0)
			{
				ASEG = 1; BSEG = 0; CSEG = 1; DSEG = 1;
			}
			else
			{
				ASEG = 1; BSEG = 1; CSEG = 1; DSEG = 1;	
			}
			break;	
		case 2:
			if(showdata[i] != 0 | showdata[0] != 0 | showdata[1] != 0)
			{
				ASEG = 1; BSEG = 1; CSEG = 0; DSEG = 1;
			}
			else
			{
				ASEG = 1; BSEG = 1; CSEG = 1; DSEG = 1;	
			}
			break;	
		case 3:
			ASEG = 1; BSEG = 1; CSEG = 1; DSEG = 0;
			break;
		default: ASEG = 1; BSEG = 1; CSEG = 1; DSEG = 1; 		
	}
	P1 = smgduan[showdata[i]];
}

void Int_init()
{
	 EA = 1;
	 EX0 = 1;
	 EX1 = 1;
}

void main()
{
	uchar i;
	Int_init();
	LED = 1;

	while(1)
	{
		for( i = 0; i< 4; i ++)
		{
			seg4show(i);
			delay(1);
			P1 = 0x00;
		}
	}
}

void INT0_routine() interrupt 0
{
	if(BUT1 == 0)
	{
		delay(10);
		LED = ~LED;
		if(BUT1 == 0)
		{
			showdata[3] ++;
			while( ! BUT1);
		}
	}
	if(showdata[3] == 10)
	{
		showdata[2]++;
		showdata[3] = 0;
	}
	if(showdata[2] == 10)
	{
		showdata[1]++;
		showdata[2] = 0;
	}
	if(showdata[1] == 10)
	{
		showdata[0]++;
		showdata[1] = 0;
	}
	if(showdata[0] == 10)
	{
		showdata[0] = 0;
	}			
}

void INT1_routine() interrupt 2
{
	if(BUT2 == 0)
	{
		delay(10);
		if(BUT2 == 0)
		{
			LED = ~LED;
			showdata[3] --;
			while( !BUT2 );
		}
	}
	if(showdata[3] > 10)
	{
		showdata[2] --;
		showdata[3] = 9;
	}
	if(showdata[2] > 10)
	{
		showdata[1] --;
		showdata[2] = 9;
	}
	if(showdata[1] > 10)
	{
		showdata[0] --;
		showdata[1] = 9;
	}
	if(showdata[0] > 10)
	{
		showdata[0] = 9;
	}
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

_Hello Spring

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

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

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

打赏作者

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

抵扣说明:

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

余额充值