大家好,又见面了,我是你们的朋友全栈君。
一、实验目的 1、掌握单片机定时/计数器的使用方法。 2、掌握定时/计数器编程方法。
二、实验设备及软件 1、PC机 2、KEIL51 3、PROTEAUS
三、实验任务 1、循环点亮流水等D1-D2-D3…D8, 延时采用定时器T0定时100ms 2、循环点亮流水等D1-D2-D3…D8, 延时采用定时器T1定时1s
四、仿真电路
五、实验源程序 1)T0定时100毫秒:
#include<reg51.h>
#include<intrins.h>
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
P0=_crol_(P0,1);
}
int main()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
P0=0xfe;
while(1);
}
2)T1定时1秒:
#include<reg51.h>
#include<intrins.h>
unsigned int count=0;
void timer0() interrupt 3
{
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
count++;
}
int main()
{
TMOD=0x50;
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
EA=1;
ET1=1;
TR1=1;
P0=0xfe;
while(1)
{
if(count==20)
{
count=0;
P0=_crol_(P0,1);
}
}
}
六、思考题:利用8051定时/计数器设计一个计时秒表,计时时间0~15秒,十六进制在一个数码管显示输出。
代码:
#include<reg51.h>
#include<intrins.h>
code char tab[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
//共阳极的接法计数
unsigned int count=0; //定时器计数
unsigned int number=0; //数码管计数
sbit p1_0=P1^0;
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
}
int main()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
while(1)
{
if(count==20)
{
count=0;
p1_0=1; //开关处于闭合状态
P0=tab[number];
number++;
if(number==16)
{
number=0;
}
}
}
}
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/158846.html原文链接:https://javaforall.cn
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有