[stm32f10x] standard library initialization issues

Source: Internet
Author: User

Hardware: STM32F103C8T6

Platform: ARM-MDK V5.11

The standard libraries provided by the STM32F series are initialized through structs. For example, here is a sample code for GPIO initialization:

  Gpio_inittypedef   gpio_initstructure;
Gpio_initstructure.gpio_pin = Gpio_pin_0 | gpio_pin_2; Gpio_initstructure.gpio_speed = Gpio_speed_50mhz; Gpio_initstructure.gpio_mode = gpio_mode_out_pp; &gpio_initstructure);

First, define an initialization struct variable, then assign a value to the elements inside the struct based on the actual requirements, and finally call the initialization function to begin initialization.

I generally like to define this struct as a local variable, because the initialization process is usually called only once, and the problem comes.

This is a problem I encountered when using the TIM1 timer, I would like to use TIM1 to produce a 8us timing, but the result is 1ms timing. The code for the TIM1 timer initialization is as follows:

voidTimebaseconfig (void) {tim_timebaseinittypedef tim_timebasestructure; Tim_timebasestructure.tim_period=575; Tim_timebasestructure.tim_prescaler=0; Tim_timebasestructure.tim_clockdivision=0; Tim_timebasestructure.tim_countermode=tim_countermode_up; Tim_timebaseinit (Em4095_timebase,&tim_timebasestructure);    Tim_arrpreloadconfig (Em4095_timebase, ENABLE); /*TIM1 Enable counter*/tim_cmd (Em4095_timebase, ENABLE); /*Update Interrupt config*/tim_itconfig (Em4095_timebase, Tim_it_update, ENABLE);}

The problem is where the initialization structure is defined! At run time, look at the value of this struct:

  

Because it is defined as a local variable, the value of the element inside the initialization struct is random, and my initialization function has less tim_repetitioncounter assignment, which should be set to 0 and the initial value is 0x80.

That is, repeat 128 times to produce a timed interrupt, about 1ms.

If the initialization struct is defined as a global variable:

  

As you can see, when a struct is defined as a global variable, all values are initialized to 0.

  Conclusion

Although the initialization structure is defined as a global variable, each element is initialized to 0, but not necessarily the value you need. Therefore, when initializing a peripheral, each element in the structure must be based on the

Actual demand assignment.

/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxthe endxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/

[stm32f10x] standard library initialization issues

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.