微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 基于51单片机的数字音乐盒C语言驱动程序

基于51单片机的数字音乐盒C语言驱动程序

时间:11-28 来源:互联网 点击:
SPKEQUP3.7; 蜂鸣器所在端口

COUNT50EQU39H
SECEQUR0
MINEQUR1
SEGEQUP2
NUMEQUP0
SEG7EQU30H
SEG6EQU31H
SEG5EQU32H
SEG4EQU33H
SEG3EQU36H
SEG1EQU 3AH

DLAYEQU34H
H_CNT1EQU37H
H_CNT2EQU38H
FLAGBIT 00H

KEYVALEQU40H
XUN1EQU41H
XUN2EQU42H
MUSICEQU43H
ORG0000H
LJMPMAIN
ORG0003H
LJMP PLY_PAU
ORG0013H
LJMP STOP
ORG000BH;定时0中断服务程序
LJMP COUNT

;------------------------------------------------------
ORG100H
MAIN:MOVSP,#60H;初始化堆栈
MOVSEC,#0H;初始化秒
MOVMIN,#0H;初始化分
MOVCOUNT50,#50;一秒定时
CLRFLAG


MOVIE,#8FH
MOVTMOD,#11H
MOVTH0,#0A5H
MOVTL0,#0FFH
MOVTH1,#0EDH
MOVTL1,#0FFH
SETBIT0
SETBIT1

MOVKEYVAL,#00H
MOVMUSIC,#01H
MOVDPTR,#TABLE1
MOV37H,#0FFH
MOV38H,#05H

;**************欢迎界面**********************
HELLO:MOV30H,#7FH
MOV31H,#0BFH
MOV32H,#0DFH
MOV33H,#0EFH
MOV36H,#0F7H
MOV 3AH,#0FDH
MOVP2,SEG3;H
MOVP0,#89H
LCALL LDELAY

MOVP2,SEG4;E
MOVP0,#86H
LCALL LDELAY

MOVP2,SEG5;L
MOVP0,#0C7H
LCALL LDELAY

MOVP2,SEG6;L
MOVP0,#0C7H
LCALL LDELAY

MOVP2,SEG7;O
MOVP0,#0C0H
LCALL LDELAY

MOVA,KEYVAL
JNZKEYJDG
DJNZ H_CNT1, HELLO
DJNZ H_CNT2,HELLO
;==========================================================
SETBTR0
LJMP SING
KEYGET:LCALL KEY
MOVA,KEYVAL
JZKEYGET
KEYJDG:CJNE A,#12H,SECON
FIRST1:MOVDPTR,#TABLE1
MOVMUSIC,#01H
MOVKEYVAL,#00H
;SETBFLAG
MOVSEC,#00H
MOVMIN,#00H
MOVCOUNT50,#50
SETBTR0

LJMP SING
SECON:CJNE A,#11H,THIR
SECON1:MOVDPTR,#TABLE2
MOVMUSIC,#02H
MOVKEYVAL,#00H
;SETBFLAG
MOVSEC,#00H
MOVMIN,#00H
MOVCOUNT50,#50
SETBTR0
LJMP SING
THIR:CJNE A,#0FH,FORTH
THIR1:MOVDPTR,#TABLE3
MOVMUSIC,#03H
MOVKEYVAL,#00H
;SETBFLAG
MOVSEC,#00H
MOVMIN,#00H
MOVCOUNT50,#50
SETBTR0
LJMP SING
FORTH:CJNE A,#0BH,UP
FORTH1:MOVDPTR,#TABLE4
MOVMUSIC,#04H
MOVKEYVAL,#00H
;SETBFLAG
MOVSEC,#00H
MOVMIN,#00H
MOVCOUNT50,#50
SETBTR0
LJMP SING
UP:CJNE A,#02H,DOWN
MOVKEYVAL,#00H
MOVR5,MUSIC
CJNE R5,#00H,N1
LJMP KEYGET
N1:CJNE R5,#01H,N2
LJMP FORTH1
N2:CJNE R5,#02H,N3
LJMP FIRST1
N3:CJNE R5,#03H,N4
LJMP SECON1
N4:LJMP THIR1
DOWN:; LCALL VLDELAY
MOVR5,MUSIC
MOVKEYVAL,#00H
CJNE R5,#00H,M1
LJMP KEYGET
M1:CJNE R5,#01H,M2
LJMP SECON1
M2:CJNE R5,#02H,M3
LJMP THIR1
M3:CJNE R5,#03H,M4
LJMP FORTH1
M4:LJMP FIRST1
;-----------------------------------------------------------
KEY: PUSH ACC
MOVP1,#0F0H;置列线为0,行线为1 ;^^^^^^^^^^^^
MOVA,P1
ANLA,#0F0H
MOVB,A
MOVP1,#0FH;置列线为1,行线为0
MOVA,P1
ANLA,#0FH
ORLA,B
CJNE A,#0FFH,UUU
AJMP QICH
UUU:CLRC
SUBB A,#0DCH
JNCHAHA
QICH:CLRA

