PIC單片機(jī)音樂播放程序
以下程序要用二個(gè)定時(shí)器資源,凡是有二個(gè)定時(shí)器的PIC單片機(jī)均可實(shí)現(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:
MOV fBeepCnt,w
addwfPCL,f
retlw.255-.130;0xf6;1;
retlw.255-.126;0xfa;2;
retlw.255-.119;0xfd;3;
retlw0
;**************************************************
WhisleTab:
MOV fBeepCnt,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:
MOV fBeepCnt,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:
MOV fBeepCnt,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_
MOV LWB’11001011’
MOV WFTRISA
MOV LWB’00001100’
MOV wfTRISB
MOV LWB’00000011’
MOV wfTRISC;SetPort_Ctoalloutputs
BANK0_
RETURN
;**************************************************
SYS_SET:
BANK1_
MOV LWB’00000111’;1:256TMR0分頻
MOV WFOPTION_R
BSFPIE1,TMR2IE;TMR2中斷允許
BANK0_
MOV LWB’00000001’;開TMR1
MOV WFT1CON
bsfINTCON,TOIE;TMR0中斷允許
BSFINTCON,PEIE;允許所有未被屏蔽之外圍接口中斷
RETURN
;**************************************************
MAIN:;Mainrotation
CALLIO_SET
MOV LWB’00000000’;關(guān)所有中斷
MOV WFINTCON
;****************************
CALLSYS_SET
;****************************
callPlayPsWelcom;上電提示音
MAINLOOP:
bsfINTCON,GIE;開所有中斷
CLRWDT;ClearWDT
callBeepFor
BMAINLOOP
;**************************************************
INTZ:
PUSH;Push;中斷服務(wù)程序
BTFSCPIR1,TMR2IF;測試TMR2中斷標(biāo)志位
bINT_TMR2;
BTFSCINTCON,TOIF;測試TMR0中斷標(biāo)志位
GOTOINT_TMR0
IntRet:
POP;Pop
RETFIE
;**************************************************
INT_TMR2:
BCFPIR1,TMR2IF;清TMR2中斷標(biāo)志位
MOV fTmrBak,w
MOV wfTMR2
CPLBeepOut
BeeperEnd:
bIntRet
;**************************************************
INT_TMR0:;定時(shí)0中斷
BCFINTCON,TOIF;clearINTF
MOV LW.255-.38;10ms
MOV WFTMR0
;====================
decfszT100ms,f
bint_tmr0_ret
MOV lwCSTIME100MS
MOV wfT100ms
bsfSflag,Fg_100ms
;=====================
int_tmr0_ret:
bIntRet
;**************************************************
PlayPsWhisle:
bsfFg,FgBeep
clrfBeepCnt
MOV lw.0
MOV wfBeepMode
MOV lwCSTIME100MS
MOV wfT100ms
retlw0
;**************************************************
PlayPsOk:
bsfFg,FgBeep
clrfBeepCnt
MOV lw.1
MOV wfBeepMode
MOV lwCSTIME100MS
MOV wfT100ms
retlw0
;**************************************************
PlayPsHang:
bsfFg,FgBeep
clrfBeepCnt
MOV lw.2
MOV wfBeepMode
MOV lwCSTIME100MS
MOV wfT100ms
retlw0
;**************************************************
PlayPsWelcom:
bsfFg,FgBeep
clrfBeepCnt
MOV lw.3
MOV wfBeepMode
MOV lwCSTIME100MS
MOV wfT100ms
retlw0
;**************************************************
;音樂播放程序
;BeepMode=0:psWhisle
;BeepMode=1:psOk
;BeepMode=2:psHang
;BeepMode=3:psWelcom
;**************************************************
BeepFor:
btfssSflag,Fg_100ms
bBeepForEnd
bcfSflag,Fg_100ms
btfssSflag,FgBeep
bBeepForEnd
MOV LWB’00000101’;開TMR21:4分頻
MOV WFT2CON
MOV fBeepMode,w
bBeepModeJmp
psWhisle:
callWhisleTab
bBeepPlay
psOk:
callOkTab
bBeepPlay
psHang:
callHangTab
bBeepPlay
psWelcom:
callWelcomTab
bBeepPlay
BeepPlay:
incfBeepCnt,f
MOV wfTmrBak
MOV fTmrBak,f
btfscstatus,z
bBeepOff
MOV lw.1
xorwfTmrBak,w
btfscstatus,z
bBeepStop
bBeepForEnd
BeepOff:
clrfBeepCnt
bcfSflag,FgBeep
bcfBeepOut
bcfT2CON,TMR2ON
bBeepForEnd
BeepStop:
bcfT2CON,TMR2ON;stop
bcfBeepOut
BeepForEnd:
retlw0
;**************************************************
END