Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

MPPT Code in Picbasic

Status
Not open for further replies.

kalaman

New Member
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 (bold area). 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);

}
}
 
You only need 3 outputs from your stamp to go to the LED driver. You really can't get lower than that, no matter what you use. I'm not really familiar with LED drivers, but this one seems fine. It may be a bit overkill, as it has 12 bit brightness control. If that is useful for you, then it's all good.

Anything you do to control more LED's than you have IOs will require some kind of a shift register like this chip has. In short, anything else will be just about as hard.

The main thing to do in your pbasic routine is control the SIN line , the SCLK line and the XLAT line. You'll also need an oscillator for the GSCLK, but you don't necessarily need to generate that from the stamp if you can get some other kind of oscillator on board.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top