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
Previous article:Simplifying Worst-Case Simulations in PSpice Using Custom Measurement Expressions
Next article:Digital thermometer based on MSP430 microcontroller and DS18B20
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Purgatory Legend-FIFO Battle
- How bias circuit works!!!
- The best articles of Bluetooth in the past 10 years
- Program to replace Matlab results with EXCEL formulas
- C2000 ±0.1° Accurate Discrete Resolver Front-End Reference Design
- EEWORLD University Hall----Live Replay: ADI MEMS Sensors Open a New Era of Conditional State Monitoring
- Unboxing ESP32-S2 and S3
- Easily solve design challenges with MSP430 MCUs
- TI Logistics Robot CPU Board
- I would like to ask which one has better performance, thank you for your recommendation