定时器与数码管之代码

小灯以1s间隔闪烁


#include <reg52.h>

sbit LED = P0 ^ 0;

sbit ADDR0 = P1 ^ 0;
sbit ADDR1 = P1 ^ 1;
sbit ADDR2 = P1 ^ 2;

sbit ADDR3 = P1 ^ 3;
sbit ENLED = P1 ^ 4;

void main(void)
{
	unsigned char cnt;//溢出次数

	//74HC138使能
	ENLED = 0;
	ADDR3 = 1;
	
	//Q16导通
	ADDR2 = 1;
	ADDR1 = 1;
	ADDR0 = 0;

	LED = 0;//点亮小灯

	TMOD = 1;//设置T0为模式1

	//设置T0初始值
	TH0 = 0x4C;
	TL0 = 0x00;

	TR0 = 1;//启动T0

	cnt = 0;//设置溢出次数为0

	while (1)
	{
		//溢出
		if (1 == TF0)
		{
			cnt++;

			//T0初始化
			TF0 = 0;

			TH0 = 0x4C;
			TL0 = 0x00;

			//溢出1s
			if (cnt >= 20)
			{
				LED = ~LED;//小灯状态相反
				cnt = 0;
			}
		}
	}
}

流水灯左跑,间隔1s[写法1]


#include <reg52.h>

sbit ADDR0 = P1 ^ 0;
sbit ADDR1 = P1 ^ 1;
sbit ADDR2 = P1 ^ 2;

sbit ADDR3 = P1 ^ 3;
sbit ENLED = P1 ^ 4;

int POW(int m)
{
	int i;
	int iRet;
	iRet = 1;
	for (i = 0; i < m; i++)
	iRet = iRet * 2;
	return iRet;		
}

void main(void)
{
	unsigned char cnt;//溢出次数
	int count;

	//74HC138使能
	ENLED = 0;
	ADDR3 = 1;
	
	//Q16导通
	ADDR2 = 1;
	ADDR1 = 1;
	ADDR0 = 0;

	TMOD = 1;//设置T0为模式1

	//设置T0初始值
	TH0 = 0x4C;
	TL0 = 0x00;

	TR0 = 1;//启动T0

	cnt = 0;//设置溢出次数为0
	count = 0;

	while (1)
	{

		//溢出
		if (1 == TF0)
		{
			cnt++;

			//T0初始化
			TF0 = 0;

			TH0 = 0x4C;
			TL0 = 0x00;

			//溢出1s
			if (cnt >= 20)
			{
				if (8 == count)
					count = 0;

				P0 = 0xFF ^ POW(count);

				count++;
				cnt = 0;
			}
		}
	}
}

流水灯左跑,间隔1s[写法2]

由于此板子上的P1口的默认值是0xFF,即每一位的默认值都是高电平

也就是说, 给板子重新上电时,P1的每一位都是高电平

故此时ADDR3、ADDR2以及ADDR1默认为高电平

故只需设置ENLED以及ADDR0为低电平即可


#include <reg52.h>

sbit ADDR0 = P1 ^ 0;
//sbit ADDR1 = P1 ^ 1;
//sbit ADDR2 = P1 ^ 2;

//sbit ADDR3 = P1 ^ 3;
sbit ENLED = P1 ^ 4;

int POW(int m)
{
	int i;
	int iRet;
	iRet = 1;
	for (i = 0; i < m; i++)
	iRet = iRet * 2;
	return iRet;		
}

void main(void)
{
	unsigned char cnt;//溢出次数
	int count;

	//74HC138使能
	ENLED = 0;
//	ADDR3 = 1;
	
	//Q16导通
//	ADDR2 = 1;
//	ADDR1 = 1;
	ADDR0 = 0;

	TMOD = 1;//设置T0为模式1

	//设置T0初始值
	TH0 = 0x4C;
	TL0 = 0x00;

	TR0 = 1;//启动T0

	cnt = 0;//设置溢出次数为0
	count = 0;

	while (1)
	{

		//溢出
		if (1 == TF0)
		{
			cnt++;

			//T0初始化
			TF0 = 0;

			TH0 = 0x4C;
			TL0 = 0x00;

			//溢出1s
			if (cnt >= 20)
			{
				if (8 == count)
					count = 0;

				P0 = 0xFF ^ POW(count);

				count++;
				cnt = 0;
			}
		}
	}
}

流水灯左跑,间隔1s[写法3][不采用位寻址]

P1口有8位,每一位都是默认1,故P1初始值为0x11111111即0xFF

