[STM32CubeMX] HAL library experiment interrupt switch lighting and serial communication

1. HAL library experiment interrupt switch lighting topic requirements

Use the GPIOA terminal of the stm32F103 core board to connect one pin to one LED, and one pin to one GPIOB port to connect to a switch (replaced by Dupont line simulation). Program in interrupt mode, when the switch is connected to a high level, the LED lights up; when the switch is connected to a low level, the LED turns off.
Check the table to view the stm32F103C8T6 pin diagram, we choose PA1 to connect to the LED, and PB1 to connect to the switch
insert image description here
insert image description here

2. CubeMX interrupt mode lighting

1. CubeMX project settings

Use CubeMX to select the Stm32F103c8 chip to create a project, and come to the pin definition interface

Set the indicator LED pin PA1, set the pin mode to output mode GPIO_Output,
set the button pin PB1, set the pin to the external interrupt function, connect PB1 to the external interrupt line EXIT1, and the
pin diagram after GPIO_EXIT1 is set is as follows:
insert image description here

For the PA1 pin corresponding to the LED, the default setting is fine.
insert image description here

For the corresponding pin PA1 of the switch, set its trigger mode to rising edge trigger

External Interrupt Mode with Rising edge trigger detectionrising edge
External Interrupt Mode with Falling edge trigger detectionfalling edge
External Interrupt Mode with Rising/Falling edge trigger detectionRising and falling edges
insert image description here

Enable the corresponding external interrupt line, click Enabled
insert image description here

Configure the interrupt priority
(in most cases, it is not necessary to set the interrupt priority, directly use the default interrupt priority set by the interrupt number)
insert image description here

Clock setting
36M is set here
insert image description here

2. Generate code

insert image description here

insert image description here
insert image description here
Click GENERATE CODEto generate a code file, then click opento open the code
insert image description here

3. Write code

The interrupt service function can be found in the gpio.c file in the Keil file

void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)

When it is caught 上升沿and triggers an interrupt, it will enter this function

Then the function will be executed HAL_GPIO_EXTI_Callback(GPIO_Pin). This function is a callback function. When we open it, we can find that there is a weak in front.
The preceding __weak indicates that this function is a virtual function and needs to be rewritten by the user. Then we find a place to write the function
insert image description here
in the main.c fileHAL_GPIO_EXTI_Callback(GPIO_Pin)

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
    
    
	if( GPIO_Pin == GPIO_PIN_1)
	{
    
    
   //

		HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_1);
	}
}

insert image description here
Compile it, no errors
insert image description here

4. Burning

Burn after connecting C8T6 and USB to TTL module
insert image description here

insert image description here
burning success

3. Interrupt mode lighting display

Connect PA1 to the negative pole of the LED, connect the positive pole of the LED to VCC, and use PB1 as a switch
. Show:
insert image description here

4. HAL library experiment serial port interruption topic requirements

Use the serial port interrupt method to redo the serial port communication work last week, and achieve respectively:
1) When the stm32 receives the character "s", it stops sending "hello windows!"; when it receives the character "t", it continues to send "hello windows!" windows!" (reminder: use a global scalar as a semaphore);
2) When stm32 receives the character "stop stm32!", stop sending "hello windows!"; when receiving the character "go stm32!", keep sending "Hello windows!" (Hint: save the received continuous characters into a character array for discriminative matching. Write a function to receive a string.

5. CubeMX serial port interruption

1. CubeMX configuration

Refer to the lighting configuration of the interrupt mode, only some configurations are different
insert image description here
Serial port interrupt enable
insert image description here
insert image description here
insert image description here

write code

Add the following function under main.c

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
  if(huart->Instance==USART1)
  {
   
    
    HAL_UART_Receive_IT(&huart1,&RxBuffer,1);
   

    
    if(RxBuffer=='s')
    {
      RxFlag=0;//ÊäÈës
      HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_13);
    }
    else if(RxBuffer=='t')
    {
      RxFlag=1;             //ÊäÈët
      HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_13);
    }
  }
  
}

The main function is as follows

int main(void)
{
    
    
  uint8_t  Hello[]={
    
    "hello windows!"};
  HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();
  MX_USART1_UART_Init();
  HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_SET);
 HAL_UART_RxCpltCallback(&huart1);
  while (1)
  {
    
    
     if(RxFlag==1)
    {
    
    
      HAL_Delay(100);
      HAL_UART_Transmit_IT(&huart1,(uint8_t*)Hello,14);
    }
  }
}

6. Display

QQ screen recording 20221021162834

Guess you like

Origin blog.csdn.net/qq_52201641/article/details/127395118