中断与数码管动态显示代码

数码管从0-999999且高位0显示


#include <reg52.h>

sbit ADDR0 = P1 ^ 0;
sbit ADDR1 = P1 ^ 1;
sbit ADDR2 = P1 ^ 2;
sbit ADDR3 = P1 ^ 3;
sbit ENLED = P1 ^ 4;

unsigned char code LedChar[] = //数码管显示字符转换表
{
	0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
	0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
};

unsigned char LedBuff[] = //数码管显示缓冲区,初值为0xFF确保启动时都不亮
{
	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};

unsigned char i = 0;//动态扫描的索引
unsigned int cnt = 0;//记录T0中断次数
unsigned char isFlag = 0;//1s标志位

void main(void)
{
	unsigned long sec = 0;//记录经过的秒数

	ENLED = 0;//使能U3,选择控制数码管
	ADDR3 = 1;//因为需要动态改变ADDR0-2的值,所以不需要再初始化了
	TMOD = 0x01;//设置T0为模式1
	TH0 = 0xFC;//设置T0赋初值为0xFC67,定时1ms
	TL0 = 0x67;
	TR0 = 1;//启动T0

	EA = 1;//使能总中断
	ET0 = 1;//使能T0中断

	while (1)
	{
		if (1 == isFlag)//判断1秒定时标志
		{
			isFlag = 0;//1秒定时标志清零
			if (999999 == sec)
				sec = 0;
			else
				sec++;//秒计数加1

			//以下代码将sec按十进制位从低到高依次提取并转为数码管显示字符
			LedBuff[0] = LedChar[sec % 10];
			LedBuff[1] = LedChar[sec / 10 % 10];
			LedBuff[2] = LedChar[sec / 100 % 10];
			LedBuff[3] = LedChar[sec / 1000 % 10];
			LedBuff[4] = LedChar[sec / 10000 % 10];
			LedBuff[5] = LedChar[sec / 100000 % 10];
		}
	}
}

/* 定时器中断服务函数 */
void interrupt0 (void) interrupt 1//T0中断
{
	TH0 = 0xFC;//重新赋初值
	TL0 = 0x67;
	cnt++;//计数值加1

	if (cnt > 1000)
	{
		cnt = 0;//清零计数值以重新开始下一秒计时
		isFlag = 1;	   //设置1秒定时标志位1
	}

	P0 = 0xFF;//显示消隐

	//以下代码完成数码管动态扫描刷新
	switch (i)
	{
	case 0: ADDR2 = 0;ADDR1 = 0;ADDR0 = 0;i++;P0 = LedBuff[0];break;
	case 1: ADDR2 = 0;ADDR1 = 0;ADDR0 = 1;i++;P0 = LedBuff[1];break;
	case 2: ADDR2 = 0;ADDR1 = 1;ADDR0 = 0;i++;P0 = LedBuff[2];break;
	case 3: ADDR2 = 0;ADDR1 = 1;ADDR0 = 1;i++;P0 = LedBuff[3];break;
	case 4: ADDR2 = 1;ADDR1 = 0;ADDR0 = 0;i++;P0 = LedBuff[4];break;
	case 5: ADDR2 = 1;ADDR1 = 0;ADDR0 = 1;i = 0;P0 = LedBuff[5];break;
	default:break;
	}
}

数码管从0-999999且高位0不显示


#include <reg52.h>

sbit ADDR0 = P1 ^ 0;
sbit ADDR1 = P1 ^ 1;
sbit ADDR2 = P1 ^ 2;
sbit ADDR3 = P1 ^ 3;
sbit ENLED = P1 ^ 4;

unsigned char code LedChar[] = //数码管显示字符转换表
{
	0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
	0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
};

unsigned char LedBuff[] = //数码管显示缓冲区,初值为0xFF确保启动时都不亮
{
	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};

unsigned char i = 0;//动态扫描的索引
unsigned int cnt = 0;//记录T0中断次数
unsigned char isFlag = 0;//1s标志位
unsigned long sec = 0;//记录经过的秒数

