0
\$\begingroup\$

so I've been trying to create a circuit with a PIC16F917, a button, and three LED's, where upon a button press the LED output changes, like the button press cycles through different modes. I've been trying to do this utilising pointers, in a way like a multiplexer, where the value of an integer depicts the certain output mode (interpreted by outputconfig()), and the push of a button increments that integer by 1; but don't really know what's going wrong.

Also, why does XC8 say upon the build, that the use of "delay()" and "outputconfig()" within main() is a "function declared an implicit int"? And why does it still work?

Any help and/or guidance would be much appreciated. Below is the code I am working with. Thankyou!

#include <xc.h>
#include <pic16f917.h>
#include "configbits.h"
#include "definitions.h"

int value;
int *output;
int result;

int main()
{
    TRISEbits.TRISE0=1;

    TRISDbits.TRISD5=0;
    TRISDbits.TRISD6=0;
    TRISDbits.TRISD7=0;

    while(1)
    {
        if(1==IN1)
        {
            delay();
            if(1==IN1)
            {
                output=&value+1;
                result=*output;
                outputconfig();
            }
        }
    }
}

int delay()
{
    int i;
    for(i=0;i<1000;i++){}
}

int outputconfig()
{
    if(0==value)
    {
        OUT1=1;
        OUT2=0;
        OUT3=0;
    }
    if(1==value)
    {
        OUT1=0;
        OUT2=1;
        OUT3=0;
    }
    if(2==value)
    {
        OUT1=0;
        OUT2=0;
        OUT3=1;
    }
    else
    {
        NOP();
    }
}
\$\endgroup\$
15
  • \$\begingroup\$ Your return type is an int, but you aren't returning anything. Change it to void delay () \$\endgroup\$
    – efox29
    Sep 29, 2015 at 4:50
  • \$\begingroup\$ What does the & and * symbols mean when dealing with pointers? What do you think they do? \$\endgroup\$
    – efox29
    Sep 29, 2015 at 4:52
  • \$\begingroup\$ put int outputconfig(); int delay(); above main() \$\endgroup\$
    – masternone
    Sep 29, 2015 at 4:53
  • 2
    \$\begingroup\$ @VigneshVicky, we need to declare the function before we use it (not "initialize" it). We can define the function anywhere: before or after where its used, or even in another compilation unit. \$\endgroup\$
    – The Photon
    Sep 29, 2015 at 5:01
  • 2
    \$\begingroup\$ @efox29: It's not best practice - its functional. An undeclared function is assumed to return int even if it does not. In this case he's lucky that the compiler's assumption is correct. But if one of the functions return float or a pointer or anything that's not int then it's a bug waiting to happen. Either use the function after the function definition or declare the function prototype before using. C allows programs that don't do either to compile but it may or may not result in a correct program. \$\endgroup\$
    – slebetman
    Sep 29, 2015 at 5:28

1 Answer 1

0
\$\begingroup\$

You aren't initialising value or result - in fact although you are assigning a value to result with: *result=output; you never use result again so this line is completely redundant. this line: output=&value+1; says take the address of value, add 1 to it (the location of the place after value ???) and then make output points to it

I think what you meant to do was:

output = &value;    // do this once before the loop to make output point at value
   // or you could initialise it when you declare it like this:
int *output = &value ;

*output = *output+1 // in the loop. increase 'value'

comments by others are correct regarding function declarations

\$\endgroup\$
2
  • \$\begingroup\$ So I place int *output = &value; at the top with the other declared int's and *output = *output+1 within the if statement? \$\endgroup\$
    – ezra_vdj
    Sep 29, 2015 at 23:00
  • \$\begingroup\$ yes. declaring the pointer and initializing to actually point at something is good practice. \$\endgroup\$
    – Icy
    Sep 30, 2015 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.