logo elektroda
logo elektroda
X
logo elektroda

Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment

p.kaczmarek2 8655 11
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    Here I will describe step by step installation, configuration and use free compiler SDCC for creating programs for PIC microprocessors.
    The whole thing will be in the form of a detailed tutorial.
    Due to the length of the whole tutorial, I will divide it into separate topics .
    In later parts I will try to describe the use of most popular peripherals, libraries, w SDCC by example PIC18F2550 .
    How many parts will eventually come out and what will be discussed depends to a large extent on what the comments will be. Of course, I accept suggestions, so if someone wants me to discuss, for example, PIC communication via I2C with a selected cube/module, please let me know.

    List of parts (separate topics) of the tutorial
    The tutorial is divided into separate topics and you can find links to them here.
    Part 1 - Configuration of the working environment
    https://www.elektroda.pl/rtvforum/topic3635522.html#18304424
    Part 2 - Blink LED, IO pins, digital inputs and outputs
    https://www.elektroda.pl/rtvforum/topic3647884.html#18389188
    Part 3 - Oscillator settings. Internal oscillator, external oscillator, quartz resonator, PLL
    https://www.elektroda.pl/rtvforum/topic3657704.html
    Part 4 - Timers, interrupts
    https://www.elektroda.pl/rtvforum/topic3676645.html#18580858
    Part 5 - Support seven-segment display
    https://www.elektroda.pl/rtvforum/topic3676650.html#18580877
    Part 6 - MM5450 LED display driver
    https://www.elektroda.pl/rtvforum/topic3845301.html
    The table of contents will be updated as I write the next parts.

    This topic is part 1, that is Setting up the working environment . Here we go.

    What is what?
    SDCC (Small Device C Compiler) is a free, open source compiler for many families of 8-bit microprocessors, including:
    - PIC16F/PIC18F (which we will cover in this topic)
    - 8031, 8032, 8051, 8052 Intel
    - DS80C390 from Maxim/Dallas
    - 68HC08 and 68HCS08 from Motorola/Freescale/NXP
    - STM8 from STMicroelectronics
    - PDK14 and PDK15 from Padauk Technology
    - Zilog Z80, Z180, eZ80
    SDCC is available under the GPL license. It supports Windows, Linux and macOS platforms

    GPUtils it is a collection of tools for PIC microcontrollers from Microchip. Includes programs like gpasm, gplink, gplib. It is available under the GPL license on Windows, Linux and macOS platforms. GPUtils is required by SDCC if we want to compile batch for PICs.

    What will the tutorial cover?
    In this tutorial (and in the next part that is on the way) I will try to include:
    - step by step installations SDCC along with setting PATH paths on Windows
    - step by step installations GPUtils along with setting PATH paths on Windows
    - step by step package installation SDCC for PIC along with setting PATH paths on Ubuntu
    - presentation of the method how to check whether SDCC/GPUtils is configured correctly
    - a short description of how to compile programs with help SDCC
    - detailed examples of programs done in SDCC For PIC18F2550 with the help of libraries available in the compiler itself and on the network
    There will be a tutorial practical , i.e. I will focus more on how to get results than on how what works internally. The instructions here will be as detailed as possible (without skipping steps, etc.), but I expect the reader to be able to use the search engine and other sources of knowledge as well. And above all - the tutorial does not replace school and books!

    Windows - Installing SDCC and GPUtils with the help of installers
    homepage SDCC is hosted on SourceForge. There is documentation on it SDCC , manual in PDF, download links and the source code of the compiler itself (it is open source).
    http://sdcc.sourceforge.net/index.php
    Itself SDCC we can download from here:
    https://sourceforge.net/projects/sdcc/files/

    I will describe the installation process in detail here. First, let's go to the link above.
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    We choose the latest version (at the time of writing it is 3.9.0 from 2019-04-15):
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    We start the installation process:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    By default, we read the entire License, SDCC is licensed GPL v2 .
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    We select the Start Menu Folder and the full version of the installation:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment

    Here it is worth changing the place where we will install SDCC . By default, the installer wants to put the compiler in "C:/Program Files/", but on Windows I experienced a problem with starting SDCC with a space in this name, so it's most convenient to install the whole thing to, for example, "C:/SDCC/".
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    We are waiting for the installation process SDCC will end:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    Finally, the installer automatically adds the SDCC path to the PATH environment variable:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment

    Now we need to install separately GPUtils in which it is located gband.exe necessary to compile the batch for PIC.
    GPUtils also has a page on Sourceforge:
    https://gputils.sourceforge.io/
    You can download them from here:
    https://sourceforge.net/projects/gputils/files/
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    We start the installation:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    GPUtils It is also licensed under the GPL. We read the license and accept it:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    We choose the full version of the installation:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    We are waiting for the installation process to finish:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    Finally, as with SDCC , the installer gives us options to automatically add a folder GPUtils to system paths:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment

    Windows - Alternative way to install SDCC and GPUtils - Pinguino Compilers
    penguin (a free, open-source Arduino equivalent for PIC) has a ready-to-use package of compilers for 8 and 32-bit PICs. It is also in it SDCC along with all the necessary accessories. This pack already includes GPUtils ( gband.exe , and others), so you don't have to install them separately afterwards.
    It can be downloaded for free from github (binaries are also available there):
    https://github.com/PinguinoIDE/pinguino-compilers
    This is a full set of compilers ( SDCC and MIPS ):
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    There are even Linux binaries there:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    Just in case, I'm putting a backup of the above repository here (at the time of writing this post):
    Windows 32:
    pinguino-c...dows32.zip Download (29.01 MB)
    Windows 64:
    pinguino-c...dows64.zip Download (29.42 MB)
    Linux 32:
    pinguino-c...inux32.zip Download (23.99 MB)
    Linux 64:
    pinguino-c...inux64.zip Download (25.64 MB)

    Installation in this way is only about unpacking the package where we want, but you need an environment variable %PATH% set manually. Otherwise, the system will not recognize the command sdcc , etc.


    Windows - Set the PATH environment variable for SDCC
    If we downloaded SDCC from the repository Pinguino-compilers or some other way without using the installer then you may need to manually add the path to SDCC to the variable PATH . This is needed for Windows to recognize the command sdcc being in any folder.
    To do this, first we write down in which folder we have the compilers installed (more precisely: what is the full path to the bins from the folder SDCC ). For example:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    And then we add it to %PATH%, either through the Windows interface or through the command line.
    Method 1: Adding a folder to the PATH via the Windows interface
    It's pretty much the same on each version. On Windows 10, we first open the properties of the computer:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    Then we open the "Advanced system settings", the "Advanced" tab, click on the "Environment Variables" button there:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    Now we see the editor of all environment variables on Windows. We select the Path variable there:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    Click on ''''Edit'''' to go to the editor of all tracks in this variable. Each of them is here separately, we do not need to use semicolons. We add a new one using the ''''New'''' button, after which we must confirm the changes by clicking ''''Ok''''.

    Method 2: Adding variable to PATH via cmd console
    We have a command there set NAME=value which sets a variable with a given value NAME to a given value.
    You can also display the current value of the variable there by typing % NAME % .
    We can therefore use these two mechanisms at once to add another path to the system paths.
    So we type in the console:
    set PATH=%PATH%;ŚCIEŻKA_DO_SDCC

    Where PATH_TO_SDCC is the full path to SDCC , to the folder bin , full.
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    This is enough to add the path to PATH and it will be remembered after restarting the computer.

    We proceed similarly with GPUtils . We also add the path to PATH (if it's not there).
    In case of Pinguino-Compilers we don't need to add the paths twice because there i sdcc.exe and gband.exe is in one folder:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment

    Windows - Check SDCC and GPUtils installation
    If everything went well (and the SDCC folder path was added to the PATH) then the command console should recognize the sdcc command. You can check this by typing sdcc in it, or eg sdcc -v to display the installed compiler version.
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    In the case of the Pinguino version, the result of the sdcc -v command may be slightly different:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment

    Similarly, we check if the tools from GPUtils, especially gpasm.exe, are correctly visible. Enter the command in the console gband -v .
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment


    Ubuntu - Install SDCC/GPUtils
    One might think that SDCC / / GPUtils can be installed on ubuntu via apt-get . Nothing more wrong!
    Actually, apt-get lets you download SDCC on ubuntu but so downloaded SDCC Unfortunately, it does not support PICs .
    Screenshots from the ''''incomplete''' installation in the spoiler SDCC By apt-get :
    Spoiler:

    Starting installation via apt-get:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    End of installation:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment

    The screenshot below shows how such an installation ends SDCC on Ubuntu - after checking the version with the "sdcc -v" command, you can see that DRINK are not supported, and the compilation command itself results in the error "cannot generate code for target ''''pic16''''".
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    Such an SDCC is of no use to us. The problem is that some of the files associated with the PICs are not under a license compatible with the repository policy apt-get so we have to download our compiler manually.

    So SDCC for Ubuntu under PICe must be downloaded manually, either from the SDCC website itself or from the one mentioned earlier Pinguino-compilers .
    I will describe here how to download SDCC from Pinguino-compilers (it is already compiled there, both for 32-bit and 64-bit Linux).
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    We open the appropriate Github repository on Ubuntu, from where you can download Pinguino compilers , here:
    https://github.com/PinguinoIDE/pinguino-compilers
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    We download the entire repository, although we only need the p8 (8-bit) binaries for 64-bit Linux.
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    We save the archive file.
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    We extract from it only what is needed for us (here: folder p8 from the folder linux64 ):
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    Choose a folder where we will unpack it. I unpacked it to /home/test/sdcc/ (the p8 directory was created there, and bin in it). It's actually not that important where we put it, because we'll configure the Linux PATH to point to the bin folder with the compiler anyway.
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    After unpacking, we see that we already have executable files sdcc , gband and other necessary tools.
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    Ubuntu - Setting the PATH environment variable for SDCC
    Contrary to appearances, setting this path on Linux systems is not as analogous to Windows as it might seem.
    In my case, assigning a new value to PATH from the Ubuntu console did not work, because the system forgot its value after rebooting.
    That is, the same set PATH=%PATH%;/new/path/ not enough.
    Only the file editing method worked correctly for me /etc/environment .
    1. We open this file as administrator:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    2. We add the path to SDCC there:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    3. We restart the system
    From now on, whenever you turn on Ubuntu, it should know the path to SDCC .

    Ubuntu - Verification if the installation was successful
    As with Windows.
    We can check the SDCC/GPUtils installation by typing commands in the console sdcc -v and gband -v .
    The screenshot shows the correct expected result.
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    Note that the command sdcc -v shows explicitly that this version of SDCC supports it pic16/pic14 .

    First test build
    Now that we know that SDCC and GPUtils are added to the system paths, we can check if the compilation process itself is working.
    We take some simple code, for example blink for PIC18F2550:
    Code: C / C++
    Log in, to see the code

    (Later in the tutorial, this code will be arranged in more detail).

    We save it in a file with the .c extension in a folder of our choice, e.g. on D:/PICProjects/SDCCTest/ .
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    We open the console, go to this folder with the help of the cd command:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    And we compile with SDCC with the help of the command:
    sdcc.exe --use-non-free -mpic16 -p18f4550 p18f4550_blink.c

    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    The result should be a .hex file ready to be uploaded to the PIC.
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    This is the correct batch ready to be uploaded to the PIC:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    This is what a PIC batch looks like when compiled in .hex format.

    If the build fails and you get the error " The name ''''gpasm.exe'''' is not recognized " means either we don't have %PATH% set to the bin folder of GPUtils, or we didn't install GPUtils at all.
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment


    We are looking at SDCC commands
    We already have the compiler installed. Now we will analyze in detail the command with which we compile the C code into a ready-made file .hex for PIC.
    In its simplest form, it looks like this:
    sdcc.exe --use-non-free -mpic16 -p18f4550 p18f4550_blink.c

    That's all it takes to compile our program.
    p18f4550_blink.c - this is of course the name of our code file. It can be basically anything, but it's worth keeping the .c extension in it.
    Switch --use-non-free allows the compiler to use Microchip's PIC libraries and is needed here in most cases.
    mpic16 here means that we are compiling for the PIC18F family. Yes, mpic16 stands for 18F - these 16 are there because the core of the PIC18F family has a 16-bit instruction word.
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    (source: PIC18F4550 datasheet)

    Similarly, there is an option mpic14 . With its help, we compile for the PIC16F family:
    sdcc.exe --use-non-free -mpic14 -p16f628 p16f628_blink.c
    mpic14 here means that we are compiling for the PIC16F family. Yes, mpic14 stands for 16F - these 14 are there because the core of the PIC16F family has a 14-bit instruction word.
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    (source: PIC16F628A datasheet)

    Using the appropriate option (mpic14 vs mpic16) is necessary, e.g. others it depends on what #include files are available (for example, pic18fregs.h is only on the PIC18F family).
    Option mpic14 also supports the family PIC12F *, e.g PIC12f675 .

    Compilation of a project consisting of several files in SDCC
    Now you should consider how to compile a project composed of several files with the source code.
    Suppose we have two files.
    - test_blink_file.c
    - p18f4550_blink_multipleFiles.c
    You definitely can't do it like this:
    sdcc.exe --use-non-free -mpic16 -p18f4550 p18f4550_blink_multipleFiles.c test_blink_file.c

    This results in a rather disturbing error that reads " at 1: warning 120: cannot compile more than one source file. file ''''test_blink_file.c'''' ignored ".


    But that's not a bad thing - SDCC can compile a project consisting of many files, you just need to break the compilation into separate stages.
    sdcc.exe --use-non-free -mpic16 -p18f4550 -c test_blink_file.c
    sdcc.exe --use-non-free -mpic16 -p18f4550 p18f4550_blink_multipleFiles.c test_blink_file.o

    In this method, we compile each additional code file separately, and at the end we provide the file with the main function and the results of previous compilations.
    It is very important that the function file main() enter here first.

    You can separate the whole process even more - compile each code file with a separate command (also a separate file with main() ) and then just combine them. However, it still requires manually adding a .lib file with functions for PIC18F.
    sdcc.exe --use-non-free -mpic16 -p18f4550 -c test_blink_file.c
    sdcc.exe --use-non-free -mpic16 -p18f4550 -c p18f4550_blink_multipleFiles.c
    sdcc.exe --use-non-free -mpic16 -p18f4550 p18f4550_blink_multipleFiles.o test_blink_file.o libc18f.lib


    Organization of work - scripts for compilation
    Of course, you don't always have to manually enter commands into the console. You can make a .bat batch script (on Windows; .sh on Linux) that will do what you want for it.
    A .bat script is simply a text document and is created just like a txt file:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    Enter its name (with the .bat extension):
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    In the event of such a message, we confirm that the file should have the .bat extension:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    Then we edit it as a text file.
    You can to the scripttype those commands that are normally executed in the console, but you don't have to worry about paths - just specify relative ones. So if the code file is in the same folder as the script, we only enter the name of the code file, etc.
    In scripts, you can also use echo to display a message and pause to stop the window from hiding.
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    Finally, one of the simplest build scripts looks like this:
    echo ''''Starting compilation script...''''
    sdcc.exe --use-non-free -mpic16 -p18f2550 p18f2550_blink.c
    pause

    Running it starts the console window and the compilation process in it. '''' command pause '''' makes the window stay and wait for closing after compilation, so we can check if the compilation was successful.

    An alternative to writing code in Notepad - SDCC in Code Blocks
    Using an integrated coding environment is not necessary here, but it can be quite convenient. Therefore, I will now show how you can configure Code Blocks to work with the compiler SDCC and writing programs for PICe.
    SDCC and GPUtils must be installed beforehand according to the instructions in this topic!
    We start by downloading Code Blocks:
    http://www.codeblocks.org/downloads

    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    At this stage, we can download the clean version of Code Blocks (without any compiler), as well as the one with MingW (except that the MingW compiler itself will not be useful to us - we will use it anyway SDCC . This is just in case we want to use CB for Windows programming as well.)
    But I emphasize that Code Blocks allows you to have different compilers at once ( MingW will not interfere with SDCC ).
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    We install Code Blocks through the Windows installer.
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    Code Blocks is of course also GPL licensed.
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    We choose the full installation; it's only 250 MB anyway.
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment

    The first time you run Code Blocks it automatically detects which compilers you have. It should detect presence SDCC , but even if it doesn't detect it, it's no problem - you can set the path manually in the settings.
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    By the way, the picture above shows how many compilers it supports Code Blocks .

    After opening the IDE, we open Settings->Compiler:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    We select SDCC there (or its full name: Small Device C Compiler; it depends on what version of Code Blocks we have):
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    There are global settings for this compiler.
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    IN Toolchain executables we make sure Compiler''''s installation directory correctly points to the folder where SDCC is (not directly to the bin, but to the folder that contains the bin):
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    Then it goes to Other settings where we click the button advanced options... . You will need to set something up there.
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    We confirm that we know what we are doing:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    Here's a fix for a common problem with output file extensions (.o versus .rel). Code Blocks expects .rel files, but in fact they have a .o extension. To fix this, enter in the field " Object file extension (e.g. o )" content ''''o'''', instead of ''''rel'''' which is there by default.
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    After these changes, we must restart Code Blocks! Turn it off completely and turn it back on!
    If we do not change the expectation of extension ''''rel'''' to ''''o'''' we will get the error " at 1: warning 119: don''''t know what to do with file ''''objReleasecb_sdcc_test.rel''''. file extension unsupported ":
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment

    Configuration Code Blocks done, now we can create a new project.
    We click File->New Project . There we select the project type '''' Empty Project '''':
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    We're going through anothersteps '''' Empty Project Wizard '''':
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    We set the name and location of our project on disk.
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    We choose the compiler we will use, which is of course SDCC :
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    We disable the configuration debug :
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    The project is ready. If there are any warnings, we ignore them.
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    We add a new file to the folder via File -> New -> Empty File :
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    This will create an empty file for us. We paste our code into it.
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    Now we need to configure our project for a specific PIC.
    We open Project->Build Options .
    There we turn on the --use-non-free switch already known to us from the command line:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    A little above it, we mark which PIC family we are compiling for (also discussed earlier -mpic14 and -mpic16 ):
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    In the adjacent tab ( Other compiler options ) we also add a switch / selection of a specific PIC under which we write. In the case of this example, it is -p18f4550 .
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    Only in Linker Settings we need to manually point to the right libraries here libc18f.lib . It is needed for the function delay1ktcy . If we want to make our own delay, it may not be needed.
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    There is only one thing left to do - change the extension of the resulting compilation file from .exe to .hex. We do it in Project/target options -> Build targets .
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment

    From now on, the project should compile correctly in Code Blocks.
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    The compilation result (hex file) should be in the /bin/Release folder in our project folder.
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment

    What will we need to start with PICs?
    First of all, you need a programmer.
    The programmer is used to upload a batch (compiled code, .hex ) from PC to DRINK and.
    Of course, the programmer is needed only for the time of programming the microcontroller, then it can be disconnected and our program, the batch will continue to work.
    Probably the most practical now will be the purchase of a programmer PICKIT3 although I prefer it PICKIT2 because the latter is capable of powering the system with a microcontroller, a PICKIT3 Unfortunately, it requires a separate power supply to the PIC.
    PICKIT2 is no longer supported, but there are open source initiatives like PICKIT2 Device Editor I am reading pic32prog which allow you to program newer PICs with it.
    There is still PICKIT4 But I haven't used it, so I can't say.
    Of course, there are other PIC programmers, but in order not to complicate things unnecessarily, we will use them PICKIT3 :
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    Here I would like to point out that PICKIT3 it really supports a lot of layouts. Even AVR y:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    (The screenshot above is from MPLAB IPE, myself personally programming Atmeg with help PICKIT3 I haven't tested yet).

    For PICKIT3 you will also need a mini USB cable:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    My entire tutorialwill be made of PIC18F2550 on the contact plate, i.e. we will not need any PCB. We will put everything on the so-called. breadboard :
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    Cables for breadboards (so-called jumpers). We will make connections with them:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    The board will need a 5V power supply. I will take them from USB, via this micro USB connector for breadboard:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    And in addition - a micro USB cable.
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    Of course, you will also need a PIC - I chose it PIC18F2550 . More on that later in the tutorial.
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    Now the things needed to get started with the PIC:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    10k resistors (one for the RESET pin from the PIC, also as pull up/down buttons), a capacitor of about 220nF for the VUSB pin, some 1k resistors for the LEDs, a standard electrolytic capacitor and a 100nF ceramic one that we will connect to the power supply.

    Ceramic/electrolytic capacitors vs. PIC
    Here I would just like to emphasize that the use of electrolytic and ceramic capacitors in order filtration/decoupling PIC power supply (and basically any system, not just microcontrollers) is very, extremely important !
    Each microcontroller power pin (even if he has several VDD Whether AVDD ) should have a row capacitor 100nF ceramic connected to ground, as close as possible to the microcontroller itself.
    In addition, it is worth using a larger electrolytic capacitor near the system.
    All this is to eliminate interference on power lines that can come both from outside and inside our system, sometimes even causing it resetting the entire program !
    It is not said that it will always reset - but it may reset at the least expected moment.
    Topicis very extensive, but I'll end it here - we simply shouldn't run any circuit without good power supply filtration.

    We start operation with the PIC18F2550
    At this point, we start the proper part of the tutorial, i.e. working with the PIC on the breadboard.
    I chose this part PIC18F2550 , because it is quite popular, has rich peripherals and hardware USB, which we may also use in the future.
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    Apart from PIC18F2550 could be used here PIC18F4550 , which
    it is very similar to it, but it offers a bit more pins (it is in a DIP40 package).
    There are also slightly newer versions of these PICs, PIC18F25K50 and PIC18F45K50 which have e.g. more accurate internal oscillator, so you can use USB on them without using an external crystal oscillator.
    PIC18F2550 operates from 4.2V to 5.5V, but a version is also available ( PIC18LF2550 , where ''''L'''' means ''''Low voltage'''') for voltages from 2V to 5.5V.
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment

    The first step will be to communicate the PIC with the programmer. To do this, connect all its power pins (VCC, GND) and a capacitor on VUSB. In addition, a 10k resistor on the RESET pin will be useful.
    But let's start at the beginning, that is, with a blank breadboard.
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    First, we connect the power supply. Obligatory electrolytic and ceramic capacitor on VDD/GND lines:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    It is also worth adding some LED to the power supply to know if 5V is present in the system:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    Then the PIC comes into action - we connect all its VDD/GND pins. There are two GND pins and one VDD pin here. In addition, 100nF as close as possible to the PIC pins.
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    Microcontrollers from the PIC18F family also often require an additional capacitor with a capacity of 220nF. It is connected to the VUSB pin, according to the data sheet:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    Interestingly, sometimes it may happen that the PIC will work properly without this capacitor. I used to have a CD PIC18F4550 and with cold solder in place of this capacitor, on which the USB support batch was uploaded and it worked on one computer, and on the other Windows showed the message " Unrecognized device. ".
    But we're not going to risk it so the 220nF capacitor is on USB We connect right away. In addition, a 10k resistor on the RESET pin (so-called pull up for MCLR). It may not be necessary for the programming itself, but to run the programmed batch it is (well, unless we disable RESET in the PIC configuration, but we don't make it that complicated).
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    ATTENTION: The breadboard I use is made so that its side power lines are divided in the middle of the board. Therefore, a jumper is needed. It is marked in red in the photo above.
    Finally, we draw the lines responsible for programming ICSP ( In Circuit Serial Programming ), i.e. pins PGC , PGD , MLCR aka RESET :
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    We connect the programmer to them.
    The programmer also needs to be connected to ground (of course) and to the power supply ( VDD ), we cannot omit these pins.
    PIC microprocessors generally support two types of programming:
    - High Voltage Programming (HVP) (which in the case of family PIC18F gives 12V on VPP pin)
    - Low Voltage Programming (LVP) (which doesn't require 12V on VPP )
    LVP can be disabled in configuration DRINK and that's why we use HPP . Good programmers HPP are PICKIT2 / / PICKIT3 .
    programmer for DRINK and we connect through the so-called connector ICSP . In Circuit Serial Programming , as the name suggests, allows you to program the microcontroller even when it is already connected in the system. So we can have our whole layout on PCBs and we don't have to take it out DRINK and from the board/stand to program it.
    ICSP has the following pins:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    pin number 6 ( PGM aka LVP ) is not needed and we do not connect it. The rest is necessary for connection.

    And this is how it looks finally finished setup for the next steps of my tutorial. On it we will run further examples with LED blinking, with interrupts, with UART, etc.
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    In such an arrangement PICKIT should already be able to read the ID of the microprocessor, program it, etc.

    The first flashing of the LED
    Now it remains to check the operation of our work created on the breadboard with the help of the simplest program - blink LED. The program will simply flash the LED.
    First of all, we need to choose the pin on which we will place the LED and place it on the board with the resistor.
    (The resistor is needed there to limit the current flowing through the diode and not burn it).
    I selected pin C0 (lowest bit from PORTC):
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    So we connect a resistor and an LED to it:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    And we compile and then upload the following code:
    Code: C / C++
    Log in, to see the code

    NOTE: This code will be discussed in detail in the next part of the tutorial. For now, we're just checking if it works.


    The compilation, of course, passes successfully and a moment later we have the .hex ready to be uploaded:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment

    Now you still need to upload the batch to the PIC.
    I will do it with help PICKIT3 . In the case of other programmers, the software used may be slightly different.
    We fire up MPLAB IPE (Integrated Programming Environment):
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    After starting IPE, it immediately detects that we have it connected PICKIT3 . It is visible in the drop-down box Tool . We choose the family of microcontrollers we are interested in and the specific microcontroller we have. Then we click Connect to connect.
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    The IPE warns us that we should not try to program 3.3V PICs with the 5V PIC settings. This is why it is important that we choose the PIC we have correctly.
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    When switching to PIC18F You have to wait a bit for the first time PICKIT3 will switch to the programming mode of this family. Next programming in turn will be carried out quite soon.
    Then just select the hex file (" Browse " With " hex file ") and you can load the batch.
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    After correctly uploading the batch, we should get the long-awaited effect - flashing LED:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    Connection diagram from the photo above:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment
    I am attaching here the full zip package with this example (.c source code, .bat file compiling it on each computer with correctly installed SDCC / / GPUtils , compiled .hex file):
    p18f2550_..nk.zip Download (9.38 kB)Points: 1 for user

    The continuation of the tutorial and examples for the PIC18F2550 will be in the next topic.

    Complementing the tutorial - materials to read
    Of course, it would be difficult to fully describe everything related hereWith SDCC and DRINK Therefore, I refer those interested in the subject to the source materials:
    Manual SDCC:
    sdccman-3...5.pdf Download (900.64 kB)
    Manual CodeBlocks:
    codeblocks...ual_en.pdf Download (1.15 MB)
    Short description of SDCC and CodeBlocks configuration for 8051 (slightly similar process for SDCC and PIC):
    CodeBlocks...torial.pdf Download (801.76 kB)

    Summary
    This was the first part of my hands-on tutorial on the free compiler SDCC and PIC18F2550 . Here I tried to show in detail the configuration of the compiler and the environment on Windows and Linux platforms. I have also shown that it can be used in conjunction with Code Blocks .
    In the next part we will program the PIC itself, we will deal with the basics of IO, i.e. LED control, button operation, simple UART communication and maybe even ADC or PWM.
    PS: I invite you to comment, however, if anyone has any less substantive comments (typos, wrong words, minor corrections) then please send them to me on PM, I will correct and update the topic, let's not make a trash on the forum.

    Cool? Ranking DIY
    Helpful post? Buy me a coffee.
    Do you have a problem with Arduino? Ask question. Visit our forum Arduino.
    About Author
    p.kaczmarek2
    Moderator Smart Home
    Offline 
    p.kaczmarek2 wrote 11846 posts with rating 9941, helped 566 times. Been with us since 2014 year.
  • ADVERTISEMENT
  • #3 18304483
    p.kaczmarek2
    Moderator Smart Home
    simw wrote:
    Titanic work, thank you.


    Thank you for the good words.

    This is rather just the beginning of the series, I intend to describe most of the activities in a practical (and accurate) way SDCC and PIC18F2550 .

    you know, that nothing can replace a good book but I have contact with various students/or other students and I know that many people (younger and older) really need such a detailed step-by-step explanation of what and how to do. I'll try to provide something like that.

    Well, I'm thinking about something similar about some 16-bit or 32-bit microcontroller from Microchip - if anything, I accept requests/suggestions.
    Helpful post? Buy me a coffee.
  • #4 18305043
    bsw
    Level 21  
    I looked at the article briefly, I was intrigued by the fragment with the USB socket - the lamp turned on: could this PIC be programmed via usb?!?
    No - unfortunately you need a programmer ...
    Thank you, I'm sticking with AVR (while they're still there...)
  • ADVERTISEMENT
  • #5 18305271
    p.kaczmarek2
    Moderator Smart Home
    bsw wrote:

    No - unfortunately you need a programmer ...


    PICKIT3 (or its clone) is very cheap.
    This is a one-time expense of $10 on Ali:
    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment

    It is only about PLN 40, and you will be able to program almost all PICs, including memory, and maybe also AVRs (as I wrote in the subject - I saw that they are supported but I did not check).
    Helpful post? Buy me a coffee.
  • #6 18305902
    tangofox
    Level 14  
    Would like to ask if it is possible to use Pickit3 as a debugger in codeblocks?
    If so, could someone point me to some reference material?
  • ADVERTISEMENT
  • #7 18306046
    nolens_volens
    Level 15  
    A piece of great work.
    Thank you!
    And I'm looking forward to the sequel :)
  • #8 18306863
    pier
    Level 24  
    bsw wrote:
    I looked at the article briefly, I was intrigued by the fragment with the USB socket - the lamp turned on: could this PIC be programmed via usb?!?
    No - unfortunately you need a programmer ...
    Thank you, I'm sticking with AVR (while they're still there...)


    Can AVRs be programmed via USB?
  • #10 18307496
    And!
    Admin of Design group
    Very good material! The PICKIT3 programmer is quite cheap, so it's easy to get started with PIC.

    As for I2C, communication with some popular RTC, e.g. DS3231, can be presented as an example.
  • #11 18316425
    _lazor_
    Moderator of Designing
    You have one extra photo in the "Windows - Setting the PATH environment variable for SDCC" section

    Very well described tutorial, I'm glad that there is some variation from AVR and ARM :D
  • ADVERTISEMENT
  • #12 18905617
    pawelr98
    Level 39  
    Just Pickit 3 can easily power the programmed circuit.
    You enter the advanced options and in the power tab you can select it.

    Tutorial PIC18F2550 + SDCC - Part 1 - Setting up the working environment

    For me, when choosing 5V, he had problems, probably too low voltage from USB.
    So I gave 4.5V and there is no problem, programming works.
    The programmer itself has a VDD pin especially for this purpose.

    It is also worth mentioning about programming in MPLAB IDE.
    I bought myself to learn on the basis of this guide PIC16F628A.
    There were no libraries like pic18reg for it.
    So I programmed in MPLAB IDE using the XC8 compiler.

    Browsing the pic16f628a.h file, I found registers from the documentation, which allowed me to find the syntax in the C language.

    In my opinion, it is more convenient, because you can immediately generate even configuration bits in C in this environment.


    And the author deserves a big thank you.
    Thanks to a friend, I was able to start working with microcontrollers seriously.

    While Pic18 is a bit different, many things are still similar.
    Various issues are explained very well.

    I am hoping to continue the series.

Topic summary

The discussion revolves around a tutorial series for setting up the SDCC (Small Device C Compiler) for programming PIC microcontrollers, specifically the PIC18F2550. The author aims to provide a detailed, step-by-step guide, addressing various peripherals and libraries in future parts. Users express appreciation for the tutorial's clarity and request additional topics, such as I2C communication examples. The conversation also touches on programming methods, including the use of PICKIT3 for programming and debugging, and the possibility of programming PICs via USB with appropriate bootloaders. Suggestions for using specific components like the DS3231 RTC in I2C communication are also made.
Summary generated by the language model.
ADVERTISEMENT