STM8 and assembly language (14) - switching clock source

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

The clock source of the STM8 microcontroller can be selected as internal or external, and can be easily switched during system operation.


The following experimental program first switches the main clock source to an external crystal oscillator with an oscillation frequency of 8MHZ, and then quickly flashes the LED indicator. Next, the main clock source is switched to the internal oscillator with an oscillation frequency of 2MHZ, and then the LED indicator flashes slowly. By observing the flashing frequency of the LED indicator, it can be seen that the same loop code changes the flashing frequency and duration due to the change of the main clock source.


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,#08

       LD PD_DDR,A ; Set PD3 to output

       LD A,#08

       LD PD_CR1,A ; Set PD3 to push-pull output

       LD A,#00

       LD PD_CR2,A ;

                            

       LD A,#$01

       LD CLK_ECKR,A ; Allow external high speed oscillator to work

WAIT_HSE_READY.L

       LD A,CLK_ECKR

       AND A,#$02

       JREQ WAIT_HSE_READY ; Wait for the external high speed oscillator to be ready

                

; Note: After the above switch, the main clock switches from HSI/8 (2MHZ) to HSE (8MHZ)       

;        

MAIN_LOOP.L

; Set the CPU clock divider below so that the CPU clock = main clock

; Through the LED, it can be seen that the speed of program running has indeed been significantly improved

       LD A,#$02

       LD CLK_SWCR,A ; SWEN <- 1

        

       LD A,#$B4   

       LD CLK_SWR,A ; Select the high-speed oscillator outside the chip as the main clock

WAIT_CLK_SWITCH_1.L

       LD A,CLK_SWCR

       AND A,#$08

       JREQ WAIT_CLK_SWITCH_1 ; Wait for the switch to succeed

        

       LD A,#$00 ; Clear the switch flag

       LD CLK_SWCR,A



       LD A,#10; LED flashes 10 times at high speed

HIGH_SPEED.L                        

       PUSH A ; save register

       LD A,#08           

       LD PD_ODR,A ; Set the output of PD3 to 1

       LD A,#100

       CALL DELAY_MS ; Delay 100MS

                            

       LD A,#00           

       LD PD_ODR,A ; Set the output of PD3 to 1

       LD A,#100

       CALL DELAY_MS ; Delay 100MS

                            

       POP A ; restore register

       DEC A

       JRNE HIGH_SPEED

                            

; Set the CPU clock divider below so that CPU clock = main clock / 4

; Through the LED, it can be seen that the speed of program running has indeed dropped significantly

       LD A,#$02

       LD CLK_SWCR,A ; SWEN <- 1

        

       LD A,#$E1 ; Select HSI as the main clock source

       LD CLK_SWR,A      

WAIT_CLK_SWITCH_2.L

       LD A,CLK_SWCR

       AND A,#$08

       JREQ WAIT_CLK_SWITCH_2 ; Wait for the switch to succeed

        

       LD A,#$00 ; Clear the switch flag

       LD CLK_SWCR,A

; Note: After the above switch, the main clock switches from HSE (8MHZ) to HSI/8 (2MHZ)       



       LD A,#10; LED flashes 10 times at low speed

LOW_SPEED.L                          

       PUSH A ; save register

                            

       LD A,#08           

       LD PD_ODR,A ; Set the output of PD3 to 1

       LD A,#100

       CALL DELAY_MS ; Delay 100MS

                            

       LD A,#00           

       LD PD_ODR,A ; Set the output of PD3 to 1

       LD A,#100

       CALL DELAY_MS ; Delay 100MS

                            

       POP A ; restore register

       DEC A

       JRNE LOW_SPEED



       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 (14) - switching clock source

Previous article:STM8与汇编语言(13)--修改CPU的时钟
Next article:STM8 and Assembly Language (15) - AD Conversion

Recommended ReadingLatest update time:2024-11-15 18:45

STVD+STM8 official firmware library to create a project template
This article draws on the experience of creating project templates in KEIL, IAR, and some IDEs. If there is any objection, please email me and I will modify it in time. If there is any copyright issue, please contact me in time to delete the post and maintain the copyright. 1. Install STVD (IDE) + COSMIC (compiler),
[Microcontroller]
STVD+STM8 official firmware library to create a project template
stm8 IAR compile error atal Error[Pe035]: #error directive
This shows that the macro of the stm8 microcontroller model is not set You can choose one of the following options and copy it to the location shown in the following figure (in bold): STM8L15X_MD medium-density device      STM8L15X_MDP Medium Density+ devices   STM8L15X_HD High-density device. Then you can compil
[Microcontroller]
stm8 IAR compile error atal Error[Pe035]: #error directive
STM8 line interrupt and port interrupt
There are two modes of STM8L interrupts, namely Pin mode and Port mode, which are the so-called line interrupts and port interrupts. In short, line interrupt means that the interrupts of the same pin of all register groups use the same interrupt number, such as, void KEY2Init() {   PB_DDR_bit.DDR2 = 0; //GPB- PIN2 S
[Microcontroller]
Based on the use of STM8 microcontroller I2C method to realize read and write operations
STM8 hardware I2C knowledge The I2C module of STM8S can not only receive and send data, but also convert data from serial to parallel data when receiving and from parallel to serial data when sending. Interrupts can be enabled or disabled. The interface is connected to the I2C bus through the data pin (SDA) and the cl
[Microcontroller]
Based on the use of STM8 microcontroller I2C method to realize read and write operations
stm8 development environment configuration and testing
Software and hardware required: IAR for stm8 (EWSTM8), stm8s standard firmware library, ST-LINK, STM8s003f3 core board Install IAR (including st-link driver), Go to this website to download the stm8s standard firmware library (STSW-STM8069), http://www.st.com/web/en/catalog/tools/PF258009 Use IAR to create a new
[Microcontroller]
STM8 Assembly Learning Notes 2: Development Environment
Written in front   As far as I know, there are two development platforms for STM8: STVD from ST and IAR for STM8. I haven't tried STVD. Anyway, IAR feels OK, and it has all the necessary functions, but the interface is not very user-friendly. I guess it's because of the low version. Since my eyes hurt from staring at
[Microcontroller]
STM8 Assembly Learning Notes 2: Development Environment
STM8 and Assembly Language (11) - UART Application 3
The following experimental program is based on the previous experimental program, and changes the character reception to interrupt mode. Whenever a character is received, the interrupt service program is entered. In the interrupt service program, the character is read from the UART receive data register and then sent
[Microcontroller]
STM8 MCU software determines the reset source
Let me tell you about the port remapping of STM32 microcontroller. Here we take the remapping of USART1 as an example. There are many I/O ports on STM32, and there are also many built-in peripherals such as I2C, ADC, ISP, USART, etc. In order to save lead pins, these built-in peripherals basically share pins with I/
[Microcontroller]
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号