设置P1的第0位和第4位为0,则此时P1的值为0x11101110即0xEE


#include <reg52.h>

int POW(int m)
{
	int i;
	int iRet;
	iRet = 1;
	for (i = 0; i < m; i++)
	iRet = iRet * 2;
	return iRet;		
}

void main(void)
{
	unsigned char cnt;//溢出次数
	int count;

	//Q16导通
	P1 = 0xEE;

	TMOD = 1;//设置T0为模式1

	//设置T0初始值
	TH0 = 0x4C;
	TL0 = 0x00;

	TR0 = 1;//启动T0

	cnt = 0;//设置溢出次数为0
	count = 0;

	while (1)
	{

		//溢出
		if (1 == TF0)
		{
			cnt++;

			//T0初始化
			TF0 = 0;

			TH0 = 0x4C;
			TL0 = 0x00;

			//溢出1s
			if (cnt >= 20)
			{
				if (8 == count)
					count = 0;

				P0 = 0xFF ^ POW(count);

				count++;
				cnt = 0;
			}
		}
	}
}

以下流水灯相关代码都有3种写法,同流水灯左跑一致,均采用第一种写法

流水灯右跑,间隔1s


#include <reg52.h>

sbit ADDR0 = P1 ^ 0;
sbit ADDR1 = P1 ^ 1;
sbit ADDR2 = P1 ^ 2;

sbit ADDR3 = P1 ^ 3;
sbit ENLED = P1 ^ 4;

int POW(int m)
{
	int i;
	int iRet;
	iRet = 1;
	for (i = 0; i < m; i++)
	iRet = iRet * 2;
	return iRet;		
}

void main(void)
{
	unsigned char cnt;//溢出次数
	int count;

	//74HC138使能
	ENLED = 0;
	ADDR3 = 1;
	
	//Q16导通
	ADDR2 = 1;
	ADDR1 = 1;
	ADDR0 = 0;

	TMOD = 1;//设置T0为模式1

	//设置T0初始值
	TH0 = 0x4C;
	TL0 = 0x00;

	TR0 = 1;//启动T0

	cnt = 0;//设置溢出次数为0
	count = 7;

	while (1)
	{

		//溢出
		if (1 == TF0)
		{
			cnt++;

			//T0初始化
			TF0 = 0;

			TH0 = 0x4C;
			TL0 = 0x00;

			//溢出1s
			if (cnt >= 20)
			{
				P0 = 0xFF ^ POW(count);

				if (0 == count)
					count = 8;
	
				count--;
				cnt = 0;
			}
		}
	}
}

流水灯左跑后右跑[间隔1s]


#include <reg52.h>

sbit ADDR0 = P1 ^ 0;
sbit ADDR1 = P1 ^ 1;
sbit ADDR2 = P1 ^ 2;

sbit ADDR3 = P1 ^ 3;
sbit ENLED = P1 ^ 4;

int POW(int m)
{
	int i;
	int iRet;
	iRet = 1;
	for (i = 0; i < m; i++)
	iRet = iRet * 2;
	return iRet;		
}

void main(void)
{
	unsigned char cnt;//溢出次数
	int count;
	int iflag;//iflag = 0时,左跑,iflag = 1时,右跑

	//74HC138使能
	ENLED = 0;
	ADDR3 = 1;
	
	//Q16导通
	ADDR2 = 1;
	ADDR1 = 1;
	ADDR0 = 0;

	TMOD = 1;//设置T0为模式1

	//设置T0初始值
	TH0 = 0x4C;
	TL0 = 0x00;

	TR0 = 1;//启动T0

	cnt = 0;//设置溢出次数为0
	count = 0;
	iflag = 0;

	while (1)
	{

		//溢出
		if (1 == TF0)
		{
			cnt++;

			//T0初始化
			TF0 = 0;

			TH0 = 0x4C;
			TL0 = 0x00;

			//溢出1s
			if (cnt >= 20)
			{
				if(0 == iflag)
				{
					if (8 == count)
					{
						count = 6;
						iflag = 1;
					}
				}
				else
				{
					if (0 == count)
						iflag = 0;
				}

				P0 = 0xFF ^ POW(count);

				if (0 == iflag)
					count++;
				else
					count--;

				cnt = 0;
			}
		}
	}
}

流水灯右跑后左跑[间隔1s]


#include <reg52.h>

sbit ADDR0 = P1 ^ 0;
sbit ADDR1 = P1 ^ 1;
sbit ADDR2 = P1 ^ 2;