void main(void)
{
	ENLED = 0;//使能U3,选择控制数码管
	ADDR3 = 1;//因为需要动态改变ADDR0-2的值,所以不需要再初始化了
	TMOD = 0x01;//设置T0为模式1
	TH0 = 0xFC;//设置T0赋初值为0xFC67,定时1ms
	TL0 = 0x67;
	TR0 = 1;//启动T0

	EA = 1;//使能总中断
	ET0 = 1;//使能T0中断

	while (1)
	{
		if (1 == isFlag)//判断1秒定时标志
		{
			isFlag = 0;//1秒定时标志清零
			if (999999 == sec)
				sec = 0;
			else
				sec++;//秒计数加1

			//以下代码将sec按十进制位从低到高依次提取并转为数码管显示字符
			LedBuff[0] = LedChar[sec % 10];
			LedBuff[1] = LedChar[sec / 10 % 10];
			LedBuff[2] = LedChar[sec / 100 % 10];
			LedBuff[3] = LedChar[sec / 1000 % 10];
			LedBuff[4] = LedChar[sec / 10000 % 10];
			LedBuff[5] = LedChar[sec / 100000 % 10];
		}
	}
}

/* 定时器中断服务函数 */
void interrupt0 (void) interrupt 1//T0中断
{
	TH0 = 0xFC;//重新赋初值
	TL0 = 0x67;
	cnt++;//计数值加1

	if (cnt > 1000)
	{
		cnt = 0;//清零计数值以重新开始下一秒计时
		isFlag = 1;	   //设置1秒定时标志位1
	}

	P0 = 0xFF;//显示消隐

	//以下代码完成数码管动态扫描刷新
	switch (i)
	{
	case 0: if (sec >= 0){ADDR2 = 0;ADDR1 = 0;ADDR0 = 0;P0 = LedBuff[0];}i++;break;
	case 1: if (sec >= 10){ADDR2 = 0;ADDR1 = 0;ADDR0 = 1;P0 = LedBuff[1];}i++;break;
	case 2: if (sec >= 100){ADDR2 = 0;ADDR1 = 1;ADDR0 = 0;P0 = LedBuff[2];}i++;break;
	case 3: if (sec >= 1000){ADDR2 = 0;ADDR1 = 1;ADDR0 = 1;P0 = LedBuff[3];}i++;break;
	case 4: if (sec >= 10000){ADDR2 = 1;ADDR1 = 0;ADDR0 = 0;P0 = LedBuff[4];}i++;break;
	case 5: if (sec >= 100000){ADDR2 = 1;ADDR1 = 0;ADDR0 = 1;P0 = LedBuff[5];}i = 0;break;
	default:break;
	}
}

数码管从999999-0且高位0显示


#include <reg52.h>

sbit ADDR0 = P1 ^ 0;
sbit ADDR1 = P1 ^ 1;
sbit ADDR2 = P1 ^ 2;
sbit ADDR3 = P1 ^ 3;
sbit ENLED = P1 ^ 4;

unsigned char code LedChar[] = //数码管显示字符转换表
{
	0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
	0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
};

unsigned char LedBuff[] = //数码管显示缓冲区,初值为0xFF确保启动时都不亮
{
	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};

unsigned char i = 0;//动态扫描的索引
unsigned int cnt = 0;//记录T0中断次数
unsigned char isFlag = 0;//1s标志位

void main(void)
{
	unsigned long sec = 999999;//秒数

	ENLED = 0;//使能U3,选择控制数码管
	ADDR3 = 1;//因为需要动态改变ADDR0-2的值,所以不需要再初始化了
	TMOD = 0x01;//设置T0为模式1
	TH0 = 0xFC;//设置T0赋初值为0xFC67,定时1ms
	TL0 = 0x67;
	TR0 = 1;//启动T0

	EA = 1;//使能总中断
	ET0 = 1;//使能T0中断

	while (1)
	{
		if (1 == isFlag)//判断1秒定时标志
		{
			isFlag = 0;//1秒定时标志清零

			if (0 == sec)
				sec = 999999;
			else
				sec--;//秒计数减1

			//以下代码将sec按十进制位从低到高依次提取并转为数码管显示字符
			LedBuff[0] = LedChar[sec % 10];
			LedBuff[1] = LedChar[sec / 10 % 10];
			LedBuff[2] = LedChar[sec / 100 % 10];
			LedBuff[3] = LedChar[sec / 1000 % 10];
			LedBuff[4] = LedChar[sec / 10000 % 10];
			LedBuff[5] = LedChar[sec / 100000 % 10];
		}
	}
}

