MODULE 3 -C Programming
MODULE 3 -C Programming
Embedded C is one of the most popular and most commonly used Programming Languages in the
development of Embedded Systems. It is popular due to its efficiency, less development time and
portability.
a) unsigned char
b) signed char
c) signed int
d) unsigned int
e) Sbit(Single bit)
f) Bit
g) Sfr
Unsigned Char:
Unsigned char is 8 bit data type in the range 0 – 255 (0 - FF). This is widely used because 8051 is
a 8 bit microcontroller. This is used as ASCII characters and to count the value
Signed char:
Signed char is an 8 bit data type in which the MSB represents the sign ( + or - ) in the range -
128 to +127.
Unsigned int:
Unsigned int is a 16 bit data type which takes the value in the range 0 to 65535 ( 0 – FFFF). This
is used for –
Defining 16 bit memory addresses
To set the counter values which is more than 256
Signed int:
Signed int is a 16 bit data type. MSB is the sign bit and the remaining 15 bits represent the
magnitude from - 32768 to +32767.
sbit:
This data type is used in case of accessing a single bit addressable registers. It allows to access to
the single bit SFR registers .
Bit:
This data type is used for accessing the bit addressable memory of RAM (20h-2fh).
sfr:
This data type is used for accessing a SFR register by another name. All the SFR registers must
be declared with capital letters
Program 1:
Program 2:
Program 3:
Program 4:
// toggle P1 forever
#include <reg51.h>
Void main(void)
Program 5:
In creating a time delay using a for loop, there are three factors that can affect the accuracy of the delay.
The number of machine cycles and the number of clock periods per machine cycle vary among different
versions of the 8051/52 microcontroller. While the original 8051/52 design used 12 clock periods per
machine cycle, many of the newer generations of the 8051 use fewer clocks per machine cycle.
The crystal frequency connected to the XI – X2 input pins. The duration of the clock period for the
machine cycle is a function of this crystal frequency.
3. Compiler choice.
If the program is in Assembly language, then the exact instructions and their sequences used in the delay
subroutine can be controlled. In the case of C programs, it is the C compiler that converts the C
statements and functions to Assembly language instructions. As a result, different compilers produce
different code. In other words, if we compile a given 8051 C programs with different compilers, each
compiler produce different hex code.
1. Write an 8051 C program to toggle bits of PI continuously forever with some delay.
Solution:
#include <reg51.h>
2. Write an 8051 C program to toggle the bits of PI ports continuously with a 250 ms
delay.
Solution:
Program 3:
Data Conversion Programs in 8051 C
Application of logic and rotate instructions in the conversion of BCD and ASCII.
ASCII numbers
On ASCII keyboards, when the key “0″ is activated, “011 0000″ (30H) is provided to the computer.
Similarly, 31H (011 0001) is provided for the key “1″, and so on, as shown in Table
After this conversion, the packed BCD numbers are processed and the result will be in packed BCD
format
Program 1:
Program 2: