Sunday, May 3, 2015

Chip programming with Linux 2 - Compilers, Development Environments, Programming Languages and Blinking LED Circuit.


Description    : Installing compilers, Integrated Development Environments, Generating and installing Hex file for Blinking LED circuit.















Compatibility : Linux kernel 2.4 or 2.6. ( Tested )
                           FreeBSD                        ( NOT Tested
                           Mac OSX 10.4                ( NOT Tested
                           Mac OSX 10.5                ( NOT Tested )
                           OS X Mavericks 10.9.2  ( Tested )


Prerequisites : You have to complete http://iam-saminda.blogspot.com/2015/03/chipset-programming-with-linux-1-pickit.html


1. Introduction

1.1 What is PIC micro controller. ?

PIC chips are manufactured by microchip. They are producing 8bit, 16bit, 32bit micro controller. These micro controllers consisted with all features like modern computers but limited capabilities. These micro controllers has inbuilt RAM, EEPROM, Inputs and Outputs, CPU, Clock and many more features. We can install hexadecimal instruction file in to PIC chip. Once it assembled in proper circuit then it will function according under external power supply.

1.2 Which PIC micro controller will match your requirement ?

PIC chips comes with 8 bit, 16 bit, 32 bit micro controllers. Each chip has numerous number of pins which are used to interconnected with circuit. These pins can be used as inputs as well as outputs. 32 bit micro controller is more capable than 16 bit and  8 bit. If you are going to deal high efficient systems then 32 bit controllers will be good choice. If you are going to deal with low power consumption applications then good to choose 8 bit controllers.

You have to read data sheet given by microchip  in order to understand capabilities of each PIC micro controller.
    

1.3 Programming concerns for PIC micro controllers.

Group of each input and output pins called PORTS. A PORT of 8 bit micro controller has 8 input and output pins. A PORT of 16 bit micro controller has 16 input and output pins. A PORT of 32 bit micro controller has 32 input and output pins.   

In each PIC micro controller consist with many PORTS. Each PORT is coupled with Registers. These Registers are called TRIS. TRIS will define the input output nature of each pin of given PORT. 8 bit micro controller has several 8 bit TRIS. Each 8 bit TRIS coupled with each 8 bit of PORT.

If TRIS bit set to logic 1, then relevant bit of PORT will set as INPUT. If TRIS bit set to logic 0, then relevant bit of PORT will set as OUTPUT. Actually PORT is the interface that interact with external circuit components as Logic input and Logic output.

When given PIC controller has multiple registers , then relevant notations will be  TRISB, PORTB, TRISD, PORTD.



 













1.4 Our experimental PIC Controller

We are going to use 8 bit PIC 16F877A


1.5 Programming Language

In this section we are emphazising only recommended language given by PIC chip designers. microchip offers Micro C which is based on ANSI C Language. microchip offers wide range of libraries to make interfacing with large number of electronic devices.

1.6. Available Compilers
In this section we are emphazising only recommended compilers given by PIC chip designers. microchip offers 3 range of C compilers. They are 8 bit compiler, 16 bit compiler and 32 bit compiler. We are going to use 8 bit XC8 compiler as we are going to use 8 bit micro controller.


1.6. Available Integrated Development Environments.
In this section we are emphazising only recommended IDE given by microchip
That is MPLAB IDE.


2. Installing MPLAB IDE and XC8 Compiler.

Step 2.1 : First download latest MPLAB IDE.
               File name will be similar to MPLABX-v2.35-linux-installer.tar

Step 2.2 : Extract it using bellow command
            tar -xvf MPLABX-v2.35-linux-installer.tar
            It will generate sh file similar to MPLABX-v2.35-linux-installer.sh

Step 2.3 : Give full execution permission by bellow command
                chmod 777 MPLABX-v2.35-linux-installer.sh
             
Step 2.4 : Logging as root user using bellow command and enter root password
             su

Step 2.5 : Now run MPLABX-v2.35-linux-installer.sh using bellow command
             ./MPLABX-v2.35-linux-installer.sh

Step 2.6 :Resolve any dependance issues if occurred like bellow.

[root@localhost Downloads]# ./MPLABX-v2.35-linux-installer.sh
64 Bit, check libraries
Check for 32 Bit libraries
These 32 bit libraries were not found and are needed for MPLAB X to run:
libc.so
libdl.so
libgcc_s.so
libm.so
libpthread.so
librt.so
libstdc++.so
libexpat.so
libX11.so
libXext.so



Step 2.7 : In my case i have to install above libraries using bellow commands.
            You have to find your dependencies by searching one by one.

            yum install pam.i686
            yum install compat-libstdc++-296.i686
            yum install libexpat.so.1
            yum install libXext.i686



Step 2.8 : Again re run above command.
            Then it will start installation process like bellow

[root@localhost Downloads]# ./MPLABX-v2.35-linux-installer.sh
64 Bit, check libraries
Check for 32 Bit libraries
Verifying archive integrity... All good.
Uncompressing MPLAB X v2.35 Installer..



Step 2.9 : Select Next , The accept License agreement , then select
              installation path and select both MPLAB IDE and MPLAB IPE.
                Then click Next  and allow it to add PATH variables.
                Next finish the installation process.


















Step 2.10 Now restart the computer to get affect updated udev rules.
                   You can find MPLAB IDE icon under your applications

Step 2.11: Now we have to download and install XC8 C compiler
                First download latest XC8 C compiler
                File name will similar to xc8-v1.34-full-install-linux-installer.run
               

Step 2.12 : Give full execution permission by bellow command
                     chmod 777 xc8-v1.34-full-install-linux-installer.run
             
Step 2.13 : Logging as root user using bellow command and enter root password
             su

Step 2.14 :  Now run the installation using bellow command
                  ./xc8-v1.34-full-install-linux-installer.run

Step 2.15 : Click next, then accept license agreement, then select Free license type, then select installation directory , then allow to add path variables settings for all users , then next and finish the installation as bellow manner.


























3. Create MPLAB Project for Blinking LED.

Step 3.1 : Start MPLAB IDE

Step 3.2 : File -> New Project


Step 3.3 : Select Category Microchip Embedded and Standalone Project














Step 3.4 : Select Device as PIC 16F877A











Step 3.5 : Select Next and Select PICKit2 programmer.
               Because we are going to use PICKit2 programmer.














Step 3.6 : Select Next and Select XC8 8bit compiler














Step 3.7 : Select Next and give a Project Name called "LEDBlinker" and finish.












Step 3.8 : Right click on source file -> New -> C Main File -> Give file name
                as LEDBlinker and click finish.














Step 3.9 : It will create C file.












Step 3.10 : Add bellow two lines top of the code as we are going to use 4 Mhz
                 crystal. You can see final code at the bottom.

#define _XTAL_FREQ 4000000
#include



Step 3.11: Add bellow configuration codes before main method
                 You can see final code at the bottom.

// BEGIN CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
//END CONFIG


Step 3.12: Next we have to set Input and Output TRIS in the main method
                 You can see final code at the bottom.


                 TRISB0 = 0; //RB0 as Output PIN

Step 3.13: Now time to see data sheet given by microchip
                
             See the data sheet given by microchip to find out Relevant PORT pin. 
             That is pin 33 called RB0. RB0 is the relevant pin for port of TRISB0.




Step 3.14 : Now we are going to Blink LED continuously until we provide
                 power supply. Add bellow codes after TRISB0 = 0;

                while (1) {
                  RB0 = 1; // LED ON
                  __delay_ms(1000); // 1 Second Delay
                  RB0 = 0; // LED OFF
                  __delay_ms(1000); // 1 Second Delay
                }


                So use infinite for loop or infinite while loop. In side the loop, we will set Logic 1 and 0 to RB0 with some delays. Then pin 33 will enable with Logic 1 and 0 time to time after assembled the circuit under power supply. Logic 1 means, enabling 5V between pin 33 and ground. Logic 0 means enabling 0V between pin 33 and ground.


Step 3.15 : You can use bellow complete code.


/*
 * File:   BlinkLED.c
 * Author: PicDeveloper
 *
 * Created on May 3, 2015, 1:35 PM
 */

#define _XTAL_FREQ 4000000

#include <xc.h>
#include <stdio.h>
#include <stdlib.h>

// BEGIN CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
//END CONFIG
/*
 *
 */
int main() {

    TRISB0 = 0; //RB0 as Output PIN
    while (1) {
        RB0 = 1; // LED ON
        __delay_ms(1000); // 1 Second Delay
        RB0 = 0; // LED OFF
        __delay_ms(1000); // 1 Second Delay
    }
    return 0;
}



Step 3.15 : Now clean and build the project to generate Hexadecimal file of above code.
                    Right click on project then Clean and Build


Step 3.16 : You can find the Hexadecimal file from bellow location.
                 DO NOT OPEN THIS HEX FILE USING ANY EDITOR. 
                 IT WILL ALTER AND WILL NOT WORK.
                                



/MPLABXProjects/LEDBlink.X/dist/default/production/LEDBlink.X.production.hex



4. Installing Hexadecimal file into micro controller.

Step 4.1 : Plug the PIC 16F877A chip to PIC Programmer

Step 4.2 : Plug the PIC Programmer to your computer


Step 4.3 : Logging as root user using bellow command and enter root password
                  su


Step 4.4 : Run the auto detection command
                  Command : pk2cmd -P
                   
                  Result : Auto-Detect: Found part PIC16F877A.
                                Operation Succeeded
 


Step 4.5 : Erase existing hexadecimal file if there is an existing file
                  Command : pk2cmd -P PIC16F877A -X -E

               Result : Erasing Device...
                                Operation Succeeded


Step 4.6 : Change current working directory to your MPLAB project build path
                  Command : cd  /MPLABXProjects/LEDBlink.X/dist/default/production/



 Step 4.7 : Find the hexadecimal file. It should be similar to LEDBlink.X.production.hex

                 DO NOT OPEN THIS HEX FILE USING ANY EDITOR. 
                 IT WILL ALTER AND WILL NOT WORK.

Step 4.8 : Now install hexadecimal file in to PIC 16F877A chip
                  Command : pk2cmd -P PIC16F877A -X -M -F A.X.production.hex

                  Result : PICkit 2 Program Report
                                4-5-2015, 17:27:12
                                Device Type: PIC16F877A

                                Program Succeeded.

                                Operation Succeeded



Step 4.9 : Now power off PIC programmer
                  Command : pk2cmd -P PIC16F877A
 
                  Result       :  Operation Succeeded
                  Now you can see power indicator LED is NOT working



5. Blinking LED with 16F877A chip

Step 5.1 : Required electronic items

                1. Breadboard
                2. Programmed PIC 16F87A chip
                3. 4 Mhz crystal
                4. 2 items of 20 PF capacitors
                5. 4.7 Kilo Ohm resister
                6. 3V LED bulb
                7. 100 Ohm resister
                8. Breadboard Jump wire packet.
                9. 5 V DC power supply.
                10. 7805 Regulator if you have more than 5V DC power supply.

Step 5.2 : Construct bellow circuit on breadboard

               



















Step 5.6 :Double check the circuit before you provide power supply.

Step 5.7 : Provide 5 V DC power supply. If you have more than 5 V DC power
                  supply then use 7805 regulator to get 5V DC regulated supply.

Step 5.8 : Check LED is blinking every second. You can add more LED with
                  considering TRIS and PORT. You have to read data sheet given by
                  microchip.


Thank you. We will do some server motor controlling in next post. 
 

2 comments: