Stm32 cainiao growth record-gpio usage

Source: Internet
Author: User
 
# Include "stm32f10x_lib.h" # include "stm32f10x. H "gpio_inittypedef gpio_initstructure; // define the macro definition volatile {for (; ncount! = 0; ncount --);}/* initialize the embedded flash interface and initialize the PLL to make it available to the system */void rcc_configuration (void) {/* setup the Microcontroller System. initialize the embedded flash interface, initialize the PLL and update the systemfrequency variable. */systeminit ();} int main () {int I; rcc_configuration (); // initialize the flash and Its PLL, and configure rcc_apb2periphclockcmd (rcc_apb2periph_gpioc | rcc_apb2periph_gpiob, enable ); // configure the peripheral clock to enable the gpioc clock frequency = gpio_pin_6 | gpio_pin_7 | gpio_pin_9; frequency = frequency; // configure the PC6 \ 7 \ 9 port as the universal push-pull output frequency = gpio_speed_50mhz; // The port Line flip speed is 50 MHz gpio_init (gpioc, & gpio_initstructure); // configure the gpioc port while (1) {gpio_setbits (gpioc, gpio_pin_6 ); // PC6 output high-level gpio_setbits (gpioc, gpio_pin_7); // pc7 output high-level gpio_setbits (gpioc, gpio_pin_9); // pc9 output high-level for (I = 0; I <1000000; I ++); Round (gpioc, gpio_pin_6); gpio_resetbits (gpioc, gpio_pin_7); // round (gpioc, gpio_pin_9); // delay (0 xaffff ); for (I = 0; I <1000000; I ++ );}}


Code engineering in my resources: Free http://download.csdn.net/detail/yx_l128125/4494861 available

Step 1 declare the structure of gpio:

Gpio_inittypedef gpio_initstructure;

 

Step 2: assign values to the gpio_initstructure member. If you only set some of the Members, we need the following code:

/*** Led1-> PC6, led2-> pc7, led3-> pc9 */

 

Gpio_initstructure.gpio_pin = gpio_pin_6 | gpio_pin_7 | gpio_pin_9;

Gpio_initstructure.gpio_speed = gpio_speed_50mhz;

Gpio_initstructure.gpio_mode = gpio_mode_out_pp;

Gpio_init (gpioc, & gpio_initstructure );

 

In fact, a function is omitted here:

Gpio_structinit, which is used to initialize Variables

Gpio_initstructure. After experiment, we found that this is not required. You can try it :) then

Modify the members in the Variable. There are three members. On the stm32 Development Board, the gpio port is connected

PC6, pc7, and pc9 pins. Therefore, we assign gpio_pin_6 | gpio_pin_7 | gpio_pin_9 to the gpio_pin member.

Assign the gpio_speed_50mhz value to the gpio_speed member,

The gpio_mode member is set to gpio_mode_out_pp, which indicates the push-pull output mode.

 

Push-pull output --> (the output high current is as large as the output low current)


The push-pull circuit is a three-circuit transistor or transistor with the same parameters. It exists in the circuit in the push-pull mode. It is responsible for waveform amplification tasks of plus or minus half weeks. When the circuit works, the two symmetric power switches have only one on-going switch each time, so the on-going loss is small and the efficiency is high. The output can either inject current to the load or extract current from the load.

The push-pull circuit is applicable to low-voltage and high-current scenarios and is widely used in power amplifier circuits and switching power supplies.

Advantages:The structure is simple, and the core utilization rate of the switch transformer is high. When the push-pull circuit is working, the two symmetric power switches have only one turn-on each time, so the turn-on loss is small.

Disadvantages:The transformer has a center tap, and the switch has a high Withstand Voltage. Due to the leakage of the original side of the transformer, when the power switch is turned off, the leakage source will generate a large voltage spike, in addition, the input current has a large ripple, so the input filter has a large volume.

Simplified Circuit Diagram of the push-pull part of the transistor

 

Step 3 call the gpio_init () function to initialize the peripheral gpio. The Code is as follows::

Gpio_init (gpioc, & gpio_initstructure );

Step 4. Note: In the Firmware Library, gpio does not have the gpio_cmd function, so this step is omitted.

Through the above four steps, we have set gpio. Another problem cannot be ignored:

Before setting a peripheral, we must call a clock function for it to enable the peripheral clock.


In the CPU user manual, we know that stm32 has several clocks. Which one do we use now?

What about the clock? Open the P25 page of the Chinese version of stm32f10xxx reference manual, as shown in the following page.

. We use the gpioc port, so we use apb2.

 

Step 5: After setting the corresponding function of apb2, we will call it:

Rcc_apb2periphclockcmd (rcc_apb2periph_gpioc, enable );

 

The first parameter indicates the clock on which port to enable. rcc_apb2periph_gpiox indicates the clock on which gpiox is enabled, and the second parameter indicates whether to enable or disable enable/disable.

 

Note that the enable function of the clock should be placed at the beginning. You must have a clock before you can perform subsequent operations.

Let's first look at how LEDs are connected in the schematic diagram:

 

From the schematic diagram, we can see that to make the three LEDs light up, the corresponding pins must be

Cleared. If you set 1, the LED is disabled. Now open the stm32 Firmware Library Document and find section 10.2, gpio

Library function. All functions set by gpio are here. Which function of the gpio library is used?

Number. Find: gpio_setbits (); and gpio_resetbits ();


The two functions, according to the instructions, respectively set a pin to high voltage and low level.

The gpio_setbits function is setHighLevel,

The gpio_resetbits function is a zeroing operation. According to our

Write the following code to get the information: the purpose is to make all the LEDs flash regularly and cyclically.

 

We also need to call systeminit (); function to initialize the entire system, including clock settings

72 MHz. After the above configuration is complete, you can download the hex

File to the board for debugging.

 

Jlink
Burn write method in my resources: The Struggle edition stm32 Development Board JTAG download steps http://download.csdn.net/detail/yx_l128125/4494855

 

Commitment! This Friday ago, I must post several articles about the system clock-tick timer, common timer, and 485 communication. Because of the time relationship, I have never been able to organize it into a blog, so I promise to put a little pressure on myself! You can't lose your face!

Related Article

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.