用stm32和Keil5完成:使用按键/ADC/LED,实现电位器控制LED亮度,按键控制LED以该亮度进行呼吸灯开关和闪烁代码
时间: 2024-03-07 10:51:27 浏览: 134
以下是一个简单的示例代码,仅供参考:
```
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_adc.h"
#include "stm32f10x_tim.h"
#define LED_PIN GPIO_Pin_13
#define LED_PORT GPIOC
#define BUTTON_PIN GPIO_Pin_0
#define BUTTON_PORT GPIOA
#define POT_PIN GPIO_Pin_1
#define POT_PORT GPIOA
#define PWM_MAX 1000
volatile uint32_t adc_value = 0;
volatile uint16_t pwm_value = 0;
volatile uint8_t mode = 0;
void GPIO_Configuration(void) {
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC, ENABLE);
// LED pin
GPIO_InitStructure.GPIO_Pin = LED_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(LED_PORT, &GPIO_InitStructure);
// Button pin
GPIO_InitStructure.GPIO_Pin = BUTTON_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(BUTTON_PORT, &GPIO_InitStructure);
// Potentiometer pin
GPIO_InitStructure.GPIO_Pin = POT_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(POT_PORT, &GPIO_InitStructure);
}
void ADC_Configuration(void) {
ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
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, 1, ADC_SampleTime_55Cycles5);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while (ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while (ADC_GetCalibrationStatus(ADC1));
}
void TIM_Configuration(void) {
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 1000 - 1;
TIM_TimeBaseStructure.TIM_Prescaler = 72 - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &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(TIM3, &TIM_OCInitStructure);
TIM_Cmd(TIM3, ENABLE);
TIM_CtrlPWMOutputs(TIM3, ENABLE);
}
void EXTI0_IRQHandler(void) {
if (EXTI_GetITStatus(EXTI_Line0) != RESET) {
mode++;
if (mode > 2) {
mode = 0;
}
EXTI_ClearITPendingBit(EXTI_Line0);
}
}
void ADC1_IRQHandler(void) {
if (ADC_GetITStatus(ADC1, ADC_IT_EOC) != RESET) {
adc_value = ADC_GetConversionValue(ADC1);
pwm_value = adc_value * PWM_MAX / 4096;
TIM_SetCompare1(TIM3, pwm_value);
ADC_ClearITPendingBit(ADC1, ADC_IT_EOC);
}
}
void delay_ms(uint32_t ms) {
for (volatile uint32_t i = 0; i < ms * 1000; i++);
}
void breathing_light(void) {
uint16_t step = 10;
uint16_t delay_time = 10;
for (uint16_t i = 0; i < PWM_MAX; i += step) {
TIM_SetCompare1(TIM3, i);
delay_ms(delay_time);
}
for (uint16_t i = PWM_MAX; i > 0; i -= step) {
TIM_SetCompare1(TIM3, i);
delay_ms(delay_time);
}
}
void blinking_light(void) {
uint16_t delay_time = 100;
for (uint8_t i = 0; i < 10; i++) {
TIM_SetCompare1(TIM3, PWM_MAX);
delay_ms(delay_time);
TIM_SetCompare1(TIM3, 0);
delay_ms(delay_time);
}
}
int main(void) {
GPIO_Configuration();
ADC_Configuration();
TIM_Configuration();
NVIC_InitTypeDef NVIC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
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);
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);
while (1) {
switch (mode) {
case 0:
break;
case 1:
breathing_light();
break;
case 2:
blinking_light();
break;
default:
break;
}
}
}
```
这段代码仅作为参考,实际应用中需要根据具体硬件平台和需求进行修改和优化。
阅读全文