用keil4写一个STM32F103中断控制4个流水灯C语言代码,要求按键1控制流水灯方向,按键2控制流水灯亮灭。
时间: 2023-10-23 21:08:25 浏览: 129
好的,以下是符合你要求的C语言代码:
```
#include "stm32f10x.h"
#define LED_GPIO GPIOB
#define LED_PIN GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15
#define KEY1_GPIO GPIOA
#define KEY1_PIN GPIO_Pin_0
#define KEY2_GPIO GPIOA
#define KEY2_PIN GPIO_Pin_1
uint8_t direction = 1; // 流水灯方向,1为向右,-1为向左
uint8_t led_on = 1; // 流水灯亮灭状态,1为亮,0为灭
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* LED GPIO 串口口配置 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = LED_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(LED_GPIO, &GPIO_InitStructure);
/* KEY1 GPIO 串口口配置 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = KEY1_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(KEY1_GPIO, &GPIO_InitStructure);
/* KEY2 GPIO 串口口配置 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = KEY2_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(KEY2_GPIO, &GPIO_InitStructure);
}
void EXTI_Configuration(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* KEY1 EXTI 串口口配置 */
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);
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* KEY2 EXTI 串口口配置 */
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 = EXTI1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void delay_ms(uint32_t n)
{
uint32_t i, j;
for (i = 0; i < n; i++)
for (j = 0; j < 7200; j++);
}
int main(void)
{
GPIO_Configuration();
EXTI_Configuration();
while (1)
{
if (led_on)
{
if (direction == 1)
{
LED_GPIO->ODR = GPIO_Pin_12;
delay_ms(100);
LED_GPIO->ODR = GPIO_Pin_13;
delay_ms(100);
LED_GPIO->ODR = GPIO_Pin_14;
delay_ms(100);
LED_GPIO->ODR = GPIO_Pin_15;
delay_ms(100);
}
else
{
LED_GPIO->ODR = GPIO_Pin_15;
delay_ms(100);
LED_GPIO->ODR = GPIO_Pin_14;
delay_ms(100);
LED_GPIO->ODR = GPIO_Pin_13;
delay_ms(100);
LED_GPIO->ODR = GPIO_Pin_12;
delay_ms(100);
}
}
else
{
LED_GPIO->ODR = 0x0000;
}
}
}
void EXTI0_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line0) != RESET)
{
EXTI_ClearITPendingBit(EXTI_Line0);
direction = -direction;
}
}
void EXTI1_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line1) != RESET)
{
EXTI_ClearITPendingBit(EXTI_Line1);
led_on = !led_on;
}
}
```
这段代码使用了STM32F103的4个GPIO口控制4个LED灯,使用了2个GPIO口作为按键输入控制流水灯的方向和亮灭状态,使用了EXTI外部中断来实现按键的检测。具体实现细节请参考代码注释。
阅读全文