#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%
}
}
百度文库里有一个相关文章,建议楼主去下载了看看。有流程图,但没有完整源程序。基于Sepic变换器的变速风力机MPPT系统的研究
http://wenku.baidu.com/view/9602d84ffe4733687e21aa18.html
另外,在百度文库查找“MPPT”能搜出很多相关文章。
*关于频率和占空比的确定,对于6M晶振,假定PWM输出频率为1KHZ,这样可以设定占空比可从(1-100)%变化,即0.01ms*100=1ms。周期用T1定时,输出高电平用T1定时。 *#include <REGX51.H>
#define uchar unsigned char
#define V_TH1 0XFE
#define V_TL1 0X0C
#define V_TMOD 0X11
void init_sys(void) /*系统初始化函数*/
unsigned char ZL,ZH
void main (void)
{
init_sys()
while(1)
{
Unsigned Int temp
int16 zkb=50;
Temp =2^16-5*zkb
ZH = temp/256
ZL = temp%256
K()
}
}
void init_sys(void) /*系统初始化函数*/
{
TMOD=V_TMOD /*定时器初化*/
TH0=ZH
TL0=ZL
TH1 = V_TH1
TL1= V_TL1
TR1 = 1
ET1 = 1
ET0=1 /*允许T0中断
EA=1 /*CPU开中断
}
Void k(int16 vk,ik)
{ static int16 prek
pk=vk*ik
prek=0
vk=0
if(prvpk==pk)
{
return
}
else
{
if(pk>prepk)
{ prek=pk
if(vk>prevk)
{
zkb++
prevk=vk
prepk=pk
}
else
{
zkb--
prevk=vk
prepk=pk
}
}
else
{
if(vk>prevk)
{
zkb++
prevk=vk
prepk=pk
}
else
{
zkb--
prevk=vk
prepk=pk
}
}
}
/*中断函数*/
void timer0(void) interrupt 1
{
P2_2=! P2_2
TR0 = 0
}
Void timer1(void) interrupt 2
{
TH1 = V_TH1 /*恢复定时器T0初始值*/
TL1 = V_TL1
P2_2=! P2_2
TH0=ZH /*恢复定时器T0初始值*/
TL0=ZL
TR0 = 1
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)