HAHA:MOVKEYVAL,A

POP ACC
RET
;-------------------------------------------------------------



;-------------------------------------------------------------
;**************定时中断服务程序***********************
COUNT:MOVTH0,#0A5H
MOVTL0,#0FFH
LCALL KEY
JNBFLAG,RETURN
DJNZ COUNT50,RETURN ;是否到一秒
;*****一秒处理****************
MOVCOUNT50,#50
INCSEC
CJNE SEC,#60,RETURN
MOVSEC,#0H
INCMIN
CJNE MIN,#100,RETURN ;一百分钟以后自动清零
MOVMIN,#00H
RETURN: LCALL DISPLAY
RETI
;**************************************************
;**************显示子程序*******************
DISPLAY:PUSHACC
PUSHB
PUSH DPH
PUSH DPL



MOVA,MIN
MOVB,#0AH
divAB
MOVSEG,SEG4
LCALL TODIS
LCALL DELAY

MOVSEG,SEG5
MOVA,B
MOVDPTR,#TABLE
MOVC A,@A+DPTR
ANL A,#7FH
MOVNUM,A
LCALL DELAY

MOVA,SEC
MOVB,#10
divAB
MOVSEG,SEG6
LCALL TODIS
LCALL DELAY

MOVSEG,SEG7
MOVA,B
LCALL TODIS
LCALL DELAY

MOVA,MUSIC
MOVSEG,SEG1
LCALL TODIS


POP DPL
POP DPH
POPB
POPACC

RET
;******数字到字型*****************
TODIS: PUSH DPH
PUSH DPL

MOVDPTR,#TABLE
MOVC A,@A+DPTR
MOVNUM,A

POP DPL
POP DPH

RET
;******************************************
;*********短延时子程序1.38毫秒*****************
DELAY:PUSHACC

MOVDLAY,#01H
LOOP:MOVACC,#0FFH
DJNZ ACC,$
DJNZ DLAY,LOOP

POPACC
RET
;******************************************
;*********长延时子程序24.9毫秒*****************
LDELAY:PUSHACC

MOVDLAY,#01H
LLOOP:MOVACC,#0FDH
DJNZ ACC,$
DJNZ DLAY,LLOOP

POPACC
RET
;******************************************

;****************外部中断0服务程序(播放/暂停)***************************
PLY_PAU:
SETBTR0

CPL FLAG
RETI
;***************外部中断1服务程序(停止)***********************************
STOP:

MOVSEC,#00H
MOVMIN,#00H
MOVCOUNT50,#50
CLR FLAG
MOV R3,#02H
; CLR TR0;*******
RETI
;**************************************************

;==============================================================
SING:MOVR3,#02H
NEXT:
MOVA,KEYVAL
JZPPB
LJMPKEYJDG
PPB:JNBFLAG,NEXT ;?

MOV A,R3

