使用stm32f103rct6标准库写用MPPT算法控制PWM波使电压输出恒为30V的代码
时间: 2023-07-16 14:11:44 浏览: 183
以下是一个简单的基于STM32F103RCT6的MPPT算法控制PWM输出电压的代码示例:
```c
#include "stm32f10x.h"
#define PWM_PERIOD 2000 // PWM周期,单位为us
#define V_REF 3.3 // 参考电压,单位为V
#define V_OUT_TARGET 30.0 // 目标输出电压,单位为V
#define V_STEP 0.1 // 电压调节步进,单位为V
float adc_value = 0.0;
float v_out = 0.0;
float duty_cycle = 0.0;
float v_max = 0.0;
void ADC_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_ADCCLKConfig(RCC_PCLK2_Div6); // ADC时钟为APB2时钟的1/6
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_ADC1, ENABLE); // 使能GPIOA和ADC1时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure); // 配置PA0为模拟输入
ADC_DeInit(ADC1);
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_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while (ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while (ADC_GetCalibrationStatus(ADC1));
}
void PWM_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = PWM_PERIOD - 1;
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &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_OC4Init(TIM4, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM4, ENABLE);
TIM_Cmd(TIM4, ENABLE);
}
void MPPT_Control(void)
{
adc_value = ADC_GetConversionValue(ADC1) * V_REF / 4096.0; // 获取ADC采集值并转换为电压值
v_out = adc_value * (V_OUT_TARGET / V_REF); // 计算当前输出电压
if (v_out < V_OUT_TARGET) // 如果输出电压小于目标电压
{
duty_cycle += V_STEP / V_OUT_TARGET; // 增加占空比
}
else // 如果输出电压大于等于目标电压
{
duty_cycle -= V_STEP / V_OUT_TARGET; // 减小占空比
}
if (duty_cycle > 1.0) duty_cycle = 1.0; // 占空比限制在0到1之间
if (duty_cycle < 0.0) duty_cycle = 0.0;
TIM_SetCompare4(TIM4, (uint16_t)(duty_cycle * PWM_PERIOD)); // 设置PWM占空比
}
int main(void)
{
ADC_Configuration();
PWM_Configuration();
while(1)
{
MPPT_Control(); // 执行MPPT控制算法
}
}
```
其中,ADC采集的电压值通过MPPT算法计算得到需要调节的PWM占空比,然后通过TIM_SetCompare4()函数设置PWM占空比来控制输出电压。需要注意的是,此处的PWM周期为2000us,也就是500Hz的频率,你可以根据具体需求进行调整。
阅读全文