STM32C8T6 + panel + 3 LED s light up the running water lamp

1, Experimental topic

The circuit is built with STM32 minimum system core board (STM32F103C8T6) + panel board + 3 red, green and blue LEDs. The three ports of GPIOB, GPIOC and GPIOD are used to control the LED lights to flash in turn, with an interval of 1 second.

1) Write the program design idea, including the register address and detailed parameters of GPIOx port;

2) It is programmed with assembly language and C language.

2, Establish engineering documents

We can establish engineering documents step by step according to our own needs, or find suitable engineering templates established by others on the Internet, which can greatly improve the efficiency. This paper introduces these two aspects to you.

1. Build your own project

  • First, create a new light project. The establishment method of the project is the same as that in the previous article. There is no more introduction here.
  • This experiment requires us to use STM32C8T6 chip, so we should select the corresponding chip before establishing the project.
  • The established project is shown in the figure.
  • Next, we need to add the startup code. The startup code is available on the network, and those who need it need to download it themselves.
  • The startup code is a piece of assembly code related to hardware. ST company provides three different startup files for chips with different capacities, namely:
    startup_stm32f10x_ld.s,startup_stm32f10x_md.s,startup_stm32f10x_hd.s
    Corresponding to:

Small capacity: FLASH ≤ 32K
Medium capacity: 64K ≤ FLASH ≤ 128K
Large capacity: 256K ≤ FLASH

We select the required startup file according to the demand. Because our chip is STMC8T6, our Flash capacity is 128K, corresponding to the medium capacity, so we choose startup_stm32f10x_md.s and download and save the file to the project file directory.

  • Next, add the startup file in the project, as shown in the figure:

    Open the. s file to see the description of this file, which belongs to the description content designed by the developer. We only need to know that it needs to be added before initialization, call and other operations can be carried out. We don't need to know too much about the specific file content.

  • Next, right-click Manage Project Items in the project folder and you can see that there is only one source group group. We add a group to store sys.c, delay.c and usart.c files. The name of the group can be determined by ourselves with a memorable name.

  • Similarly, we can create a new group to store our. c files. Of course, we can put it in the existing group source group without creating it. It depends on our personal habits.

  • Next, we need to modify the global macro definition, click the magic wand icon and modify the definition in C + +.

  • Check generate hex file at output.

    In this way, our project will be established.

2. Use online template

Rational use of Internet resources can greatly improve our work efficiency. The manual establishment project is introduced above, but there are relevant resources on the Internet, which saves us the trouble of establishing the project every time. Here I personally use yangtao electronics, and those interested can go to the official website to download.

As shown in the figure, we only need to directly create the project in the template folder. After opening, you can see the established related groups and files on the left, including the startup file.

3, Turn on the LED

1. Code implementation

  • According to the requirements of the topic, we must first initialize the interface of LED lights and configure the clock. Select LED_ The mode of init three target ports is push-pull output, and the default output is 1, which completes the initialization of the port.
    led.c:
#include "led.h"

void LED_Init(void){ //LED lamp interface initialization
	GPIO_InitTypeDef  GPIO_InitStructure; 	
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);   //Turn on the clock
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //Select the port number (0 ~ 15 or all)                        
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //Select the working mode of IO interface. Here is the push-pull output mode       
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //Set IO interface speed (2/10/50MHz)    
	GPIO_Init(GPIOA, &GPIO_InitStructure);	
	GPIO_ResetBits(GPIOA,GPIO_Pin_0);//The initial value is low

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);       
    GPIO_InitStructure.GPIO_Pin =GPIO_Pin_0; //Select the port number (0 ~ 15 or all)                        
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //Select the operation mode of IO interface       
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //Set IO interface speed (2/10/50MHz)    
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	GPIO_ResetBits(GPIOB,GPIO_Pin_0);

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);       
    GPIO_InitStructure.GPIO_Pin =GPIO_Pin_13; //Select the port number (0 ~ 15 or all)                        
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //Select the operation mode of IO interface       
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //Set IO interface speed (2/10/50MHz)    
	GPIO_Init(GPIOC, &GPIO_InitStructure);
	GPIO_ResetBits(GPIOC,GPIO_Pin_13);

	
}

Because the topic requires three ports, here I use GPIOA, GPIOB and GPIOC, and the tube slogans are 0, 0 and 15 in turn, because the corresponding chip circuit is easy to connect.

