Home Article 51单片机定时器控制

51单片机定时器控制

Release time:2020-07-25 10:03:04 Author:admin Reading volume:157

相关寄存器:


操作步骤:

void delay1s()
{
	int count = 0;
	TMOD &= 0xf0;//将定时器0清空
	TMOD |= 0x01;//配置定时器模式1
	TH0 = 0x3c;
TL0 = 0xb0; //定时50ms TR0 = 1 ;//开始计时 while(1) { if(TF0 == 1) { count ++; TF0 = 0; } if(count == 20) //定时1s后退出 { break; } } }
用定时器实现数码加到60s并且流水灯滚动。
#include "reg52.h"			 
#include "intrins.h"
typedef unsigned int u16;	 
typedef unsigned char u8;


sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
u16 bai = 0;
u16 shi = 0;
u16 ge = 0;
u8 count1 = 0;
					// 0   1    2    3    4    5    6    7
u8 code smgduan[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
					0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
					// 8   9    A    B    C   D     E    F
void delayms(u8 ms)
{
	int i,j;
	for(i = ms;i>0;i--)
		for(j=110;j>0;j--);
}

void delay1s()
{
	int count = 0;
	TMOD &= 0xf0;//将定时器0清空
	TMOD |= 0x01;//配置定时器模式1
	TH0 = 0xfc;
	TL0 = 0x18;	   //定时1ms
	TR0 = 1 ;  //开始计时

	while(1)
	{
		if(TF0 == 1)
		{
			count ++;
		}
		 if(count == 1) //定时1s后退出
		{	
			count1++;
			break;
		}
		
	}
}


void displayadd()
{
int i;
 	P2 = _crol_(0,2);	 //左移	 P2.2 2.3,2.4接38译码器
	P0=smgduan[ge];
	delayms(2);
	P2 = _crol_(1,2);
	P0=smgduan[shi];	//P0接数码管和LED
	delayms(2);
	P2 = _crol_(2,2);
	P0=smgduan[bai];
	delayms(2);
	
	if(count1 == 100)
	{
		ge++;
		if(ge == 10)
		{
			ge = 0;
			shi++;
		}
		if(shi == 6)
		{
			shi = 0;
			
		P2 = 0xfe;
		for(i = 0; i< 8;i++)
		{
			delayms(1000);
			P2=_crol_(P2,1);//左移
		}
		for(i = 0; i< 8;i++)
		{
			delayms(1000);
			P2=_cror_(P2,1); //右移
		}

		}
		count1 = 0 ;
	}
	
}


void main()
{	
	while(1)
	{
		 
		 	delay1s();
		   displayadd();
	
	}		
}

支付宝打赏 微信打赏
  
I want to comment

Search

Leave a message
http://blog.rjxj513.com/
User login
You have not written any reviews yet!
You have commented!
Can only praise once!
You have a collection!