Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

XC8 pointer behavior!

Status
Not open for further replies.

Pommie

Well-Known Member
Most Helpful Member
I have converted a program to XC8 and came across the following problem.

This code,
Code:
void SeedRand(void){
char *point=0;
    while((int)point<128){
        Seed.bytes.byte3^=*point++;
        if(Seed.bytes.byte3&0x80){
            Seed.word<<=1;
            Seed.word+=1;
        }else
            Seed.word<<=1;
    }
}
errors on the while line with something about the types being compared aren't the same.

I wondered if it was assuming a rom pointer and so I changed the assignment to *point = &INDF0; to force it to a ram pointer. This warned of a suspicious pointer conversion - volatile to non volatile!!

I have now changed it back and it compiles fine.

Does anyone know how to tell this stupid compiler what kind of pointer you want?

I must say that first impressions of XC8 are not good.

Edit, forgot, this compiled fine on a 18F14K50 and only errored on a 16F1823.
Edit2, just found in the manual that you should never compare pointers to constants. So, how do I write the above code? It's basically reading the first 128 bytes of RAM.

Mike.
 
Last edited:
I would personally use variable for the pointer to use..
C:
void SeedRand(void){
int x = 0;
char *point=x;
    while( x ++ < 128 ){
        Seed.bytes.byte3^=*point++;
        if(Seed.bytes.byte3&0x80){
            Seed.word<<=1;
            Seed.word+=1;
        }else
            Seed.word<<=1;
    }
}

I didn't like the pointer cast to a int anyway!!
 
That code gives a warning: (357) illegal conversion of integer to pointer.

So I tried placing a dummy variable at 0 to point the pointer at,
Code:
char dummy @0x00;
But this wont compile even though the section on absolute variables in the manual says it should. (Search absolute variables)

A bit more reading and I find, (Search Absolute Addressing)
In XC8, change absolute object definitions such as the following example:
int scanMode @ 0x200;
to:
int scanMode __at(0x200);
They could have mentioned that in the other help bit.

So, I change it to
Code:
char dummy __at(0x00);
And, that won't compile either. It expects a comma and closing bracket!!!

When they can't get the help file correct it makes me wonder about the compiler.
[/winge mode]

Mike.
 
What exactly are you trying to do?

If you want to write to a specific memory location, (in the following example 0x20) you write

(*((uint8_t*)(0x20))) = value;
 
I found several similar issues when using MPLABX..... Once or twice I have tried to compile a piece of code in MPLABX with a load of stress bolted on.... Recompiled in MPLAB IDE 8.x and have had no problems at all.
C:
void SeedRand(void){
char x =0;
char*point=0;
   while( x ++<128){
        Seed.bytes.byte3^=*point++;
       if(Seed.bytes.byte3&0x80){
            Seed.word<<=1;
            Seed.word+=1;
       }else
            Seed.word<<=1;
   }
}
Did you try this.....
 
PIC16 memory is not flat. It is divided into banks, each bank spans 128 bytes, but only 80 bytes are useable. When you address it through a pointer (FSR) it will go through the entire 128 bytes for each bank.

The useable 80-byte portions of the banks are mapped together into a linear region at address 0x2000 (as I remeber) were they come one after another. So, to get a 256 bytes of continuous memory, you need to start from 0x2000.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top