單片機開發商深圳英銳恩分享PIC單片機實現音樂播放。
PIC單片機實現音樂播放 *
********************************
源程序:
以下程序要用二個定時器資源,凡是有二個定時器的PIC單片機均可實現,該范例需要的MCU是MICROCHIP PIC16C62
INCLUDE "D:\PIC\P16XX.EQU" ;該文件在MICROCHIP光盤中可找到
;**************************************************
#define BeepOut RC,4
;**************************************************
W_TEMP EQU 0X20 ;(0XA0)
STATUS_TEMP EQU 0X21
BeepCnt equ 30h
TmrBak equ 31h
BeepMode equ 32h
Sflag equ 33h
;**************************************************
CSTIME100MS equ .8;
;**************************************************
;Sflag
Fg_100ms equ 0
FgBeep equ 1
;**************************************************
ORG 000H;
GOTO MAIN ; Skip over interrupt vecter
ORG 04H ; Interrupt Vector
GOTO INTZ
;**************************************************
OkTab:
movf BeepCnt,w
addwf PCL,f
retlw .255-.130;0xf6 ;1 ;
retlw .255-.126;0xfa ;2 ;
retlw .255-.119; 0xfd ;3 ;
retlw 0
;**************************************************
WhisleTab:
movf BeepCnt,w
addwf PCL,f
retlw .255-.239;523Hz ;0 ;
retlw .255-.179;698Hz ;1 ;
retlw .255-.159;784Hz ;2 ;
retlw .255-.119;1046Hz ;3 ;
retlw .0
;**************************************************
HangTab:
movf BeepCnt,w
addwf PCL,f
retlw .255-.159; 784Hz ;0 ;
retlw .255-.119; 1046Hz ;1 ;
retlw .255-.150; 830Hz ;2 ;
retlw .255-.112 ;1109Hz ;3 ;
retlw .255-.142 ;880Hz ;4 ;
retlw .255-.106;1174Hz ;5 ;
retlw .255-.134;932Hz ;6 ;
retlw .255-.100;1244Hz ;7
retlw .255-.126;988Hz ;8;
retlw .255-.94;1318Hz ;9;
retlw .255-.119;1046Hz ;10 ;
retlw .255-.89;1397Hz ;11 ;
retlw .0
;**************************************************
WelcomTab:
movf BeepCnt,w
addwf PCL,f
retlw .255-.89;1397Hz ;11 ;
retlw .255-.119;1046Hz ;10 ;
retlw .255-.94;1318Hz ;9;
retlw .255-.126;988Hz ;8;
retlw .255-.100;1244Hz ;7
retlw .255-.134;932Hz ;6 ;
retlw .255-.106;1174Hz ;5 ;
retlw .255-.142 ;880Hz ;4 ;
retlw .255-.112 ;1109Hz ;3 ;
retlw .255-.150; 830Hz ;2 ;
retlw .255-.119; 1046Hz ;1 ;
retlw .255-.159; 784Hz ;0 ;
retlw .0
;**************************************************
BeepModeJmp:
addwf PCL,f
b psWhisle ;0
b psOk ;1
b psHang ;2
b psWelcom ;3
;**************************************************
IO_SET:
BANK1_
MOVLW B'11001011'
MOVWF TRISA
MOVLW B'00001100'
movwf TRISB
MOVLW B'00000011'
movwf TRISC ; Set Port_C to all outputs
BANK0_
RETURN
;**************************************************
SYS_SET:
BANK1_
MOVLW B'00000111' ; 1:256 TMR0分頻
MOVWF OPTION_R
BSF PIE1,TMR2IE ;TMR2中斷允許
BANK0_
MOVLW B'00000001' ;開TMR1
MOVWF T1CON
bsf INTCON,TOIE ;TMR0中斷允許
BSF INTCON,PEIE ;允許所有未被屏蔽之外圍接口中斷
RETURN
;**************************************************
MAIN: ; Main rotation
CALL IO_SET
MOVLW B'00000000' ;關所有中斷
MOVWF INTCON
;****************************
CALL SYS_SET
;****************************
call PlayPsWelcom ;上電提示音
MAINLOOP:
bsf INTCON,GIE ;開所有中斷
CLRWDT ; Clear WDT
call BeepFor
B MAINLOOP
;**************************************************
INTZ:
PUSH ; Push ;中斷服務程序
BTFSC PIR1,TMR2IF ;測試TMR2中斷標志位
b INT_TMR2 ;
BTFSC INTCON,TOIF ;測試TMR0中斷標志位
GOTO INT_TMR0
IntRet:
POP ; Pop
RETFIE
;**************************************************
INT_TMR2:
BCF PIR1,TMR2IF ;清TMR2中斷標志位
movf TmrBak,w
movwf TMR2
CPL BeepOut
BeeperEnd:
b IntRet
;**************************************************
INT_TMR0: ;定時0中斷
BCF INTCON,TOIF ; clear INTF
MOVLW .255-.38 ;10ms
MOVWF TMR0
;====================
decfsz T100ms,f
b int_tmr0_ret
movlw CSTIME100MS
movwf T100ms
bsf Sflag,Fg_100ms
;=====================
int_tmr0_ret:
b IntRet
;**************************************************
PlayPsWhisle:
bsf Fg,FgBeep
clrf BeepCnt
movlw .0
movwf BeepMode
movlw CSTIME100MS
movwf T100ms
retlw 0
;**************************************************
PlayPsOk:
bsf Fg,FgBeep
clrf BeepCnt
movlw .1
movwf BeepMode
movlw CSTIME100MS
movwf T100ms
retlw 0
;**************************************************
PlayPsHang:
bsf Fg,FgBeep
clrf BeepCnt
movlw .2
movwf BeepMode
movlw CSTIME100MS
movwf T100ms
retlw 0
;**************************************************
PlayPsWelcom:
bsf Fg,FgBeep
clrf BeepCnt
movlw .3
movwf BeepMode
movlw CSTIME100MS
movwf T100ms
retlw 0
;**************************************************
;音樂播放程序
;BeepMode=0 : psWhisle
BeepMode=1 : psOk
;BeepMode=2 : psHang
;BeepMode=3 : psWelcom
;**************************************************
BeepFor:
btfss Sflag,Fg_100ms
b BeepForEnd
bcf Sflag,Fg_100ms
btfss Sflag,FgBeep
b BeepForEnd
MOVLW B'00000101' ;開TMR2 1:4分頻
MOVWF T2CON
movf BeepMode,w
b BeepModeJmp
psWhisle:
call WhisleTab
b BeepPlay
psOk:
call OkTab
b BeepPlay
psHang:
call HangTab
b BeepPlay
psWelcom:
call WelcomTab
b BeepPlay
BeepPlay:
incf BeepCnt,f
movwf TmrBak
movf TmrBak,f
btfsc status,z
b BeepOff
movlw .1
xorwf TmrBak,w
btfsc status,z
b BeepStop
b BeepForEnd
BeepOff:
clrf BeepCnt
bcf Sflag,FgBeep
bcf BeepOut
bcf T2CON,TMR2ON
b BeepForEnd
BeepStop:
bcf T2CON,TMR2ON ;stop
bcf BeepOut
BeepForEnd:
retlw 0
;**************************************************
END