STM8 and Assembly Language (17) - Buzzer

Publisher:MysticalWhisperLatest update time:2021-10-15 Source: eefocusKeywords:STM8 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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


Keywords:STM8 Reference address:STM8 and Assembly Language (17) - Buzzer

Previous article:STM8 and Assembly Language (16) - PWM
Next article:STM8 clock register

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号