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

8051 Keyboard Interfacing

Keyboards are organized in a matrix of rows and columns. The CPU accesses both rows and columns through ports. Therefore, with two 8-bit ports, an 8 x 8 matrix of keys can be connected to a microprocessor. When a key is pressed, a row and a column make a contact. Otherwise, there is no connection between rows and columns. In IBM PC keyboards, a single microcontroller takes care of hardware and software interfacing. A 4x4 matrix connected to two ports. The rows are connected to an output port and the columns are connected to an input port. Grounding Rows and reading Columns:- 8051 keyboard interfacing It is the function of the microcontroller to scan the keyboard continuously to detect and identify the key pressed. To detect a pressed key, the microcontroller grounds all rows by providing 0 to the output latch, then it reads the columns. If the data read from columns is D3 – D0 =1111, no key has been pressed and the process continues till key press is detected. If one of t