STM32CubeMX Series|Marquee

Marquee

1. Introduction to GPIO port

Each GPIO port has two 32-bit configuration registers (GPIOx_CRL and GPIOx_CRH), two 32-bit data registers (GPIOx_IDR and GPIOx_ODR), a 32-bit position/reset register (GPIOx_BSRR), a 16-bit reset register (GPIOx_BRR) and one 32-bit lock register (GPIOx_LCKR)
Each bit of the GPIO port can be configured into multiple modes by software: input floating, input pull-up, input pull-down, analog input, open drain output, push-pull output, push-pull multiplex
The basic structure of the I/O port bit with the function and open-drain multiplexing function is as follows
Insert picture description here

2. Hardware design

One end of the LED (D0~D7) is connected to the pull-up 3.3V, and the other end is connected to the PC0-PC7 pins of STM32F103. In this experiment, only D1 and D2 are used. The connection schematic diagram is as follows:
Insert picture description here

3. Software design

3.1 STM32CubeMX settings
  • RCC set external HSE, clock is set to 72M
  • PC0 and PC2 are set to GPIO push-pull output mode, pull-up, high-speed, and the default output level is high

Insert picture description here

  • Enter the project name, select the project path (no Chinese), select MDK-ARM V5; check Generated periphera initialization as a pair of'.c/.h' files per IP; click GENERATE CODE to generate the project code
3.2 MDK-ARM software programming
  • You can see the initialization function of the PC0/PC1 pins in the gpio.c file
void MX_GPIO_Init(void)
{
    
    
  GPIO_InitTypeDef GPIO_InitStruct = {
    
    0};

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOC_CLK_ENABLE();		//开启GPIOC时钟

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOC, LED1_Pin|LED2_Pin, GPIO_PIN_SET);	//PC0和PC1置1,默认初始化后灯灭

  /*Configure GPIO pins : PCPin PCPin */
  GPIO_InitStruct.Pin = LED1_Pin|LED2_Pin;			//PC0和PC1
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;		//推挽输出
  GPIO_InitStruct.Pull = GPIO_PULLUP;				//上拉
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;		//高速
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
}
  • Write the marquee program in the main.c function (note that the written program should be in the corresponding USER CODE range, otherwise when you use CubeMX to modify the configuration again, the program outside the USER CODE range will be cleared)
int main(void)
{
    
    
  /* USER CODE BEGIN 1 */
  /* USER CODE END 1 */
  /* MCU Configuration--------------------------------------------------------*/
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
  /* USER CODE BEGIN Init */
  /* USER CODE END Init */
  /* Configure the system clock */
  SystemClock_Config();
  /* USER CODE BEGIN SysInit */
  /* USER CODE END SysInit */
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  /* USER CODE BEGIN 2 */
  /* USER CODE END 2 */
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    
    
    HAL_GPIO_WritePin(GPIOC,GPIO_PIN_0,GPIO_PIN_RESET);	//LED1亮
	HAL_GPIO_WritePin(GPIOC,GPIO_PIN_0,GPIO_PIN_SET);	//LED1灭
	HAL_Delay(500);										//延时500ms
	HAL_GPIO_WritePin(GPIOC,GPIO_PIN_1,GPIO_PIN_RESET);	//LED2亮
	HAL_GPIO_WritePin(GPIOC,GPIO_PIN_1,GPIO_PIN_SET);	//LED2灭
	HAL_Delay(500);										//延时500ms
	/* USER CODE END WHILE */
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

4. Download verification

  • Compile the project and get the following results:

Insert picture description here
As can be seen from the above compilation information, our code occupies the FLASH size: 2956 bytes (2604+352), and the used SRAM size is: 1040 bytes (16+1024). The
following are the meanings of the data in the compilation result :
-Code: indicates the size of FLASH occupied by the program (FLASH)
-RO-data: Read Only-data, indicates the constant defined by the program (FLASH)
-RW-data: Read Write-data, indicates the variable that has been initialized (SRAM)
-ZI-data: Zero Init-data, which means uninitialized variable (SRAM).
With this, you can know the size of flash and sram you are currently using, so it must be noted that the size of the program is not. The size of the hex file, but the sum of the compiled Code and RO-data

  • Select the corresponding emulator to download the program

Insert picture description here

Guess you like

Origin blog.csdn.net/Chuangke_Andy/article/details/108557150