英锐恩单片机论坛,Microchip单片机,模拟器件,接口电路,麦肯单片机,单片机应用交流
标题:
PIC单片机实现音乐播放
[打印本页]
作者:
winnie
时间:
2009-5-6 09:57
标题:
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
欢迎光临 英锐恩单片机论坛,Microchip单片机,模拟器件,接口电路,麦肯单片机,单片机应用交流 (http://enroobbs.com/)
Powered by Discuz! X3.2