Array of pointers to 16 series port SFRs.

Status
Not open for further replies.

Pommie

Well-Known Member
Most Helpful Member
I need an array of pointers to SFRs in a 16 series chip. The only way I've found so far that doesn't give me warnings is,
Code:
uint8_t * ports[8]={(uint8_t *)&LATB,(uint8_t *)&LATB,(uint8_t *)&LATB,(uint8_t *)&LATB,
                    (uint8_t *)&LATC,(uint8_t *)&LATC,(uint8_t *)&LATC,(uint8_t *)&LATC};
Is there a simpler way to achieve the same thing?

I know there are other ways to code this but eventually it'll have to be an array of pointers.

Thanks,

Mike.
 
Aren't there more than 256 registers in a 16-series chip? So Uint8_t does not have enough bits to identify the register (with bank).

also, aren't the LATB and LATC registers sequential so you only need one of each or, one overall since the LATC registers are next to the LATB registers?

and all possible values of LATC or LATB or LATA are one register (address). What exactly are you trying to do?

 
Last edited:
It's a pointer to a uint8_t and will be 16 (or 14) bit. I used LAT as an example, the actual SFRs I'm writing to are the RBnPPS & RCnPPS registers and I didn't want people to get distracted by the actual registers. The above code seems a little long winded and I was wondering if there was a simpler way.

Mike.
Edit, in case someone comes across this in the future, to place the array in flash and not ram requires the const keywork after the *.
I.E.
Code:
uint8_t * const ports[8]={(uint8_t *)&LATB,(uint8_t *)&LATB,(uint8_t *)&LATB,(uint8_t *)&LATB,
                    (uint8_t *)&LATC,(uint8_t *)&LATC,(uint8_t *)&LATC,(uint8_t *)&LATC};
 
Last edited:
Sorry Mike.. Only just seen this..

I usually go straight to the header file.. Herein lies many pointers to SFR's so I normally copy what they have done...

Everything in the header is normally typedef'd anyway... But they scope the pointers with "attribute section" so You seem to have helped me out..
 
It's actually making sense now as &PORTB is just a 16 bit address so the compiler needs to know it's a uint8_t pointer. However, I thought it was obvious I was defining an array of uint8_t pointers. To be fair, it did compile but gave lots of pointer conversion type warnings. I don't like any warnings now and try to get rid of them as in the past I've missed actually errors due to them being obscured by warnings. Also not sure why the const has to be placed after the *.

Mike.
 
SFR's are weird.... They shouldn't be constant, as they are theoretically ram. But the position is.. So setting a pointer should be with the @ symbol

Intel does it this way...
 
The const puts the array of ram pointers in flash (program memory). Remove the const and ram usage goes up by 16 bytes.

Mike.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…