Click here to Skip to main content
15,867,328 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
I have written the code for perturb and observe(p&o) MPPT algorithm for the microcontroller PIC16F877A in c language, and while compiling, it was showing some errors (like 'Undeclared identifier 'setup_adc' in expression') . Can anyone provide suggestions so that it can be executed in microC.....Please its urgent!!! Refer the code below.

What I have tried:

#if defined(__PCM__)
#include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=16000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#endif

#include <touch.c>
#include <string.h>
#include <stdlib.h>

void main(void)
{
signed int direction;
int pwm;
float voltageold;
float powerold;
float measuredpower;
float measuredvoltage;
float measuredcurrent;
float deltav;
float deltap;

voltageold=0;
powerold=0;
pwm=0x1F;
direction=0.001;

setup_adc(ADC_CLOCK_DIV_32);
setup_adc_ports(ALL_ANALOG);

output_low(PIN_C1);           /* set CCP1 output low */

setup_ccp1(ccp_pwm);          /* setup pin CCP1 (RC2) to do PWM */

setup_timer_2(T2_DIV_BY_1,159,1);   /* 25 KHZ */


while(1)
{
delay_ms(1000);
set_adc_channel(0);     /* select RA */
delay_ms(20);            /* wait to read ADC */
measuredvoltage=read_adc();   /* Read the voltage input from ADC channel 0 */

set_adc_channel(1);           /* select RA1 */
delay_ms(20);                 /* wait to read ADC */
measuredcurrent =read_adc();  /* Read the current input from ADC channel 1 */


measuredpower= measuredvoltage * measuredcurrent;

deltav=measuredvoltage-voltageold;
deltap= measuredpower - powerold;

if(deltap==0)
{
	pwm=pwm;
}

else
{
	if (deltap>0)
	{
		if (deltav>0)
		{
			pwm--;
		}
		else
		{
			pwm++;
		}
	}
	else
	{
		if(deltav<0)
		{
			pwm--;
		}
		else
		{
			pwm++;
		}
	}
}

voltageold =measuredvoltage;
powerold=measuredpower;
set_pwm1_duty(pwm);

}
}
Posted
Updated 2-Jun-21 3:10am
v4
Comments
Dave Kreskowiak 1-Jun-21 15:38pm    
Uh, no. We're not here to do your work for you.
Merin Mariam Jose 2-Jun-21 1:45am    
I didn't ask you to do my work. All I did was ask for suggestions. If you are not willing to help, then you could have ignored the question.
Dave Kreskowiak 2-Jun-21 9:50am    
Actually, you asked people "Can anyone correct the code so that it can be executed in microC". That doesn't sound like suggestions.

Yes, we get a LOT of people begging for code to be written for them. This sat on the edge between that and suggestions.
jeron1 1-Jun-21 16:10pm    
You haven't stated that there are any errors. What errors are you refering to?
Merin Mariam Jose 2-Jun-21 1:38am    
It was showing that some statements were undeclared('Undeclared identifier 'setup_adc' in expression p&o.c' to be exact)

1 solution

Not your question, but:

Advice: Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
C++
#if defined(__PCM__)#include <16F877A.h>

#fuses HS, NOWDT, NOPROTECT, NOLVP
#use delay(clock = 16000000)
#use rs232(baud = 9600, xmit = PIN_C6, rcv = PIN_C7)
#endif

#include <touch.c>

#include <string.h>

#include <stdlib.h>

void main(void) {
  signed int direction;
  int pwm;
  float voltageold;
  float powerold;
  float measuredpower;
  float measuredvoltage;
  float measuredcurrent;
  float deltav;
  float deltap;

  voltageold = 0;
  powerold = 0;
  pwm = 0x1F;
  direction = 0.001;

  setup_adc(ADC_CLOCK_DIV_32);
  setup_adc_ports(ALL_ANALOG);

  output_low(PIN_C1); /* set CCP1 output low */

  setup_ccp1(ccp_pwm); /* setup pin CCP1 (RC2) to do PWM */

  setup_timer_2(T2_DIV_BY_1, 159, 1); /* 25 KHZ */

  while (1) {
    delay_ms(1000);
    set_adc_channel(0); /* select RA */
    delay_ms(20); /* wait to read ADC */
    measuredvoltage = read_adc(); /* Read the voltage input from ADC channel 0 */

    set_adc_channel(1); /* select RA1 */
    delay_ms(20); /* wait to read ADC */
    measuredcurrent = read_adc(); /* Read the current input from ADC channel 1 */

    measuredpower = measuredvoltage * measuredcurrent;

    deltav = measuredvoltage - voltageold;
    deltap = measuredpower - powerold;

    if (deltap == 0) {
      pwm = pwm;
    } else {
      if (deltap > 0) {
        if (deltav > 0) {
          pwm--;
        } else {
          pwm++;
        }
      } else {
        if (deltav < 0) {
          pwm--;
        } else {
          pwm++;
        }
      }
    }

    voltageold = measuredvoltage;
    powerold = measuredpower;
    set_pwm1_duty(pwm);

  }
}

Indentation style - Wikipedia[^]
Best C++ Formatter and Beautifier[^]
Online C/C++ Formatter, Indenter and Beautifier – Techie Delight[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
Enabling Open Innovation & Collaboration | The Eclipse Foundation[^]
-----
Quote:
Undeclared identifier 'setup_adc' in expression p&o.c

The compiler tells you that it don't know what is setup_adc, your code never tells what it is.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900