Store and read serial number in PIC16F1936 with pickit3 - Page 1
Forum section: electronics / general

author: Jaxaay
author_rank: Newbie
timestamp: May 25, 2025, 11:50:23 am
content: Hi everyone,I'm trying to store a serial number (like "2504-0001-6AC9") directly in the flash memory of a PIC16F1936 microcontroller, so that I can read it later using MPLAB IPE and a PICkit 3.I tried to place the string at a fixed address (e.g., 0x1F00) and be able to read it in under the Program Memory tab in MPLAB IPE after flashing.I'm using MPLAB X IDE with XC8 compiler, and programming with MPLAB IPE v6.20.I wrote this line in my code:const char serial_number __at(0x1F00) = "2504-0001-6AC9";Problem : The string in memory does not show correctly, you can see the picture belowcode :#define _XTAL_FREQ 4000000#include <xc.h>// CONFIG BITS#pragma config FOSC = INTOSC#pragma config WDTE = OFF#pragma config PWRTE = OFF#pragma config MCLRE = OFF#pragma config CP = OFF#pragma config BOREN = ON#pragma config CLKOUTEN = OFF#pragma config IESO = OFF#pragma config FCMEN = OFF#pragma config WRT = OFF#pragma config PLLEN = OFF#pragma config STVREN = ON#pragma config BORV = LO#pragma config LVP = OFF// SERIAL NUMBER IN FLASHconst char serial_number __at(0x1F00) = "2504-0001-6AC9";void wait_seconds(unsigned char s) {for (unsigned char i = 0; i < s; i++) {__delay_ms(1000);}}void wait_minutes(unsigned char m) {for (unsigned char i = 0; i < m; i++) {wait_seconds(60);}}void blink_startup(void) {for (unsigned char i = 0; i < 4; i++) {RC1 = 1;__delay_ms(500);RC1 = 0;__delay_ms(500);}}void main(void) {OSCCONbits.IRCF = 0b1101; // 4MHzOSCCONbits.SCS = 0b10;TRISA0 = 0; // relayTRISC1 = 0; // LEDTRISC0 = 1; // inputANSELAbits.ANSA0 = 0;RC1 = 0;blink_startup();while (1) {wait_minutes(10);RA0 = 1;__delay_ms(10);if (RC0 == 0) RC1 = 1;__delay_ms(90);RA0 = 0;if (RC1 == 1) wait_seconds(30);RA0 = 1;RC1 = 0;__delay_ms(100);RA0 = 0;}}

author: Ian.M
author_rank: Super Contributor
timestamp: May 25, 2025, 12:17:20 pm
content: That's due to the 14 bit program memory word size of all Midrange PICs, + limitations that some devices have reading their own program memory causing XC8-PIC to always use RETLW tables (opcode 0x34nn) for const strings, except on PIC18. There is no fix if you demand that the serial number should be in program memory and in the C source in human readable ASCII form.Workarounds so its readable in the IDE or programmer memory views:Put the serial number in EEPROM as that's 8 bit words, (See XC8 user guide section 5.4.5 Variables in EEPROM), though you'll have great difficulty 'fixing' the address of the variable if there are other EEPROM variables allocated.Use HEXMATE (section 8.2) to post-process the generated hex file before programming to insert the serial number string, with the high byte of each program memory word set to 0x00. You'll then have to explicitly read the program memory word by word to access the serial string in your code.

author: ggchab
author_rank: Frequent Contributor
timestamp: May 25, 2025, 12:23:54 pm
content: Not all bits are implemented in the physical memory. Note that a cleared cell contains 0x3FFF and not 0xFFFF

author: zezo2030
author_rank: Contributor
timestamp: May 26, 2025, 03:34:24 pm
content: Quote from: Jaxaay on May 25, 2025, 11:50:23 amHi JaxaayI think there is no problem, after flashing the program on the mcu ,just read the serial no(read flash address) then send it to uart terminal or print it on the lcd.

author: Jaxaay
author_rank: Newbie
timestamp: July 22, 2025, 09:26:10 am
content: Hello everybody,My idea is not easy to implement, so I will limit myself to using only stick-on labels.Thank you for your support.