附图5 主电路
[size=15.0000pt]5.2. 参考代码此处给出固定电压输出的DEMO 代码,采用电流模式控制:(此处只给出主函数程序,若要详细代码,请自行到Q群下载)
/*--------Includes--------*/
#include "system_typedef.h"
#include "hp_pid_code.h"
#include "onboard_led.h"
#include "timer.h"
#include "usart.h"
#include "hrpwm.h"
#include "stdio.h"
#include "adc.h"
#include "stdio.h"
#include "string.h"
#define fabs(x) (x < 0 ? -x : x) //¾ø¶ÔÖµ¼ÆËã
#define Err_OK 0x00
#define Err_HV 0x01
#define Err_LV 0x02
#define Err_ 0x03
float32 VIPWR = 0;
float32 VOPWR = 0;
float32 VOEXT = 0;
float32 IOPWR = 0;
float32 PWRIN = 0;
float32 PWROUT = 0;
float32 VoutREF = 24;
float32 VinREF = 28;
uint16 ADC_VIPWR_Value;
uint16 ADC_VOPWR_Value;
uint16 ADC_VOEXT_Value;
uint16 ADC_IOPWR_Value;
uint32 PWM_Period;
uint32 PWM_Duty;
PID_TypeDef pid_voltage_loop;
PID_TypeDef pid_current_loop;
void Get_Voltage_Current(void);
int main(void)
{
static uint64 i = 0;
u8 Status = 0;
GPIO_InitTypeDef GPIO_Struct;
SysTick_Config(SystemCoreClock / 1000);//Systick³õʼ»¯,SysTick end of count event each 1ms
Delay_ms(100);
LED_Init();
LED1_On();
LED2_On();
Delay_ms(200);
LED1_Off();
LED2_Off();
Delay_ms(200);
LED1_On();
LED2_On();
Delay_ms(200);
LED1_Off();
LED2_Off();
//-----------USART1_Rx == PB7-------------//
GPIO_Struct.GPIO_Pin = GPIO_Pin_7;
GPIO_Struct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_Struct.GPIO_OType = GPIO_OType_PP;
GPIO_Struct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Struct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_Struct);
TIMER3_init(0.1e3);
USART1_init(115200);
PWM_Period = HrPWM_CHA_Init(200e3);//PWM³õʼ»¯
ADC_init();//ADCÍâÉè³õʼ»¯
PID_DeInit( &pid_voltage_loop );
pid_voltage_loop.T = 1000;//PI²ÉÑùÖÜÆÚ£¬µ¥Î»Îªus
pid_voltage_loop.Kp = 0.2;
pid_voltage_loop.Ti = 2000;
pid_voltage_loop.Td = 0;
pid_voltage_loop.OutMax = 5;
pid_voltage_loop.OutMin = -4;
PID_init(&pid_voltage_loop);//µçѹ»·PI²ÎÊý³õʼ»¯
PID_DeInit( &pid_current_loop );
pid_current_loop.T = 10;//PI²ÉÑùÖÜÆÚ£¬µ¥Î»Îªus
pid_current_loop.Kp = 12;
pid_current_loop.Ti = 100;
pid_current_loop.Td = 0;
pid_current_loop.OutMax = 0.90*PWM_Period;
pid_current_loop.OutMin = 0.05*PWM_Period;
PID_init(&pid_current_loop);//µçÁ÷»·PI²ÎÊý³õʼ»¯
Delay_ms(5);
HrPWM_CHA_Update(PWM_Period, 0.05*PWM_Period);
HRTIM_WaveformOutputStart(HRTIM1, HRTIM_OUTPUT_TA1 | HRTIM_OUTPUT_TA2 );
Delay_ms(300);
LED1_Off();
LED2_Off();
while (1)
{
VIPWR = ADC_VIPWR_Value * 3.3/4095.0*30.00;
VOPWR = ADC_VOPWR_Value * 3.3/4095.0*30.00;
VOEXT = ADC_VOEXT_Value * 3.3/4095.0*30.00;
if(((VIPWR < 26 + 1.0) || fabs(IOPWR) > 6.0) && i > 5 )
{
HRTIM_WaveformOutputStop(HRTIM1, HRTIM_OUTPUT_TA1 | HRTIM_OUTPUT_TA2 );
LED2_Off();
if(i%50 == 0)LED1_Toggle();//ָʾµÆ½»ÌæÉÁ˸
}
else
{
LED1_Off();
if(i%50 == 0)LED2_Toggle();//ָʾµÆ½»ÌæÉÁ˸
}
PID_Calc( &pid_voltage_loop, VOPWR, VoutREF); //µçѹ»·PI¼ÆËã
if(i%1000 == 0)
{
printf("Vin = %fv
Vout = %fv
Vext = %fv
Iout = %fA
PWM = %f
", VIPWR, VOPWR, VOEXT, IOPWR, (float)PWM_Duty/PWM_Period);
}
Delay_ms(1);
i++; //¼Æʱ
}
}
void TIM3_IRQHandler(void) //TIM3ÖжÏ
{
if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) {
GPIOB->ODR ^= GPIO_Pin_7;
ADC_IOPWR_Value = ADC_GetInjectedConversionValue(ADC1, ADC_InjectedChannel_1);
ADC_VOPWR_Value = ADC_GetInjectedConversionValue(ADC1, ADC_InjectedChannel_2);
ADC_VOEXT_Value = ADC_GetInjectedConversionValue(ADC1, ADC_InjectedChannel_3);
ADC_VIPWR_Value = ADC_GetInjectedConversionValue(ADC1, ADC_InjectedChannel_4);
IOPWR = -(ADC_IOPWR_Value * 3.3/4095 - 1.585)/0.265;
PWM_Duty = PID_Calc( &pid_current_loop, IOPWR, pid_voltage_loop.Output);
HrPWM_CHA_Update(PWM_Period, PWM_Duty);//¸üÐÂPWMÕ¼¿Õ±È
}
TIM_ClearITPendingBit(TIM3, TIM_IT_Update); //Çå³ýTIMxµÄÖжϴý´¦Àíλ:TIM ÖжÏÔ´
}
#pragma arm section code = "RAMCODE"
void Get_Voltage_Current(void)
{
// IOPWR = (float32)(-((ADC_GetInjectedConversionValue(ADC1, ADC_InjectedChannel_1) * 3.3)/4095.0 - 1.681)/0.201 );
// VOPWR = (float32)((ADC_GetInjectedConversionValue(ADC1, ADC_InjectedChannel_2) * 3.3)/4095.0*31.50);
// VOEXT = (float32)((ADC_GetInjectedConversionValue(ADC1, ADC_InjectedChannel_3) * 3.3)/4095.0*31.60);
// VIPWR = (float32)((ADC_GetInjectedConversionValue(ADC1, ADC_InjectedChannel_4) * 3.3)/4095.0*32.30);
}
#pragma arm section
技术讨论群:474805564
`