Why program 8051 in C
Compilers produces hex file that is downloaded to
ROM of the Micro-Controller. The size of the
hex file is the
main concern of the Micro-Controller programmers. The reasons behind it are
- Micro-Controllers have limited on-chip ROM.
- Code space for the 8051 is limited to 64K bytes.
Assembly language produces a file that is much
smaller in size than produced by the C, but 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 produced is much larger than Assembly language. The reasons for writing
code for Micro-Controller in C language are:
- It is easier and less time consuming to write in C than Assembly.
- C is easier to modify and update.
- You can use code available in function libraries.
- C code is portable to other MC with little or no modification.
C Data Types for 8051
Since every 8051 programmer want small .hex file so it is worthwhile to re-examine C data types for the 8051. Here we discuss the most frequently used data types in 8051 programming.
- Char (Unsigned, signed)
- Int (Unsigned, Signed)
- Sbit
- Bit
- Sfr
Char data type
The char (character) data type is the most natural choice of the programmers because 8051 is an 8-bit MC. The unsigned char is an 8-bit data type in the range of 0-255 (00-FFH).
Signed char is an 8-bit data type uses the MSB D7 to represent - and + and gives values from -128 to +127. By default C compiler uses the signed char if there is no unsigned keyword. This data type is used widely for 8051 in setting up Counter values and ASCII characters.
Int data type
The unsigned int is 16-bit data type. It takes a value in the range of 0 to 65535 (00-FFFFH). It is used to define 16-bit variables such as memory addresses and setting up counter values of more than 256. The misuse of int data type may result a larger .hex file as it is 16-bit data type and registers and memory accesses are in 8-bit chunks.
Signed int is a 16-bit data type uses the D15 to represent -and + sign. This data type ranges from -32,768 to +32767
Sbit data type
This keyword is widely used in 8051 C data type. It is designed specifically to access single-bit addressable registers. It allows access to the single bits of the SFR registers. It also allows access to single bit of the ports P0 to P3.
Bit data type
The bit data type is used for the bit-addressable section of RAM space 20 - 2FH.
SFR data type
This data type is used to access the byte-size SFR registers.