CJNEA,#00H,XIA
INCDPH
XIA:MOVC A,@A+DPTR ;?
JZSTART
MOVR7,A
INCR3
MOVA,R3
MOVC A,@A+DPTR
MOVR2,A
ACALL SONG
INCR3
SJMP NEXT
START:CLR FLAG
LJMPKEYGET

;===============================================================================
;===歌曲播放子程序 ===
SONG:PUSHDPH
PUSHDPL
MOVA,R2; 取出节拍
RL A
; RL A
JNZKEEP
MOVA,#01H
KEEP:MOVR2,A

REPEAT:ACALL EIGHTH
DJNZ R2,REPEAT
SETBSPK
MOVR6,#05H
YU:MOVR4,0FFH
ACALL SDELAY
DJNZ R6,YU
POPDPL
POPDPH
RET

;===============================================================================
;===产生1/8拍延时子程序===
EIGHTH:CJNE R7,#20H,NEXT1
MOVR5,15H
Y3:MOVR4,0FAH
Y2:MOVR6,#09H
NOP
Y1:DJNZ R6,Y1
DJNZR4,Y2
DJNZ R5,Y3
RET
NEXT1:MOVA,R7; 查表取出廷时参数,保存到R4
MOVDPTR,#DELAY_T
MOVC A,@A+DPTR
MOVR4,A
MOVA,R7; 查表取出1/8拍周期数,保存到R5
MOVDPTR,#S_PARA
MOVCA,@A+DPTR
MOVR5,A
NEXTCYC:ACALL SOUND
DJNZ R5,NEXTCYC
RET
;===============================================================================
;===发声子程序 ===
SOUND:SETBSPK
ACALL SDELAY
CLR SPK
ACALL SDELAY
RET

;===============================================================================
;===延时子程序===
SDELAY:
MOVA,R4; 廷时值在R4内
MOVXUN1,A
XL2:MOVXUN2,#03H
DL1:DJNZ XUN2,DL1
DJNZ XUN1,XL2
RET
;===============================================================================
;===1/8拍周期表===
S_PARA:DB10H,12H,14H,16H,18H,1BH,1FH
DB21H,24H,29H,2CH,31H,37H,3EH
DB3FH,45H,52H,57H,62H,6EH,7BH

;===============================================================================
;===延时参数表===
DELAY_T:DB0C3H,0AEH,9BH,92H,83H,74H,68H;00-06
DB62H,57H,4DH,49H,41H,3AH,34H;07-0DH
DB31H,2EH,27H,24H,20H,1DH,19H;0E-14H

;===============================================================================
;**************************************************
TABLE:DB0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H,88H,83H,0C6H,0A1H,86H,83H

;===歌曲表===
TABLE1:DW0000H,0504H,0806H,0802H,0802H,0802H,0702H,0802H,0906H,0702H,0506H
DW0502H,0A04H,0804H,0302H,0302H,0402H,0402H,0504H,0504H,0504H,2004H
DW0502H,0806H,0902H,0A02H,0902H,0801H,0701H,0601H,0501H,0406H,0602H,0804H,0902H
DW0802H,0506H,0602H,0502H,0402H,0202H,0302H,0104H,0102H,2004H
DW0504H,0806H,0802H,0802H,0802H,0702H,0802H,0B06H,0A01H,0901H,0906H
DW0902H,0A04H,0804H,0302H,0302H,0402H,0402H,0504H,0504H,0504H,2004H
DW0502H,0806H,0902H,0A02H,0902H,0801H,0701H,0601H,0501H,0406H,0602H,0804H,0902H
DW0802H,0506H,0602H,0502H,0402H,0202H,0302H,0104H,2002H
DW0502H,0806H,0902H,0A02H,0902H,0801H,0701H,0601H,0501H,0406H,0602H,0804H,0902H
DW0802H,0506H,0602H,0502H,0402H,0202H,0302H,0106H,2004H
DW0000H
TABLE2:
DW0000H,0901H,0A01H,0B02H,0B02H,0B02H,0702H,0801H,0702H,0904H,2002H;人生路上甜苦和喜忧
DW0801H,0702H,0702H,0502H,0701H,0502H,0403H,2004H ;

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top