/**************************************************
实验名称:定时器/计数器提高实验一
实验目的:利用定时器0工作方式0,实现第一个发光管
以1s闪烁。
实验备注:
**************************************************/
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit P10 = P1^0;
uchar num;
void main()
{
num = 0;
P10 = 0;
TMOD = 0;
TH0 = (8192-4608)/32;
TL0 = (8192-4608)%32;
EA = 1;
ET0 = 1;
TR0 = 1;
while(1)
{
if(num == 200)
{
num = 0;
P10 = ~P10;
}
}
}
void timer0() interrupt 1
{
TH0 = (8192-4608)/32;
TL0 = (8192-4608)%32;
num++;
}
/**************************************************
实验名称:定时器/计数器提高实验二
实验目的:利用定时器0工作方式2,实现第一个发光管
以1s闪烁。
实验备注:
**************************************************/
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit P10 = P1^0;
uint num;
void main()
{
P10 = 0xfe;
TMOD = 0x02;
TH0 = 0;
TL0 = 0;
EA = 1;
ET0 = 1;
TR0 = 1;
while(1)
{
if(num == 3600)
{
num = 0;
P10 = ~P10;
}
}
}
void Timer0() interrupt 1
{
num++;
}
/**************************************************
实验名称:定时器/计数器提高实验三
实验目的:利用定时器0工作方式3,利用TL0实现第一个数
码管以0.5s闪烁,利用TH0实现第二个数码管以
1s闪烁。
实验备注:
**************************************************/
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit P10 = P1^0;
sbit P11 = P1^1;
uint num_0,num_1;
void main()
{
TMOD = 0x03;
TH0 = 0;
TL0 = 0;
EA = 1;
ET0 = 1;
ET1 = 1;
TR0 = 1;
TR1 = 1;
P10 = 0;
P11 = 0;
num_0 = 0;
num_1 = 0;
while(1)
{
if(num_0 == 1800)
{
num_0 = 0;
P10 = ~P10;
}
if(num_1 == 3600)
{
num_1 = 0;
P11 = ~P11;
}
}
}
void Timer0() interrupt 1
{
num_0++;
}
void Timer1() interrupt 3
{
num_1++;
}
/**************************************************
实验名称:定时器/计数器提高实验四
实验目的:利用定时器0工作方式1,用一根导线连接GND,
另一端接触T0(P3.4)引脚,接触一次计数器
记一次数。数值实时显示在数码管前两位,
计满清零。
实验备注:实验失败,程序只显示00,用导线一端连接GND
另一端接触P3.4没反应。
**************************************************/
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit DUAN = P2^6;
sbit WEI = P2^7;
uchar code duan_table[]=
{0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
uchar code wei_table[]=
{0xfe,0xfd,0xfb,
0xf7,0xef,0xdf};
uint num, shi, ge;
uint read();
void delay_1ms(uchar);
void display(uchar, uchar);
void main()
{
TMOD = 0x05;
TH0 = 0;
TL0 = 0;
// EA = 1;
// ET0 = 0;
TR0 = 0;
while(1)
{
num = read();
if(num >= 100)
{
num = 0;
TH0 = 0;
TL0 = 0;
}
shi = num/10;
ge = num%10;
display(shi,ge);
}
}
uint read()
{ uint th0,th1,tl0;
int sum;
while(1)
{
th0 = TH0;
tl0 = TL0;
th1 = TH0;
if(th0 == th1)
break;
}
sum = th0*256+tl0;
return sum;
}
void delay_1ms(uchar x)
{
uchar i,j;
for(i = 0; i < x; i++)
for(j = 0; j < 110; j++);
}
void display(uchar shi, uchar ge)
{
DUAN = 1;
P0 = duan_table[shi];
DUAN = 0;
P0 = 0xff;
WEI = 1;
P0 = wei_table[0];
WEI = 0;
delay_1ms(1);
DUAN = 1;
P0 = duan_table[ge];
DUAN = 0;
P0 = 0xff;
WEI = 1;
P0 = wei_table[1];
WEI = 0;
delay_1ms(1);
}