使用STM32的外部中断控制四个外部独立按键分别控制TIM2时钟下的 PWM输出的四路方波点亮的LED灯的亮灭的完整代码及外部独立按键和LED的控制引脚
时间: 2024-03-19 10:41:15 浏览: 87
STM32-控制PA3TIM2-CH4输出PWM值,stm32的pwm输出,C,C++
好的,下面是使用STM32外部中断控制四个外部独立按键分别控制TIM2时钟下的PWM输出的四路方波点亮的LED灯的亮灭的完整代码及外部独立按键和LED的控制引脚。
```c
#include "stm32f10x.h"
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void EXTI_Configuration(void);
void TIM2_Configuration(void);
int main(void)
{
GPIO_Configuration();
NVIC_Configuration();
EXTI_Configuration();
TIM2_Configuration();
while(1)
{
}
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
// 配置外部独立按键引脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 配置LED灯引脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
// 配置外部中断0所在的中断向量表
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// 配置外部中断1所在的中断向量表
NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
NVIC_Init(&NVIC_InitStructure);
// 配置外部中断2所在的中断向量表
NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;
NVIC_Init(&NVIC_InitStructure);
// 配置外部中断3所在的中断向量表
NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn;
NVIC_Init(&NVIC_InitStructure);
}
void EXTI_Configuration(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource1);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource2);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource3);
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);
EXTI_InitStructure.EXTI_Line = EXTI_Line1;
EXTI_Init(&EXTI_InitStructure);
EXTI_InitStructure.EXTI_Line = EXTI_Line2;
EXTI_Init(&EXTI_InitStructure);
EXTI_InitStructure.EXTI_Line = EXTI_Line3;
EXTI_Init(&EXTI_InitStructure);
}
void TIM2_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 999; // 设置定时器周期为1ms
TIM_TimeBaseStructure.TIM_Prescaler = 71; // 设置时钟分频系数为72,得到定时器时钟为1MHz
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_OC3Init(TIM2, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_OC4Init(TIM2, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM2, ENABLE);
TIM_Cmd(TIM2, ENABLE);
}
void EXTI0_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line0) != RESET)
{
TIM_SetCompare1(TIM2, 250); // 设置PWM输出的占空比为25%
GPIO_SetBits(GPIOB, GPIO_Pin_8); // 点亮LED1
EXTI_ClearITPendingBit(EXTI_Line0);
}
}
void EXTI1_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line1) != RESET)
{
TIM_SetCompare2(TIM2, 500); // 设置PWM输出的占空比为50%
GPIO_SetBits(GPIOB, GPIO_Pin_9); // 点亮LED2
EXTI_ClearITPendingBit(EXTI_Line1);
}
}
void EXTI2_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line2) != RESET)
{
TIM_SetCompare3(TIM2, 750); // 设置PWM输出的占空比为75%
GPIO_SetBits(GPIOB, GPIO_Pin_10); // 点亮LED3
EXTI_ClearITPendingBit(EXTI_Line2);
}
}
void EXTI3_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line3) != RESET)
{
TIM_SetCompare4(TIM2, 999); // 设置PWM输出的占空比为100%
GPIO_SetBits(GPIOB, GPIO_Pin_11); // 点亮LED4
EXTI_ClearITPendingBit(EXTI_Line3);
}
}
```
其中,外部独立按键引脚为PA0、PA1、PA2、PA3,LED灯引脚为PB8、PB9、PB10、PB11。在`EXTI0_IRQHandler()`、`EXTI1_IRQHandler()`、`EXTI2_IRQHandler()`、`EXTI3_IRQHandler()`中分别处理四个外部中断,设置PWM输出的占空比和点亮对应的LED灯。在`TIM2_Configuration()`中配置了TIM2的基本参数和四个PWM输出通道的参数。
阅读全文