用stm32cube写stm32f103zet6带8个灯的流水灯,使用外部中断4个按钮分别控制开始,暂停,加速,减速,具体代码
时间: 2024-02-15 18:03:26 浏览: 83
以下是基于STM32CubeIDE的代码实现,用于控制8个LED的流水灯,并使用4个外部中断按钮进行控制:
```c
#include "main.h"
#define LED_COUNT 8
#define LED_DELAY_MS 250
uint8_t ledState[LED_COUNT] = {0};
uint8_t ledIndex = 0;
uint8_t ledSpeed = 1;
uint8_t ledPaused = 0;
void EXTI4_IRQHandler(void) {
if (HAL_GPIO_ReadPin(BTN_START_GPIO_Port, BTN_START_Pin) == GPIO_PIN_SET) {
ledPaused = 0;
} else if (HAL_GPIO_ReadPin(BTN_PAUSE_GPIO_Port, BTN_PAUSE_Pin) == GPIO_PIN_SET) {
ledPaused = 1;
} else if (HAL_GPIO_ReadPin(BTN_SPEED_UP_GPIO_Port, BTN_SPEED_UP_Pin) == GPIO_PIN_SET) {
if (ledSpeed < 8) {
ledSpeed *= 2;
}
} else if (HAL_GPIO_ReadPin(BTN_SPEED_DOWN_GPIO_Port, BTN_SPEED_DOWN_Pin) == GPIO_PIN_SET) {
if (ledSpeed > 1) {
ledSpeed /= 2;
}
}
HAL_GPIO_EXTI_IRQHandler(BTN_START_Pin | BTN_PAUSE_Pin | BTN_SPEED_UP_Pin | BTN_SPEED_DOWN_Pin);
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
// do nothing
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
if (ledPaused) {
return;
}
for (uint8_t i = 0; i < LED_COUNT; i++) {
if (i == ledIndex) {
ledState[i] = 1;
} else {
ledState[i] = 0;
}
}
ledIndex = (ledIndex + 1) % LED_COUNT;
HAL_GPIO_WritePin(LED_1_GPIO_Port, LED_1_Pin, ledState[0] ? GPIO_PIN_SET : GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_2_GPIO_Port, LED_2_Pin, ledState[1] ? GPIO_PIN_SET : GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_3_GPIO_Port, LED_3_Pin, ledState[2] ? GPIO_PIN_SET : GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_4_GPIO_Port, LED_4_Pin, ledState[3] ? GPIO_PIN_SET : GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_5_GPIO_Port, LED_5_Pin, ledState[4] ? GPIO_PIN_SET : GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_6_GPIO_Port, LED_6_Pin, ledState[5] ? GPIO_PIN_SET : GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_7_GPIO_Port, LED_7_Pin, ledState[6] ? GPIO_PIN_SET : GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_8_GPIO_Port, LED_8_Pin, ledState[7] ? GPIO_PIN_SET : GPIO_PIN_RESET);
}
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM3_Init(void);
int main(void) {
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_TIM3_Init();
HAL_TIM_Base_Start_IT(&htim3);
while (1) {
}
}
void SystemClock_Config(void) {
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
Error_Handler();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC | RCC_PERIPHCLK_USB;
PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
PeriphClkInit.USBClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
Error_Handler();
}
}
static void MX_TIM3_Init(void) {
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
htim3.Instance = TIM3;
htim3.Init.Prescaler = 7199;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = LED_DELAY_MS - 1;
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
if (HAL_TIM_Base_Init(&htim3) != HAL_OK) {
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK) {
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK) {
Error_Handler();
}
}
static void MX_GPIO_Init(void) {
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = BTN_START_Pin | BTN_PAUSE_Pin | BTN_SPEED_UP_Pin | BTN_SPEED_DOWN_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = LED_1_Pin | LED_2_Pin | LED_3_Pin | LED_4_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
GPIO_InitStruct.Pin = LED_5_Pin | LED_6_Pin | LED_7_Pin | LED_8_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
HAL_NVIC_SetPriority(EXTI4_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI4_IRQn);
}
void Error_Handler(void) {
__disable_irq();
while (1) {
}
}
```
需要注意的是,这里使用了定时器TIM3作为流水灯的控制器,定时器的中断回调函数`HAL_TIM_PeriodElapsedCallback()`用于实现流水灯的效果,外部中断回调函数`HAL_GPIO_EXTI_Callback()`用于处理外部中断触发的事件。
另外,需要自行定义按钮的引脚和LED的引脚。在这个例子中,使用了PORTA的引脚0-3作为外部中断按钮,使用了PORTC和PORTD的引脚0-3作为LED的控制引脚。
阅读全文