PIC microcontroller realizes music playback

Publisher:HeavenlyCloudsLatest update time:2011-02-18 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Source program:



The following program needs to use two timer resources. Any PIC microcontroller with two timers can implement it. The MCU required for this example is MICROCHIP PIC16C62

INCLUDE "D:PICP16XX.EQU" ; This file can be found in the MICROCHIP CD

; ******************************************************

#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



;****************************************************** xfa ;2 ; retlw .255 - .119; 0xfd ;3 ; retlw 0 ;******************************************************

WhisleTab : movf BeepCnt,

w

addwf PCL ,f retlw .255-.239;523Hz ;0 ; retlw 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;1 046Hz ;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; 1174Hz; 5; retlw .255- .142; 880Hz; 4; retlw .255-.112; 1109Hz; 3; retlw .255-.150 ; .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 frequency division

MOVWF OPTION_R

BSF PIE1,TMR2IE; TMR2 interrupt enable

BANK0_

MOVLW B'00000001' ;Open TMR1

MOVWF T1CON

bsf INTCON,TOIE ;TMR0 interrupt enable

BSF INTCON,PEIE ;Enable all unmasked peripheral interface interrupts

RETURN

;******************** *********************************

MAIN: ; Main rotation

CALL IO_SET

MOVLW B'00000000' ; Turn off all interrupts

MOVWF INTCON

;****************************

CALL SYS_SET

;***************** ***********

call PlayPsWelcom ;Power-on reminder tone

MAINLOOP:

bsf INTCON,GIE ;Open all interrupts

CLRWDT ; Clear WDT

call BeepFor

B MAINLOOP

;************* *************************************

INTZ:

PUSH ; Push ; Interrupt service routine

BTFSC PIR1,TMR2IF ; Test TMR2 interrupt flag bit

b INT_TMR2 ;

BTFSC INTCON,TOIF ; Test TMR0 interrupt flag

GOTO INT_TMR0

IntRet:

POP ; Pop

RETFIE

;********************************* *********************

INT_TMR2:

BCF PIR1,TMR2IF ; Clear TMR2 interrupt flag

movf TmrBak,w

movwf TMR2

CPL BeepOut

BeeperEnd:

b IntRet

;**** **********************************************

INT_TMR0: ; Timing 0 interrupt

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

Reference address:PIC microcontroller realizes music playback

Previous article:Simplifying Worst-Case Simulations in PSpice Using Custom Measurement Expressions
Next article:Digital thermometer based on MSP430 microcontroller and DS18B20

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号