实验四:定时器实验

一、实验目的
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;
               }
          }
     }
}

  • 5
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值