A Little Perplexed About Being Mis-Hexed  - Page 1
Forum section: electronics / general

author: SuzyC
author_rank: Frequent Contributor
timestamp: April 07, 2015, 07:16:36 pm
content: I am compiling C-Code for the PIC 18F25K22 and trying to set configuration bits.According to the Hi-Tech 18 compiler Ver 9.63, and according to their manual, the configuration bits can be set for the 7 config words used with this chip with the simple __CONFIG(Confign,x) where n identifies the configuration word and x is the 16-bit configuration payload.However, when I compile code and look a the resulting InteL Format Hex File, the results are inconsistent with the values set by code. The compiler command line .bat file clearly identifies the chip with --CHIP=18F25X22 and the results shows there are no compiler errors or any warnings at all relating to configuration words.Not all 16-bits of the configuration word bit fileds are used in each configuration word, and unused bits are read as 0's according to the PIC18F2xF22 datasheet.However, the values that I see in the Intel Hex File to program the chip are astonishingly different that one would expect.For example:#include <htc.h>#include <string.h>#include <stdio.h>#include <math.h>#include <P18F25K22.h>__CONFIG(1,0xDFFF);__CONFIG(2,0xFF00);__CONFIG(3,0xFF00);__CONFIG(4,0xFF00);__CONFIG(5,0xFFFF);__CONFIG(6,0xFFFF);__CONFIG(7,0xFFFF);.................................................and then a dump of the hex file show this result: (These are the last three lines of the Hex File, CONFIG words info):020000040030CA //Following bytes will be burned at extended linear address. Configuration Word start address:0E000000FF05E03F00BF7AFF0FC00FE00F408A //configuration words 1 to 7 record:00000001FF //end of fileThe middle line can be parsed to show the individual values more clearly::0E 0000 00 FF05 E03F 00BF 7AFF 0FC0 0FE0 0F40 8AThe middle line is in the following Intel Hex format::OE------------------number of data bytes payload0000------------address offset from 0x30000 where configuration bytes reside00---------Indicates the following bytes are consecutive data (these are the configuration reg words)FF05---Configuration Word 1 hex value to burnE03F---Configuration Word 2008F--- 37AFF--- 40FC0-- 50FE0-- 60F40--Configuration Word 7--8A---Two's Complement ChkSum for this record.:0E 0000 00 FF05 E03F 00BF 7AFF 0FC0 0FE0 0F40 8AHuh???

author: dannyf
author_rank: Super Contributor
timestamp: April 07, 2015, 08:17:12 pm
content: QuoteThey provided configuration word macros for you to use.

author: sunnyhighway
author_rank: Frequent Contributor
timestamp: April 07, 2015, 09:02:31 pm
content: Quote from: SuzyC on April 07, 2015, 07:16:36 pmHint: Little Endian (Little End first)

author: SuzyC
author_rank: Frequent Contributor
timestamp: April 07, 2015, 09:12:48 pm
content: Thanks dannyf, but I used the __CONFIG(n,x) macros to get these unexpected results?Thanks sunnyhighway, but little or big endean, the results are not making any sense to me in the .hex file.I seem to think I am doing everything right to get everything so wrong.

author: android
author_rank: Regular Contributor
timestamp: April 07, 2015, 10:00:18 pm
content: What happens when you use the configuration bit names instead of literal values? For example (shamelessly stolen from the interweb...not actually your config bits):Code:__CONFIG(1, FCMDIS & IESODIS & XT);__CONFIG(2, BORDIS & BORV45 & PWRTEN & WDTDIS & WDTPS1);__CONFIG(3, CCP2RB3 & LPT1DIS & MCLRDIS);// & 0xFDFF);__CONFIG(4, DEBUGDIS & XINSTDIS & LVPDIS & STVRDIS);It's probably better in the long run to do it that way anyway (easier to read)

author: mikerj
author_rank: Super Contributor
timestamp: April 07, 2015, 10:01:53 pm
content: Quote from: SuzyC on April 07, 2015, 09:12:48 pmDanny means that you should use the predefined fuse names instead of a "magic number" like 0xDFFF. The compilerrelease notesare clear on this:Quote from: HiTechYou are using a literal value, and getting unexpected results...The format is just as android has shown, the fuse names are defined in the PICC18 device header file, but are mostly the same or very similar to the names used in the datasheet.

author: SuzyC
author_rank: Frequent Contributor
timestamp: April 07, 2015, 10:37:41 pm
content: Aha!Silly me..didn't read the newest errata!16-bit Hex literals are literally not allowed.Thanks to all, I just needed to use this form of setting config bytes with the correct fuse names=values#pragma configNow I am hexing it up!Solved!

author: dannyf
author_rank: Super Contributor
timestamp: April 07, 2015, 10:49:32 pm
content: If you put the typpical config word settings in a file, grouped with conditional compilation directives, you can simply include it in your future projects - no need to look them up each time.I have those config files, about 50KB for my PIC24s and 100KB for PIC12/16/18s. It saves lots of time.

author: 22swg
author_rank: Frequent Contributor
timestamp: April 08, 2015, 06:26:52 pm
content: Good to see you back in PICworld...