DIY编程器网

标题: 可预置可逆4位计数器 [打印本页]

作者: liyf    时间: 2014-10-5 10:30
标题: 可预置可逆4位计数器
可预置可逆4位计数器

<P style="MARGIN-LEFT: 18pt; TEXT-INDENT: -18pt; mso-list: l3 level1 lfo2; tab-stops: list 18.0pt">1. 实验任务
利用AT89S51单片机的P1.0-P1.3接四个发光二极管L1-L4,用来指示当前计数的数据;用P1.4-P1.7作为预置数据的输入端,接四个拨动开关K1-K4,用P3.6/WR和P3.7/RD端口接两个轻触开关,用来作加计数和减计数开关。具体的电路原理图如下图所示 <P style="MARGIN-LEFT: 18pt; TEXT-INDENT: -18pt; mso-list: l3 level1 lfo2; tab-stops: list 18.0pt">2. 电路原理图
<P style="TEXT-ALIGN: center" align=center>图4.12.1
3. 系统板上硬件连线 <P style="MARGIN-LEFT: 36pt; TEXT-INDENT: -36pt; mso-list: l0 level1 lfo3; tab-stops: list 36.0pt">(1. 把“单片机系统”区域中的P1.0-P1.3端口用8芯排线连接到“八路发光二极管指示模块”区域中的L1-L4上;要求:P1.0对应着L1,P1.1对应着L2,P1.2对应着L3,P1.3对应着L4; <P style="MARGIN-LEFT: 36pt; TEXT-INDENT: -36pt; mso-list: l0 level1 lfo3; tab-stops: list 36.0pt">(2. 把“单片机系统”区域中的P3.0/RXD,P3.1/TXD,P3.2/INT0,P3.3/INT1用导线连接到“四路拨动开关”区域中的K1-K4上; <P style="MARGIN-LEFT: 36pt; TEXT-INDENT: -36pt; mso-list: l0 level1 lfo3; tab-stops: list 36.0pt">(3. 把“单片机系统”区域中的P3.6/WR,P3.7/RD用导线连接到“独立式键盘”区域中的SP1和SP2上; <P style="MARGIN-LEFT: 18pt; TEXT-INDENT: -18pt; mso-list: l3 level1 lfo2; tab-stops: list 18.0pt">4. 程序设计内容 <P style="MARGIN-LEFT: 36pt; TEXT-INDENT: -36pt; mso-list: l4 level1 lfo4; tab-stops: list 36.0pt">(1. 两个独立式按键识别的处理过程; <P style="MARGIN-LEFT: 36pt; TEXT-INDENT: -36pt; mso-list: l4 level1 lfo4; tab-stops: list 36.0pt">(2. 预置初值读取的问题 <P style="MARGIN-LEFT: 36pt; TEXT-INDENT: -36pt; mso-list: l4 level1 lfo4; tab-stops: list 36.0pt">(3. LED输出指示 <P style="MARGIN-LEFT: 18pt; TEXT-INDENT: -18pt; mso-list: l3 level1 lfo2; tab-stops: list 18.0pt">5. 程序框图
 
 
 
 
 
 
 
 
 
 
 
  <P style="TEXT-ALIGN: center" align=center>图4.12.2 6. 汇编源程序
COUNT EQU 30H
ORG 00H
START: MOV A,P3
ANL A,#0FH
MOV COUNT,A
MOV P1,A
SK2: JB P3.6,SK1
LCALL DELY10MS
JB P3.6,SK1
INC COUNT
MOV A,COUNT
CJNE A,#16,NEXT
MOV A,P3
ANL A,#0FH
MOV COUNT,A
NEXT: MOV P1,A
WAIT: JNB P3.6,WAIT
LJMP SK2
SK1: JB P3.7,SK2
LCALL DELY10MS
JB P3.7,SK2
DEC COUNT
MOV A,COUNT
CJNE A,#0FFH,NEX
MOV A,P3
ANL A,#0FH
MOV COUNT,A
NEX: MOV P1,A
WAIT2: JNB P3.7,WAIT2
LJMP SK2
DELY10MS: MOV R6,#20
MOV R7,#248
D1: DJNZ R7,$
DJNZ R6,D1
RET
END
7. C语言源程序
#include

unsigned char curcount;

void delay10ms(void)
{
unsigned char i,j;
for(i=20;i>0;i--)
for(j=248;j>0;j--);
}


void main(void)
{
curcount=P3 & 0x0f;
P1=~curcount;
while(1)
{
if(P3_6==0)
{
delay10ms();
if(P3_6==0)
{
if(curcount>=15)
{
curcount=15;
}
else
{
curcount++;
}
P1=~curcount;
while(P3_6==0);
}
}
if(P3_7==0)
{
delay10ms();
if(P3_7==0)
{
if(curcount<=0)
{
curcount=0;
}
else
{
curcount--;
}
P1=~curcount;
while(P3_7==0);
}
}
}
}
作者: 李小路    时间: 2021-9-12 19:52
谢谢分享




欢迎光临 DIY编程器网 (http://diybcq.com./) Powered by Discuz! X3.2