Saturday, March 15, 2014

The Blinker in C

We are going to write our first program in C language, using MikroC compiler. It's not a free compiler, but we can use it for free upto some level.
 
 

 
 First, download the compiler and install it properly. Then create a new project and save it.  Select your microcontroller and clock speed properly. In this case, I'm using a PIC16f877A with a 8MHz clock.
 PIC16f877A with a 8MHz clock.
 
Then, type the following code in the text area you get, and then save it. This progra will switch on all the LEDs connected to port D once per second and then switch them off.
 
Pin connection
 
Code:
 
void main( )  {

         TRISD=0x00;             // make all the pins of PORTD output
         PORTD=0x00;           // make all the pins 0
 
 
         while(1) {                   // create an endless loop
                           PORTD=255;      //switch on all LEDs
                           Delay_ms(1000); //wait for 1s
                           PORTD=0x00;    //swithc off ann LEDs
                           Delay_ms(1000);  // wait for another 1s
                        }      // end of while(1)
 
       }  // end of the main program
 
 
Write the code and compile it by clicking the build button. Then you can simulate the code using Proteus ISIS or directly program it to the PIC microcontroller.