用stm32和Keil5完成:使用按键/ADC/LED,实现电位器控制LED亮度,按键控制LED以该亮度进行呼吸灯开关和闪烁分模块代码
时间: 2024-03-13 15:48:17 浏览: 79
好的,下面是用STM32和Keil5实现电位器控制LED亮度,按键控制LED以该亮度进行呼吸灯开关和闪烁的分模块代码:
```c
//头文件
#include "stm32f10x.h"
#include "delay.h"
//宏定义
#define LED_Pin GPIO_Pin_13
#define LED_Port GPIOC
#define KEY_Pin GPIO_Pin_0
#define KEY_Port GPIOA
#define ADC_Channel ADC_Channel_0
#define ADC_Port GPIOA
#define ADC_Pin GPIO_Pin_0
#define ADC_CLK RCC_APB2Periph_ADC1
//变量定义
__IO uint16_t ADC_Value = 0;
__IO uint16_t LED_Brightness = 0;
__IO uint8_t LED_State = 0;
//函数声明
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void ADC_Configuration(void);
void TIM_Configuration(void);
void EXTI_Configuration(void);
void LED_Breath(void);
void LED_Flash(void);
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
NVIC_Configuration();
ADC_Configuration();
TIM_Configuration();
EXTI_Configuration();
while(1)
{
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
ADC_Value = ADC_GetConversionValue(ADC1);
LED_Brightness = ADC_Value / 4; //将ADC值映射到0-255
if(LED_State == 1)
{
LED_Breath();
}
else if(LED_State == 2)
{
LED_Flash();
}
else
{
GPIO_WriteBit(LED_Port, LED_Pin, Bit_RESET);
}
}
}
//RCC配置
void RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC | RCC_APB2Periph_ADC1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
}
//GPIO配置
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//LED端口配置
GPIO_InitStructure.GPIO_Pin = LED_Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(LED_Port, &GPIO_InitStructure);
//按键端口配置
GPIO_InitStructure.GPIO_Pin = KEY_Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(KEY_Port, &GPIO_InitStructure);
//ADC端口配置
GPIO_InitStructure.GPIO_Pin = ADC_Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(ADC_Port, &GPIO_InitStructure);
}
//NVIC配置
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
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);
}
//ADC配置
void ADC_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel, 1, ADC_SampleTime_28Cycles5);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
}
//定时器配置
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_InitStructure;
TIM_InitStructure.TIM_Period = 1000 - 1;
TIM_InitStructure.TIM_Prescaler = 72 - 1;
TIM_InitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_InitStructure);
TIM_Cmd(TIM3, ENABLE);
}
//外部中断配置
void EXTI_Configuration(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
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);
}
//LED呼吸灯
void LED_Breath(void)
{
uint8_t i = 0;
for(i = 0; i < LED_Brightness; i++)
{
GPIO_WriteBit(LED_Port, LED_Pin, Bit_SET);
delay_ms(10);
GPIO_WriteBit(LED_Port, LED_Pin, Bit_RESET);
delay_ms(10);
}
for(i = LED_Brightness; i > 0; i--)
{
GPIO_WriteBit(LED_Port, LED_Pin, Bit_SET);
delay_ms(10);
GPIO_WriteBit(LED_Port, LED_Pin, Bit_RESET);
delay_ms(10);
}
}
//LED闪烁
void LED_Flash(void)
{
GPIO_WriteBit(LED_Port, LED_Pin, Bit_SET);
delay_ms(500);
GPIO_WriteBit(LED_Port, LED_Pin, Bit_RESET);
delay_ms(500);
}
//外部中断回调函数
void EXTI0_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line0) != RESET)
{
EXTI_ClearITPendingBit(EXTI_Line0);
LED_State++;
if(LED_State > 2)
{
LED_State = 0;
}
}
}
```
其中,`delay.h`是自己编写的延时函数头文件,可以根据自己的需要进行修改。`LED_Breath`函数实现了LED呼吸灯效果,`LED_Flash`函数实现了LED闪烁效果。在`main`函数中,通过ADC读取电位器的值,并将其映射到0-255的范围内,然后根据按键状态选择LED的显示模式。按下按键,按照呼吸灯-闪烁-关闭的循环顺序进行。
阅读全文