【第九个程序-篮球计分器】
/////////////////////////////////////////////
汇编语言程序
/////////////////////////////////////////////
/***********************************************************************/
/***名称:8位数码管 ********************************************/
/***功能:依次显示0-7 ********************************************/
/***********************************************************************/
/*---------------------------------------------------------------------*/
/*-- 端口定义 ---------------------------------------*/
/*---------------------------------------------------------------------*/
PORT_3 EQU P3
PORT_2 EQU P2
PORT_1 EQU P1
PORT_0 EQU P0
KEY1 EQU P3.0
KEY2 EQU P3.1
KEY3 EQU P3.2
KEY4 EQU P3.3
/*---------------------------------------------------------------------*/
/*-- 变量定义 ---------------------------------------*/
/*---------------------------------------------------------------------*/
DEL_TEMP1 EQU R7
DEL_TEMP2 EQU R6
DEL_TEMP3 EQU R5
LOOP_TEMP EQU R4
LOOP_TEMP1 EQU R3
MOVE_NUM EQU R2
DISP_NUM1 EQU 30H
DISP_TEMP EQU 31H
DISP_TEMP1 EQU 32H
/*---------------------------------------------------------------------*/
/*-- 程序开始 ---------------------------------------*/
/*---------------------------------------------------------------------*/
ORG 0000H
SJMP MAIN
ORG 0030H
/*---------------------------------------------------------------------*/
/*-- 主程序 ---------------------------------------*/
/*---------------------------------------------------------------------*/
MAIN: MOV PORT_3,#0FFH //端口初始化
MOV PORT_2,#0FFH //端口初始化
MOV PORT_1,#00H
MOV PORT_0,#00H
MOV DISP_NUM1,#0
WORK: CALL DISPLAY
CALL KEY
SJMP WORK
/*---------------------------------------------------------------------*/
/*-- 按键子程序 ---------------------------------------*/
/*--入口地址: - -------------------------------------*/
/*---------------------------------------------------------------------*/
KEY: MOV A,PORT_3
CJNE A,#0FFH,KEY21
SJMP EXIT
KEY21: CALL DELAY_MS
CALL DELAY_MS
MOV A,PORT_3
CJNE A,#0FFH,KEY22
SJMP EXIT
KEY22: JNB KEY1,ADD1
JNB KEY2,ADD2
JNB KEY3,ADD3
JNB KEY4,SUBB1
SJMP EXIT
ADD1: CALL DISPLAY
JNB KEY1,ADD1
MOV A,DISP_NUM1
ADD A,#1
MOV DISP_NUM1,A
SJMP EXIT
ADD2: CALL DISPLAY
JNB KEY2,ADD2
MOV A,DISP_NUM1
ADD A,#2
MOV DISP_NUM1,A
SJMP EXIT
ADD3: CALL DISPLAY
JNB KEY3,ADD3
MOV A,DISP_NUM1
ADD A,#3
MOV DISP_NUM1,A
SJMP EXIT
SUBB1: CALL DISPLAY
JNB KEY4,SUBB1
MOV A,DISP_NUM1
CJNE A,#0,SUBB2
MOV DISP_NUM1,#0
SJMP EXIT
SUBB2: SUBB A,#1
MOV DISP_NUM1,A
SJMP EXIT
EXIT: RET
/*---------------------------------------------------------------------*/
/*-- 显示子程序 ---------------------------------------*/
/*--入口地址:DISP_NUM1 - -------------------------------------*/
/*---------------------------------------------------------------------*/
DISPLAY: MOV LOOP_TEMP,#4
MOV LOOP_TEMP1,#0
MOV MOVE_NUM,#0F7H
MOV DPTR,#LED_TAB
MOV DISP_TEMP1,DISP_NUM1
LOOP: MOV A,DISP_TEMP1
MOV B,#10
DIV AB
MOV DISP_TEMP,A
MOV A,B
MOVC A,@A+DPTR
MOV PORT_0,A
MOV PORT_2,MOVE_NUM
CALL DELAY_MS //延时1S
MOV PORT_0,#00H //消鬼影
MOV PORT_2,#0FFH //消鬼影
MOV A,MOVE_NUM
RR A
MOV MOVE_NUM,A
INC LOOP_TEMP1
MOV DISP_TEMP1,DISP_TEMP
DJNZ LOOP_TEMP,LOOP
RET
/*---------------------------------------------------------------------*/
/*-- 1s延时程序 ---------------------------------------*/
/*---------------------------------------------------------------------*/
DELAY_1S:MOV DEL_TEMP1,#8
DEL1: MOV DEL_TEMP2,#250
DEL2: MOV DEL_TEMP3,#248
DEL3: DJNZ DEL_TEMP3,DEL3
DJNZ DEL_TEMP2,DEL2
DJNZ DEL_TEMP1,DEL1
RET
/*---------------------------------------------------------------------*/
/*-- MS延时程序 ---------------------------------------*/
/*---------------------------------------------------------------------*/
DELAY_MS:MOV DEL_TEMP1,#10
DEL22: MOV DEL_TEMP3,#10
DJNZ DEL_TEMP3,$
DJNZ DEL_TEMP2,DEL22
RET
/*---------------------------------------------------------------------*/
/*-- LED显示数组 ---------------------------------------*/
/*---------------------------------------------------------------------*/
LED_TAB:
DB 3Fh,06h,5Bh,4Fh,66h,6Dh,7Dh,07h,7Fh,6Fh,77h,7Ch,39h,5Eh,79h,71h
/*---------------------------------------------------------------------*/
/*-- 程序结束 ---------------------------------------*/
/*---------------------------------------------------------------------*/
END
/////////////////////////////////////////////
C语言程序
/////////////////////////////////////////////
/***********************************************************************/
/***名称:篮球计分器 ********************************************/
/***功能: 按《加1分》显示数字加1 按《加2分》显示数字加2 ****/
/*按《加3分》显示数字加3 按《减1分》显示数字减1 ****/
/***********************************************************************/
#include
typedef unsigned char U8;
typedef unsigned int U16;
/*---------------------------------------------------------------------*/
/*-- 端口定义 ---------------------------------------*/
/*---------------------------------------------------------------------*/
#define PORT_3 P3
#define PORT_2 P2
#define PORT_1 P1
#define PORT_0 P0
sbit KEY1=P3^0;
sbit KEY2=P3^1;
sbit KEY3=P3^2;
sbit KEY4=P3^3;
/*---------------------------------------------------------------------*/
/*-- 其他定义 ---------------------------------------*/
/*---------------------------------------------------------------------*/
U8 code LED_TEB[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; //LED数组
U16 DISP_NUM1;
/*---------------------------------------------------------------------*/
/*-- 延时子程序 ---------------------------------------*/
/*---------------------------------------------------------------------*/
void DELAY_1MS(U16 z)
{
U16 x,y;
for(x=z;x>0;x--)
for(y=123;y>0;y--);
}
/*---------------------------------------------------------------------*/
/*-- 显示子程序 ---------------------------------------*/
/*---------------------------------------------------------------------*/
void display(U16 DISP_NUM)
{
U8 i,j;
i=0x08;
for(j=0;j<4;j++)
{
PORT_0=LED_TEB[DISP_NUM%10];
PORT_2=~i;
DELAY_1MS(5);
PORT_0=0X00;
PORT_2=0XFF;
i=i>>1;
DISP_NUM/=10;
}
}
/*---------------------------------------------------------------------*/
/*-- 按键子程序 ---------------------------------------*/
/*---------------------------------------------------------------------*/
void KEY()
{
if(PORT_3!=0xff)
{
DELAY_1MS(20);
if(PORT_3!=0xff)
{
if(KEY1==0)
{
while(!KEY1)
{
display(DISP_NUM1);
}
DISP_NUM1+=1;
}
if(KEY2==0)
{
while(!KEY2)
{
display(DISP_NUM1);
}
DISP_NUM1+=2;
}
if(KEY3==0)
{
while(!KEY3)
{
display(DISP_NUM1);
}
DISP_NUM1+=3;
}
if(KEY4==0)
{
while(!KEY4)
{
display(DISP_NUM1);
}
if(DISP_NUM1>0)
DISP_NUM1-=1;
else
DISP_NUM1=0;
}
}
}
}
/*---------------------------------------------------------------------*/
/*-- 主程序 ---------------------------------------*/
/*---------------------------------------------------------------------*/
void main()
{
PORT_3=0XFF;
PORT_2=0XFF;
PORT_1=0X00;
PORT_0=0X00;
DISP_NUM1=0;
while(1)
{
display(DISP_NUM1);
KEY();
}
}
/*---------------------------------------------------------------------*/
/*-- 程序结束 ---------------------------------------*/
/*---------------------------------------------------------------------*/