PIC单片机音乐播放程序
扫描二维码
随时随地手机看文章
以下程序要用二个定时器资源,凡是有二个定时器的PIC单片机均可实现,该范例需要的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’;关所有中断
MOV WFINTCON
;****************************
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中断标志位
MOV fTmrBak,w
MOV wfTMR2
CPLBeepOut
BeeperEnd:
bIntRet
;**************************************************
INT_TMR0:;定时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