1、熟练掌握单片机定时器的原理和应用方法。
1)时钟周期:单片机时序中的最小单位,具体计算的方法就是时钟源分之一。
2)机器周期:我们的单片机完成一个操作的最短时间。
3)定时器:打开定时器“储存寄存器”的值经过一个机器周期自动加一,也就是说,机器周期是一个计数周期。
4)
5)
6)
7)
2,代码实现
#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、使用定时器来实现延时,完成左右移动的流水灯的程序。
#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、了解数码管的原理,掌握数码管的真值表的计算方法。
5、编程实现数码管静态显示秒表的倒计时。
#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;
}}}
}
}