I am completely new to PIC programming in SDCC. I was trying for last couple of weeks to make some simple programs and make them work. But I have a roadblock -and also quite a few doubts. Anybody's help would be largely appreciated.
I am very confused about
1. How to use the libraries for the PICs ?
2. How to use the initial start-ups namely crt files of PIC16
3. Also I am facing difficulty in using the various linker options namely -l / -L , can u pls provide some eg. how to use the linking options with the path name
4. Last but not the least, how can I know about my code size, i mean whether its within the size of my uC.
For the record, I am using PIC18F452, in windows platform
Thanking in Advance,
>1. How to use the libraries for the PICs ?
Like in any other compiler. You use the function and tell the linke to link against the file (see example below)
>2. How to use the initial start-ups namely crt files of PIC16
>3. Also I am facing difficulty in using the various linker options namely -l / -L , can u pls provide some eg. how to use the >linking options with the path name
For compiling one source file i use:
/usr/local//bin/sdcc -mpic16 -p18f2620 -debug -mpic16 -p18f2620 -debug -stack-auto -std-sdcc99 -D some_defines -I. -Imyincludedirs -c ./i2c/i2c_master.c -o output/
For linking the final binary i use:
/usr/local//bin/sdcc -debug -L/usr/local//share/sdcc/lib/pic16/ -llibio18f2620.lib -llibc18f.lib -Wl-c -Wl-m -debug -L/usr/local/share/sdcc/lib/pic16/ -Wl-c -Wl-m -use-crt=crt0i.o -Wl-s18f2620.lkr,-m -mpic16 -p18f2620 output/file1.o output/file2.o -o output/binfile.hex
>4. Last but not the least, how can I know about my code size, i mean whether its within the size of my uC. For the record, I am >using PIC18F452, in windows platform Thanking in Advance,
For each file you compile you will get a .lst file which will have some statistic as shown below at the end of the file:
00253 ; Statistics:
00254 ; code size: 64 (0x0040) bytes ( 0.05%)
00255 ; 32 (0x0020) words
00256 ; udata size: 0 (0x0000) bytes ( 0.00%)
00257 ; access size: 0 (0x0000) bytes
Thanks a lot keinOr, for ur effort in managing out some time & helping a newbie like me .
Firstly thanks for your answer of my query nos. 1,2 & 4(Those r just the stuffs I was looking for, THANKS A TON!!).
But unfortunately owing to my novice knowledge in using SDCC, I just cld nt grasp everything about your suggestion against Q3.
Can u pls provide a working example, with entire command set to compile them properly (try to undrstnd, I hardly find any resource !!).
I have been trying just toggle a LED (using PIC16f88 this time), but still having hell lot of problem. The code is as below
/* Setup chip configuration */
typedef unsigned int config;
config at 0x2007 __CONFIG = _CP_OFF &
void delay_ms(long ms)
for (i=0; i < 330; i++);
/* PORTA.1,2,3 output pins */
TRISA = 0xF0;
PORTA = 0x00;
PORTA = 0xFF;
And compiling by SDCC -mpic14 -p16f88 test88.c
For the record test88.c is the Filename, and MPLAB to dump the hex file on my mini ckt.
THE PROBLEM IS THE LEDs are just not toggling. It just remains switched ON.
I guess I am doing some mistake in CONFIG or something regarding compiling options
CAN U PLS SHED some light !!
Thanking in advance,
Are you sure that your setup for PORTA is correct. I use pic18f2620 and pic18f2320 which have additional ADC at PORTA. To make PORTA output i need to do the following:
ADCON1 = 0x0c; /* set an0:an2 as analog input, rest as digital i/o */
ADCON1 = 0xff; /* make all pins of portA digital in/out ports */
TRISA = 0x00; /* make PORTA output, 1=input 0=output */
Additionally you need to specify a location where sdcc can place its stack put something like the following at the beginning of your main source file:
#pragma stack 0x80 0x16
The first number is the location and the second one is the sice. Of course you need to adopt the location to your device. The example is taken from a 18f2320
Usually i only include #include <pic18fregs.h> and tell the compiler which device i use. The various #ifdef in the latter file will then take care that the correct file is used for compilation. But this just makes your code more portable.
Last but not least you forget to link some startup code.
Hi , keinOr THANKS A LOT for your reply.
Firstly thats stupid of me not to specify the stack by using #pragma stack ….
Secondly, I would surely try out the port configuration suggested by u, hope it will work. Will surely inform u later.
Lastly, sorry to bother u again, can u pls suggest how to link some startup code…..plsss.
Thanking in advance,
i just tried to compile code for a p16f device. Seems like those don't have/need startup code. Don't ask me how variable are initialized there. When reading the .asm file it seems like this is done in __sdcc_gsinit_startup which is linked automatically here and at its end jump to main function. Unfortunately you cannot switch it off.
I just compiled my code with
sdcc -mpic14 -p16f88 pictest.c
The resulting .asm file looks fine to me.
- Try to specify the stack location and size (see datasheet for an appropriate location) on command line
- Change PORTA initialization or better use PORTB
- make your function statc (see sdcc manual why)
Finally after taking a closer look at the sdcc manual it seems that in the p16 port stack size and location is specified on comandline with -stack-loc and -stack-size
Thanks mate, will surely chk out and inform u.
Thanks for all your effort.
I just compiled & run my little LED blinking program using SDCC -for the first time with success.
Thanks especially to u and all others, for ur effort & time in helping me.
Now am looking forward to work with 18F lot.
I was just wondering about a thing - can u pls shed some light --
HOW TO OPTIMISE THE CODE (To lessen the code size). Frankly speaking reading the SDCC Manual hasnt helped me!!
Thanking in Advance,
Thanks everybody especially kein0r for ur sincere & prompt help.
As far as now, code running well.