LED flicker control for STM32 instances and related precautions

Source: Internet
Author: User

In this case, the main purpose is to realize the flashing of the LED lamp. First analysis led driving mode, this experiment is used openm3v, built-in 8 LEDs are used to drive the flow (low-power Pingliang). If you want to achieve its flicker, you need to give the corresponding port a constant alternating level of high and low.

In the software structure design, adding the use of LED8, you need to be in the PD7 port continuously output high and low level. First you need to initialize the system clock, and then start configuring the PD7 as the output, open the peripheral clock and finally control the PD7 output continuous high and low rotation.

The following gives the specific code (the code runs on KEIL5).

In the software code writing needs to realize the LED flicker is actually very simple, only need to take turns low, the high control LED8 I/O port line is good.

#include <stm32f10x_lib.h>//Include header file, use library function to join the header file

int main (void)

{

Rcc_configuration ();//Configure the system clock to set the system clock to 72MHz

Nvic_configuration ();//Configuration Interrupt

Enable the a,b,c,d,e peripheral clock, if used on that peripheral, must open its peripheral clock

Rcc_apb2periphclockcmd (rcc_apb2periph_gpioa| rcc_apb2periph_gpiob|

rcc_apb2periph_gpioc| rcc_apb2periph_gpiod|

rcc_apb2periph_gpioe,enable);

Gpio_initsrtucture.gpio_pin = gpio_all;//All pin 0~15 are selected;

Gpio_initsrtucture.gpio_mode = gpio_ain;//is configured as analog input;

Gpio_init (Gpioa, &gpio_initstructure);//All pins of a port are configured as analog inputs

Gpio_init (Gpiob, &gpio_initstructure);//Configure the B port all pins as analog input

Gpio_init (GPIOC, &gpio_initstructure);//Configure the C port all pins as analog input

Gpio_init (Gpiod, &gpio_initstructure);//Configure the D port all pins as analog input

Gpio_init (Gpioe, &gpio_initstructure);//Configure the E-port all pins as analog input

Enable A,b,c,d,e peripheral clock, turn off a,b,c,d,e peripherals

Rcc_apb2periphclockcmd (rcc_apb2periph_gpioa| rcc_apb2periph_gpiob|

rcc_apb2periph_gpioc| rcc_apb2periph_gpiod|

rcc_apb2periph_gpioe,disable);

The 6,7,8,9 pin of the peripheral that the LED is configured for is output

Gpio_initsrtucture.gpio_pin = gpio_pin6| gpio_pin7| gpio_pin8| Gpio_pin9;

Gpio_initsrtucture.gpio_mode = gpio_mode_out_pp;//configured as output

Gpio_initsrtucture.gpio_speed = Gpio_speed_50mhz;

Gpio_init (gpio_led,&gpio_initsrtucture);

while (1)//In order to generate loops

{

}

}


The following is the complete program (where the above program is included in the header file)

#include <stm32f10x_lib.h>

void Delay (void)

{

unsigned long k;

for (k=0;k<0xffff8;k++);

}

int main ()

{

Rcc_configuration ();//Configure the system clock to set the system clock to 72MHz

Nvic_configuration ();//Configuration Interrupt

Rcc_apb2periphclockcmd (rcc_apb2periph_gpiod,enable);//Open peripheral D's clock

Configure the 7-pin output of the peripheral where the LEDs are located

Gpio_initsrtucture.gpio_pin = gpio_pin_7;

Gpio_initsrtucture.gpio_mode = Direction of gpio_mode_out_pp;//i/od

Gpio_initsrtucture.gpio_speed = Gpio_speed_50mhz;

Gpio_init (gpiod,&gpio_initstructure);//Initialize the 7 port of peripheral D

while (1)

{

Gpio_setbits (gpiod,gpio_pin_7);//Set PD7, extinguish LED8

Delay (); Delay

Gpio_resetbits (gpiod,gpio_pin_7);//clear PD7, light LED8

Delay (); Delay

}

}

The above is the complete program, but when using peripherals, the following points are very important.

1.FWLAB file group to add the peripheral library file

2. In the header file Stm32f10x_conf.h, open the macro definition for the corresponding peripheral

3. Initialize the function peripherals according to the requirements of the system

4. Turn on the peripheral's clock

5. Configure nested vector interrupts in the nvic_configuration () function

Describes how to use I/O peripherals:

1. Need to add clock (RCC), Interrupt (NVIC), Flash, System (LIB) and GPIO related library files to the FWLAB filegroup, so that the library file about I/O port is also added;

2. Whether the peripheral definition in the header file is open (macro definition);

3. First, you need to use the Gpio_init (gpiod,&gpio_initstructure) function to initialize port D, set port 7 as output;

4. Call the Rcc_apb2periphclockcmd (rcc_apb2periph_gpiod,enable) function to turn on peripheral D's clock, because there is no interrupt, so no interrupt priority is configured, and adding interrupts is required to configure the interrupt priority level.

(Want to use peripherals correctly, the above summary is necessary to be involved)

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.