Skip to main content

Introduction to 8051 embedded C

For 8051 we need to include the file reg51.h. This file contains the all the definitions of 8051 registers. With this information C compiler produces hex file that can be downloaded into the ROM of the microcontroller. It is important to note that the size of the hex file produced by the assembly language is much larger than the hex file produced by C compiler. Apart from this fact, there is many reasons for writing programs in C instead of assembly:
●It is much easier and less time consuming to write programs in C assembly.
●C is more flexible; it is easier to modify and update.
●Programming in C allows to use code available in function libraries.
●Program written inC for one microcontroller is portable to other microcontrollers with little or no modifications.

Data Types in 8051 Embedded C

The table shown below lists the data types that are available in typical C51 compiler. The gives information about the size of the data variable and it's value range.
Data type Bits Bytes Value range
bit 1 0 to 1
signed char 8 1 -128 to +127
unsigned char 8 1 0 to 255
enum 8 or 16 1 or 2 -128 to +127 or -32768 to +32767
signed short 16 2 -32768 to +32767
unsigned short 16 2 0 to 65535
signed int 16 2 -32768 to +32767
unsigned int 16 2 0 to 65535
signed long 32 4 -2147483648 to 2147483647
unsigned long 32 4 0 to 4294967295
float 32 4 ±1.175494E-38 to ±3.402823E+38
sbit 1 0 to 1
sfr 8 4 0 to 255
sfr16 16 2 0 to 65535

Using the Appropriate Data Type

It is necessary to keep in mind that the code space for 8051 or 89c51 is limited to 64 Kilobytes and it has limited on-chip ROM. Thus, it is necessary to look at the size the created hex file. One way to keep the size of the data file optimum is to use appropriate data type. The following points must be considered while selecting the data type.
•The unsigned char is an 8-bit data type. Thus, it must be to store the value in the range of 0-255 (00H-FFH). Since the 8051 is an 8-bit microcontroller, it is the one of the most widely used data type.
•Use unsigned data types when there is no need of signed data.
•C compiler uses signed data type as default. Thus when we want to use unsigned data type, we must use unsigned keywords.
sbit data type should be used to access bit addressable registers. It allows use to access single bits of SFR registers.
•SFR data type should be used to access the byte size SFR registers.

Logical Operations in 8051 C

One of the most frequent operations required in embedded applications is monitoring a single or a group of bits of a port, checking the status of bits and controlling an external device connected to a bit of an output port. C allows logical operations such as AND, OR, X-OR as well as shifting a byte to the left and right. The table shown below provides the syntax for such operations;
Operations In assembly In C Example in C
NOT CPL A ~ A=~A
AND ANL A,#DATA & A=A&DATA
OR ORL A,#DATA A=A DATA
EX-OR XRL A,#DATA ^ A=A^DATA
Shift right by n-bits RR A >> A=A>>n
Shift left by n-bits RL A << A=A<<n

Comments

Post a Comment

Popular posts from this blog

8051 Simple Programs

Sum of 8-bit Numbers Stored in Memory Here  we will discuss about 8051 programs but we can't discuss about all of the 8051 instructions. For programming 8051 we should know about assembler directives as well as instruction set. Click  here to download Atmel c51 user guide that will discuss about 8051(c51 family microcontroller) instruction set, assembler directives, c51 cross assembler from Atmel.  Program ORG 00H MOV R0,#50H   ; get memory location in memory pointer R0 MOV R1,#51H   ; get memory location on memory pointer register R1 MOV A,@R0       ; get content of memory location 50H to accumulator ADD A,@R1        ; add content of A with content of memory location 51H and store result in A MOV R0,#52H    ; get 52H to memory pointer R0 MOV@R0,A         ; copy content of A to memory location 52H END Add 16-bit Numbers Program ORG 00H MOV DPTR,#2040H   ; get 2040H into DPTR MOV A,#2BH              ; get lower byte of second 16-bit number on accumulator MO

Even and Odd Memory Banks

The 8086 microprocessor uses a 20-bit address to access memory. With 20-bit address the processor can generate 2 20 = 1 Mega address. The basic memory word size of the memories used in the 8086 system is 8-bit or 1-byte (i.e., in one memory location an 8-bit binary information can be stored). Hence, the physical memory space of the 8086 is 1Mb (1 Mega-byte). For the programmer, the 8086 memory address space is a sequence of one mega-byte in which one location stores an 8-bit binary code/data and two consecutive locations store 16-bit binary code/data. But physically (i.e., in the hardware), the 1Mb memory space is divided into two banks of 512kb (512kb + 512kb = 1Mb). The two memory banks are called Even (or Lower) bank and Odd (or Upper) bank. The organization of even and odd memory banks in the 8086-based system is shown in Figure. The 8086-based system will have two sets of memory IC's. One set for even bank and another set for odd bank. The data lines D 0 -D 7 are conne