(使用直流电机点亮LED. 发电机??? NO?YES?)
原理:采集电机输入的电压信号(adc)动态改变PWM
STM32_Study: STM32_Study (gitee.com)
不逼逼 贴代码
motor_led.c
/**
* @file motor_led.c
* @author WaterFairy (995637517@qq.com)
* @brief 由电机信号强弱点灯
* @version 1.0.0
* @date 2022-04-28 14:04:59
*
* 1.电机负极接GND,正极接PA1.(反接也可;PA1:adc采集端口)
* 2.PA8接PC13(PA8:PWM输出端;PC13:LED负极端口)
* @copyright Copyright (c) 2022
*
*/
#include "stm32f10x.h"
#include <stdio.h>
/**
* @brief 初始化adc
* 由电机转动输入电压信号
*
*/
void init_adc(void)
{
// 1. gpio 配置
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitTypeDef gpio_structure;
gpio_structure.GPIO_Mode = GPIO_Mode_AIN; //模拟输入
gpio_structure.GPIO_Pin = GPIO_Pin_1; // pin_1
gpio_structure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &gpio_structure);
// 2. adc 配置
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
RCC_ADCCLKConfig(RCC_PCLK2_Div6); //分频
ADC_InitTypeDef adc_structure; // adc结构体
adc_structure.ADC_Mode = ADC_Mode_Independent; //独立模式(单adc模式) / 双adc模式 = 单
adc_structure.ADC_DataAlign = ADC_DataAlign_Right; //对齐方式 = 右对齐
adc_structure.ADC_ContinuousConvMode = DISABLE; //连续模式 = 非
adc_structure.ADC_ScanConvMode = DISABLE; //扫描模式 = 非
adc_structure.ADC_NbrOfChannel = 1; //通道数 = 1
adc_structure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //外部触发转换源 = 使用软件触发
ADC_Init(ADC1, &adc_structure);
// 3. 规则组通道配置
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_239Cycles5);
// 4. 开启adc / 开启软件转换
ADC_Cmd(ADC1, ENABLE);
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
// 5. 校准配置
ADC_ResetCalibration(ADC1);
while (ADC_GetResetCalibrationStatus(ADC1))
;
ADC_StartCalibration(ADC1);
while (ADC_GetCalibrationStatus(ADC1))
;
}
/**
* @brief 初始化pwm
* 由电压信号转换波形信号
*
*/
void init_pwm(void)
{
// 1.gpio
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitTypeDef gpio_structure;
gpio_structure.GPIO_Mode = GPIO_Mode_AF_PP; //输入模式
gpio_structure.GPIO_Pin = GPIO_Pin_8; //引脚
gpio_structure.GPIO_Speed = GPIO_Speed_50MHz; //速度
GPIO_Init(GPIOA, &gpio_structure);
// 2.tim
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
TIM_InternalClockConfig(TIM1);
TIM_TimeBaseInitTypeDef tim_base_structure;
tim_base_structure.TIM_ClockDivision = TIM_CKD_DIV1; //时钟分频
tim_base_structure.TIM_CounterMode = TIM_CounterMode_Up; //计数方式
tim_base_structure.TIM_Period = 4096 - 1; //周期
tim_base_structure.TIM_Prescaler = 72 - 1; //预分频
tim_base_structure.TIM_RepetitionCounter = 0; //重新计数
TIM_TimeBaseInit(TIM1, &tim_base_structure);
TIM_ClearFlag(TIM1, TIM_FLAG_Update);
TIM_Cmd(TIM1, ENABLE);
// 3.pwm
TIM_OCInitTypeDef tim_oc_structure;
TIM_OCStructInit(&tim_oc_structure);
tim_oc_structure.TIM_OCMode = TIM_OCMode_PWM1; // PWM1模式
tim_oc_structure.TIM_OCPolarity = TIM_OCPolarity_Low; //占空比内输出低电平
tim_oc_structure.TIM_OutputState = TIM_OutputState_Enable; //输出
tim_oc_structure.TIM_Pulse = 0; //默认0 ,阈值 CCR Capture Compare Register
TIM_OC1Init(TIM1, &tim_oc_structure);
//注:高级定时器使用(STM1)
TIM_CtrlPWMOutputs(TIM1, ENABLE); // MOE 主输出使能
// 4.nvic
TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE); // TIM1中断配置
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); // NVIC 优先级分组
NVIC_InitTypeDef nvic_structure;
nvic_structure.NVIC_IRQChannel = TIM1_UP_IRQn;
nvic_structure.NVIC_IRQChannelCmd = ENABLE;
nvic_structure.NVIC_IRQChannelPreemptionPriority = 1;
nvic_structure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&nvic_structure);
}
/**
* @brief
* 获取adc值
*/
uint16_t get_motor_led_adc_value(void)
{
uint8_t times = 10; //采样次数
uint32_t total_value = 0; //采样总值
for (uint8_t i = 0; i < times; i++)
{
ADC_SoftwareStartConvCmd(ADC1, ENABLE); //使能指定的 ADC1 的软件转换启动功能
while (!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC)) //规则组/注入组转换结束
;
total_value += ADC_GetConversionValue(ADC1);
}
return total_value / times; //求平均
}
/**
* @brief TIM1的更新中断:TIM1_UP_IRQHandler
* 动态改变输出波形
* 关键:TIM_SetCompare1
* 为避免其它文件重定义该函数,注销该代码.如使用,请打开.
*/
void TIM1_UP_IRQHandler(void)
{
if (TIM_GetITStatus(TIM1, TIM_IT_Update))
{
//电压
uint16_t voltage = get_motor_led_adc_value();
printf("voltage:%f\n", (float)voltage / 4096 * 3.3);
TIM_SetCompare1(TIM1, voltage);
//手动清除中断标志位
TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
}
}
void motor_led_init(void)
{
init_adc();
init_pwm();
}
motor_led.h
/**
* @file motor_led.h
* @author WaterFairy (995637517@qq.com)
* @brief
* @version 1.0.0
* @date 2022-04-28 14:04:08
*
* @copyright Copyright (c) 2022
*
*/
#ifndef __MOTOR_LED_H__
#define __MOTOR_LED_H__
void motor_led_init(void);
#endif
网友评论