PDA

View Full Version : Serial Number in PIC Basic Pro



Keith03
- 13th July 2011, 19:40
Hello All,

I have been lurking about this forum for some time. I really enjoy the ideas and assistance that you all have offered here. I have a project that I am unable to figure out or find anywhere so I joined the club to see if anyone else may have an idea.

I have a PIC-based device that I am using PIC Basic Pro on. I have a serial number stored in EEPROM on a 16F876A device. This serial number is currently programmed by a serial command and stored in the EEPROM after the device firmware is programmed. This is an extra step that is somewhat error-prone and the serial number can be deleted or changed after the board ships.

So, I had an idea to put a dummy pattern in the code space (flash memory) and use a shell script (BAT file) to locate the pattern in the HEX file and replace it with a real serial number right before the firmware is written to the device.

I am using a very customized build environment on a Linux computer. I have a custom build scripting system for revisioning, automatic back ups of builds and running PBPW.exe, etc...

My question is not how to write the shell script nor am I expecting anyone to understand how my build environment works. I just am looking for ideas that I can use to store a dummy pattern in the code space of the PIC device from PIC Basic Pro and be able to locate these bytes in the resulting HEX file. Simple right? LOL

My serial number is eleven ASCII characters. Basically, just the date followed by three digits (i.e. 20110713001). I was thinking of using a pattern like $A5 repeated eleven times.

Thank you in advance if anyone has done anything like this before and/or has any ideas. I will keep plugging away at it.

Thank you for bearing with me on this long description.

Best regards All,
Keith

mister_e
- 13th July 2011, 19:53
There's MPASM __idlocs who allow to do it like so, for 16F you can have an ID from 0 to 0xFFFF, just use this simple line in your code

@ __idlocs 0xffff
However, you can't read it at run time, only 18F can

You could still use a data table


GOTO OverSerial
ASM
SERIALNUMBER
DT "123456789ab",0
ENDASM
OverSerial:
SERIALNUMBER CON EXT

this will be stored in your code space and you can read it anytime with a READCODE loop using SERIALNUMBER constant as your start address.

hth

Keith03
- 13th July 2011, 20:06
Thank you! I will give it a shot. This is an excellent idea!

I was just investigating where constant HSerOut strings are stored. It looks like a dead end though. I would have to parse the HEX file and rewite it or parse/replace the ASM file and create a whole bunch or HEX files. Yuk!

Cheers!
Keith

Keith03
- 13th July 2011, 20:34
RESULT:

Slightly modified, I got your solution to work! I am so grateful for your help! This is exactly what I was looking for. I was able to find the pattern in the HEX file (with a regular expression).

Now the real hard part. I will need to locate the pattern and replace it just prior to programming the device. It tends to span lines in the HEX file and there are checksums to contend with. I am going to look and see if there is a way to tell the assembler to split out this line(s) in the HEX file. Then I just have to parse part of the file. Regardless, I can parse and rewrite the whole thing if I need to.

Thanks again! I am truely amazed. Now that I am a member, I will check back periodically and see if I can help others in their endeavors.

Regards to All,
Keith

rmteo
- 13th July 2011, 20:59
Exactly the reason why many modern MCU's have a built-in unique 64-128 bit ID. These IDs can be read during programmming and/or runtime (but cannot be overwritten/erased) giving you total control over your part.

mackrackit
- 14th July 2011, 00:26
This may also help
http://www.picbasic.co.uk/forum/showthread.php?t=137

Charles Linquis
- 14th July 2011, 00:38
And yet *ANOTHER* reason to use only 18F chips. Using them, it is relatively easy to put a serial number in codespace, (above MCLoader, if you use that program) or in the userID section.

jellis00
- 20th July 2011, 01:49
There's MPASM __idlocs who allow to do it like so, for 16F you can have an ID from 0 to 0xFFFF, just use this simple line in your code

@ __idlocs 0xffff
However, you can't read it at run time, only 18F can
[code]

Steve, I would appreciate an explanation of how this works...does @_idlocs XXXXXX automatically read the TBLPTRx addresses from addresses 200000-20000h in an 18F MCU and put it into EEPROM or what? I have been doing this by the use of below code. If they are equivalent, your ASM single line of code is definitely preferable!
Regards and extremely glad to see you back in the forum and contributing, John Ellis

'Read MCU Serial Number from device ID Locations
TBLPTRU=$20 ' Address $200000
TBLPTRH=$00
TBLPTRL=$00
For i = 0 To 7
@ TBLRD*+ ; Get value and increment the address
ID(i) = TABLAT ; Store the byte as ID(i)
Next
deviceID1 = ID(0) ' MSB of MCU Device Serial Number
deviceID2 = ID(1) ' LSB of MCU Device Serial Number
WRITE 240, ID(0) ' Write serial number to EEPROM
WRITE 241, ID(1)

mister_e
- 20th July 2011, 02:02
the idlocs line is an MPASM directive, it does the job at compile time. it store the value in a specific PIC location.. kinda black magic ;) Check the MPASM user guide for all the details.