Saturday, October 9, 2010

1.6 Embedded C & its Program Structure

Hello Readers:

I have started this blog to show you Embedded C programming for 8051 family microcontroller using KEIL compiler. The KISS philosophy is used to write this tutorial. What is KISS? Keep It Simple, Stupid!

1.6 Embedded C & its Program Structure

Why program the 89S52 in C?: Compilers produce hex files that we download in to the ROM of the microcontroller. The size of the hex file produced by the compiler is one of the main concerns of microcontroller programmers, for two reasons:

  1. Microcontrollers have limited on-chip ROM.
  2. The code space for the 8051 is limited to 64Kbytes.
How does the choice of programming language affect the compiled program size? While Assembly language produces a hex file that is much smaller than C, programming in Assembly language is tedious and time consuming. C programming, on the other hand, is less time consuming and much easier to write, but the hex file size produced is much larger than if we used Assembly language. The following are some of the major reasons for writing programs in C instead of Assembly:

  1. It is easier and less time consuming to write in C than Assembly.
  2. C is easier to modify and update.
  3. You can use code available in function libraries.
  4. C code is portable to other microcontrollers with little or no modification.
C data types: Since one of the goals of 8051 C programmers is to create smaller hex files, it is worthwhile to re-examine C data types for 8051 C. In other words, a good understanding of C data types for the 8051 can help programmers to create smaller hex files.

Since the 8051 is an 8-bit microcontroller, the character data type is the most natural choice for many applications. The unsigned char is an 8-bit data type that takes a value in the range of 0-255 (00-FFh). It is one of the most widely used data types for the 8051. In many situations, such as setting a counter value, where there is no need for signed data we should use the unsigned char instead of the signed char. Remember that C compilers use the signed char as the default if we do not put the keyword unsigned in front of the char.

Table 1.6.1 shows widely used data types for 8051 C. Note that default data type is signed char.


Table 1.6.1
Data Type
Size in Bits
Data Range/Usage
unsigned char
8-bit
0 to 255
(signed ) char
8-bit
-128 to +127
unsigned int
16-bit
0 to 65535
(signed) int
16-bit
-32,768 to +32,767
sbit
1-bit
SFR bit-addressable only
bit
1-bit
RAM bit-addressable only
sfr
8-bit
RAM addresses 80-FFh only

Program Structure of Embedded C
Generally the program structure of embedded c is as shown in Fig 1.6.2. Note that it is similar to C language. 




The comments are done by putting two forward slashes in front of single-line comment or putting block comments in between /* and */. The first line indicates the program number of the source code.

The include line or line two is necessary as it includes intrinsic functions, declarations to our program. We are including REG51.H header file as we are using 51 series microcontroller chip as our target chip.

The third line declaring the function MSDelay() before main, and is known as function prototyping. It is necessary as MSDelay() function is written after the main and compiler executes the source code from top. The word void indicates function returns nothing.

As C language, embedded C needs main() function and block codes written inside it are executed first. The function code is written in between ‘{‘ and ‘}’

In embedded C also semicolon is used as statement, command terminator. 

The sixth line shows how to declare the variables. The tenth line shows how to call function MSDelay().The thirteenth line is function heading of MSDelay().  

<End of 1.6   Embedded C & its Program Structure >

...till next post bye-bye & take care.

No comments:

Post a Comment