HAL library experiment interrupt switch lighting and serial port 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
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:
For the PA1 pin corresponding to the LED, the default setting is fine.
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
Enable the corresponding external interrupt line, click Enabled
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)
Clock setting
36M is set here
2. Generate code
Click GENERATE CODE
to generate a code file, then click open
to open the code
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
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);
}
}
Compile it, no errors
4. Burning
Burn after connecting C8T6 and USB to TTL module
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:
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
Serial port interrupt enable
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