PDA

View Full Version : MPPT code in picbasic



showtime
- 29th September 2007, 21:50
Hello my friends,
I have just found a code about MPPT. I couldn't translate in Picbasic. I suppose an error in adjusting pwm duty. If you can translate the code i will be very happy. Thank you for now.
Best regards.

#include <16f876.h>
#DEVICE *=16 ICD=TRUE
#use Delay(Clock=20000000)
#use RS232(Baud=9600,Xmit=PIN_C6,Rcv=PIN_C7,brgh1ok)

float average_ad(byte chan) {

int i,avg_num = 32;
float avg = 0, temp_avg = 0;

set_adc_channel(chan);
delay_us(100);
for (i=1; i<=avg_num; ++i) {
avg += read_adc();
delay_us(100);
}
return(avg/avg_num);
}

void pwm1_duty100(long pwm_percent, long pwm_maximum) {

set_pwm1_duty((pwm_maximum/100)*pwm_percent);
}

main()
{
CONST float CHARGED = 14.4;
CONST long NDELTA = -1;
CONST byte AMP_CHAN = 0, BAMP_CHAN = 3, BVOLT_CHAN = 2, VOLT_CHAN = 1;
float current, voltage, watts = 0.0, old_watts = 0.0;
float batamps, batvolts, batwatts;
float temp_watts, watts100, amp100, volt100;
long delta = 1, pwm_max, pwm100;
long boost = 0, boost_counter;
byte led_count = 0;

output_low(PIN_C3);
setup_port_a(ALL_ANALOG);
setup_adc(adc_clock_div_32);
setup_timer_2(T2_DIV_BY_1, 49, 1);
setup_ccp1(CCP_PWM);
pwm_max = 200;
pwm100 = 85;
pwm1_duty100(pwm100, pwm_max);
output_high(PIN_C3);
output_high(PIN_C0);
delay_ms(1000);

while(TRUE) {
if (batvolts > CHARGED) {
delta = NDELTA;
output_high(PIN_C5);
}
else if (old_watts > watts) {
delta = -delta;
output_low(PIN_C5);
}
old_watts = watts;
pwm100 += delta;
if (pwm100 > 100) pwm100 = 100;
else if (pwm100 < 0) pwm100 = 0;
pwm1_duty100(pwm100, pwm_max);

current = average_ad(AMP_CHAN)/40.885;
voltage = average_ad(VOLT_CHAN)/18.2887;
batamps = average_ad(BAMP_CHAN)/41.3428;
batvolts = average_ad(BVOLT_CHAN)/20.4831;

watts = current * voltage;
batwatts = batamps * batvolts;

output_bit(PIN_C0, (++led_count & 0x01));
delay_ms(500);

}
}

showtime
- 1st October 2007, 21:09
Nobody have to help in this point. Also I don't know if it is a problem wishing a code translation. But please help me. I would like just to know if there is an error in this code.

mister_e
- 1st October 2007, 21:13
There's a really few amount of member here who deal with C... hence why there's no real answer to your previous post. There's also few C variant... CCS, Hi-tech, MikroC etc etc etc

This would help if you tell us what you need to do and if you really understand the MPPT basics...

showtime
- 2nd October 2007, 01:11
Of course my target is MPPT project. But I didn't understand anything. I have got some idea about MPPT in theory but not in code level.