STM32F103C8T6 LED light (flow lamp) (initial multiplexing function of IO port)

Source: Internet
Author: User
(1) The preface has just begun to learn STM32, of course, is lit LED lights, from arm "Hello word" began. Software installation, hardware connection, the previous article has been said, there is no more to say.
(2) Experimental purpose: Use STM32F10C8T6 to complete the function of running lights.
(3) The introduction of the experiment encountered the problem: The program is finished, the lamp is not bright, commissioning for two hours, finally found the reason, leave a suspense, and later.

(4) Nonsense not to say, first on the circuit diagram:

Figure 1 Pin diagram

(5) OK, dry, code: C

#include "stm32f10x.h"//gpio Structure gpio_inittypedef gpio_initstructure;  Delay Function void Mydelay_ms (int ms) {volatile int i;  int k = ms * 2333;  for (i=0; i;  }}//led Init Function void Led_init (void) {//initialize System Timer systeminit (); Enable PB3 and PB4, and turn off PB3 and PB4 multiplexing functions to make the IO function effective rcc_apb2periphclockcmd (Rcc_apb2periph_afio |  RCC_APB2PERIPH_GPIOB, ENABLE);  Gpio_pinremapconfig (gpio_remap_swj_jtagdisable,enable); GPIO Structure Gpio_initstructure.gpio_pin = Gpio_pin_3 | Gpio_pin_4; Chose Pin3 and Pin4 gpio_initstructure.gpio_speed = Gpio_speed_50mhz; Set Frequency 50MHz gpio_initstructure.gpio_mode = gpio_mode_out_pp;  Temporarily do not understand, only know seems to be push-pull//gpio init gpio_init (GPIOB, &gpio_initstructure);  } int main (void) {led_init (); while (1) {//led3 on && LED4 OFF gpio_setbits (GPIOB, gpio_pin_3); Gpio_resetbits (Gpiob, gpio_pin_4);  Mydelay_ms (500); LED3 OFF && LED4 on Gpio_resetbits (Gpiob, gpio_pin_3); Gpio_setbits (Gpiob, gpio_pin_4); Mydelay_ms (500); } }


(6) Code content analysis:        First, the code is divided into three parts: The delay function, the initialization function, the main function, the specific meaning of the code comment.        Second, here are some points to note in the code:           A. There are two LED lights, which are controlled by pin PB3 and PB4, respectively. So the two pins are configured as: IO output, high level. There are many GPIO ports in the          b.stm32, respectively, in the form of "Gpio+x", the x represents the specific port, PB in the figure above, The GPIOB is used to denote the mouth. Therefore, the control in the code should use GPIOB. The          c.gpio_pin_3 represents a 3-pin operation, and the same gpio_pin_4 represents a 4-pin operation. In combination with GPIOB, operations PB3 and PB4 are indicated.          d.gpio_setbits () is set to High, and Gpio_resetbits () is set to low.
(7) The problem encountered in this experiment:        began to check the code is normal, seemingly no error, but the lamp is not bright, and later read the manual found that the original PB3 PB4 pin has a multiplexing function, The system uses the reuse function by default, so it is no longer IO output function, it needs to turn off the multiplexing function to output IO signal normally. Dry (figure):


Figure 2 stm32f103c data sheet, PB port function
       as you can see, the default function is Jtdo and jntrst, so turn these two functions off. The        corresponding code is the red part of the LED initialization function  led_init ().
(8) Finish the work, continue my rookie success trip. I hope the experienced aunt can give a correction if she sees the wrong place, thank ~~~[for covering his face]

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.