LED BLINK.

                      EMBEDDED C   TUTORIAL
                          TIMOTHY  ADEGBITE

MODULE 4: LED BLINK.




Welcome to today’s Trinnex electronics tutorial on Embedded C. Hope you have been enjoying the tutorials. Remember, you are free to send me your feedbacks and comments.


In continuation from the last tutorial,about our first project, LED BLINK, I will be explaining the steps in writing the codes necessary to achieve it. I hope you are also doing your own personal studies. The best students do not only source information from the class, take note.


This is the simple and basic program required to blink an LED.
I will be explaining the function and purpose of each line of the code.
Below shows the program written to achieve the blinking of the LED.

//This is a simple project, demonstrating the blinking of an LED using the PIC16f84A     microcontroller.

#include        <  xc.h  >                         // header file that includes all the files in the pic16f84a

 #define _XTAL_FREQ 4000000       // clockng requency is 4Mhz

void main(void)                              // This is the main function.it receives no argument and returns no value. it is a void function.
{
    TRISA=0B00000000;                  //  equates all PORTA as an output port

    TRISB=0B00000000;                 // equates all PORTB as an output port

    PORTA=0;                                  // clears PORTA bits

    PORTB=0;                                 // clears PORTB bits

    while(1)                                     // While loop; it itarates contineously
    {
        PORTBbits.RB0 =1;               // Set bit 0 of PORTB
        __delay_ms(100);                   // delay for 100ms
        PORTBbits.RB0 = 0;              // clears bit 0 of PORTB
        __delay_ms(100);                   // delay for (100ms
    }

}

The image below shows the exact way the code should be written.

 This is how it appears in the MPLABX IDE.




After inputting the code  shown above into the MPLABX IDE,

1. Click on the build project icon.
2. Wait to see the BUILD SUCCESSFUL message displayed. If it shows any error, it means you have to review the code for corrections.


line1:   This shows the purpose of the program. it is written as a comment. In C++, double slash sign, // is used to add comment to your program.
When used, the compiler will not see anything after the // sign.


line2:   #include . This is where the header file is included into the source code. The header file included here is the xc.h file. This file contains all the files and their addresses of the PIC16F84A. The compiler need to know the files and their addresses to be able to communicate with the microcontroller.
Always include this file in every of your program. It saves you the hassle of having to type the names of the files and their address. Although, there are other ways to go about this. But for the purpose of this tutorial, let us stick to this method.


line3:  #define _XTAL_FREQ 4000000. This is where the crystal oscillator frequency of the microcontroller is determined. The oscillating frequency chosen here, must be the same as the frequency of the crystal oscillator that will be used in the schematics.
I have chosen the frequency to be 4MHZ.

line4: This is called a void function because it receives no argument and returns no value. The actual compilation of the program starts here. In C++, this is called a void function. It starts on line5 and ends on line17. The part before this is called the header, line1 – line3, Where variables are defined and files are included in the source code.

line6 - line7: This is where the ports are configured. You can either configure each bit of the port as an input or an output channel.

TRISA is the register that controls PORTA of the microcontroller. PORTs are the channels through which the microcontroller exchanges information with the outside world. The PIC16F84A has just two ports, PORTA and PORTB. PORTA has five channels or five bits, PORTB has 8 channels or 8 bits. Each bit can be configured either as an input or an output channel.

If a channel is an input channel, it means that that channel is dedicated for receiving information from the outside world.

If a channel is an output channel, it means that, that channel is dedicated for sending information to the outside world.


Example. If I wish to configure bit4 of PORTA as an input bit, I simple set the bit4 of the PORTA to 1.

TRISA=0B00010000;

The counting of the bit starts from the LSB( least significant bit) to the MSB (most significant bit).

Let us assume this is an 8bit file.


1      0      1   0      1      0   0         1

MSB                                                        LSB                                                                  
       7        6       5      4        3      2       1        0



The same applies to TRISB.

line8 - line9:  This is where the ports are cleared. There might be some unwanted values in the PORT, so the ports has to be initialized to its original state configured by the TRIS register.


line10 - line16:  line10 is where the while loop starts. The while loop is also called the forever loop. The loop continues to iterate as long as the value in the bracket is not zero. It continues to execute the program between its curly brackets forever as long as there is no cause for reset or power failure. The curly brackets shows where the while loop starts and ends. It starts on line 11 and ends on line16.

line12:  This is where the individual bit of the port is set. The syntax for achieving this is (portname)bits.R(portnumber) example PORTBbits.RB0 , PORTAbits.RA1,
PORTBbits.RB4 and so on.

line13 and line 14 is the syntax for inputting delay in your program. The delay can either be in milli seconds or micro seconds. __delay_ms(value)  or  __delay_us(value). Example __delay_ms(100) this signifies 100ms delay  ,  __delay_us(1000) this signifies a 1000us delay.


After building, you have to import the HEX file into the microcontroller.

Below shows how this is achieved



1.  Place the cursor on the microcontroller
2. Right click
3. A window appears.
4. Click on Edit Properties

After doing this, the image below is displayed.




Click on the file icon and locate where the project folder was created. If you followed my example in the previous tutorial, the search path to the project folder is this:  Firstly, locate he file called Embedded Tutorial.
 
          a. Embedded Tutorial
           b. HELLO WORLD.X
c. dist
d. default
e. production
f. HELLO_WORLD.X.production.hex


After locating HELLO_WORLD.X.production.hex, the image below is shown


1. Click on the hex file.
2. Click on open

After doing this, you need to set the oscillating frequency of the microcontroller. To do this, right click on the microcontroller, click on Edit properties, edit the processor clock frequency to 4MHz as shown below.



After editing, simple click on OK, as shown below.



Run the simulation by clickin on the play button as shown below.




The video below, shows the finished prototype of the project.





To really have a good understanding of Embedded C, You need to have a good indebt of C++. Download books and materials on C++. It will definitely be of great help. Your progress depends on you. The world is in need of embedded programmers. There are so many problems waiting for your solutions.
























Comments