The following program needs two timer resources. It can be implemented by any PIC microcontroller with two timers. The MCU required for this example is MICROChip PIC16C62
INCLUDE "D:\PIC\P16XX.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
;**************************************************
OkTab:
MOV f BeepCnt,w
addwf PCL,f
retlw .255-.130;0xf6 ;1 ;
retlw .255-.126;0xfa ;2 ;
retlw .255-.119; 0xfd ;3 ;
retlw 0
;**************************************************
WhisleTab:
MOV f 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:
MOV f 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:
MOV f 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_
MOV LW B'11001011'
MOV WF TR ISA
MOV LW B'00001100'
MOV wf TRISB
MOV LW B'00000011'
MOV wf TRISC ; Set Port_C to all outputs
BANK0_
RETURN
;******************************************************
SYS_SET:
BANK1_
MOV LW B'00000111' ; 1:256 TMR0 frequency division
MOV WF OPTION_R
BSF PIE1,TMR2IE ;TMR2 interrupt enable
BANK0_
MOV LW B'00000001' ; Turn on TMR1
MOV WF T1CON
bsf I NTC ON,TOIE ;TMR0 interrupt enable
BSF INTCON,PEIE ; Enable all unmasked peripheral interface interrupts
RETURN
;**************************************************
MAIN: ; Main rotation
CALL IO_SET
MOV LW B'00000000' ; Disable all interrupts
MOV WF INTCON
;********************************
CALL SYS_SET
;********************************
call PlayPsWelcom ; Power-on reminder tone
MAINLOOP:
bsf INTCON,GIE ; Enable 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 bit
GOTO INT_TMR0
IntRet:
POP ; Pop
RETFIE
;**************************************************
INT_TMR2:
BCF PIR1,TMR2IF ; Clear TMR2 interrupt flag
MOV f TmrBak,w
MOV wf TMR2
CPL BeepOut
BeeperEnd:
b IntRet
;******************************************************
INT_TMR0: ; Timer0 interrupt
BCF INTCON,TOIF ; clear INTF
MOV LW .255-.38 ;10ms
MOV WF TMR0
;===================
decfsz T100ms,f
b int_tmr0_ret
MOV lw CSTIME100MS
MOV wf T100ms
bsf Sflag,Fg_100ms
;=====================
int_tmr0_ret:
b IntRet
;************************************************
PlayPsWhisle:
bsf Fg,FgBeep
clrf BeepCnt
MOV lw .0
MOV wf BeepMode
MOV lw CSTIME100MS
MOV wf T100ms
retlw 0
;************************************************
PlayPsOk:
bsf Fg,FgBeep
clrf B eepCnt
MOV lw .1
MOV wf BeepMode
MOV lw CSTIME100MS
MOV wf T100ms
retlw 0
;**************************************************
PlayPsHang:
bsf Fg,FgBeep
clrf BeepCnt
MOV lw .2
MOV wf BeepMode
MOV lw CSTIME100MS
MOV wf T100ms
retlw 0
;**************************************************
PlayPsWelcom:
bsf Fg,FgBeep
clrf BeepCnt
MOV lw .3
MOV wf BeepMode
MOV lw CSTIME100MS
MOV wf 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
MOV LW B’00000101’ ;开TMR2 1:4分频
MOV WF T2CON
MOV f 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
MOV wf TmrBak
MOV f TmrBak,f
btfsc status,z
b BeepOff
MOV lw .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
Previous article:C language interrupt save/restore statements for PIC17CXX devices
Next article:PICC electronic clock program
- Popular Resources
- Popular amplifiers
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- Resolver software decoding solution based on C2000
- MSP430 Learning Summary 3-MSP430 Basic Clock Module
- Introduction and basic concepts of antennas commonly used in daily EMC and RF testing
- How to speed up the IO port flipping speed
- Programming example: External authentication between CPU card and ESAM
- The National Day holiday is over, why are holidays always so short?
- [TI recommended course] #[High Precision Laboratory] Interface: RS-485#
- How to convert analog audio signals into digital audio in PDM format.
- 【TI recommended course】# TINA-TI training course#
- Amplifier Circuit