You are on page 1of 5

Experiment No.

08

AIM: To write the C program for 8051 microcontroller

(a) to send values 00-FF to Port 1 (P1)

(b) to toggle bits of Port 1 (P1) continuously with some time delay.

OUTCOME: Students will learn how to use Keil compiler for writing
program and its execution.

Software Used: Kiel μVision 4 (compiler).

Theory:

Keil is a German software which was the first C compiler designed from
the ground-up specifically for the 8051 microcontrollers. Using Keil
software it is easy to perform the assembly as well as C programs.

The 8051 microcontroller is an 8-bit microcontroller widely used in embedded


systems. It consists of a CPU, RAM, ROM, I/O ports, timers/counters, and
other peripherals. Ports P1 and P2 are two of its general-purpose I/O ports.

Program (a):

The two header files are used:

1. reg51.h: This header file is specific to 8051 microcontrollers and contains


definitions for various registers and special function registers (SFRs) used in
8051 programming.

2. stdio.h: This header file is commonly used for input/output operations.


However, in embedded systems programming (like 8051), the standard I/O
functions (such as printf and scanf() may not be available or may need specific
configuration.
The main function which is used in the program is the entry point for the program
execution. Also in the program we initializes a array by providing a certain value to
it.There is also a simple delay function . It is a busy-wait loop that introduces a delay
based on the value of the count parameter. The actual delay depends on the clock
frequency and the number of iterations in the loop . Here we assigns the value of the
current element of mynum to the P1 port. The specifics of how this affects the hardware
depend on the microcontroller's configuration and the connected peripherals.
PROGRAM (a):

#include <reg51.h> #Include header file

#include<stdio.h> #Declares the


delay function void main(void)
{

unsigned char mynum [ ] = “012345ABCD”; # Initialize


array mynum unsigned char z; # declare
char z
for (z=0; z<=9; z++)

P1=mynum[z]; # assign mynum to P1

FLOWCHART (a):
OUTPUT (a):

Program (b):

Toggling refers to the process of changing the state of a digital signal or a pin
from high to low or vice versa. The 8051 microcontroller has special function
registers (SFRs) that are used to control the I/O ports, and manipulating these
registers allows you to toggle pins. The reg51.h header file is specific to 8051
microcontrollers and contains definitions for various registers and special
function registers (SFRs) used in 8051 programming. There is also an infinite
loop. The absence of loop initialization and condition creates an infinite loop,
which means the code inside it will run continuously. This is a simple delay
loop. It increments the variable x until it reaches the value 40000. The loop acts
as a delay mechanism, and the actual delay introduced depends on the
execution speed of the microcontroller and the number of iterations in the
loop.After configuring P1 with 0xAA, the program enters into another infinite
loop, continuing to execute the code inside indefinitely. The overall behavior of
the program is to continuously alternate the values 0x55 and 0xAA on the P1
port with a delay loop in between. This kind of code is often used to generate a
simple pattern on an output port for testing or demonstration purposes. The
actual timing and visual effects depend on the specific hardware and clock
frequency of the microcontroller.
PROGRAM (b) :

#include <reg51.h> #Include header

file void main(void)

unsigned int x; # declare int x


for(; ;)
{
P1=0x55; #Set port1 55
hex for(x=0; x<40000; x++);
P1=0xAA; ##Set port1 AA hex
for(x=0; x<40000; x++)
}
}

FLOWCHART (B):
OUTPUT (B):

CONCLUSION:

Thus, we have performed the C program of 8051 microcontroller using Keil software for

(a) To send values 00-FF to Port1 (P1)


(b) To toggle bits of Port 1 (P1) continuously with some time delay.

You might also like