stm3按键循环,另一个按键切换并执行另一个循环代码,用标准库写
时间: 2023-12-06 08:45:27 浏览: 79
stm32f103zet6+hal库+按键中断(资源详细适合初学者)
以下是一个基于标准库的示例代码,实现了STM32的按键循环和切换功能。在此示例代码中,我们使用了两个按键,一个用于切换循环代码,另一个用于执行循环代码。当按下第一个按键时,我们会切换到第二个循环代码,并在串口中输出一条提示消息。当按下第二个按键时,我们会回到第一个循环代码,并再次在串口中输出一条提示消息。
```c
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_usart.h"
#include "stm32f10x_exti.h"
#include "misc.h"
#define LED_GPIO GPIOC
#define LED_PIN GPIO_Pin_13
#define BUTTON_GPIO GPIOA
#define BUTTON1_PIN GPIO_Pin_0
#define BUTTON2_PIN GPIO_Pin_1
volatile uint8_t loop1_flag = 0;
volatile uint8_t loop2_flag = 0;
void init_gpio(void);
void init_usart(void);
void init_exti(void);
void loop1(void);
void loop2(void);
int main(void)
{
init_gpio();
init_usart();
init_exti();
while(1)
{
if(loop1_flag)
{
loop1();
}
else if(loop2_flag)
{
loop2();
}
}
}
void init_gpio(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = LED_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(LED_GPIO, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = BUTTON1_PIN | BUTTON2_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(BUTTON_GPIO, &GPIO_InitStructure);
}
void init_usart(void)
{
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
void init_exti(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);
EXTI_InitStructure.EXTI_Line = EXTI_Line0;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource1);
EXTI_InitStructure.EXTI_Line = EXTI_Line1;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void loop1(void)
{
static uint8_t led_state = 0;
GPIO_WriteBit(LED_GPIO, LED_PIN, led_state ? Bit_SET : Bit_RESET);
led_state = !led_state;
USART_SendData(USART1, 'L');
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
void loop2(void)
{
static uint8_t led_state = 0;
GPIO_WriteBit(LED_GPIO, LED_PIN, led_state ? Bit_SET : Bit_RESET);
led_state = !led_state;
USART_SendData(USART1, 'H');
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
void EXTI0_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line0) != RESET)
{
EXTI_ClearITPendingBit(EXTI_Line0);
loop2_flag = !loop2_flag;
loop1_flag = !loop2_flag;
if(loop2_flag)
{
USART_SendData(USART1, 'S');
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
else
{
USART_SendData(USART1, 'R');
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
}
}
void EXTI1_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line1) != RESET)
{
EXTI_ClearITPendingBit(EXTI_Line1);
if(loop1_flag)
{
loop1();
}
else if(loop2_flag)
{
loop2();
}
}
}
```
在以上示例代码中,我们使用了一个循环标志位 `loop1_flag` 和 `loop2_flag` 来切换循环代码。当 `loop1_flag` 为 1 时,我们执行 `loop1` 函数;当 `loop2_flag` 为 1 时,我们执行 `loop2` 函数。在 `EXTI0_IRQHandler` 函数中,我们检测到第一个按键按下时,会切换循环标志位,并在串口中输出提示消息。在 `EXTI1_IRQHandler` 函数中,我们检测到第二个按键按下时,会执行当前循环代码。
阅读全文