# step by step learning 51 microcontroller# timer and digital tube#not.4

1. Be proficient in the principles and application methods of microcontroller timers.

1) Clock cycle: The smallest unit in the microcontroller timing. The specific calculation method is one-third of the clock source.

2) Machine cycle: the shortest time for our microcontroller to complete an operation.

3) Timer: When the timer is turned on, the value of the "storage register" is automatically increased by one after one machine cycle. In other words, the machine cycle is a counting cycle.

4)

5)

6)

7)

2. Code implementation

#include <REGX52.H>
sbit add0 = P1^0;
sbit add1 = P1^1;
sbit add2 = P1^2;
sbit add3 = P1^3;
sbit ENLED = P1^4;
unsigned int cnt = 1,i ,a = 0,arr = 1;
void main()
{
	ENLED = 0;
	add3 = 1;
	add0 = 0;
	add1 = 1;
	add2 = 1;
	TMOD = 0x01;//开启总开关
	TH0 = 0xfc;//置高位计值
	TL0 = 0x00;//置低位开始计值
	TR0 = 1;//计时器开始计时
  while(1) 
{
	
	if(1 == TF0)//判断是否进制
	{
	TF0 = 0;
	TH0 = 0xfc;
	TL0 = 0x00;
		if(100 > cnt)
		{
		cnt = 0;
			hans();
		}
	}


3. Use a timer to implement delay and complete the program of the running water lamp moving left and right.

#include <REGX52.H>
sbit add0 = P1^0;
sbit add1 = P1^1;
sbit add2 = P1^2;
sbit add3 = P1^3;
sbit ENLED = P1^4;
unsigned int cnt = 0,arr3,a = 0,arr = 0,arr2 = 0;
void main()
{
	ENLED = 0;
	add3 = 1;
	add0 = 0;
	add1 = 1;
	add2 = 1;
	TMOD = 0x01;
	TH0 = 0xb8;
	TL0 = 0x00;
	TR0 = 1;
  while(1) 
{
	if(1 == TF0)
	{
	TF0 = 0;
	TH0 = 0xb8;
	TL0 = 0x00;
		arr2++;
		arr++;
		if(50 == arr2)
		{
			arr2 = 0;
		P0 = ~(0x80 >> cnt);
				cnt++; 
		}
		if(cnt >= 8)
	{
		  cnt = 0;
			arr2 = 51;
	}
		if(arr == 400)
			{
		P0 = ~(0x01 << a);
		  a++;
		  arr = 350;
		}		
	} if(a >= 8)
	{
	  arr2 = 0;
		cnt = 0;
		a = 0;
		arr = 0;
	}
		}
	}	


4. Understand the principles of digital tubes and master the calculation method of the truth table of digital tubes.


5. Program the digital tube to statically display the countdown of the stopwatch.

#include <REGX52.H>
sbit add0 = P1^0;
sbit add1 = P1^1;
sbit add2 = P1^2;
sbit add3 = 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 i = 0x0f,a = 0;
void main()
{
	ENLED = 0;
	add3 = 1;
	add0 = 0;
	add1 = 0;
	add2 = 0;
	TMOD = 0x01;
	TH0 = 0xb8;
	TL0 = 0x00;
	TR0 = 1;
  while(1) 
{
	if(1 == TF0)
	{
	TF0 = 0;
	TH0 = 0xb8;
	TL0 = 0x00;
		a++;
		if(a >= 50)
			{
				a = 0;
	P0 = LedChar[i];
	if(i > 0)
{
i--;
}
else 
{
i = 0x0f;
}
	}
	}	
}
}

Guess you like

Origin blog.csdn.net/2301_77479336/article/details/132839583