【Proteus仿真】定时器0作为16位计数器使用示例

【Proteus仿真】定时器0作为外部16位计数器使用示例


  • Proteus仿真
    在这里插入图片描述

功能说明

将定时器0作为外部16位计数器使用,当外部引脚P34引脚触发时,计数器0寄存器+1,然后,通过串口将定时器0寄存器值(TH0,TH0)打印出来.(平时一般用作定时器功能比较多。)

示例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/\* Main.c file generated by New Project wizard
\*
\* Created: 周五 6月 24 2022
\* Processor: AT89C51
\* Compiler: Keil for 8051
\*/

#include <reg51.h>
#include <stdio.h>
static volatile unsigned char cnt=0;
sbit led = P1^0;

/\*------------------------------------------------
MAIN C function
b,h,l来对输入字节宽的设置
一个单字节变量时要使用%bd
1.b八位
2.h十六位
3.l三十二位
%d 两个字节变量 int
%bd 单字节变量 char
%ld 四字节变量 long int
%x unsigned int
------------------------------------------------\*/
void main (void)
{
/\*--------------------------------------
Set serial port for 9600 baud at
11.0592 MHz. Note that we use Timer 1
for the baud rate generator.
--------------------------------------\*/
SCON = 0x50;
TMOD |= 0x20;
TH1 = 0xFA;
TR1 = 1;
TI = 1;

PCON |= 0x80;

printf ("\r\nCounter 0 Example Program\r\n");
printf ("\r\nStart toggling P3.4 (T0 input)\r\n\r\n");

/\*--------------------------------------
Set Timer0 for 16-bit counter mode.
Set the Timer0 Run control bit.
--------------------------------------\*/
TMOD = (TMOD & 0xF0) | 0x05;
TR0 = 1;

/\*--------------------------------------
Output the value of the counter. As you
toggle P3.4, the timer/counter 0 value
will increase.
--------------------------------------\*/
while (1)
{

if(cnt != TL0)
{
printf ("\rCounter 0 = %2.2bX%2.2bXh", TH0, TL0);//Counter 0 = 0000h
led = ! led ; //work LED flash
cnt = TL0;
}
}
}


示例代码和仿真资源

本实验基于Proteus8.12平台

1
2
3
链接:https://pan.baidu.com/s/1xNy0uIOxpSA-d-W1rZXWbA 
提取码:diqn


学会低调,懂得藏拙,大智若愚,韬光养晦,才可能赢得人生。