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 ...

Frequency of Oscillation of RC Phase Shift Oscillator

Derivation of Frequency of Oscillation We have to find out the transfer function of RC feedback network. Feedback Circuit of RC Phase Shift Oscillator Applying KVL to various loops on the figure, we get, $$I_1 \left(R+\frac{1}{j \omega C }\right) -I_2R=V_i \text{ ....(1)}$$ $$-I_1R+I_2\left (2R+\frac {1}{j\omega C}\right)-I_3R=0\text{ ... (2)}$$ $$0-I_2R+I_3\left(2R+ \frac{1}{j\omega C}\right)=0\text{ ...(3)}$$ Replacing \(j\omega\) with \(s\) and writing equations in the matrix form, $$\begin{bmatrix}R+\frac{1}{sC} & -R & 0 \\-R & 2R+\frac{1}{sC} & -R \\0 & -R & 2R+\frac{1}{2sC} \end{bmatrix}\begin{bmatrix}I_1\\I_2\\I_3\end{bmatrix}=\begin{bmatrix}V_i\\0\\0\end{bmatrix}$$ Using Cramer's rule to find out \(I_3\), $$\text{Let, }D=\begin{bmatrix}R+\frac{1}{sC} & -R & 0 \\-R & 2R+\frac{1}{sC} & -R \\0 & -R & 2R+\frac{1}{2sC} \end{bmatrix}$$ \(|D|=\begin{vmatrix}R+\frac{1}{sC} & -R & 0 \\-R & 2R+\frac{1}{...