sbit ADDR3 = P1 ^ 3;
sbit ENLED = P1 ^ 4;

int POW(int m)
{
	int i;
	int iRet;
	iRet = 1;
	for (i = 0; i < m; i++)
	iRet = iRet * 2;
	return iRet;		
}

void main(void)
{
	unsigned char cnt;//溢出次数
	int count;
	int iflag;//iflag = 0时,左跑,iflag = 1时,右跑

	//74HC138使能
	ENLED = 0;
	ADDR3 = 1;
	
	//Q16导通
	ADDR2 = 1;
	ADDR1 = 1;
	ADDR0 = 0;

	TMOD = 1;//设置T0为模式1

	//设置T0初始值
	TH0 = 0x4C;
	TL0 = 0x00;

	TR0 = 1;//启动T0

	cnt = 0;//设置溢出次数为0
	count = 7;
	iflag = 1;

	while (1)
	{

		//溢出
		if (1 == TF0)
		{
			cnt++;

			//T0初始化
			TF0 = 0;

			TH0 = 0x4C;
			TL0 = 0x00;

			//溢出1s
			if (cnt >= 20)
			{
				if(0 == iflag)
				{
					if (8 == count)
					{
						count = 6;
						iflag = 1;
					}
				}
				else
				{
					if (0 == count)
						iflag = 0;
				}

				P0 = 0xFF ^ POW(count);

				if (0 == iflag)
					count++;
				else
					count--;

				cnt = 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;

void main(void)
{
	//74HC138使能
	ENLED = 0;
	ADDR0 = 1;


	//Q7导通
	ADDR2 = 1;
	ADDR1 = 0;
	ADDR0 = 1;

	P0 = 0xC0;//数码管abcdef段亮,即显示0

	while(1)
		;
}

数码管显示数字1


#include<reg52.h>

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

void main(void)
{
	//74HC138使能
	ENLED = 0;
	ADDR0 = 1;


	//Q7导通
	ADDR2 = 1;
	ADDR1 = 0;
	ADDR0 = 1;

	P0 = 0xF9;//数码管bc段亮,即显示1

	while(1)
		;
}

 数码管显示数字2


#include<reg52.h>

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

void main(void)
{
	//74HC138使能
	ENLED = 0;
	ADDR0 = 1;


	//Q7导通
	ADDR2 = 1;
	ADDR1 = 0;
	ADDR0 = 1;

	P0 = 0xA4;//数码管abged段亮,即显示2

	while(1)
		;
}

 数码管显示数字3


#include<reg52.h>

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

void main(void)
{
	//74HC138使能
	ENLED = 0;
	ADDR0 = 1;


	//Q7导通
	ADDR2 = 1;
	ADDR1 = 0;
	ADDR0 = 1;

	P0 = 0xB0;//数码管abgcd段亮,即显示3

	while(1)
		;
}

 数码管显示数字4


#include<reg52.h>

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

void main(void)
{
	//74HC138使能
	ENLED = 0;
	ADDR0 = 1;


	//Q7导通
	ADDR2 = 1;
	ADDR1 = 0;
	ADDR0 = 1;

	P0 = 0x99;//数码管fgbc段亮,即显示4

	while(1)
		;
}

 数码管显示数字5


#include<reg52.h>

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

void main(void)
{
	//74HC138使能
	ENLED = 0;
	ADDR0 = 1;


	//Q7导通
	ADDR2 = 1;
	ADDR1 = 0;
	ADDR0 = 1;

	P0 = 0x92;//数码管afgcd段亮,即显示5

	while(1)
		;
}

 数码管显示数字6


#include<reg52.h>

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

void main(void)
{
	//74HC138使能
	ENLED = 0;
	ADDR0 = 1;


	//Q7导通
	ADDR2 = 1;
	ADDR1 = 0;
	ADDR0 = 1;

	P0 = 0x82;//数码管afgcde段亮,即显示6

	while(1)
		;
}

 数码管显示数字7


#include<reg52.h>

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

void main(void)
{
	//74HC138使能
	ENLED = 0;
	ADDR0 = 1;


	//Q7导通
	ADDR2 = 1;
	ADDR1 = 0;
	ADDR0 = 1;

	P0 = 0xF8;//数码管abc段亮,即显示7

	while(1)
		;
}

 数码管显示数字8


#include<reg52.h>

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

void main(void)
{
	//74HC138使能
	ENLED = 0;
	ADDR0 = 1;


	//Q7导通
	ADDR2 = 1;
	ADDR1 = 0;
	ADDR0 = 1;

	P0 = 0x80;//数码管abcdefg段亮,即显示8

	while(1)
		;
}

 数码管显示数字9


#include<reg52.h>

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

void main(void)
{
	//74HC138使能
	ENLED = 0;
	ADDR0 = 1;


	//Q7导通
	ADDR2 = 1;
	ADDR1 = 0;
	ADDR0 = 1;

	P0 = 0x90;//数码管abcdfg段亮,即显示9

	while(1)
		;
}

 数码管显示字母A


#include<reg52.h>

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

void main(void)
{
	//74HC138使能
	ENLED = 0;
	ADDR0 = 1;


	//Q7导通
	ADDR2 = 1;
	ADDR1 = 0;
	ADDR0 = 1;

	P0 = 0x88;//数码管abcefg段亮,即显示A

	while(1)
		;
}

 数码管显示字母b


#include<reg52.h>

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

void main(void)
{
	//74HC138使能
	ENLED = 0;
	ADDR0 = 1;


	//Q7导通
	ADDR2 = 1;
	ADDR1 = 0;
	ADDR0 = 1;

	P0 = 0x83;//数码管cdefg段亮,即显示b

	while(1)
		;
}

 数码管显示字母C


#include<reg52.h>

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

void main(void)
{
	//74HC138使能
	ENLED = 0;
	ADDR0 = 1;


	//Q7导通
	ADDR2 = 1;
	ADDR1 = 0;
	ADDR0 = 1;

	P0 = 0xC6;//数码管afed段亮,即显示C

	while(1)
		;
}

 数码管显示字母d


#include<reg52.h>

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

void main(void)
{
	//74HC138使能
	ENLED = 0;
	ADDR0 = 1;


	//Q7导通
	ADDR2 = 1;
	ADDR1 = 0;
	ADDR0 = 1;

	P0 = 0xA1;//数码管bcdef段亮,即显示d

	while(1)
		;
}

 数码管显示字母E


#include<reg52.h>

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

void main(void)
{
	//74HC138使能
	ENLED = 0;
	ADDR0 = 1;


	//Q7导通
	ADDR2 = 1;
	ADDR1 = 0;
	ADDR0 = 1;

	P0 = 0x86;//数码管adefg段亮,即显示E

	while(1)
		;
}

 数码管显示字母F


#include<reg52.h>

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

void main(void)
{
	//74HC138使能
	ENLED = 0;
	ADDR0 = 1;


	//Q7导通
	ADDR2 = 1;
	ADDR1 = 0;
	ADDR0 = 1;

	P0 = 0x8E;//数码管aefg段亮,即显示F

	while(1)
		;
}

数码管正向计时[间隔1s][从0到9]


#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
};

void main(void)
{
	unsigned cnt;//溢出次数
	int sec;//秒

	//74HC138使能
	ENLED = 0;
	ADDR0 = 1;

	//Q7导通
	ADDR2 = 1;
	ADDR1 = 0;
	ADDR0 = 1;

	TMOD = 1;//To设置为模式0

	//设置T0初始值
	TH0 = 0xB4;
	TL0 = 0x00;

	TR0 = 1;//启动T0
	cnt = 0;
	sec = 0;

	while(1)
	{
		if (1 == TF0)//溢出
		{
			//设置T0初始值
			TF0 = 0;
			TH0 = 0xB4;
			TL0 = 0x00;

			cnt++;

			//溢出1s
			if (cnt >= 20)
			{
				cnt = 0;

				if (10 == sec)
					sec = 0;

				P0 = LedChar[sec];
				sec++;
			}
		}
	}
}

数码管倒计时[间隔1s][从9到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
};

void main(void)
{
	unsigned cnt;//溢出次数
	int sec;//秒

	//74HC138使能
	ENLED = 0;
	ADDR0 = 1;

	//Q7导通
	ADDR2 = 1;
	ADDR1 = 0;
	ADDR0 = 1;

	TMOD = 1;//To设置为模式0

	//设置T0初始值
	TH0 = 0xB4;
	TL0 = 0x00;

	TR0 = 1;//启动T0
	cnt = 0;
	sec = 9;

	while(1)
	{
		if (1 == TF0)//溢出
		{
			//设置T0初始值
			TF0 = 0;
			TH0 = 0xB4;
			TL0 = 0x00;

			cnt++;

			//溢出1s
			if (cnt >= 20)
			{
				cnt = 0;

				P0 = LedChar[sec];

				if (0 == sec)
					sec = 10;

				sec--;
			}
		}
	}
}