Embedded C: What the heck it is?

Most of us heard about C programming language by Dennis Ritchie between 1969 and 1973. Language was huge improvement over all previous languages and this is the language due to which UNIX and all other following operating systems became portable, i.e., with few modifications code program can be ported to different architectures without re-doing every bit of code but it has some problem that it’s good for systems like servers, workstations and desktop PC which has much more RAM then embedded systems.

Let’s see what is embedded C and brief programming introduction to it’s coding:

What is Embedded C?

First let me make clear that Embedded C is not a different language after it still follows same ISO as of original C. It’s a extension of standard C by C standards committee. It’s developed to address issues with C between different embedded systems.

Difference between Embedded C and Standard C

Embedded C Standard C
Dependent on hardware architecture Independent on hardware architecture
Use in limited resource systems Use in systems with much higher resource
OS dependent compilers Requires compilers to create files to be downloaded to the microcontrollers/microprocessors where it needs to run

Fun Part

Data types with range

Data Type Size Range
Char or signed char 1 byte -128 to +128
Unsigned char 1 byte 0 to 255
Int or signed int 2 byte -32768 to +32767
Unsigned char 2 byte 0 to 65535

Keyword

sbin – Accessing of single bit of SFR register

  • Syntax – sbit = SFR bit;
  • eg – sbit a P2^1
  • Use ‘a’ instead of p2.1 anywhere in program

bit – Accessing of bit addressable memory of RAM

  • Syntax – bit
  • eg – bit c
  • bit sequence setting in a small data area , will be used by program to remember something

SFR- Accessing of sfr register by another name

  • Syntax – SFR = SFR
  • Eg – SFR port0 = 0x80
  • Use 0x80 anywhere in program

That’s lots of SFR I see. Let’s see what SFR is:

SFR stands for ‘Special Function Register’. 256 bytes RAM of 8051 microcontroller is divided for data storage and for SFR register with 128 bytes each. Peripherals like I/0 ports, timers and counter are stored in this SFR register with each element having unique address.

Hello World

As tradition for learning a new programming language.


void main(){

    printf("Hello World");

    while(1);

}

This code will run forever until microcontroller is reset. This program because this shows basic difference between a PC and microcontroller. As embedded microcontroller don’t have any operating system like PC where once program has done execution, it return control to operating system. So, program has to run everytime and is not allowed to fall out.

Preprocessor directive is stdio.h for printf() function.

Basic Structure

  • Comments
  • Preprocessor directives
  • Port configuration
  • Global variables
  • main() function or core function
  • Variable declaration
  • Program Logic

A complex example


#include <reg52.h>

sbit LED = P2^3;

void Delay(void);

void main(void){
    while(1){
      LED = 0;
      Delay();
      LED = 1;
      Delay();
    }
}

void Delay(void){
    int j;
    int i;
    for(i=0; i<10; i++){
      for(j=0; j<10000; j++){
      }
    }
}

Compile it and a hex file will be generated in your project folder.

Preprocessor reg52.h has special function register declarations for the intended 8051 derivaties.

Defining LED pin using ‘sbit LED =t P2^0;’.

As embedded system has no operating system, so program has to run without exiting and that’s reason whole logic is under while(1){} block. ‘LED = 0;’ refers to LED ON and ‘LED = 1’ refers to LED OFF.

Simulators are also available if you want to test it before uploading hex to real hardware. Some simulators are (all simulators listed are free to use):

  • EdSim 51 – made for students and teachers with ease of learning Included keyboard and many other peripherals. (official website)
  • JSIM-51 – made by a frustrated engineer due to pricing for enterprise simulators
  • IAR Workbench – paid version but evaluation version is available for all most all microcontroller.
  • MIDE – IDE – IDE for 8051 from Opcube, with assembler, compiler and simulator. They use the free assembler and SDCC free compiler, which makes no code limit.
  • gSim – open source 8051 simulator
  • Rigel
Advertisements

One thought on “Embedded C: What the heck it is?

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

A WordPress.com Website.

Up ↑

%d bloggers like this: