Difference between revisions of "Using the Code::Blocks IDE with SDCC on PIC MCUs"

From Code::Blocks
(Add Edited Copy of PIC_Programming from wikibooks.org)
 
m (warning 119: don't know what to do with file 'obj\Release\main.rle'. file extension unsupported)
 
(28 intermediate revisions by one other user not shown)
Line 1: Line 1:
 
== Introduction ==
 
== Introduction ==
  
I am editing this right now to work with the Code::Blocks IDE
+
I am done with the main items on this, but I may need to expand some sections.
 +
Please use this thread for questions. /index.php/topic,8373.msg61848.html
 +
 
 
Tim S
 
Tim S
  
Line 8: Line 10:
 
This module assumes
 
This module assumes
 
* you, the reader, know a little about programming using C and assembly.
 
* you, the reader, know a little about programming using C and assembly.
* you have a MPLAB ICD 2 and a PICDEM 2 Plus demo board.
+
* you have a Code::Blocks IDE, SDCC, GPUTILS and a PICDEM 2 Plus demo board.
* The code examples will be on the PIC16F877 and PIC18F452 microcontrollers.
+
* The code examples will be using the PIC16F877 and PIC18F452 micro-controllers.
  
 
Before getting started,
 
Before getting started,
* install latest Code::Blocks IDE from [http://www.microchip.com Microchip Technology Inc] and the SDCC compiler.
+
* install latest Code::Blocks IDE from [https://www.codeblocks.org CodeBlocks.org], the SDCC compiler [http://sdcc.sourceforge.net Small Device C Compiler] and [http://gputils.sourceforge.net GNU PIC Utilities].
  
 
== Getting Started with Simple I/O ==
 
== Getting Started with Simple I/O ==
Line 18: Line 20:
 
We will start with a simple input output program using PORTA and PORTB. Let light some LEDs on the board. We should assign value 0x0A to PORTB. This will light up LEDs at RB3 and RB1. Everytime the switch button at RA4 is pressed, the value will be negated; thus the LEDS will switch between RB3/RB1 and RB2/RB0.
 
We will start with a simple input output program using PORTA and PORTB. Let light some LEDs on the board. We should assign value 0x0A to PORTB. This will light up LEDs at RB3 and RB1. Everytime the switch button at RA4 is pressed, the value will be negated; thus the LEDS will switch between RB3/RB1 and RB2/RB0.
  
First, start your MPLAB IDE. In the menu bar, select Project → Project Wizard. Hit next, and select PIC18F452 as your device. Select next again. Under Active Toolsuite, select Microchip C18 Toolsuite and MPLAB C18 C Compiler (mcc18.exe) under Toolsuite Contents. Now, we have to give a name to our project and select a directory for it. Since we going to do a simple I/O, let us name it Simple_IO at Project Name. Then, select a directory. In this case, I would use c:\example\simpleIO. Hit the next button. We do not have any files to add to the project. So, hit the next button again. Now, we are finished with the Project Wizard. Hit the finish button.
+
First, start your Code::Blocks IDE. Then use menu "Settings" -> "Compiler and Debugger" to setup the SDCC compiler options.
  
Once the project is setup, we should create a C file called SimpleIO.c. At the menu bar, select File → New. Once the new editor pops, select File → Save As. At the new window, save the newly created file as SimpleIO.c. On the Project Window (where Simple_IO.mcv is the title) right click on Source Files. Select Add Files... from the menu. Select SimpleIO.c from where you save just now. On the same window, select Linker Scripts and select Add Files... from the menu. Under Microchip C18 C compiler directory (default is C:\MCC18), go to c:\''installed directory''/lkr and type 18f452.lkr at the File name. Type the following code into the editor.
+
# Select the "SDCC Compiler"
 +
# Click on Button "Set as Default"
 +
# Click on Tab "Toolchain executables"
 +
# Click on Tab "Additional Paths"
 +
# Verify the paths for installation and additional directories.
 +
## Normal installation directory is "C:\Program Files\SDCC"
 +
## Normal additional directories are "C:\Program Files\SDCC\bin" and "C:\Program Files\gputils\bin"
 +
# Click on Tab "Search Directories"
 +
# Verify the search directories
 +
## Normal Compiler search directory of "C:\Program Files\SDCC\include"
 +
## PIC Normal Compiler search directory for SDCC 3.0 and later of "C:\Program Files\SDCC\non-free\include"
 +
## Normal Linker search directory of "C:\Program Files\SDCC\lib"
 +
# ''for sdcc 3.2.1'' Click on Tab "Other Settings"
 +
## Click on Button "Advanced options..."
 +
## Click on "Others" tab
 +
## Modify the "Object file extension"'s value from '''rle''' to '''o'''.
 +
 
 +
Second, create a empty project. In the menu bar, select "File" -> "New" -> "Project". Select "Empty Project" and click on GO. Enter the required information. You will get warning messages that Code::Blocks does not know how to setup somethings; ignore them.  
 +
 
 +
Now change the project so it works with SDCC; In the menu bar, select "Project" -> "Properties"; select the tab "Build Target". Under target options change "Type" to "Native". Note, you need to set all the targets to type "Native". So, select the other target if they exist on the right plane. Set the Project build options; In the menu bar, select "Project" -> "Build Options". Under the "Compiler Settings" Tab and under the tab "Compiler Flag", set either the CPU flag for PIC 16 or 14 bit instructions.
 +
 
 +
Note: Sometimes it is necessary to use "Compiler Settings" Tab and under the tab "Options" to set the MCU; example is adding "-p18f452" without the double quotes to code for the PIC18F452 MCU.
 +
 
 +
Once the project is setup, we will create a C file called SimpleIO.c. Add the new file called SimpleIO.c.  
 +
 
 +
Type the following code into the editor.
  
 
<code><pre><nowiki>
 
<code><pre><nowiki>
#include <p18f452.h>
+
#include <pic16/pic18f452.h>
  
 
// Configurations
 
// Configurations
#pragma config OSC = HSPLL, OSCS = ON, PWRT = ON, BOR = OFF, WDT = OFF, LVP = OFF
+
code char at __CONFIG1H conf1 = _OSC_HS_PLL_1H & _OSCS_ON_1H;  // Select HS PLL OSC
 +
code char at __CONFIG2L conf2 = _PUT_ON_2L;
 +
code char at __CONFIG2H conf3 = _WDT_OFF_2H;                    // Disable WDT
 +
code char at __CONFIG4L conf4 = _LVP_OFF_4L;                    // Disable LVP
  
 
// Main body
 
// Main body
Line 41: Line 71:
 
     // Set value 0x0A to PORTB
 
     // Set value 0x0A to PORTB
 
     PORTB = 0x0A;
 
     PORTB = 0x0A;
+
 
 
     // If button is pressed, toggle PORTB
 
     // If button is pressed, toggle PORTB
 
     while(1) {
 
     while(1) {
Line 47: Line 77:
 
             PORTB = ~PORTB;
 
             PORTB = ~PORTB;
 
     }
 
     }
}</nowiki></pre></code>
+
}
 +
</nowiki></pre></code>
 +
 
 +
<code><pre><nowiki>
 +
#include <pic/pic16f877.h>
 +
 
 +
// Configurations
 +
    typedef unsigned int config;
 +
    config at 0x2007 __CONFIG = _HS_OSC & _PWRTE_ON & _BODEN_OFF & _WDT_OFF & _LVP_OFF;
 +
 
 +
// Main body
 +
void main() {
 +
 
 +
    // Initializing ports
 +
    PORTA = 0;
 +
    PORTB = 0;
 +
 
 +
    // Set RA4 as input and RB3-RB0 as output
 +
    TRISA |= 0x10;
 +
    TRISB &= 0xF0;
 +
 
 +
    // Set value 0x0A to PORTB
 +
    PORTB = 0x0A;
 +
 
 +
    // If button is pressed, toggle PORTB
 +
    while(1) {
 +
        if(RA4 != 0)
 +
            PORTB = ~PORTB;
 +
    }
 +
}
 +
</nowiki></pre></code>
  
Hit the save button on the toolbar or from the menu bar, select File &rarr; Save. Next, click on Project &rarr; Build All or Build All button at the toolbar at the top. Make sure you have no error or warning at the Output window. Hook up your MPLAB ICD 2 to your computer and connect PICDEM 2 Plus Demo board to the ICD (Please refer to the manual provided). Now, select Debugger &rarr; Select Tool &rarr; MPLAB ICD 2. Make sure there is no error at the output window. Next, click on Debugger &rarr; Program to program the microcontroller. Once you have successfull programed the microcontroller, click on Debugger &rarr; Run. You can also hit the Run button at the toolbar. On the demo board, LEDs at RB3 and RB1 should light up.
+
Hit the save button on the toolbar or from the menu bar, select File &rarr; Save. Next, click on Build &rarr; Build or Build button at the toolbar at the top. Make sure you have no error or warning at the Output window.  
  
Now, pressed the switch at RA4 on the demo board a few times. Noticed something wrong? The LEDs do flick, but do not switch between RB3/RB1 and RB2/RB0 all the time. And if you just pressed the switch without releasing it, all the LEDs do light up. This is not something that we want. Let us look at the code again to find out what is the problem. Noticed that in the while loop, as long as RA4 is read 0, value at PORTB will be inverted. With the microcontroller running at 16MHz, a flash push at the switch, which may consume up to 500us, may cause the value in PORTB to invert more than 1000 times. That is why the LEDs appear to be light up in random whenever the switch is pushed. The code has to be modified to detect a push, and a release at the switch.
+
I used the MPLAB to program the demo board. I did it by using "File" -> "Import" of the hex file created by SDCC/gputils. Then, I program the board the way I do under MPLAB.
  
== This program based on code and information from the following ==
+
== This page based on code and information from the following ==
 
http://en.wikibooks.org/wiki/Embedded_Systems/PIC_Programming
 
http://en.wikibooks.org/wiki/Embedded_Systems/PIC_Programming
 +
 +
== List of SDCC PIC programing Sites I used to get the code to compile and link ==
 +
 +
http://www.btinternet.com/~Peter.Onion/sdcc/sdcc-intro.html
 +
 +
http://www.freenet.org.nz/sdcc/
 +
 +
http://burningsmell.org/pic16f628/
 +
 +
== Related pages on other sites ==
 +
http://curuxa.org/en/Write_programs_for_PICs_in_C_using_SDCC_and_Code::Blocks_on_Windows
 +
 +
http://curuxa.org/en/Write_programs_for_PICs_in_C_using_SDCC_and_Code::Blocks_on_Linux

Latest revision as of 17:20, 17 March 2013

Introduction

I am done with the main items on this, but I may need to expand some sections. Please use this thread for questions. /index.php/topic,8373.msg61848.html

Tim S


This module assumes

  • you, the reader, know a little about programming using C and assembly.
  • you have a Code::Blocks IDE, SDCC, GPUTILS and a PICDEM 2 Plus demo board.
  • The code examples will be using the PIC16F877 and PIC18F452 micro-controllers.

Before getting started,

Getting Started with Simple I/O

We will start with a simple input output program using PORTA and PORTB. Let light some LEDs on the board. We should assign value 0x0A to PORTB. This will light up LEDs at RB3 and RB1. Everytime the switch button at RA4 is pressed, the value will be negated; thus the LEDS will switch between RB3/RB1 and RB2/RB0.

First, start your Code::Blocks IDE. Then use menu "Settings" -> "Compiler and Debugger" to setup the SDCC compiler options.

  1. Select the "SDCC Compiler"
  2. Click on Button "Set as Default"
  3. Click on Tab "Toolchain executables"
  4. Click on Tab "Additional Paths"
  5. Verify the paths for installation and additional directories.
    1. Normal installation directory is "C:\Program Files\SDCC"
    2. Normal additional directories are "C:\Program Files\SDCC\bin" and "C:\Program Files\gputils\bin"
  6. Click on Tab "Search Directories"
  7. Verify the search directories
    1. Normal Compiler search directory of "C:\Program Files\SDCC\include"
    2. PIC Normal Compiler search directory for SDCC 3.0 and later of "C:\Program Files\SDCC\non-free\include"
    3. Normal Linker search directory of "C:\Program Files\SDCC\lib"
  8. for sdcc 3.2.1 Click on Tab "Other Settings"
    1. Click on Button "Advanced options..."
    2. Click on "Others" tab
    3. Modify the "Object file extension"'s value from rle to o.

Second, create a empty project. In the menu bar, select "File" -> "New" -> "Project". Select "Empty Project" and click on GO. Enter the required information. You will get warning messages that Code::Blocks does not know how to setup somethings; ignore them.

Now change the project so it works with SDCC; In the menu bar, select "Project" -> "Properties"; select the tab "Build Target". Under target options change "Type" to "Native". Note, you need to set all the targets to type "Native". So, select the other target if they exist on the right plane. Set the Project build options; In the menu bar, select "Project" -> "Build Options". Under the "Compiler Settings" Tab and under the tab "Compiler Flag", set either the CPU flag for PIC 16 or 14 bit instructions.

Note: Sometimes it is necessary to use "Compiler Settings" Tab and under the tab "Options" to set the MCU; example is adding "-p18f452" without the double quotes to code for the PIC18F452 MCU.

Once the project is setup, we will create a C file called SimpleIO.c. Add the new file called SimpleIO.c.

Type the following code into the editor.

#include <pic16/pic18f452.h>

// Configurations
code char at __CONFIG1H conf1 = _OSC_HS_PLL_1H & _OSCS_ON_1H;   // Select HS PLL OSC
code char at __CONFIG2L conf2 = _PUT_ON_2L;
code char at __CONFIG2H conf3 = _WDT_OFF_2H;                    // Disable WDT
code char at __CONFIG4L conf4 = _LVP_OFF_4L;                    // Disable LVP

// Main body
void main() {

    // Initializing ports
    PORTA = 0;
    PORTB = 0;

    // Set RA4 as input and RB3-RB0 as output
    TRISA |= 0x10;
    TRISB &= 0xF0;

    // Set value 0x0A to PORTB
    PORTB = 0x0A;

    // If button is pressed, toggle PORTB
    while(1) {
        if(PORTAbits.RA4 != 0)
            PORTB = ~PORTB;
    }
}
#include <pic/pic16f877.h>

// Configurations
    typedef unsigned int config;
    config at 0x2007 __CONFIG = _HS_OSC & _PWRTE_ON & _BODEN_OFF & _WDT_OFF & _LVP_OFF;

// Main body
void main() {

    // Initializing ports
    PORTA = 0;
    PORTB = 0;

    // Set RA4 as input and RB3-RB0 as output
    TRISA |= 0x10;
    TRISB &= 0xF0;

    // Set value 0x0A to PORTB
    PORTB = 0x0A;

    // If button is pressed, toggle PORTB
    while(1) {
        if(RA4 != 0)
            PORTB = ~PORTB;
    }
}

Hit the save button on the toolbar or from the menu bar, select File → Save. Next, click on Build → Build or Build button at the toolbar at the top. Make sure you have no error or warning at the Output window.

I used the MPLAB to program the demo board. I did it by using "File" -> "Import" of the hex file created by SDCC/gputils. Then, I program the board the way I do under MPLAB.

This page based on code and information from the following

http://en.wikibooks.org/wiki/Embedded_Systems/PIC_Programming

List of SDCC PIC programing Sites I used to get the code to compile and link

http://www.btinternet.com/~Peter.Onion/sdcc/sdcc-intro.html

http://www.freenet.org.nz/sdcc/

http://burningsmell.org/pic16f628/

Related pages on other sites

http://curuxa.org/en/Write_programs_for_PICs_in_C_using_SDCC_and_Code::Blocks_on_Windows

http://curuxa.org/en/Write_programs_for_PICs_in_C_using_SDCC_and_Code::Blocks_on_Linux