51/AVR单片机技术驿站!  <在线翻译>     便利工具    特色网址   无弹窗、无插件的绿色站点...  英才招聘   学历查询  喜欢>>收藏我站 

当前位置:首页 > 单片机技术文章 > PIC单片机 > 详细内容
PIC单片机实现音乐播放
发布时间:2009/7/24  阅读次数:485  字体大小: 【】 【】【
          
源程序:

以下程序要用二个定时器资源,凡是有二个定时器的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 B11001011
       MOVWF TRISA
       MOVLW B00001100
       movwf TRISB
       MOVLW B00000011
       movwf TRISC ; Set Port_C to all outputs
       BANK0_
       RETURN
;**************************************************
SYS_SET:
       BANK1_
       MOVLW B00000111 ; 1:256 TMR0分频
       MOVWF OPTION_R
       BSF PIE1,TMR2IE ;TMR2中断允许
       BANK0_
       MOVLW B00000001 ;开TMR1
       MOVWF T1CON
       bsf INTCON,TOIE ;TMR0中断允许
       BSF INTCON,PEIE ;允许所有未被屏蔽之外围接口中断
       RETURN
;**************************************************
MAIN: ; Main rotation
       CALL IO_SET
       MOVLW B00000000 ;关所有中断
       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 B00000101 ;开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

我要评论
  • 匿名发表
  • [添加到收藏夹]
  • 发表评论:(匿名发表无需登录,已登录用户可直接发表。) 登录状态:未登录
最新评论
所有评论[0]
    暂无已审核评论!

网站导航 管理登陆 ┊ 免责声明 问题反馈  友链说明
本站部分内容来自网络共享资源,如有冒犯您的权利请来信告之删除或纠正!
不得对本站进行复制、盗链或镜像,转载内容须获得同意或授权;欢迎友情链接、站务合作!

    我要报警 Alexa
 mcusy_cn#126.com (请把#改成@) 交流:522422171
本站学习交流群:138..158(高级群1-)、77930286(高级群2)、61804809(群3)
Copyright© MCUSY All Rights Reserved
本站网警备案号: WZ36040002485
  ICP备案证书号:粤ICP备09034963号