Data Type in 8051

5th sem electrical
1400223109007
140023109008
140023109009
140023109011
140023109012
140023109013
140023109014
140023109015
GUIDED BY:
Prof. KETAN PATEL
1
The main concern in mocrocontroller programming is program memory space.
Assembly languge prpgramming produced a hex file that is much smaller than that poduced y
pprogramming Iin c
But pograming in c has fllowing advantages over assembly.
1.programming in C less tedious and less time consuming as compared to
assembly.
2. it is easier to modify and update.
3 it is ortableto other mirocontrollers with little modification.
4.C also provide some library funcaton.
Keil vision has a Header file microcontroller. This header file contains variable
delaraon,macro definition for special function registers(SFRs).The header file
for 8051 is reg 51h.thos file given namses for all the SFRs and hence become
convention fr the programmer to write the program to directly by using these
names.
The main even to the SFRs are as used by us in normal programming such as
P0 for PORT OR port1,TCON for control register etc.
Data Type in 8051
Data type
Size in bits
Range of usage
Unsigned char
8
0 to 255(00H to FFH)
(signed char)
8
-128 to +127
Unsigned into
16
0 to 65535(0000H to
FFFFH)
(signed)int
16
-32768 to +32767
S bit
1
Bit addressable location of
SFRs
Bit
1
Bit addressable location of
internal RAM
SFR
8
SFR location of 8051 i.e.
80H to FFH(only used
location)
Program1: Write an embedded C program for 8051 to count 00H to
FFHon port P2
#include<reg 51.h>
void main ()
{
unsigned char a;
for (a=0; a<=255;a++)
{
P2=a; //assign variable a to P2 pins
}
}
Program2: write on 8051 C program to send hex values for ASCII characteres of
1 2 3 4 5 A B C X Y and Z to port P1.
Program:
#include<reg51.h>
void main (void)
{
unsigned char mydisp[]="012345ABCXYZ";
unsigned char i;
For (i=0;i<12;i++)
P1=my disp[i];
}
Program3: Write 8051 C program to toggle MSB bit of port1.
PROGRAM:
#include<reg51.h>
sbit mybit=P1^7;
//P1^7 represents MSB bit of port 1
void main(void)
{
unsigned into i;
for (i=0;i<=10000;i++)
{
mybit=0;
mybit=1;
}
}
Control statments and loops in Embedded C
• Control loops
The control loops are used for specify the manner in which the computation in a program are to
below. the Different type programming are:
1. IF
2. IF ELESE
3. FOR-LOOP
4. WHILE
5. SWITCH
1.
•
IF statement
IF the condition is true the statement following the if statement is executed otherwise,it
skipped.
It has format:
If condition
Statement;
If(mybit==1)
P1=0xFFH //if mybit=1 then data on p1=FFH
2.
•

3.
if…else statement
The if..Else statement is a conditional statement. IF the condition is true the statement
following the if statement executed, otherwise the statement following else is executed.
Statement;
if(mybit==1)
p1=oxFF;
else
p1=0x00
//if mybit=1 then the data on p1=FFH otherwise p1=00h.
for-loop statement
The for loop statement is conditional statement.
its format is :
for (expression1;expression2;expression3)
 Statement;
where ,expression1 and expression3 are function calls and expression 2 is a relative
expression.
e.g. the following for loop turn on LED 100 times:
(i=0;i<100;i++)
{
LED=1;
}
4.
•

