INTRODUCTION TO EMBEDDED C

on Monday, 23 September 2013
Before we start our topic i want to make you few points clear. First thing i want to make you clear is that, this is not the C tutorial section to learn  C programming  language, here you will only learn how to program the PIC microcontroller using MikroC compiler. So before you go further to read my posts first get basics of C language from any source. You have many tutorials on net and you need not to be an expert in C but just learn the syntax and few basic programs and come here.

There are several compilers available for the PIC microcontroller. They are almost similar but little change in their library functions and other features. Some of the famous compilers are 

  • MikroC
  • CCS
  • Hitech C



Is it different to program for different compilers ? Answer is yes, but differ in least, the basic C programming is the same but if you are including library functions then you need to know what are the library functions offering by that particular compiler, check the 'help' section of your compiler to know all the library functions.
After compiling, various files will be generated, but we only concern with the file with extension .hex , because we burn or load this file into our microcontroller.

As we use PIC microcontroller and MikroC compiler, so we only concern with it. We look at structure of mikroC program,libraries and other features...

Look at the general structure below

Void main( )
{
   Program statement;  // comments
   .
   .
   .
}


All the statements will be in between parenthesis i.e { } and every program statement should be terminated with ; and comments should be after // . Its all similar to C language.
Let us look at simple example of blinking LEDs which are connected to Portb of PIC microcontroller.

Void main( )
{
TRISB=0;                     //PORTB as output
  while(1)                              //loop forever
  {
      PORTB=0b00000000; // All leds are off i.e 0
      Delay_Ms(1000);         // 1000 Milli sec delay i.e 1sec delay
      PORTB=0b11111111;// ON all the leds connected to portb
      Delay_Ms(1000);        // 1sec delay
   }
}

led blinking program



  • Here 0b00000000 is binary value.Portb have 8 pins so each bit value corresponds to each pin. i.e right most bit lsb 0 is portb.0 next bit portb.1 , next bit portb.2 and so on upto portb.7. When we make pin 0 then  the led connected to it will be in OFF state, when portb=1 then ON state. 
  • Delay_Ms( ) is the mikroC library function to generate the delay. We have generated 1000ms i.e 1sec delay between OFF and ON.

Note this is just an example program which is intended to show you the structure, we will look more deeply as our topics go further.

Download the mikroC for PIC software from this link
http://www.mikroe.com/mikroc/pic/


3 comments:

Tulasiram Manchikanti said...

Can we use KEIL for PIC programs........

YOUSTRON SIC said...

Yes you can use keil compiler, but remember there will be different library functions... I think i have said this in my other posts...

Aman Rustagi said...

Thanks a lot for explaining the difference between C programming language and microcontroller programming tutorial. I was unaware about the difference between c programming and embedded C tutorial. However most of the syntax of C and embedded C language is same.

Post a Comment