EXP.
NO: 04 DATE:
Basic and arithmetic Programs Using Embedded C
Aim:
To write basic and arithmetic programs using embedded C.
SOFTWARE REQUIRED:
1. Keil uVision IDE
Theory:
Why Program 8051 in C?
Compilers produce hex file that is downloaded to ROM of microcontroller.
The size of hex fil e generated by the compiler is the main concern, for two
reasons:
Microcontrollers have limited on-chip ROM
Code space for 8051 is limited to 64K bytes
The language produces a hex file that is much smaller than C. But when both are compared,
programming in assembly is tedious and time consuming while C programming is less time
consuming, but has larger hex file size.
The reasons for writing programs in C instead of assembly are:
It is easier and less time consuming to write in C than Assembly
C is easier to modify and update
You can use code available in function libraries
C code is portable to other microcontroller with little or no modification
The different data types are:
STEPS TO CREATE AND COMPILE Keil µVision-5 PROJECT:
1. PC Desktop Keil Uvision icon
2. File New
3. Type the program code
4. File save as filename.c
5. Project new uvision project
6. In create new project window filename without extension save
7. Select device for target window AT89c51 click ok
8. It opens a window as Copy startup.a51 to project folder and add file to project
click No.
9. In project window click Target
10. Right click source group and Add existing files to group ‘source group 1’
11. In add files to group window Type your file name.c add close
12. In project window, click + symbol near source group, can get your filename.c file
and Double click on the file name.
13. Click on Project translate
14. Click on Project build target
15. Click on Project rebuild all target files
16. Debug click start/stop debug session
17. Click step icon or press F11 to run the program step by step or press F5 and run
the program at once.
18. Peripherals I/O Ports select the appropriate port given in the program.
[Link] click on target option in the project window and select options for
target ‘ Target 1’
[Link] options for target ‘ Target 1’ window,
click on target and type XTAL frequency as 11.0592 MHz and tick the
checkbox use on chip ROM.
Click on output and tick create HEX file.
21. Project rebuild, can see where the hex stored in build output window.
STEPS TO CREATE PROTEUS SIMULATION:
1. PC Desktop Proteus 8 professional icon
2. Double click on schematic capture icon
3. Select component mode click p under keywords search box type the
required component and select it and click close.
4. Click on the schematic, selected components will be placed on the
schematic.
5. Repeat step 3 and select all the components required and form a complete
circuit.
6. Double click on the microcontroller select the program file [Link] and
click ok.
7. Start the simulation by clicking play icon and stop the same by clicking stop
icon.
Procedure:
1. Launch the Simulator
2. Write the Assembly Program and save it with .c extension
3. Click the build target option from project menu to assemble the program
4. Click the Debug menu to Start/Stop Debug Session
5. Press STEP button to single step the Program
i. Write a 8051 C program to toggle all the bits of P0 and P2 continuously
with a 250 ms delay.
#include <reg51.h>
void msdelay(unsigned int);
void main(void)
{
while(1) // infinite loop
{
P2=0x55;
msdelay(250);
P2=0xaa;
msdelay(250);
}
}
void msDelay(unsigned int itime)
{
unsigned int i,j;
for(i=0;i<itime;i++)
for(j=0;j<1275;j++);
ii. A door sensor is connected to the P1.1 pin, and a buzzer is connected to
P1.7. Write an 8051 C program to monitor the door sensor, and when it
opens, sound the buzzer.
#include<reg51.h>
sbit door = P1^1;
sbit buzzer = P1^7;
void delay(unsigned int time);
void main(void)
{
while(1)
{
if(door==1)
{
buzzer=1;
delay(5);
buzzer=0;
delay(5);
}
else
{
buzzer=0;
}
}
}
void delay(unsigned int time)
{
unsigned int i,j;
for(i=0;i<time;i++)
{
{
for(j=0;j<1275;j++);
}
}
}
iii. Write and Run a program to show the following operations result on
simulator.
a. 0x35 & 0x0F = 0x05
b. 0x04 | 0x068 = 0x6c
c. 0x54 ^ 0x78=0x2c
d. ~0x55 = 0xAA
e. Shift the value 0x9A to right 3 times
f. Shift the value 0x6 to left 3 times
#include<reg51.h>
void main(void)
{
P0 =0x35 & 0x0F;
P1 =0x04 | 0x068;
P2 = 0x54^0x78;
//P0=~0x55;
//P1=0x9a>>3;
//P2=0x6<<4;
}
iv. Write a program to perform all arithmetic operations and show the
results in ports.
#include<reg51.h>
void delay(unsigned int time);
void main(void)
{
P0=9+1;
delay(5);
P1=10-1;
delay(5);
P2=7*2;
delay(5);
P3=20/2;
void delay(unsigned int time)
{
unsigned int i,j;
for(i=0;i<time;i++)
{
{
for(j=0;j<1275;j++);
}
}
}
Result:
Thus the embedded C programs to perform basic Programs are verified.