While statement
While statement is true ,the while loop repeats the a statement or block of code.
Program
#include<reeg51.h>
sbit mybit=p1^4;
unsigned char x;
Void main()
{
while(1)
{
mybit=0;
for(x=0;x<=5000;x++)
mybit=1;
for(x=0;x<=5000;x++)
}
}
Programming for time delay
There are two ways to create a time delay in 8051 C:
1.Using a simple for loop
2.Using the 8051 timers
In either case, when we write a time delay we must use the oscilloscope to measure the duration of our time
delay. Next, we use the for loop to create time delays. Discussion of the use of the 8051 timer to create time
delays is postponed until Chapter 9.
In creating a time delay using a for loop, we must be mindful of three factors that can affect the accuracy of
the delay.
1.The 8051 design. Since the original 8051 was designed in 1980, both the fields
of 1C technology and microprocessor architectural design have seen great
advancements. As we saw in Chapter 3, 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. For example, the DS5000 uses 4 clock peri
ods per machine cycle, while the DS89C420 uses only one clock per machine
cycle.
2.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. The third factor that affects the time delay is the compiler
used to compile the C program. When we program in Assembly language, we
can control the exact instructions and their sequences used in the delay sub
routine. In the case of C programs, it is the C compiler that converts the C
statements and functions to Assembly language instructions
program5.: write an embedded program for 8051 to toggle the bits of p1 continuously
using software delay.
program:
#include<reg51.h>
void main()
{
into;
while (1)
{
P1=0x00;
for(x=0;x<=5000;x++)
P1=0xFF;
for(x=0;x<=5000;x++)
I/O Programming in 8051C
• bytes Size I/O
The port P0-P3 are byte accessible.we use labels P0-P3 as defined in 8051/5c
Programe6: write an 8051 c program to get a byte of data p1 wait for ½ second
and then send it to P2.use crystal frequency of 11.059mHz.
Program:
#include <reeg51.h.
void main (void)
{
P1=0xFF;
unsigned char x;
while (1)
{
x=P1;
delay(500);
}
}
void delay(unsigned into time)
{
unsigned into i;
for(i=0;<itime;i++)
for(j=0;j<1275;j++)
}
header file.
Bit Addressable I/O Programming
• the port p0-P3 are bit addressable.we can access a single bit without disturbing
the other port bits.
•A way of doing it is using Pi^j where I is port 0,1,2or3 and j is the bit 0-7 of that port.
Program8: write an embedded C program for 8051 to continuously toggle port 1 pin4
without disturbing other pins of port 1 using software delay.
program:
#include<reg51.h>
sbit mybit=P1^4;
unsigned char x;
void main ()
{
while(1)
{
mybit=0;
for (x=0;x,=5000;x++)
mybit=1
for(x=0;x<=5000;x++)
}
}
Using bit type data for bit Addressable RAM.
we can use 8bit data type with only bit addressable SFR registers. To access
addressable section of data RAM space 20-2FH,.
Program 10:Write an 8051 C program to get the status of port 2.7 pin and send it to
port 2.0 pin continuously.
Program:
#include<reg51.h>
sbit in =P2^7;
sbit out=P2^0;
void main ()
{
while(1)
{
out in;
}
}
Logic operations in 8051C
A
B
AND
A&B
OR
A/B
EX-OR
A^B
INVERTER
Y=-B
0
O
0
0
0
1
0
1
0
1
1
O
1
0
0
1
1
1
1
1
1
0
PROGRAME10:Write an 8051 C program to toggle all pins of port 0,1,2,and3 continuously with a software
delay. Use Ex-or Operator.
Program:
Sfrp0=0x80;
sfrP1=0x90;
sfrP2=oxA0;
sfrP3=0xB0;
Unsigned char I;
Void main ()
{
while(1)
{
P0=0x00;
P1=0x00;
P2=0x00;
P3=0x00;
for9i=0;i<100;i++)
P0=P0^0xFF;
P1=P1^0xFF;
P2=P2^0xFF;
P3=P3^0xFF;
}
}
DATA Serializations using 8051 C
•There are two method to implement this
1.Serial port
2. serialization: In serialization we use shift instruction
and transfer the bit one bit.
programe12:write a C program to send out the value 56h serially at a time via P3.0.the LSB should go
out first.
Program:
#include<reg51.h>
Ssbit out=P3^0;
Sbit serial=ACC^0;
Void main ()
{
unsigned char dat=0x56;
signed char I;
for (i=0;<8;i++)
{
ACC=dat;
out=serial;
ACC=ACC>>1;
}
while(1)
}
Program 13:Write C program to bring in a byte of data serially one bit at a time via
P3.0.this LSB should come in first.
Program
#include<reg51.h>
Sbit serial=ACC^7;
Sbit in=P3^0;
Void main ()
{
unsigned char dat 0,I;
for 9i=0;i,<7;i++)
{
ACC=dat;
serial=in;
dat=ACC;
dat=dat>>1;
}
}
Serial Port Programming in C
•The SFR register 8051 are directly accessible in 8051 C by adding reg 51.h file.
•To use second serial port we have to decalre the byte address of new SFR register.
Programe14:write an 8051 C program to transfer WELCOME serially at 9600 baud rate do this
continuously.
Program:
#include<reg51.h>
Unsigned char a[]=“welcome”
Void main()
{
TMOD=0x20;
TH1=0xFD;
SCON=)x50;
TR1=1;
while(1)
{
for(i=0,i<=15;i++)
{
SBUF=a[i];
while(T1)
T1=0
}
}
}
Program15: program the 8051 in c to receive the data serially and put them in P1.set
the baud rat at 4800,8bit data and 1stop bit.
Program:
#include<reg51.h>
Void main (void0
{
TMOD=0x20;
TH1=0xFA;
SCON=0x50;
TRI=1;
WHILE(1)
{
while(RI!=1);
P1=SSBUF
RI=0;
}
}
7/29/2017
APPLICATIONS OF OP AMP
21