CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

How to access a 'C' const int defined at an absolute address

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
nurquhar



Joined: 05 Aug 2006
Posts: 149
Location: Redditch, UK

View user's profile Send private message Visit poster's website

How to access a 'C' const int defined at an absolute address
PostPosted: Wed Aug 16, 2006 4:24 am     Reply with quote

I want to program each of my PIC's with a unique serial number in the top few bytes of program memory (0x7F0->0x7FF on 2K device). I obviously want to read this number in my program. I want to use the serial number to derive a unique communication address for each slave PIC device on an RS485 comunication bus.

My plan was to have my code in one hex file that is all below the top address of 0x7f0. I would then have a simple C prog on my PC to generate the serial numbers and produce a small hex file for the 0x7F0-0x7FF. I could then program the PIC with both hex files. This would allow me to overlay new code without wipeing out my serial numbers.

The following genertates the correct code to use in the top of memory but would overwrite the serial number again when the hex file is programmed into the PIC.

// Put constant at the top of program memory
#org 0x07F0,0x07FF
int32 const ROM_UID = 0xFFEEDDCC;

7F0 140A BSF 0xa, 0
7F1 148A BSF 0xa, 0x1
7F2 150A BSF 0xa, 0x2
7F3 0782 ADDWF 0x2, F
7F4 34CC RETLW 0xcc
7F5 34DD RETLW 0xdd
7F6 34EE RETLW 0xee
7F7 34FF RETLW 0xff
7F8 3400 RETLW 0

The value can be accessed with :-

int32 n32 ;
n32 = ROM_UID ;

However what I realy want to use is something like this :

int32 n32 ;
n32 = *(0x07F0) ;

Needles to say this does not work !!

Any ideas how to force the compiler to use ROM_UID without actually declaring the constant value of variable, just its address ?

The compliler is PCM 3.249 and I am working with the PIC16F628A.
Ttelmah
Guest







PostPosted: Wed Aug 16, 2006 7:05 am     Reply with quote

#ROM address={data}

This puts the 'data' at the specified address.
So, for a four byte value at 0x7FC:

#ROM 0x7FC={1,2,3,4}

You have to read data stored this way, with the 'read_program_memory' function.

int32 n32;
read_program_memory(0x7FC,&n32,4);

However there is a big 'caveat'. Many of the smaller chips (with 2K memory), won't support this function. This is not a CCS limitation, but a limitation of the chips themselves (they don't have the hardware to allow reading of their own ROM). On these 'constants', are actually stored as 'RETLW' functions, and code is needed to call these to get the bytes...
Provided the CCS code leaves the bytes set 'high', then you should be able to overwrite the required bytes by just programming from the other file, after the code is loaded.

Best Wishes
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Wed Aug 16, 2006 8:29 am     Reply with quote

What about #ID or #Serialize
nurquhar



Joined: 05 Aug 2006
Posts: 149
Location: Redditch, UK

View user's profile Send private message Visit poster's website

PostPosted: Wed Aug 16, 2006 12:28 pm     Reply with quote

I looked at #ID as an option but there does'nt appear to be any way of reading back the value under program control with the 16F parts. I think this is possible with some 18F parts.

I am not so sure about #serialise, it seems to be for use with ICD. Where are are the values stored and how do you read them back in the app ? I have seen that Microchip can mask program serial no's at the factory. Can I do this with my ICD2 programmer on the 16F628A ?

The #ROM option may be workable if it can put 14 bit op codes into memory, ie a table like this. But this would be the same as my original #org soloution.
7F0 140A BSF 0xa, 0
7F1 148A BSF 0xa, 0x1
7F2 150A BSF 0xa, 0x2
7F3 0782 ADDWF 0x2, F
7F4 34CC RETLW 0xcc
7F5 34DD RETLW 0xdd
7F6 34EE RETLW 0xee
7F7 34FF RETLW 0xff


but this would put values into the output hex file at 0x7F0 that would then overwrite the programming of the serial no if the code is updated after.

The only sure answere I can think of is to use

#org 0x07F0,0x07FF
int32 const ROM_UID = 0xFFEEDDCC;

in the source code and then rip out the line of hex code in the output hex file at address 0x7F0. This could be done by a simpe PC 'C' Prog.

Anybody know how to run a PC .exe prog post complilation ? I am using MPLAB IDE ?
Ttelmah
Guest







PostPosted: Wed Aug 16, 2006 2:17 pm     Reply with quote

What you do, is include the lines that set up the retrieval, with the ROM statement, but leave out the RETLW lines. Your 'extra' hex file, will contain just these lines, which can then be added to the file.
Some of the 16F chips, can directly read the program memory. Chips that support the use of a bootloader can do this. Unfortunately, not the 628A. However, why not put your serial number in the EEPROM?. If you are not using this to hold values that change, you can enable code protection for this area, and then the value will be protected.

Best Wishes
nurquhar



Joined: 05 Aug 2006
Posts: 149
Location: Redditch, UK

View user's profile Send private message Visit poster's website

PostPosted: Thu Aug 17, 2006 4:49 am     Reply with quote

I have gone for the following soloution :

I have ordered the CCS Mach X Programmer that looks like it can create the serialised code. I guess this is via the use of #serialise

I will then use this snippet to get the value back into my program

#define ROM_UID_CALL 0x7F0

#define getRomUid(val) #asm MOVLW 0x3 \
CALL ROM_UID_CALL \
MOVWF (&val+3)\
MOVLW 0x2\
CALL ROM_UID_CALL \
MOVWF (&val+2)\
MOVLW 0x1\
CALL ROM_UID_CALL \
MOVWF (&val+1)\
MOVLW 0x0\
CALL ROM_UID_CALL \
MOVWF (&val+0) #endasm
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group