PIC單片機實現(xiàn)音樂播放
源程序:
以下程序要用二個定時器資源,凡是有二個定時器的PIC單片機均可實現(xiàn),該范例需要的MCU是MICROCHIPPIC16C62
INCLUDE"D:PICP16XX.EQU";該文件在MICROCHIP光盤中可找到
;**************************************************
#defineBeepOutRC,4
;**************************************************
W_TEMPEQU0X20;(0XA0)
STATUS_TEMPEQU0X21
BeepCntequ30h
TmrBakequ31h
BeepModeequ32h
Sflagequ33h
;**************************************************
CSTIME100MSequ.8;
;**************************************************
;Sflag
Fg_100msequ0
FgBeepequ1
;**************************************************
ORG000H;
GOTOMAIN;Skipoverinterruptvecter
ORG04H;InterruptVector
GOTOINTZ
;**************************************************
OkTab:
movfBeepCnt,w
addwfPCL,f
retlw.255-.130;0xf6;1;
retlw.255-.126;0xfa;2;
retlw.255-.119;0xfd;3;
retlw0
;**************************************************
WhisleTab:
movfBeepCnt,w
addwfPCL,f
retlw.255-.239;523Hz;0;
retlw.255-.179;698Hz;1;
retlw.255-.159;784Hz;2;
retlw.255-.119;1046Hz;3;
retlw.0
;**************************************************
HangTab:
movfBeepCnt,w
addwfPCL,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:
movfBeepCnt,w
addwfPCL,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:
addwfPCL,f
bpsWhisle;0
bpsOk;1
bpsHang;2
bpsWelcom;3
;**************************************************
IO_SET:
BANK1_
MOVLWB’11001011’
MOVWFTRISA
MOVLWB’00001100’
movwfTRISB
MOVLWB’00000011’
movwfTRISC;SetPort_Ctoalloutputs
BANK0_
RETURN
;**************************************************
SYS_SET:
BANK1_
MOVLWB’00000111’;1:256TMR0分頻
MOVWFOPTION_R
BSFPIE1,TMR2IE;TMR2中斷允許
BANK0_
MOVLWB’00000001’;開TMR1
MOVWFT1CON
bsfINTCON,TOIE;TMR0中斷允許
BSFINTCON,PEIE;允許所有未被屏蔽之外圍接口中斷
RETURN
;**************************************************
MAIN:;Mainrotation
CALLIO_SET
MOVLWB’00000000’;關所有中斷
MOVWFINTCON
;****************************
CALLSYS_SET
;****************************
callPlayPsWelcom;上電提示音
MAINLOOP:
bsfINTCON,GIE;開所有中斷
CLRWDT;ClearWDT
callBeepFor
BMAINLOOP
;**************************************************
INTZ:
PUSH;Push;中斷服務程序
BTFSCPIR1,TMR2IF;測試TMR2中斷標志位
bINT_TMR2;
BTFSCINTCON,TOIF;測試TMR0中斷標志位
GOTOINT_TMR0
IntRet:
POP;Pop
RETFIE
;**************************************************
INT_TMR2:
BCFPIR1,TMR2IF;清TMR2中斷標志位
movfTmrBak,w
movwfTMR2
CPLBeepOut
BeeperEnd:
bIntRet
;**************************************************
INT_TMR0:;定時0中斷
BCFINTCON,TOIF;clearINTF
MOVLW.255-.38;10ms
MOVWFTMR0
;====================
decfszT100ms,f
bint_tmr0_ret
movlwCSTIME100MS
movwfT100ms
bsfSflag,Fg_100ms
;=====================
int_tmr0_ret:
bIntRet
;**************************************************
PlayPsWhisle:
bsfFg,FgBeep
clrfBeepCnt
movlw.0
movwfBeepMode
movlwCSTIME100MS
movwfT100ms
retlw0
;**************************************************
PlayPsOk:
bsfFg,FgBeep
clrfBeepCnt
movlw.1
movwfBeepMode
movlwCSTIME100MS
movwfT100ms
retlw0
;**************************************************
PlayPsHang:
bsfFg,FgBeep
clrfBeepCnt
movlw.2
movwfBeepMode
movlwCSTIME100MS
movwfT100ms
retlw0
;**************************************************
PlayPsWelcom:
bsfFg,FgBeep
clrfBeepCnt
movlw.3
movwfBeepMode
movlwCSTIME100MS
movwfT100ms
retlw0
;**************************************************
;音樂播放程序
;BeepMode=0:psWhisle
;BeepMode=1:psOk
;BeepMode=2:psHang
;BeepMode=3:psWelcom
;**************************************************
BeepFor:
btfssSflag,Fg_100ms
bBeepForEnd
bcfSflag,Fg_100ms
btfssSflag,FgBeep
bBeepForEnd
MOVLWB’00000101’;開TMR21:4分頻
MOVWFT2CON
movfBeepMode,w
bBeepModeJmp
psWhisle:
callWhisleTab
bBeepPlay
psOk:
callOkTab
bBeepPlay
psHang:
callHangTab
bBeepPlay
psWelcom:
callWelcomTab
bBeepPlay
BeepPlay:
incfBeepCnt,f
movwfTmrBak
movfTmrBak,f
btfscstatus,z
bBeepOff
movlw.1
xorwfTmrBak,w
btfscstatus,z
bBeepStop
bBeepForEnd
BeepOff:
clrfBeepCnt
bcfSflag,FgBeep
bcfBeepOut
bcfT2CON,TMR2ON
bBeepForEnd
BeepStop:
bcfT2CON,TMR2ON;stop
bcfBeepOut
BeepForEnd:
retlw0
;**************************************************
END
評論