A. 單片機控制74LS164控制數碼管顯示怎麼編程(匯編語言)
74LS164是串入並出轉換晶元,可以藉助串口來實現,也可以用普通IO口模擬實現。舉例如下:
#include<reg51.h>
#define uchar unsigned char
uchar table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f};
sbit keyup=P3^6;
sbit keydn=P3^7;
void delay()
{
int i=1000;
while(i--);
}
main()
{
uchar i;
i=0;
SCON=0x00;
TI=1;
while(1)
{
SBUF=table[i];
while(TI==0);
TI=0;
delay();
if(keyup==0)
{
i++;
while(keyup==0);
}
if(keydn==0)
{
if(i>0)i--;
while(keydn==0);
}
i%=10;
}
}
B. 51單片機中,如何編程使其按下第一個開關數顯顯示0(或1)...按下第8個開關數顯顯示7(或8)...
程序如下:
ORG0000H
;-----------------------------------------
START:
MOVA,P1;讀入8個按鍵.
MOVB,A
CPLA
JZSTART
MOVR6,#8
CALLDL_R6_MS;延時消抖.
MOVA,P1;讀入8個按鍵.
CJNEA,B,START
;-----------------------------------------
WAIT_KEY:
MOVA,P1;讀入8個按鍵.
CPLA
JNZWAIT_KEY;等待按鍵釋放.
;-----------------------------------------
MOVR2,#0;按鍵號碼.
JNBB.0,_0
JNBB.1,_1
JNBB.2,_2
JNBB.3,_3
JNBB.4,_4
JNBB.5,_5
JNBB.6,_6
;-----------------------------------------
INCR2
_6:INCR2
_5:INCR2
_4:INCR2
_3:INCR2
_2:INCR2
_1:INCR2
_0:
;-----------------------------------------
MOVA,R2
MOVDPTR,#DUAN
MOVCA,@A+DPTR;
CPLA
MOVP0,A
SJMPSTART
;----------------------------------------------
DUAN:
DB0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H
;0123456789
;----------------------------------------------
DELAY1MS:;延時:999T=1ms
MOVR7,#241
DJNZR7,$
DJNZR7,$
RET
;----------------------------------------------
DL_R6_MS:;延時:R6*1ms
ACALLDELAY1MS
DJNZR6,$-2
RET