CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Problem with codes for MPPT
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
quium



Joined: 19 Jan 2007
Posts: 27

View user's profile Send private message

Problem with codes for MPPT
PostPosted: Wed Mar 28, 2007 9:32 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Mar 28, 2007 11:25 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Mar 28, 2007 11:41 am     Reply with quote

can i use 4MHz in the above codes to geta pwm of 50Khz
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Mar 28, 2007 11:45 am     Reply with quote

Use the forumla in this thread to see if you can do it.
http://www.ccsinfo.com/forum/viewtopic.php?t=17993
quium



Joined: 19 Jan 2007
Posts: 27

View user's profile Send private message

PostPosted: Wed Mar 28, 2007 11:52 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Mar 28, 2007 12:01 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Mar 28, 2007 12:11 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Mar 28, 2007 12:36 pm     Reply with quote

Hi
The ceramic resontor which I am having is a three pin one and I have connected the capacitors of 12pf as per the information in the data sheet. Here is the datasheet of the ceramic resonator which I am using


http://docs-europe.electrocomponents.com/webdocs/0655/0900766b80655dc4.pdf
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Mar 28, 2007 12:40 pm     Reply with quote

What's the part number of the resonator ?
quium



Joined: 19 Jan 2007
Posts: 27

View user's profile Send private message

PostPosted: Wed Mar 28, 2007 12:43 pm     Reply with quote

CSTLS20M0X53
kevcon



Joined: 21 Feb 2007
Posts: 142
Location: Michigan, USA

View user's profile Send private message

PostPosted: Wed Mar 28, 2007 12:49 pm     Reply with quote

That part has built in capacitors.
quium



Joined: 19 Jan 2007
Posts: 27

View user's profile Send private message

PostPosted: Wed Mar 28, 2007 12:55 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Mar 28, 2007 12:57 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Mar 28, 2007 1:16 pm     Reply with quote

I will make the alteration and check my codes again
Thanks
quium



Joined: 19 Jan 2007
Posts: 27

View user's profile Send private message

PostPosted: Wed Mar 28, 2007 5:38 pm     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group