View previous topic :: View next topic |
Author |
Message |
Arsleosmithe
Joined: 15 Nov 2016 Posts: 3
|
some code problems |
Posted: Tue Nov 15, 2016 12:30 pm |
|
|
I'm having some problems understanding this MPPT code.
What are the exact input pins ? I don't get it.
Sorry I'm not a code expert.
Code: | #include <16f876.h>
#device *=16 ADC=10
#use delay(Clock=20000000)
#fuses XT, NOWDT, NOPROTECT, NOPUT, NOBROWNOUT,NOLVP
#define BATTERY_CHARGED 63
#define night_voltage 61
float mean_adc(byte channel) // Reads the adc port 30 times and gives the mean value
{
int i,mean_total = 30;
float mean = 0,mean1 = 0;
set_adc_channel(channel);
delay_us(100);
for (i=1; i<=mean_total; i++)
{
mean =mean + read_adc();
delay_us(100);
mean1=(mean/mean_total);
}
return(mean1);
}
main()
{
const byte current_channel = 0; // sets the channel one to read solar panel current
const byte voltage_channel = 1; // set to read solar panel voltage
const byte voltage_battery_channel=2;
const byte current_battery_channel=4;// set to read battery voltage
float current; // initialising variables
float battery_voltage;
float voltage;
float power_new = 0.0;
float power_old= 0.0;
long duty_change = 1;
long pwm_value;
setup_adc_ports(ALL_ANALOG); // setting up all port a as analog
setup_adc(adc_clock_div_32);
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_1, 99, 1);// pwm frequency 50KHz
set_pwm1_duty(60)
delay_ms(1000);
while(TRUE) {
current = mean_adc(current_channel);
voltage = mean_adc(voltage_channel);
battery_voltage=mean_adc(voltage_battery_channel);
battery_current=mean_adc(current_battery_channel);
power_new = current * voltage;
if (battery_voltage > 517 || battery_current>670) // 517 corresponding to 14.4 battery voltage and 670 corresponding to 3A
{
set_pwm1_duty(0);
output_high(battery_charged);
}
if (power_old >= power_new) {
duty_change -= duty_change; // decrease duty cycle
}
else if (power_old<power_new)
{
duty_change += duty_change; // increase duty cycle
}
power_old = power_new;
pwm_value =pwm_value+ duty_change;
if (pwm_value > 160)// maximum duty cycle
{
pwm_value = 160;
}
else if (pwm_value< 36) // duty cycle minimum
{
pwm_value = 36;
}
if (voltage<429)//429 CORRESPONDING TO 12V
{
pwm_value=0;
out_high(night_voltage);
}
set_pwm1_duty(pwm_value);
delay_ms(1000);
} // end of while
} //end main
|
|
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Nov 15, 2016 2:56 pm |
|
|
For the used input pins have a look at this code line: Code: | set_adc_channel(channel); | Then, look in the manual for this function and you find: Quote: | Syntax: set_adc_channel (chan [,neg]))
Parameters: chan is the channel number to select. Channel numbers start at 0 and are labeled in the data sheet AN0, AN1. For devices with a differential ADC it sets the positive channel to use. |
The channels are defined as: Code: | const byte current_channel = 0; // sets the channel one to read solar panel current
const byte voltage_channel = 1; // set to read solar panel voltage
const byte voltage_battery_channel=2;
const byte current_battery_channel=4;// set to read battery voltage | Now lookup those pins in the datasheet for your chip. (AN0, AN1, AN2 and AN4)
Code: | #use delay(Clock=20000000)
#fuses XT, NOWDT, NOPROTECT, NOPUT, NOBROWNOUT,NOLVP | I doubt this program was ever tested on real hardware...
The XT fuse only goes to 4MHz. For 20MHz it should be changed to HS. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Tue Nov 15, 2016 8:43 pm |
|
|
Another 'minor' detail NOT commented in the program is what value are the voltage divider resistors for the battery and solar panel voltages AND what are the current load resistors.
It's kinda IMPORTANT to have a schematic with this code !
Without it, the program will generate garbage and kill the batteries...
Jay |
|
|
Arsleosmithe
Joined: 15 Nov 2016 Posts: 3
|
|
Posted: Thu Nov 17, 2016 11:19 am |
|
|
temtronic wrote: | Another 'minor' detail NOT commented in the program is what value are the voltage divider resistors for the battery and solar panel voltages AND what are the current load resistors.
It's kinda IMPORTANT to have a schematic with this code !
Without it, the program will generate garbage and kill the batteries...
Jay |
Well thanks for the heads up... actually found the MPPT code on here.
I have a project on making a boost solar panel MPPT & I'm a power electrical engineer & have like zero programming skills... guess I'll be having LOTS of problems with the programming :( |
|
|
Arsleosmithe
Joined: 15 Nov 2016 Posts: 3
|
|
Posted: Thu Nov 17, 2016 11:21 am |
|
|
ckielstra wrote: | For the used input pins have a look at this code line: Code: | set_adc_channel(channel); | Then, look in the manual for this function and you find: Quote: | Syntax: set_adc_channel (chan [,neg]))
Parameters: chan is the channel number to select. Channel numbers start at 0 and are labeled in the data sheet AN0, AN1. For devices with a differential ADC it sets the positive channel to use. |
The channels are defined as: Code: | const byte current_channel = 0; // sets the channel one to read solar panel current
const byte voltage_channel = 1; // set to read solar panel voltage
const byte voltage_battery_channel=2;
const byte current_battery_channel=4;// set to read battery voltage | Now lookup those pins in the datasheet for your chip. (AN0, AN1, AN2 and AN4)
Code: | #use delay(Clock=20000000)
#fuses XT, NOWDT, NOPROTECT, NOPUT, NOBROWNOUT,NOLVP | I doubt this program was ever tested on real hardware...
The XT fuse only goes to 4MHz. For 20MHz it should be changed to HS. |
Thank you so much for the help..
And about the mode...will just changing the mode to HS solve the problem ? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Thu Nov 17, 2016 3:03 pm |
|
|
Potentially it'll solve that problem. However the error suggests the code is untested, which then tends to imply there may well be other faults.... |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Thu Nov 17, 2016 4:09 pm |
|
|
As with all PIC projects, you should code/compile and run a 1HZ LED program to confirm both hardware and software do function properly.
After that THEN code/compile your MPPT program.
Jay |
|
|
|