1. 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.
    Dismiss Notice

XC8 pointer behavior!

Discussion in 'Microcontrollers' started by Pommie, May 29, 2014.

  1. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,023
    Likes:
    317
    Location:
    Brisbane Australia
    ONLINE
    I have converted a program to XC8 and came across the following problem.

    This code,
    Code (text):

    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: May 29, 2014
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,170
    Likes:
    910
    Location:
    Rochdale UK
    I would personally use variable for the pointer to use..
    Code (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!!
     
  3. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,023
    Likes:
    317
    Location:
    Brisbane Australia
    ONLINE
    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 (text):

    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)
    They could have mentioned that in the other help bit.

    So, I change it to
    Code (text):

    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.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland

    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;
     
  6. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,170
    Likes:
    910
    Location:
    Rochdale UK
    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.
    Code (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.....
     
  7. NorthGuy

    NorthGuy Well-Known Member

    Joined:
    Sep 8, 2013
    Messages:
    1,218
    Likes:
    206
    Location:
    Northern Canada
    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.
     
  8. Mosaic

    Mosaic Well-Known Member

    Joined:
    Jun 3, 2010
    Messages:
    2,569
    Likes:
    128
    Location:
    Caribbean
    That doesn't seem right Northguy, are u talking about EPROM?
     
  9. Mike - K8LH

    Mike - K8LH Well-Known Member

    Joined:
    Jan 22, 2005
    Messages:
    3,637
    Likes:
    109
    Location:
    Michigan, USA
    He's referring to the linear mapping of RAM on the "enhanced mid-range" devices, which includes the 16F1823.
     
  10. NorthGuy

    NorthGuy Well-Known Member

    Joined:
    Sep 8, 2013
    Messages:
    1,218
    Likes:
    206
    Location:
    Northern Canada
    True.
     

Share This Page