For the introduction of push-pull output mode, please refer to my previous article, which introduces eight output modes.
Link: https://blog.csdn.net/cleveryoga/article/details/120927837.

  • Next, we create an. h file to declare the name of the function we write in the. c file, LED_Init(void), as we have mentioned in the. c file, its function is to initialize the interface of LED lights. We declare this function in the. h file so that it can be easily called in the main program.
    led.h
#include "sys.h"

void LED_Init(void);//initialization
  • The next step is to write the main program. Let's clarify our ideas first. We have initialized the clock initialization interface in the. h file and. c file, set the port number corresponding to the corresponding pin nozzle, and set the speed. According to the requirements of the topic, we need to make a water lamp with three colors flashing alternately. The realization of alternating flashing corresponds to high and low power in the circuit In the flat configuration, the high level 3.3V is on and the low level 0V is off. At the same time, there is an interval between the flashes of the three colors, and a delay time needs to be set. This can be long or short according to our own needs. With a clear idea, we can write the main program.
    main.c:
int main (void){//main program
	RCC_Configuration(); //Clock setting
	LED_Init();
	while(1){
		 GPIO_SetBits(GPIOA,GPIO_Pin_0);	//This is to set the IO port to high level, which is 3.3V
   
             delay_s(1);
         GPIO_ResetBits(GPIOA,GPIO_Pin_0);//This is to set the IO port to low level, which is 0V
         delay_s(1);  
     //  Maintain the above state for 1s, because the negative pole of our LED lamp is connected to the negative pole of the power supply. At this time, the MCU outputs 0V, and there is no voltage difference at both ends of the LED, so the LED lamp will not be on
		delay_s(1); //Delay 1 second
		
				 GPIO_SetBits(GPIOB,GPIO_Pin_0);	//This is to set the IO port to high level, which is 3.3V
   
             delay_s(1);
         GPIO_ResetBits(GPIOB,GPIO_Pin_0);//This is to set the IO port to low level, which is 0V
         delay_s(1);  
     //  Maintain the above state for 1s, because the negative pole of our LED lamp is connected to the negative pole of the power supply. At this time, the MCU outputs 0V, and there is no voltage difference at both ends of the LED, so the LED lamp will not be on
		delay_s(1); //Delay 1 second

		
				 GPIO_SetBits(GPIOC,GPIO_Pin_13);	//This is to set the IO port to high level, which is 3.3V
   
             delay_s(1);
         GPIO_ResetBits(GPIOC,GPIO_Pin_13);//This is to set the IO port to low level, which is 0V
         delay_s(1);  
     //  Maintain the above state for 1s, because the negative pole of our LED lamp is connected to the negative pole of the power supply. At this time, the MCU outputs 0V, and there is no voltage difference at both ends of the LED, so the LED lamp will not be on
		delay_s(1); //Delay 1 second
	}
}

It can be seen that in the main program, the led.h file is called to initialize the clock and interface, followed by a cycle. In the cycle body, GPIO_SetBits first turns on the high level 3.3V of port 0 of GPIOA pin, and after a delay of one second, GPIO_ResetBits turns off port 0 of GPIOA pin. It is also a delay of one second, and then changes to the corresponding port numbers of GPIOB and GPIOC. The same operation is followed all the time Ring the program.

2. Compilation


As shown in the figure, there is no error in the compilation result of the program, indicating that there is no problem in syntax and logic. Specifically, the circuit needs to be connected to check whether the flashing is correct.

3. Simulation waveform

Open the simulation, click setup in the analysis window above, and add the pin number you want to view.

The following simulation waveform is obtained:

4. Burning

First, we need to download FlyMCU on the PC, which can be found on the Internet. The open interface is shown in the figure.

  • Properly connect USB-TTL and bread board, and pay attention to the corresponding relationship:
    PA9-RXD
    PA10-TXD
    3.3V power connection
    GND grounding
  • Check the programming on the FlyMcu interface, select the read hex file path, select RTS low-level reset in the lowest corner, and enter DTR high-level into BootLoader

5. Circuit connection

Let the program run when it is powered on. You need to set BOOT0 to 0 and BOOT1. Connect the circuit according to the program, as shown in the figure.

6. Operation results


As shown in the figure, the LED flashes alternately with an interval of 1s.

Posted on Sat, 23 Oct 2021 23:22:20 -0400 by bluwe