View previous topic :: View next topic |
Author |
Message |
quium
Joined: 19 Jan 2007 Posts: 27
|
Problem with codes for MPPT |
Posted: Wed Mar 28, 2007 9:32 am |
|
|
Hi
Below are my codes for keeping the solar panel at its maximum power point, I am reading battery and solar voltage and curent and later setting up the pwm, But the problem is I am not getting any PWM at ccp1.Is there any problem with my codes, If so please help me out with any suggessions
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 |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Mar 28, 2007 11:25 am |
|
|
Quote: | #include <16f876.h>
#device *=16 ADC=10
#use delay(Clock=20000000)
#fuses XT, NOWDT, NOPROTECT, NOPUT, NOBROWNOUT,NOLVP |
You're running a 20 MHz crystal with XT mode. I've never found
that to work. It won't oscillate. You need to use HS mode. |
|
|
quium
Joined: 19 Jan 2007 Posts: 27
|
|
Posted: Wed Mar 28, 2007 11:41 am |
|
|
can i use 4MHz in the above codes to geta pwm of 50Khz |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
quium
Joined: 19 Jan 2007 Posts: 27
|
|
Posted: Wed Mar 28, 2007 11:52 am |
|
|
I have done the calculation and I am getting the value of pr2 as 19 which is with in the range of 0-255, So I think I can use the 4MHz to get a pwm of 50KHz. |
|
|
quium
Joined: 19 Jan 2007 Posts: 27
|
|
Posted: Wed Mar 28, 2007 12:01 pm |
|
|
I am using a 20Mhz resonator and does it work if I change the fuse setting from XT to HS, Sorry I am a power Engineering student thats why I am asking these basic question and as well new to PIC and CCS C |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Mar 28, 2007 12:11 pm |
|
|
Go to the web page for the 16F876:
http://www.microchip.com/stellent/idcplgidcplg?IdcService=SS_GET_PAGE&nodeId=1335&dDocName=en010239
Download the data sheet. The link is in the upper left corner.
Go to section 12.2 "Oscillator Configurations". There is a table on
the right side of the page. It shows suggested oscillator settings
based on the frequency of the resonator. They don't test it as high
as 20 MHz. But, extrapolating from that table, you should use the
highest speed setting with a 20 MHz resonator. That setting is HS.
Read the data sheet. Most answers are in the data sheet. |
|
|
quium
Joined: 19 Jan 2007 Posts: 27
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Mar 28, 2007 12:40 pm |
|
|
What's the part number of the resonator ? |
|
|
quium
Joined: 19 Jan 2007 Posts: 27
|
|
Posted: Wed Mar 28, 2007 12:43 pm |
|
|
CSTLS20M0X53 |
|
|
kevcon
Joined: 21 Feb 2007 Posts: 142 Location: Michigan, USA
|
|
Posted: Wed Mar 28, 2007 12:49 pm |
|
|
That part has built in capacitors. |
|
|
quium
Joined: 19 Jan 2007 Posts: 27
|
|
Posted: Wed Mar 28, 2007 12:55 pm |
|
|
So in that case I have to remove the 12Pf capacitors that I have connected and then change the fuse setting to HS mode and hopefuly with my above codes I will be able to get a PWM of 50KHz.
Sorry for behaving like a dummy,but I am working on my project and I think I should nothave opted for PIC being an electrical power student |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Mar 28, 2007 12:57 pm |
|
|
Quote: | I have connected the capacitors of 12pf as per the information in the data sheet.
CSTLS20M0X53 |
Look at the CSTLS page in the data sheet. (Page 19 in Acrobat Reader).
http://docs-europe.electrocomponents.com/webdocs/0655/0900766b80655dc4.pdf
On the left side of the page it says:
Quote: | Oscillation circuits do not require external load capacitors. |
On page 3 (in Acrobat Reader) it shows the meaning of the part number.
CS = Ceramic resonator
T = Built-in Capacitance
LS = Round lead type
20M0 = 20.0 MHz
X53 = 3rd overtone mode
Most answers are in the data sheet. PIC data sheet. Resonator data
sheet. Data Sheet. |
|
|
quium
Joined: 19 Jan 2007 Posts: 27
|
|
Posted: Wed Mar 28, 2007 1:16 pm |
|
|
I will make the alteration and check my codes again
Thanks |
|
|
quium
Joined: 19 Jan 2007 Posts: 27
|
|
Posted: Wed Mar 28, 2007 5:38 pm |
|
|
Hi
I have made alterations in the fuse setting changed to HS mode and the resonator as advised by you but still I did not manage to get the required PWM.
Many Thanks |
|
|
|