Hello everybody!
I have to make some very minor changes to the firmware of a 20 year old product. Issue is that i have no idea of how to compile the code....
The only thing i have to go of is that its a PIC16C773 and the code is dated September 2003, there is only one file
main.c and it references
<pic.h> (presumably abstraction from the IDE).
I do know that MPLAB IDE 8.50 is used to program the processor so i've tried to compile it using that version of the IDE but just get ~100 errors about
Illegal character (X),
Illegal opcode (X) and that it is unable to open
"pic.h". I assume that this is the source for a lot of errors but wouldn't it be a part of the IDE/toolchain? This error number 36 so quite a few other complaint before missing library files.
Maybe another compiler/library was used but i have no idea, there is zero documentation of this.
Does anybody have any idea on how to compile this?
I've never used PIC nor any toolchain from this time, other than gcc and make.
I appreciate any help
"Illegal Opcode" 'smells' like you are attempting to use the Microchip MPASM assembler toolchain for C sourcecode!
IIRC 20 years ago Microchip didn't have their own PIC16 C compiler though there were a couple of third party ones.
You'd need to post enough of the source file for us to identify the compiler, which we do by recognising the names of system functions, how the CONFIG word is set, device or compiler specific includes, and various data types. Then you'd need to find a copy of that compiler and install it as MPLAB 8 didn't come with compilers (though you could optionally install demo versions of several with limited device support or program size restrictions). Then you have the problem of obtaining a valid licence key for a legacy compiler as without it, most will drop back to demo mode, and even if the demo mode supports your chip, either code size restrictions or lack of optimization will bite your ass.
Luckily the .mcs file is a text file, and should have enough information to identify the toolchain
"Illegal Opcode" 'smells' like you are attempting to use the Microchip MPASM assembler toolchain for C sourcecode!
Haha you're probably right, looking through the different configurations of MPLAB suggests that....
Thought i was using the built in PIC-C C-compiler but it doesn't seem to exist, falling back to the MPASM suit :/
I have hower tried to compile it with the MPLAB X IDE with a C-compiler and it does not work. Im however able to create a project for the old processor in this modern IDE.
There are two flags/defines and such that aren't defined in the C-file or referenced in the datasheet.
- STAT_BF
Found one mention in this paper https://picmania.garcia-cuervo.net/recursos/ccs_picc_pic16f87x_tutorial_-_1a.pdf.
Below is stated in the paper, i have no idea what PCW is, google suggests its "The Philippine Commission on Women" but i doubt the code is compiled there :/
Program UNION_1.C compiles under PCW. However, when compiling in MPLAB, MPLAB hangs. I saw this
problem at a recent workshop and also a posting on the PICLIST describing the same problem.
- ADGO
Cant find any mention on google regarding this.
Almost all of the code seems to be handwritten/not using external functions. Other than the above mentioned "defines".
Pic.h refers to the old Hitech compiler, now XC8.
Mplab is just an IDE, doesn't come with any compiler by default.
Mplab 8 is ancient, but older xc8 versions will work fine.
Download a v1.xx version from the archive (v2.xx works only in mplabX):
https://www.microchip.com/en-us/tools-resources/archives/mplab-ecosystemI think v1.45 worked in mplab 8.92, but I recall having issues with certain deprecated functions/pragmas and reverting to v1.33. So I'd try that one first.
IIRC all pic.h includes should be replaced with xc.h.
MPLab 8.92 shows that Microchip's(Compiler 9.60) is compatible with that chip.
Pic.h refers to the old Hitech compiler, now XC8.
Mplab is just an IDE, doesn't come with any compiler by default.
Mplab 8 is ancient, but older xc8 versions will work fine.
Download a v1.xx version from the archive (v2.xx works only in mplabX):
https://www.microchip.com/en-us/tools-resources/archives/mplab-ecosystem
I think v1.45 worked in mplab 8.92, but I recall having issues with certain deprecated functions/pragmas and reverting to v1.33. So I'd try that one first.
IIRC all pic.h includes should be replaced with xc.h.
Thanks for the help, i downloaded the XC8 compiler v1.33. Created a test-file and im able to compile it seemingly fine with the following command.
PS C:\Program Files (x86)\Microchip\xc8\v1.33\bin> ./xc8.exe --chip=16C773 --outdir=C:\PIC ./test.c
However i'm still getting compiler Errors for
STAT_BF &
ADGO references. I can't seem to find where they come from.
I'm using the commandline since MPLAB v8.92 lacks the option for adding the XC
8 compiler?
Side note, is there really this big of a difference between the free and pro version of the compiler or just "clever" marketing?
(1273) Omniscient Code Generation not available in Free mode (warning)
Memory Summary:
Program space used 6h ( 6) of 1000h words ( 0.1%)
Data space used 2h ( 2) of 100h bytes ( 0.8%)
EEPROM space None available
Data stack space used 0h ( 0) of 50h bytes ( 0.0%)
Configuration bits used 0h ( 0) of 1h word ( 0.0%)
ID Location space used 0h ( 0) of 4h bytes ( 0.0%)
Running this compiler in PRO mode, with Omniscient Code Generation enabled,
produces code which is typically 40% smaller than in Free mode.
See http://microchip.com for more information.
You probably need to
#define _LEGACY_HEADERS
before #including xc.h to tell the compiler to use the old Hi-Tech (pre v9.81) SFR and bit names. Legacy support is in XC8 v1.45 and earlier, and also Hi-Tech PICC v9.81 upwards. I'm not sure about later versions of XC8 - check the include folder for a sub-folder called 'legacy'.
Please post some code lines where those are used.
Probably a deprecated thing, easily fixed with some subtitutions.
Pro enables Os (Optimization for size), and yeah, it saves quite a lof of space.
But it's free up to O2, which is still a decent optimization level.
I have a fix somewhere to make xc8 visible in Mplab, let me search it.
Edit:
Open a cmd window in admin mode, execute this and reboot afterwards. XC8 should appear in mplab now.
(If you're using a different/older Windows version, write the correct path to the DLL)
regsvr32 "C:\Program Files (x86)\Microchip\xc8\v1.33\bin\MPLABXC8.dll"
New day, new energy
There does not seem to be a difference when using the legacy headers. I still get the same errors.
Thanks, this worked!
regsvr32 "C:\Program Files (x86)\Microchip\xc8\v1.33\bin\MPLABXC8.dll"
STAT_BFThis seems to be used as a "ready wait"/"buffer full" status flag from the SPI status registers. (SSPSTAT Bit 0).
CS(ON);
delayx(2);
SSPBUF=0x02; //command to write to eeprom
while (!STAT_BF);
SSPBUF=adress>>8; //Send addr
while (!STAT_BF);
SSPBUF=(char) adress;
while (!STAT_BF);
SSPBUF=data;
while (!STAT_BF);
ADGOThis seems to be a "start ADC conversion" bit and should equal bit 2 (GO/DONE) in ADCONx.
ADGO=1; // start AD conversion
while (ADGO==1); // wait for end of conversion
delayx(15); // 15 u sek delay
I could simply replace them with the modern equivalent name but how would I trust that all other references are correct or that the binary compiles correctly?
The only mention I could find of these "Bit names" is from this paper
https://picmania.garcia-cuervo.net/recursos/ccs_picc_pic16f87x_tutorial_-_1a.pdf where they are used in the same way.
It is dated January 2001 and contains a lot of coding examples that look quite similar to the code I’m trying to compile.
Try replacing these with
SSPSTATbits.BF
ADCONbits.ADGO
SSPSTATbits.BF seems to work fine.
ADCONbits.ADGO doesn't seem to work. Changed it to
ADCON0bits.GO seems to work?
This should also be equivalent to the bitmasks?
ADCON0 |= 4; //Start ADC
while (!(ADCON0 & 4)); //Wait for finish
Now i have another problem, the code does not fit. And the optimize flag results in the same as non optimized. Is it possible to use the "pro" version of the compiler? I can't find a reference to it in the compilers manual?
PS C:\Program Files (x86)\Microchip\xc8\v1.33\bin> ./xc8.exe --chip=16C773 --outdir=C:\PIC --opt=all ./main.c
Microchip MPLAB XC8 C Compiler (Free Mode) V1.33
Part Support Version: 1.33 (A)
Copyright (C) 2014 Microchip Technology Inc.
License type: Node Configuration
(1273) Omniscient Code Generation not available in Free mode (warning)
./main.c: 84: (1250) could not find space (30 bytes) for variable _outputBufSer0
(908) exit status = 1
(908) exit status = 1
PS C:\Program Files (x86)\Microchip\xc8\v1.33\bin>
EDIT:
Finally got the microchip licence page to load and now im able to compile the code in pro mode!
Thanks for all the help everyone
Yes, ADCON0bits.GO is the same.
Did you try adding ”-O2" to xc8 arguments?
(O= letter, not zero)
Good luck!
I tried the standard optimization flags but i don't think that they are supported by XC8.
They seem to use the option
--opt for optimization but dont have the option for code optimization type (speed/size & aggressiveness).
One last question before i close this issue, is there a non OTP version of the processor or drop in replacement?
Can't find one at Microchip (PIC18F2423 is similar but not exact) and don't want to waste a bunch of OTP processor for some minor code tests! The UV version does not seem to come in my IC-package...
Thanks again for all the help
Just open the project in Mplab and make it there!
Yeah the binary size in "pro mode" and full optimization results in a program usage of 57% and 81% data-space usage, everything fits!
The docs i posted in my previous post are the correct one, the compiler does not recognize "-O2, -Os etc".
Now i'm just waiting for new processors from Digikey to test the changes. I only have programmed processors and they are OTP....
If you need the DIP package then you could get a windowed SSOP part and mount it on an standard SSOP-20 to DIP-20 adapter PCB (cheap versions available from eBay).
Porting the code to the newer pic shouldn't be too hard!