求MPPT最大功率点跟踪C程序算法!

最好能给出C51的源程序,小弟拜谢了!
2025-04-17 02:08:08
推荐回答(1个)
回答1:

#include "16F877.h"
#device ADC = 8 // 一个8位寄存器ADC模式
#fuses HS, NOWDT, PUT, NOPROTECT, BROWNOUT, NODEBUG, NOLVP // High-Speed 20MHz, No Watchdog, No Protection, Brownout Protection,
#use delay(clock=20000000) // 20MHz Crystal
//int is defined as 8-bit unsigned integer using CCS compiler

void main (void)
{

signed int direction;

int delta;
int pwm;
int upperbound;
int lowerbound;

float power;
float powerold;
float voltage;
float voltagedrop;
float voltagedifference;
float currentma;
float measuredvoltage;
float measuredvoltagedrop;

direction = 1; // Set initial direction to positive
delta = 1; // Amount by which to adjust the PWM - 7-bit resolution so duty step of 2%
pwm = 26; // Initial position of the PWM - 50% Duty Cycle with 7-bit resolution.
upperbound = 49; // Upper bound of the PWM %
lowerbound = 1; // Lower bound of the PWM %
power = 0; // Initial Value of Power

setup_adc(ADC_CLOCK_DIV_32); // ADC clock
setup_adc_ports(ALL_ANALOG); // Set all inputs to analog

output_low(PIN_C1); // Set CCP1 output low
setup_ccp1(ccp_pwm); // setup pin CCP1 (RC2) to do PWM
setup_timer_2(T2_DIV_BY_1,12,1); // 384.615kHz

while (1)
{

//delay_ms(1000) // Wait 1 Second

set_adc_channel(0); // Select RA0
//delay_ms(20); // Wait to Read ADC
measuredvoltage = read_adc(); // Read the voltage input from ADC channel 0
set_adc_channel(1); // Select RA1
//delay_ms(20); // Wait to Read ADC
measuredvoltagedrop = read_adc(); // Read the Voltage dropped across the R from ADC channel 1

voltage = measuredvoltage/51; // Measured Voltage is 51 steps per Volt at a Reference Voltage of 5V
voltagedrop = measuredvoltagedrop/51;
voltagedifference = voltage - voltagedrop;

currentma = voltagedifference; // Calculating Current using 1K Resistance

powerold = power; // Calculate the Power from the inputs
power = voltage * currentma;

pwm = pwm + direction*delta; // Adjust Pulse Width Modulation Value by Delta value

if (power < powerold) // If at top of curve, change direction
{
direction = -direction;
continue;
}

if (pwm > upperbound) // If at maximum PWM, Stop here
{
pwm = upperbound;
continue;
}

if (pwm < lowerbound) // If at minimum PWM, Stop here
{
pwm = lowerbound;
continue;
}

set_pwm1_duty(pwm); // Set PWM Mark-Space Radio to approx 50%

}
}