0% found this document useful (0 votes)
4 views8 pages

MODULE 3 -C Programming

The document discusses data types and time delay in 8051 C programming for embedded systems, highlighting various data types such as unsigned char, signed char, and sfr. It explains methods for creating time delays, including using for loops and timers, while noting factors affecting delay accuracy. Additionally, it covers data conversion programs for converting between ASCII and packed BCD formats.

Uploaded by

smritidas.0505
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views8 pages

MODULE 3 -C Programming

The document discusses data types and time delay in 8051 C programming for embedded systems, highlighting various data types such as unsigned char, signed char, and sfr. It explains methods for creating time delays, including using for loops and timers, while noting factors affecting delay accuracy. Additionally, it covers data conversion programs for converting between ASCII and packed BCD formats.

Uploaded by

smritidas.0505
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Data types and Time Delay in 8051 C

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.

Data Types in Embedded C


Data Types in C Programming Language help us declaring variables in the program. The data
types in Embedded C are-

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:

Write an 8051 C program to toggle all bits of P1 continuously

// toggle P1 forever
#include <reg51.h>
Void main(void)

Program 5:

Write an 8051 C program to toggle bit D0 of the port 1 50000 times

Widely used data types in 8051 C


Time delay
There are two methods to create a time delay 8051 C

a) Using a simple for loop


b) Using the 8051 timers

Time delay using for loop:

In creating a time delay using a for loop, there are three factors that can affect the accuracy of the delay.

1. The 8051 design.


2. The crystal frequency
3. Compiler choice.

1. The 8051 design:

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.

2. The crystal frequency

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:

// Toggle PI forever with some delay in between “on” and “off”,

#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

ASCII Code for Digits 0 – 9

Packed BCD to ASCII conversion


The RTC (Real Time Clock) provides the time of day (hour, minute, second) and the date (year, month,
day) continuously, regardless of whether the power is on or off. However, this data is provided in packed
BCD. To convert packed BCD to ASCII, it must first be converted to unpacked BCD. Then the unpacked
BCD is tagged with 011 0000 (30H). The following demonstrates converting from packed BCD to ASCII.

ASCII to packed BCD conversion


To convert ASCII to packed BCD, it is first converted to unpacked BCD (to get rid of the 3), and then
combined to make packed BCD.
For example, 4 and 7 on the keyboard give 34H and 37H, respectively. The goal is to produce 47H or
“0100 0111″, which is packed BCD.

After this conversion, the packed BCD numbers are processed and the result will be in packed BCD
format

Program 1:
Program 2:

You might also like