Buzzers are very common in microcontroller application systems and are often used to implement alarm functions. For this reason, STM8 has specially integrated a buzzer module, which is very convenient to use.
When using the buzzer module, first turn on the low-speed RC oscillator on the chip (it should also be possible to use an external high-speed clock, but I haven't tried it), with a frequency of 128KHZ. Then set BEEPDIV[4:0] in the buzzer control register BEEP_CSR to get an 8KHZ clock, and then use BEEPSEL to generate a 1KHZ, 2KHZ, or 4KHZ buzzer clock. Finally, enable the BEEPEN bit in the register to generate the buzzer output.
The following experimental program first initializes the low-speed oscillator, then starts the buzzer, delays for 2.5 seconds, and then turns off the buzzer.
Still using ST's development tools, generate an assembler framework, and then modify main.asm. The modified code is as follows.
stm8/
#include "mapping.inc"
#include "STM8S207C_S.INC"
; Define the starting and ending positions of the stack space
stack_start.w EQU $stack_segment_start
stack_end.w EQU $stack_segment_end
segment 'rom'; Below is the definition of a segment, which is located in ROM
main.l ; Define the label of the first instruction after reset (i.e. entry address)
;
; First, initialize the stack pointer
LDW X,#stack_end
LDW SP,X
LD A,CLK_ICKR
OR A,#$08
LD CLK_ICKR,A ; Turn on the low-speed oscillator LSI inside the chip
WAIT_LSI_READY.L
LD A,CLK_ICKR
AND A,#$10
JREQ WAIT_LSI_READY ; Wait for oscillator to stabilize
LD A,#$2e ; BEEPDIV[1:0] = 00
; BEEPDIV[4:0] = 0e
; BEEPEN = 1
; Output frequency = Fls / ( 8 * (BEEPDIV + 2) ) = 128K / (8 * 16) = 1K
LD BEEP_CSR,A ; Turn on the buzzer
LD A,#10 ; Delay 250MS*10
DELAY_1.L
PUSH A
LD A,#250 ; Delay 250MS
CALL DELAY_MS
POP A
DEC A
JRNE DELAY_1
LD A,#$1E ; Turn off the buzzer
LD BEEP_CSR,A
MAIN_LOOP.L
JRA MAIN_LOOP
; Function: Delay
; Input parameter: Register A - the number of milliseconds to delay, assuming the CPU frequency is 2MHZ
; Output parameters: None
; Return value: None
; Note: None
DELAY_MS.L
PUSH A ; save the entry parameters to the stack
LD A,#250; Register A<-250, as the following loop number
DELAY_MS_1.L
NOP; Use no operation instruction to delay 4T
NOP
NOP
NOP
NOP
DEC A; Register A<-A-1, the execution time of this instruction is 1T
JRNE DELAY_MS_1; If not equal to 0, then loop,
; The execution time of this instruction is 2T (jump time) or 1T (no jump time)
POP A ; restore entry parameters from the stack
DEC A ; The number of MSs to be delayed - 1
JRNE DELAY_MS; If not equal to 0, then loop
RET ; function returns
interrupt NonHandledInterrupt
NonHandledInterrupt.l
iret
; The interrupt vector table is defined below
segment 'vectit'
dc.l {$82000000+main} ; reset
dc.l {$82000000+NonHandledInterrupt}; trap
dc.l {$82000000+NonHandledInterrupt}; irq0
dc.l {$82000000+NonHandledInterrupt}; irq1
dc.l {$82000000+NonHandledInterrupt}; irq2
dc.l {$82000000+NonHandledInterrupt}; irq3
dc.l {$82000000+NonHandledInterrupt}; irq4
dc.l {$82000000+NonHandledInterrupt}; irq5
dc.l {$82000000+NonHandledInterrupt}; irq6
dc.l {$82000000+NonHandledInterrupt}; irq7
dc.l {$82000000+NonHandledInterrupt}; irq8
dc.l {$82000000+NonHandledInterrupt}; irq9
dc.l {$82000000+NonHandledInterrupt} ; irq10
dc.l {$82000000+NonHandledInterrupt} ; irq11
dc.l {$82000000+NonHandledInterrupt} ; irq12
dc.l {$82000000+NonHandledInterrupt} ; irq13
dc.l {$82000000+NonHandledInterrupt} ; irq14
dc.l {$82000000+NonHandledInterrupt} ; irq15
dc.l {$82000000+NonHandledInterrupt} ; irq16
dc.l {$82000000+NonHandledInterrupt} ; irq17
dc.l {$82000000+NonHandledInterrupt} ; irq18
dc.l {$82000000+NonHandledInterrupt} ; irq19
dc.l {$82000000+NonHandledInterrupt}; irq20
dc.l {$82000000+NonHandledInterrupt} ; irq21
dc.l {$82000000+NonHandledInterrupt}; irq22
dc.l {$82000000+NonHandledInterrupt}; irq23
dc.l {$82000000+NonHandledInterrupt} ; irq24
dc.l {$82000000+NonHandledInterrupt}; irq25
dc.l {$82000000+NonHandledInterrupt} ; irq26
dc.l {$82000000+NonHandledInterrupt}; irq27
dc.l {$82000000+NonHandledInterrupt}; irq28
dc.l {$82000000+NonHandledInterrupt} ; irq29
end
Previous article:STM8 and Assembly Language (16) - PWM
Next article:STM8 clock register
- Popular Resources
- Popular amplifiers
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
- 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
- [Silicon Labs BG22-EK4108A Bluetooth Development Evaluation] + Development Board Power Consumption Detection and Comparison
- 【TI recommended course】#Motor control voltage and current sampling solution#
- [AutoChips AC7801x motor demo board review] + key peripheral test-HALL acquisition
- CCS uninstallation and installation process and problems encountered
- 6 Commonly Used C Language Conversion Tool Functions
- What does this sentence mean? How to understand it?
- A brief discussion on the insulation safety standards of driver chips
- Help download the source code of an encryption algorithm Hmac sha1
- Calculating the common-mode inductance of a switching power supply is actually not difficult!
- EEWORLD University - IC design process that even grandmas can understand