Zero-dead corner game stm32-read excerpt 1

Source: Internet
Author: User

I. Clock-related

From the analysis of the clock tree, we can see that after a series of Frequency Doubling and frequency division, we have obtained several clock closely related to our development.

Sysclk: system clock. The clock source of most stm32 devices. It is mainly allocated to various components by the AHB pre-divider.

Hclk: The hclk is directly output by the AHB pre-divider. It is the clock signal of the high-speed bus AHB and is provided to the memory, DMA and cortex kernel. It is the clock run by the cortex kernel. The CPU clock speed is the signal, its size is closely related to the stm32 operation speed and data access speed.

Fclk: it is also obtained from the AHB pre-divider output, which is the kernel's "free-running clock ". "Free" means that it does not come from the hclk, so fclk continues to run when the hclk clock stops. Its existence ensures that during sleep of the processor, it can also sample and track sleep events, and it is synchronized with hclk.

Pclk1: The peripheral clock, which is obtained from the apb1 pre-divider output. The maximum frequency is 36 MHz and is provided to peripherals mounted on the apb1 bus.

Pclk2: The peripheral clock, which is output by the apb2 pre-divider. The maximum frequency is 72 MHz and is provided to the peripherals mounted on the apb2 bus.

Why is the stm32 clock system so complex that there are frequency doubling, frequency division, and a series of peripheral clock switches. The frequency doubling takes electromagnetic compatibility into account. For example, if a 72 MHz crystal oscillator is provided externally, a high frequency of oscillation may make the circuit board difficult. The reason for the frequency division is that stm32 has both high-speed peripherals and low-speed peripherals, and the working frequency of various peripherals is different. Just like the North-South Bridge on a PC, it manages high-speed and low-speed devices separately. Finally, each peripheral is equipped with a peripheral clock switch. When we do not use a peripheral, we can turn off the peripheral clock to reduce the overall power consumption of stm32. So when we use peripherals, remember to turn on the peripheral clock.

Note: The 3.5 library calls systeminit () in the startup file, so you do not have to call it again in the main () function. However, if you are using a library of version 3.0, you must call systeminit () in the main function to set the system clock, because systeminit () is not called in the startup code of version 3.0 () function. (For details, refer to the startup code)

If we use the I/O pin multiplexing function, we need to enable the reuse function clock.

For example, the pin4 of gpioc can also be used as the adc1 input pin. Now we use it as adc1. In addition to enabling the gpioc clock, we also need to enable the adc1 clock:

Rcc_apb2periphclockcmd (rcc_apb2periph_gpioc, enable );

Rcc_apb2periphclockcmd (rcc_apb2periph_adc1, enable );

/*

Led. h

*/

# Ifndef _ led_h

# DEFINE _ led_h

# Include "stm32f10x. H"

/* The macro definition to trigger the led on or OFF

* 1-off

-0-on

*/

# Define on 0

# Define off 1

// The macro with parameters can be used like an inline function.

# Define led1 (A) if ()\

Gpio_setbits (gpioc, gpio_pin_3 );\

Else \

Gpio_resetbits (gpioc, gpio_pin_3)

# Define led2 (A) if ()\

Gpio_setbits (gpioc, gpio_pin_4 );\

Else \

Gpio_resetbits (gpioc, gpio_pin_4)

# Define led3 (A) if ()\

Gpio_setbits (gpioc, gpio_pin_5 );\

Else \

Gpio_resetbits (gpioc, gpio_pin_5)

Void led_gpio_config (void );

# Endif/* _ led_h */

This header file (LED. h) does not have much content, but it is also a separate header file for later extension or porting. It is hoped that the readers will develop good engineering habits and add compilation conditions similar to the following when writing header files.

# Ifndef _ led_h

# DEFINE _ led_h

......

# Endif

This prevents Repeated inclusion of header files and improves project compatibility. The reader asked why two underscores (_) are required "__"? Here, we can avoid duplicate macro identifiers with other definitions by adding two underscores, because the macro or variable defined in other code usually does not contain such underlined names.

Standard bit Operation Method

The implementation of library functions involves many bit operations. First, we will introduce several common bit operation methods to readers to eliminate the obstacles to reading code.

1. Clear the seventh (bit6) of char variable A to 0, and the other BITs remain unchanged.

A & = ~ (1 <6); // shifts 1 to the left of 6 digits in parentheses, resulting in a binary number: 0100 0000

// Returns the bitwise inverse of 1011 to 1111. The obtained number is equal to the bitwise AND ampersands (&) of,

// The 7th bits (bit6) of a are set to zero, while those of other BITs remain unchanged.

2. Similarly, set the 7th bit (bit6) of variable A to 1. The method for changing other bits is as follows.

A | = (1 <6); // set the seventh digit (bit6) to 1.

3. Reverse the seventh bit (bit6) of variable A, and the other BITs remain unchanged.

A ^ = (1 <6); // reverse the seventh digit (bit6), and the other digits remain unchanged (^ is or symbol)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.