1

I tried to write a PIC program with C and what it does is turn on a series of 8 LED's (like Knight Rider LED illumination :D ). I have created the circuit design and tested with a simple c program that sets the state of the lights and it works. Now I want to streamline my code.

So i have created 2 functions for delaying and getting the required HEX value of the LED. the 2 methods are like this.

#define MAX 8
#define LEFT 1
#define RIGHT 2
#define BOTH 0

void delay_ms(int ms) {
    while(ms > 0)
        ms--;
}

int getHex(int delay, int dir, int *pin) {
    int hex[] = {0x01, 0x02, 0x04, 0x10, 0x20, 0x40, 0x80};

    if (dir == RIGHT) {
        for (int i = 0; i < MAX; i++)
        {
            *pin = hex[i];
            delay_ms(delay);
        }
    } else if( dir == LEFT ) {
        for (int i = MAX - 1; i == 0; i--)
        {
            pin = hex[i];
            delay_ms(delay);
        }
    }
}

and i have this in the main() function

main() {
    TRISA = 1;
    TRISB = 0;

    while(1) {

        if(RA0 == 0)
            getHex(5000, RIGHT, PORTB);
    }
}

What im trying to do is pass the PORTB predefined variable as a pointer to the function so it is set with the proper hex value. But i get the following warnings when i compile with the MPLAB IDE,

Warning [357] D:\...\main.c; 24.13 illegal conversion of integer to pointer
Warning [357] D:\...\main.c; 37.22 illegal conversion of integer to pointer

and when the program hex is added and run in the Proteus 8 nothing happens and the following warning appears.

[PIC16 MEMORY]PC=0x03AF. Attempt to write unimplemented memory location 0x0087 with 0x01 ignored
[PIC16 MEMORY]PC=0x03AF. Attempt to write unimplemented memory location 0x0087 with 0x02 ignored
[PIC16 MEMORY]PC=0x03AF. Attempt to write unimplemented memory location 0x0087 with 0x04 ignored

the final Hex value in the error correspond with the hex value i am trying to set as the value of PORTB. What am i doing wrong.. Please help.

5
  • 1
    &42 makes no sense. Values do not have addresses. Commented Aug 11, 2014 at 9:25
  • Please show the definition of PORTB Commented Aug 11, 2014 at 9:31
  • I found this online, This renames the PORTB. static volatile unsigned char LCDOUT @ (unsigned)&PORTB; Here is the link Commented Aug 11, 2014 at 9:35
  • That's some extension of your compiler; it's not standard C Commented Aug 11, 2014 at 9:41
  • 1
    The @ non-standard extension usually means "allocate this variable at (@) this address". Commented Aug 11, 2014 at 10:08

2 Answers 2

2

There are several ways that such pre-defined registers may be declared. I don't know how Mplab does it, but the most common way is this:

#define PORTB (*(volatile uint8_t*)0x1234)

where 0x1234 is the hardware address of that register. This macro accesses the register directly, but the * to the left makes it so that you can use the macro as if PORTB was an ordinary variable and not a pointer.

If this is the case, then you need to pass &PORTB to your function that expects a pointer.

Sign up to request clarification or add additional context in comments.

Comments

1

Changed the getHex Method definition to this.

int getHex(int delay, int dir, int *pin) {
    int hex[] = {0x01, 0x02, 0x04, 0x10, 0x20, 0x40, 0x80};

    if (dir == RIGHT) {
        for (int i = 0; i < MAX; i++)
        {
            *pin = hex[i];
            delay_ms(delay);
        }
    } else if( dir == LEFT ) {
        for (int i = MAX - 1; i == 0; i--)
        {
            *pin = hex[i];
            delay_ms(delay);
        }
    }
}

and then called the function with the following..

getHex(2000, RIGHT, &PORTB);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.