【单片机】脉冲计数(定时/计数器实验)
- 一、操作目的
- 二、操作内容
- 三、程序框图
- 四、程序接线图
- 五、程序清单
- 六、代码详解
一、操作目的
- 熟悉单片机内部定时/计数器功能,掌握初始化编程方法。
二、操作内容
- 把定时器0外部输入的脉冲进行计数,并送显示器显示。
三、程序框图
四、程序接线图
五、程序清单
ORG 0000H
LJMP SE15
ORG 06E0H
SE15:
MOV SP,
MOV P2,
MOV A,
MOV DPTR,
MOVX @DPTR,A
MOV TMOD,
MOV TH0,
MOV TL0,
SETB TR0
LO29:
MOV R2,TH0
MOV R3,TL0 ;取出TH0,TL0内容
LCALL ZOY0
MOV R0,
MOV A,R6
LCALL PTDS
MOV A,R5
LCALL PTDS
MOV A,R4
LCALL PTDS ;送显示缓冲区
LCALL SSEE ;显示
SJMP LO29 ;循环计数
ZOY0:
CLR A
MOV R4,A
MOV R5,A
MOV R6,A ;0送R4 R5 R6
MOV R7,
LO30:
CLR C ;R2 R3左移,移出的
MOV A,R3 ;最高位送CY
RLC A
MOV R3,A
MOV A,R2
RLC A
MOV R2,A
MOV A,R6
ADDC A,R6 ;(R4 R5 R6)*2送R4 R5 R6
DA A ;十进制调整
MOV R6,A
MOV A,R5
ADDC A,R5
DA A
MOV R5,A
MOV A,R4
ADDC A,R4
DA A
MOV R4,A
DJNZ R7,LO30 ;循环16次
RET
PTDS:
MOV R1,A ;拆送显示缓冲区
ACALL PTDS1
MOV A,R1
SWAP A
PTDS1:
ANL A,
MOV @R0,A
INC R0
RET
SSEE:
SETB RS1 ;换工作区
MOV R5,
SSE2:
MOV 30H,
MOV 31H,
MOV R7,
SSE1:
MOV R1,
MOV A,30H
CPL A
MOVX @R1,A ;字位送入
MOV R0,31H
MOV A,@R0
MOV DPTR,
MOVC A,@A+DPTR ;取字形代码
MOV R1,
MOVX @R1,A ;字形送入
MOV A,30H ;右移
RR A
MOV 30H,A
DEC 31H
MOV A,
MOVX @R1,A ;关显示
DJNZ R7,SSE1 ;6位显示完了吗?
DJNZ R5,SSE2 ;5次显示完了吗?
CLR RS1
RET
DDFF:
DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H
DB 88H,83H,0C6H,0A1H,86H,8EH,0FFH,0CH,89H,0DEH
END
六、代码详解
#include
#include
#define uchar unsigned char
#define com8255 XBYTE[0xff23]
#define pa8255 XBYTE[0xff20]
#define pb8255 XBYTE[0xff21]
uchar code table[10] = { 0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90 };
uchar code wei[] = { 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
unsigned int number = 0;
unsigned int temp = 0;
void delay(unsigned int i)
{
unsigned int j, k;
for (k = 0; k < i; k++)
for (j = 0; j < 100; j++)
;
}
void main(void)
{
uchar m = 0;
com8255 = 0x80;
pa8255 = 0x00;
pb8255 = 0x00;
TMOD = 0x15;
TH0 = 0x00;
TL0 = 0x00;
TH1 = 0x3c;
TL1 = 0xb0;
EA = 1;
ET0 = 0;
TR0 = 1;
ET1 = 1;
TR1 = 1;
delay(2);
while (1)
{
pb8255 = table[(number / 1) % 10];
pa8255 = ~wei[5];
pb8255 = table[(number / 10) % 10];
pa8255 = ~wei[4];
pb8255 = table[(number / 100) % 10];
pa8255 = ~wei[3];
pb8255 = table[(number / 1000) % 10];
pa8255 = ~wei[2];
pb8255 = table[(number / 10000) % 10];
pa8255 = ~wei[1];
pb8255 = table[(number / 100000) % 10];
pa8255 = ~wei[0];
}
}
void Dealy(void) interrupt 3 using 0
{
TH1 = 0x3c;
TL1 = 0xb0;
temp = TH0;
temp = temp * 256;
number = TL0;
number = number + temp;
TH0 = 0x00;
TL0 = 0x00;
TF0 = 0;
}