/* 定时器中断服务函数 */
void interrupt0 (void) interrupt 1//T0中断
{
	TH0 = 0xFC;//重新赋初值
	TL0 = 0x67;
	cnt++;//计数值加1

	if (cnt > 1000)
	{
		cnt = 0;//清零计数值以重新开始下一秒计时
		isFlag = 1;	   //设置1秒定时标志位1
	}

	P0 = 0xFF;//显示消隐

	//以下代码完成数码管动态扫描刷新
	switch (i)
	{
	case 0: ADDR2 = 0;ADDR1 = 0;ADDR0 = 0;i++;P0 = LedBuff[0];break;
	case 1: ADDR2 = 0;ADDR1 = 0;ADDR0 = 1;i++;P0 = LedBuff[1];break;
	case 2: ADDR2 = 0;ADDR1 = 1;ADDR0 = 0;i++;P0 = LedBuff[2];break;
	case 3: ADDR2 = 0;ADDR1 = 1;ADDR0 = 1;i++;P0 = LedBuff[3];break;
	case 4: ADDR2 = 1;ADDR1 = 0;ADDR0 = 0;i++;P0 = LedBuff[4];break;
	case 5: ADDR2 = 1;ADDR1 = 0;ADDR0 = 1;i = 0;P0 = LedBuff[5];break;
	default:break;
	}
}

数码管从999999-0且高位0不显示


#include <reg52.h>

sbit ADDR0 = P1 ^ 0;
sbit ADDR1 = P1 ^ 1;
sbit ADDR2 = P1 ^ 2;
sbit ADDR3 = P1 ^ 3;
sbit ENLED = P1 ^ 4;

unsigned char code LedChar[] = //数码管显示字符转换表
{
	0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
	0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
};

unsigned char LedBuff[] = //数码管显示缓冲区,初值为0xFF确保启动时都不亮
{
	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};

unsigned char i = 0;//动态扫描的索引
unsigned int cnt = 0;//记录T0中断次数
unsigned char isFlag = 0;//1s标志位
unsigned long sec = 999999;//秒数

void main(void)
{
	

	ENLED = 0;//使能U3,选择控制数码管
	ADDR3 = 1;//因为需要动态改变ADDR0-2的值,所以不需要再初始化了
	TMOD = 0x01;//设置T0为模式1
	TH0 = 0xFC;//设置T0赋初值为0xFC67,定时1ms
	TL0 = 0x67;
	TR0 = 1;//启动T0

	EA = 1;//使能总中断
	ET0 = 1;//使能T0中断

	while (1)
	{
		if (1 == isFlag)//判断1秒定时标志
		{
			isFlag = 0;//1秒定时标志清零

			if (0 == sec)
				sec = 999999;
			else
				sec--;//秒计数减1

			//以下代码将sec按十进制位从低到高依次提取并转为数码管显示字符
			LedBuff[0] = LedChar[sec % 10];
			LedBuff[1] = LedChar[sec / 10 % 10];
			LedBuff[2] = LedChar[sec / 100 % 10];
			LedBuff[3] = LedChar[sec / 1000 % 10];
			LedBuff[4] = LedChar[sec / 10000 % 10];
			LedBuff[5] = LedChar[sec / 100000 % 10];
		}
	}
}

/* 定时器中断服务函数 */
void interrupt0 (void) interrupt 1//T0中断
{
	TH0 = 0xFC;//重新赋初值
	TL0 = 0x67;
	cnt++;//计数值加1

	if (cnt > 1000)
	{
		cnt = 0;//清零计数值以重新开始下一秒计时
		isFlag = 1;	   //设置1秒定时标志位1
	}

	P0 = 0xFF;//显示消隐

	//以下代码完成数码管动态扫描刷新
	switch (i)
	{
	case 0: if (sec >= 0){ADDR2 = 0;ADDR1 = 0;ADDR0 = 0;P0 = LedBuff[0];}i++;break;
	case 1: if (sec >= 10){ADDR2 = 0;ADDR1 = 0;ADDR0 = 1;P0 = LedBuff[1];}i++;break;
	case 2: if (sec >= 100){ADDR2 = 0;ADDR1 = 1;ADDR0 = 0;P0 = LedBuff[2];}i++;break;
	case 3: if (sec >= 1000){ADDR2 = 0;ADDR1 = 1;ADDR0 = 1;P0 = LedBuff[3];}i++;break;
	case 4: if (sec >= 10000){ADDR2 = 1;ADDR1 = 0;ADDR0 = 0;P0 = LedBuff[4];}i++;break;
	case 5: if (sec >= 100000){ADDR2 = 1;ADDR1 = 0;ADDR0 = 1;P0 = LedBuff[5];}i = 0;break;
	default:break;
	}
}
posted @ 2024-11-08 15:29  EricsT  阅读(25)  评论(0编辑  收藏  举报