|
定时计数器T0作定时应用技术(二)
<P style="MARGIN-LEFT: 1cm; TEXT-INDENT: -1cm; mso-list: l1 level1 lfo2; tab-stops: list 1.0cm">1. 实验任务
用AT89S51的定时/计数器T0产生2秒钟的定时,每当2秒定时到来时,更换指示灯闪烁,每个指示闪烁的频率为0.2秒,也就是说,开始L1指示灯以0.2秒的速率闪烁,当2秒定时到来之后,L2开始以0.2秒的速率闪烁,如此循环下去。0.2秒的闪烁速率也由定时/计数器T0来完成。 <P style="MARGIN-LEFT: 1cm; TEXT-INDENT: -1cm; mso-list: l1 level1 lfo2; tab-stops: list 1.0cm">2. 电路原理图
<P style="TEXT-ALIGN: center" align=center>图4.16.1 <P style="MARGIN-LEFT: 1cm; TEXT-INDENT: -1cm; mso-list: l1 level1 lfo2; tab-stops: list 1.0cm">3. 系统板硬件连线 <P style="MARGIN-LEFT: 36pt; TEXT-INDENT: -36pt; mso-list: l0 level1 lfo3; tab-stops: list 36.0pt">(1. 把“单片机系统”区域中的P1.0-P1.3用导线连接到“八路发光二极管指示模块”区域中的L1-L4上 <P style="MARGIN-LEFT: 1cm; TEXT-INDENT: -1cm; mso-list: l1 level1 lfo2; tab-stops: list 1.0cm">4. 程序设计内容 <P style="MARGIN-LEFT: 57pt; TEXT-INDENT: -36pt; mso-list: l1 level2 lfo2; tab-stops: list 57.0pt">(1. 由于采用中断方式来完成,因此,对于中断源必须它的中断入口地址,对于定时/计数器T0来说,中断入口地址为000BH,因此在中断入口地方加入长跳转指令来执行中断服务程序。书写汇编源程序格式如下所示:
ORG 00H
LJMP START
ORG 0BH ;定时/计数器T0中断入口地址
LJMP INT_T0
START: NOP ;主程序开始
.
.
INT_T0: PUSH ACC ;定时/计数器T0中断服务程序
PUSH PSW
.
.
POP PSW
POP ACC
RETI ;中断服务程序返回
END <P style="MARGIN-LEFT: 57pt; TEXT-INDENT: -36pt; mso-list: l1 level2 lfo2; tab-stops: list 57.0pt">(2. 定时2秒,采用16位定时50ms,共定时40次才可达到2秒,每50ms产生一中断,定时的40次数在中断服务程序中完成,同样0.2秒的定时,需要4次才可达到0.2秒。对于中断程序,在主程序中要对中断开中断。 <P style="MARGIN-LEFT: 57pt; TEXT-INDENT: -36pt; mso-list: l1 level2 lfo2; tab-stops: list 57.0pt">(3. 由于每次2秒定时到时,L1-L4要交替闪烁。采用ID来号来识别。当ID=0时,L1在闪烁,当ID=1时,L2在闪烁;当ID=2时,L3在闪烁;当ID=3时,L4在闪烁 <P style="MARGIN-LEFT: 1cm; TEXT-INDENT: -1cm; mso-list: l1 level1 lfo2; tab-stops: list 1.0cm">5. 程序框图
T0中断服务程序框图 <P align=center>
主程序框图 <P align=center>
图4.16.2 <P style="MARGIN-LEFT: 1cm; TEXT-INDENT: -1cm; mso-list: l1 level1 lfo2; tab-stops: list 1.0cm">6. 汇编源程序 6. 汇编源程序
TCOUNT2S EQU 30H
TCNT02S EQU 31H
ID EQU 32H
ORG 00H
LJMP START
ORG 0BH
LJMP INT_T0
START: MOV TCOUNT2S,#00H
MOV TCNT02S,#00H
MOV ID,#00H
MOV TMOD,#01H
MOV TH0,#(65536-50000) / 256
MOV TL0,#(65536-50000) MOD 256
SETB TR0
SETB ET0
SETB EA
SJMP $
INT_T0: MOV TH0,#(65536-50000) / 256
MOV TL0,#(65536-50000) MOD 256
INC TCOUNT2S
MOV A,TCOUNT2S
CJNE A,#40,NEXT
MOV TCOUNT2S,#00H
INC ID
MOV A,ID
CJNE A,#04H,NEXT
MOV ID,#00H
NEXT: INC TCNT02S
MOV A,TCNT02S
CJNE A,#4,DONE
MOV TCNT02S,#00H
MOV A,ID
CJNE A,#00H,SID1
CPL P1.0
SJMP DONE
SID1: CJNE A,#01H,SID2
CPL P1.1
SJMP DONE
SID2: CJNE A,#02H,SID3
CPL P1.2
SJMP DONE
SID3: CJNE A,#03H,SID4
CPL P1.3
SID4: SJMP DONE
DONE: RETI
END
7. C语言源程序
#include <AT89X51.H>
unsigned char tcount2s;
unsigned char tcount02s;
unsigned char ID;
void main(void)
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TR0=1;
ET0=1;
EA=1;
while(1);
}
void t0(void) interrupt 1 using 0
{
tcount2s++;
if(tcount2s==40)
{
tcount2s=0;
ID++;
if(ID==4)
{
ID=0;
}
}
tcount02s++;
if(tcount02s==4)
{
tcount02s=0;
switch(ID)
{
case 0:
P1_0=~P1_0;
break;
case 1:
P1_1=~P1_1;
break;
case 2:
P1_2=~P1_2;
break;
case 3:
P1_3=~P1_3;
break;
}
}
} |
|