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 support@ccsinfo.com

coding problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
stu



Joined: 11 Oct 2006
Posts: 3
Location: south australia

View user's profile Send private message

coding problem
PostPosted: Wed Oct 11, 2006 9:26 pm     Reply with quote

Hi,

im trying to build a 16F876A based maximum power point tracker (dc-dc converter). the role of the PIC is to adjust the PWM duty cycle to track the optimal output power/current. my code compiles ok but the PIC just sits there doing nothing.

Ive consulted friends and the compilers help directory but still havent found the problem.

The PIC was tested with a quick asm code and performed as expected.

im using pcw compiler version 3.6.0.93.

Can anybody see anything obvious that im doing wrong or havent done??

Thanks in advance,

Stuart.

Code:

#include <16f876a.h>
#DEVICE *=16 ICD=TRUE
#use Delay(Clock=20000000)
// set up serial interface
#use RS232(Baud=9600,Xmit=PIN_C6,Rcv=PIN_C7,brgh1ok)

//-------------------------------------------------------------------------------
// To obtain an unbiased sample of the input, take a few and average
// the set. Using the central limits theorem, we should use at least 30.
//--------------------------------------------------------------------------------
float av_sample(byte pin_to_read)
{
   int i,lim = 30;
   float av = 0;
   set_adc_channel(pin_to_read);
   delay_us(100);
   for (i=1; i<=lim; ++i) {
      av += read_adc();
      delay_us(100);
   }
   return(av/lim);
}

//--------------------------------------------------------------------------------
// The following has a bit of a setup procedure, then enters an infinite
// loop, continually searching for the optimal power point.
//--------------------------------------------------------------------------------
main()
{
   CONST byte Ibatt_pin = 0;
   //only consider output current as this is roughly proportional to P_out
   float Psend, Ibatt = 0.0, old_Ibatt = 0.0;
   long delta = 1, pwm_coeff, duty;
   byte led = 0;

   output_low(PIN_C3);         // turn off FETs by clearing EN pin on FET driver
   setup_port_a(ALL_ANALOG);      // set up A/D ports
   setup_adc(adc_clock_div_32);   // start A/D clocks
   setup_timer_2(T2_DIV_BY_1, 49, 1);   // set pwm frequency to 100kHz
   setup_ccp1(CCP_PWM);         // Set CCP1 as a PWM
   pwm_coeff = 2;            // so PWM = 100% @duty = 100
   duty = 85;            // set init duty cycle to 85%
   set_pwm1_duty(pwm_coeff*duty);   // set initial PWM duty cycle
   output_high(PIN_C3);         // set enable pin on FET driver
   output_high(PIN_C0);         // turn on heartbeat led
   delay_ms(1000);

   while(TRUE)            // loop forever
   {
      if (old_Ibatt > Ibatt)      // past the peak?
      {
         delta = -delta;         // then reverse the direction of search
      }

      old_Ibatt = Ibatt;         // remember the old value for comparison
      duty += delta;            // keep climbing

      if (duty > 100)              // check limits of duty cyle
      {
          duty = 100;
      }
      else if (duty < 0)
      {
          duty = 0;
      }
      set_pwm1_duty(pwm_coeff*duty);   // restet actual duty cycle

      Ibatt = av_sample(Ibatt_pin)/41;   // approximate fudge factor to convert to Amps

      Psend = Ibatt*14;               // As P=VI and output voltage = 14V

      printf("%c\r\n", Psend);          //send power info via RS232

      output_bit(PIN_C0,(++led & 0x01));   //Flash LED so know prog hasn't frozen
      delay_ms(500);
   }
}


Last edited by stu on Thu Oct 12, 2006 12:01 am; edited 2 times in total
stu



Joined: 11 Oct 2006
Posts: 3
Location: south australia

View user's profile Send private message

PostPosted: Wed Oct 11, 2006 9:36 pm     Reply with quote

Thanks for the editing tip PCM programmer.

Last edited by stu on Thu Oct 12, 2006 12:03 am; edited 1 time in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Oct 11, 2006 10:45 pm     Reply with quote

Quote:
for (i=1; i<lim> Ibatt)

Notice how this section of your code (in the first part) didn't post
correctly. You need to edit your post, and delete the whole first
section, and make a fresh copy and paste from your original program.

Except this time, disable HTML when you post it. Look for the checkbox
just below the posting window. Select that box. Example:
Quote:
x Disable HTML in this post
kypec



Joined: 20 Sep 2003
Posts: 54

View user's profile Send private message

PostPosted: Thu Oct 12, 2006 12:41 am     Reply with quote

There is missing oscillator setting in your example, you should specify what is the clock source for your PIC in this application (XT or HS or IntRC?)
Moreover, you have included ICD=TRUE statement. Unless you use the ICD unit to program and run your target application, you should omit that statement, otherwise your PIC expects it is controlled from the external ICD debugger hardware and will not run alone.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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