You are on page 1of 24

INDEX

SR
NO

TITLE

Interfacing of LED, RELAY, Pushbutton

Sending and receive data serially to and from PC

Using a Watchdog Timer

Design a 8 bit Binary counter

DC Motor Control using PWM module

Interfacing of Temp Sensor

Interfacing seven segment Display

Scrolling text message on LED dot Matrix

SIGN

Practical no: 1 Interfacing of LED, RELAY, Pushbutton


Aim
Write a Program to blink an LED using 8051
Theory
We now want to flash a LED. It works by turning ON a LED & then turning
it OFF & then looping back to START. Figure shows how to interface the
LED to microcontroller. The Anode is connected through a resistor to Vcc
& the Cathode is connected to the Microcontroller pin. So when the Port
Pin is HIGH the LED is OFF & when the Port Pin is LOW the LED is
turned ON.

1.
2.
3.
4.
5.
6.

Start.
Turn ON LED.
Wait for some time (delay).
Turn OFF LED.
Wait for some time (delay).
Go To 2.

LED Interfacing Program


#include <reg51.h>
MSDelayNew(unsigned int v);
void main()
{
while(1)
{
P1 = 0x00;

// Start of main() function


// Infinite loop. The program will run forever

MSDelayNew(50);
P1 = 0X01;
MSDelayNew(50);
}
}
MSDelayNew(unsigned int v)
{
unsigned int x, y;
for (x=0;x<v;x++)

for(y=0;y<120;y++) }

Practical no: 2
Sending and receive data serially to and from PC
Aim : Write a program for Serial data communication interface
Theory

The Edkits51 kit communicates with PC via the serial port using a crossed serial cable (2 3, 3 - 2, 5 50. Timer 1 is used in mode 2 (auto-reload) to generate the desired baud rate. The
mode is set using TMOD register. And the baud rate is set by loading appropriate value in TH
register. The serial data communication parameters such as baud rate, no. of data bits, parity
etc. are set using SCON register. SBUF register is used for transmitting or receiving a byte of
data. The byte of data to be transmitted in placed in SBUF. Similarly, SBUF holds the byte of
data when it is received. The bit nos 0 and 1 in SCON register are Receive and Transmit flags
RI and TI. These flags are monitored in program using a loop to check whether the byte is
transmitted or received.
IC 232 is a RS-232 line driver/receiver which converts the voltage levels from TTL/CMOS
to RS-232 compatible level and vice versa. The voltage level for RS-232 are +3V to +15V
for Hi and -3V to -15V for Lo. Also the TxD and RxD lines use negative logic. i.e. the
voltage at the connectors is +3V to +15V when 0 is transmitted (or received) and -3V to -15V
when 1 is transmitted (or received). IC 232 gates also invert these levels.
Write a program to transfer the message Welcome serially at 9600 baud, 8-bit data, 1 stop
bit. Do; this continuously. Program is written for crystal 11.0592 MHz.

ORG 0000h
MOV TMOD,#20h ;timer 1, mode 2
MOV TH1,#-3 ;9600 baud
MOV SCON,#50h ;8-bit, 1 stop bit, REN enabled
SETB TR1
;start timer 1
AGAIN: MOV A,#'W'
;transfer 'W'
ACALL TRANS
MOV A,#'E'
;transfer 'E'
ACALL TRANS
MOV A,#'L'
;transfer 'L'
ACALL TRANS
MOV A,#'C'
;transfer 'C'
ACALL TRANS
MOV A,#'O'
;transfer 'O'
ACALL TRANS
MOV A,#'M'
;transfer 'M'
ACALL TRANS
MOV A,#'E'
;transfer 'E'
ACALL TRANS
MOV A,#' ' ;transfer ' '
ACALL TRANS
SJMP AGAIN
;keep doing it ;--Serial data transfer subroutine
TRANS: MOV SBUF,A ;load SBUF
HERE: JNB TI,HERE ;wait for last bit to transfer

CLR
RET
END

TI

Practical no: 3
Using a Watchdog Timer
Aim : Write a program for using watchdog timmer

PROGRAM OF WATCHDOG TIMMER


#include <reg51.h>

void init_WDT();
void reset_WDT();
void MSDelayNew(unsigned int v);
unsigned int FourBitCtr;
void main (void)
{
P1 = 0x01;

MSDelayNew(150);
P1 = 0x0;
MSDelayNew(150);

P1 = 0x01;
MSDelayNew(150);
P1 = 0x0;
MSDelayNew(150);

P1 = 0x01;
MSDelayNew(150);
P1 = 0x0;
MSDelayNew(150);

MSDelayNew(1500);

init_WDT();
while(1)
{
P1 = 0x05;
MSDelayNew(250);
P1 = 0x0a;
MSDelayNew(250);
//
}
}

reset_WDT(); //if we do not reset the WDT it will cause a reset

void init_WDT()
{
WDTC=0x08;
WDTD=0xDD; // feeds the dog instruction
WDTC|=0x02;
WDTC|=0x01;
}

void reset_WDT()
{
WDTD=0xDD; // feeds the dog instruction
WDTC|=0x02;
}

void MSDelayNew(unsigned int v)


{
unsigned int x, y;

for (x=0;x<v;x++)
for(y=0;y<120;y++);
}

1s

Practical no: 4
Design a 8 bit Binary counter
Aim : Write a program for using 8 bit binary counter

PROGRAM OF 8 BIT BINARY COUNTER


#include <reg51.h>

void MSDelayNew(unsigned int v);


unsigned int FourBitCtr;
void main()
{

FourBitCtr = 0;

P1 = 0;

// Start of main() function

MSDelayNew(5);
while(1)
{

P1 = FourBitCtr;
MSDelayNew(50);
FourBitCtr++;
if(FourBitCtr >= 16)
{
FourBitCtr = 0;
}
/*P1 = 0x03;
MSDelayNew(150);
P1 = 0x02;
MSDelayNew(150);
P1 = 0x01;
MSDelayNew(150);*/

}
}

void MSDelayNew(unsigned int v)


{
unsigned int x, y;

for (x=0;x<1275;x++)

// Infinite loop. The program will run forever

for(y=0;y<v;y++);
}

Practical no: 5
DC Motor Control using PWM module
Aim:-Implement interfacing of ADC0808 with 8051 microcontroller.
PROGRAM OF DC MOTORS USING PWM MODULE
#include <reg51.h>
unsigned char ONTIME=20;
sbit PWMOP=P1^0;
void main(void)
{
EA = 1;
ET0 = 1;
TMOD = 0x01;//(TMOD & 0xF0) | 0x01; /* Set T/C0 Mode */
TH0=0x00;
TL0=0x00;
TR0=1;
for (;;);
}
void timer0 (void) interrupt 1
{
static int a=0;
a++;
if (a == 3)
a=1;

switch (a)
{
case 1://Ton time, For exa - if Ton = 250us and Toff= 750us,
then it will generate 1.25v appo ath the port pin
TH0 = 0xFF;//FF;//ONTIME; FF18 = 250us,FF32 = 500us,FD4B =
750us,FC66 = 1000us
TL0 = 0x18;//18;//FFff-FED3 = 12c = 300d = 326us
PWMOP = 1;
break;
case 2: //Toff, For exa - if Ton = 750us and Toff= 250us, then it
will generate 3.75v appo ath the port pin
TH0 = 0xFD;//FD;
TL0 = 0x4B;//4B;//OFFTime FF18 = 250us,FF32 = 500us,FD4B
= 750us,FC66 = 1000us
PWMOP = 0;
}
TR0=1;
}

Practical no: 6
Interfacing of Temp Sensor
Aim:-Implement interfacing of TempSensor

Theory
Analog-to-digital Converters accept analog inputs such as light, temperature etc. and
convert them to a digital form. The ADC0808/0809 used here uses successive
approximation as a conversion technique and has 8 input channels from where it can
access 8 analog signals.
The timing Diagram shown below illustrates the conversion sequence as follows:
1. Place analog input on any desired channel.
2. Place address of the selected input channel on address lines ADD A, ADD B & ADD
C.
3. Latch the input address using ALE (Address Latch Enable) signal.
4. Apply START pulse, commanding the ADC to start conversion.
5. Wait for the EOC (End of Conversion) signal to become Low and then High informing
that the conversion from analog to digital is complete.
6. Enable the Output by making OE line High.
7. Read in the digital data from ADCs output lines.

In the circuit presented here, we are taking voltage as an analog input from a pot of 10K
ohms. We are using port P1 for reading in the converted digital data and port P3 for
control lines. We have chosen IN2 as an input channel using statements (CLR ADD_A,
SETB ADD_B and CLR ADD_C). A preset is used to set the V ref+ to a desired value. If
we set the V ref+ to 2.56 V then we get a convenient step size of 10 mV. We vary the
analog input voltage to the ADC by rotating the pot. Since the output of ADC is
connected to port P1, we can see the digital output on bi-colour LEDs connected to P1.
The clock for ADC
is derived by dividing frequency of XTAL2 pin of the crystal by IC 4040, the Binary
Counter.
We are taking only one sample at a time so that observation can be made easily. Hence
after every sample, we change the analog input voltage by rotating the pot, making sure
that the input voltage does not exceed 2.56V; because this is an 8-bit ADC and the
maximum digital output can expect is 255. Use Multimeter to measure this voltage
(There is a orange coloured socket provided on the ADC interface for this purpose). Then
press RESET buttons so that program re-executes. The converted digital output can then
be observed on bi-colour LEDs of port P1. To convert the digital value observed on the
LEDs, use Windows calculator in Scientific mode.
In calculator View Scientific. Select Bin, then feed 1s and 0s displayed on P1 starting

from MSB. Then click Dec to convert the result to decimal. Compare the input voltage
with the digital output voltage. They should be almost equal. For example, if the input
analog voltage is 1.9V (=1900 mV), then the digital output will be approximately 190
(=1900/10). For next sample, vary the pot and press RESET button.

INTERFACING ADCs
INTERFACING PROGRAM FOR ANALOG-TO-DIGITAL CONVERTERS

#include <AT89x52.h>
#include <intrins.h>
#define IN_PORT P1

//

#define ON

#define

OFF

sbit ADDR_A = P3^3;

//

sbit ADDR_B = P3^1;

//

sbit ADDR_C = P3^0;

//

sbit ALE

= P3^2;

sbit OE

= P3^5;

sbit START

= P3^4;

sbit EOC

= P3^7;

unsigned char nop;


void main( void )

IN_PORT = 0xff;

// make as input

EOC = 1;
ALE = 0;
START = 0;
OE = 0;
//
while(1)

// infinite loop

{
ADDR_C = 0;
ADDR_B = 1;
ADDR_A = 1;
nop++; ALE = 1;
nop++; START = 1;
nop++; ALE = 0;
START = 0;
while(EOC)

{_nop_();}

while(!EOC)

{_nop_();}

OE = 1;
} //end of while
}

// end of main

Practical no: 7
Interfacing seven segment Display
Aim:-Implement interfacing of seven segment Display
PROGRAM OF SEVEN SEGMENT DISPLAY
//Seven Segment DisplayCounter
#include<reg51f.h>
#include<stdio.h>
unsigned int dly1,dly2;

unsigned char ds1,ds2,ds3,ds4;


//look up table
unsigned int
luk_up[10]={0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6};
unsigned char cnt;
sbit
sbit
sbit
sbit

sd1=P3^2;
sd2=P3^3;
sd3=P3^4;
sd4=P3^5;

void delay(unsigned int);


void inc_ds();
void display();
void main()
{
ds1=0;
ds2=0;
ds3=0;
ds4=0;
while(1)
{
cnt=28;
while(cnt!=0)
{
cnt--;
display();
}
inc_ds();
}
}
void inc_ds()
{
ds1++;
if(ds1==0x0A)
{
ds1=0;
ds2++;
if(ds2==0x0A)
{
ds2=0;

ds3++;
if(ds3==0x0A)
{
ds3=0;
ds4 ++;
if(ds4==0x0A)
{
ds1=0;
ds2=0;
ds3=0;
ds4=0;
}
}
}
}
}
void display()
{
sd1=0;
sd2=1;
sd3=1;
sd4=1;
P0=luk_up[ds1];
delay(3);
sd1=1;
sd2=0;
sd3=1;
sd4=1;
P0=luk_up[ds2];
delay(3);
sd1=1;
sd2=1;
sd3=0;
sd4=1;
P0=luk_up[ds3];
delay(3);
sd1=1;

sd2=1;
sd3=1;
sd4=0;
P0=luk_up[ds4];
delay(3);
}
void delay(unsigned int d)
{
for(dly1=0;dly1<d;dly1++)
{
for(dly2=0;dly2<120;dly2++);
}
}

Practical no: 8
Scrolling text message on LED dot Matrix
Aim:-Implement interfacing of LED dot matrix
An LED dot matrix display consists of a matrix of LEDs arranged in a rectangular
configuration. The desired character or graphics can be displayed by switching
ON /OFF a desired configuration of LEDs. Common display configurations available
are 75, 88, 715, etc. LED dot matrix can be used in simple display applications
where the resolution is not a big concern. The figure below shows the arrangement
of LEDs in a typical 75 dot matrix display.Any individual LED or a group of LEDs in
the matrix can be activated by switching the required number of rows and columns.

Working
Light-emitting diodes (LEDs) provide a cheap and convenient way to display
information electronically. LEDs are tiny light sources that are illuminated solely by
the movement of electrons in semiconducting materials. They emit light when
forward-biased, fit easily into an electrical circuit, and are durable. LEDs are often
arranged in patterns to display information. The seven-segment configuration of an
LED arranged in the form of the digit 8 can be restrictive in that it does not
adequately allow the display of some alphanumeric characters. By contrast, the
versatility of a dot-matrix arrangement allows an LED unit to display complicated
shapes.

Stable Character This diagram is for practice and programming, wants you
have it working you can put transistors and resistors on. Here we put the letter A on

the display as you can see in video, using a breadboard.


Lets understand how it
works. First connect row and column to the microcontroller PORT P3 and PORT P2
respectively. Then by using programing sends data to first column and at same time
sends data to row and then after some milli-seconds change the column and send
data to row again. And do this till last column and then repeat it again and again
around speed of greater then 20 frame per second.

Moving Character - How to scroll a character across the display? The trick is to
build one character on the display by scanning the columns very fast, and let say
each 20 times (20 frames) scroll it one position to the left, this will give the effect of
a walking text across the dot-matrix display. So first build one frame, repeat this 20
times, and after that, read the data one address later, if you do this 5 times (5
columns) the character scroll from right to left from the display. (The refresh goes so
fast that your brain can't keep up, and what you see is the A scrolling over the
display

Moving String
And in third part of the code, here is displaying Dot Matrix Display By NSK .
The working of displaying string of character on Dot matrix display is also same as
last paragraph.

PROGRAM OF MATRIX SCROLLING DISPLAY


//Scrolling Matrix LED
#include<reg51f.h>

sbit
sbit
sbit
sbit
sbit

cl5=P3^2;
cl4=P3^3;
cl3=P3^4;
cl2=P3^5;
cl1=P3^6;

unsigned int i;
unsigned char pattern[3][5]={
0x7e,0x09,0x09,0x09,0x7e,
0x7f,0x49,0x49,0x49,0x36,
0x7f,0x41,0x49,0x49,0x78};//0x7f,0x20,0x10,0x20,0x7f};//0x4f,0x49,0x49,0
x49,0x79};//0x7f,0x41,0x41,0x41,0x3e};//0x3e,0x41,0x41,0x41,0x22};
void scroll(unsigned char rw);
void delay(unsigned int d)
{
unsigned int d1,d2;
for(d1=0;d1<=d;d1++)
for(d2=0;d2<=120;d2++);
}
void main()
{
P1=0x00;
while(1)
{
P1=0x00;
while(1)
{
scroll(0);
delay(100);
scroll(1);
delay(100);
scroll(2);
delay(100);
}
}
}
void scroll(unsigned char rw)
{

//------1st Column---------//
for(i=0;i<60;i++)
{
cl5=0;
cl1=cl2=cl3=cl4=1;
P1=pattern[rw][0];
delay(1);
}
//------2nd Column---------//
for(i=0;i<30;i++)
{
cl4=0;
cl1=cl2=cl3=cl5=1;
P1=pattern[rw][0];
delay(1);
cl5=0;
cl1=cl2=cl3=cl4=1;
P1=pattern[rw][1];
delay(1);
}
//------3rd Column---------//
for(i=0;i<20;i++)
{
cl3=0;
cl1=cl2=cl4=cl5=1;
P1=pattern[rw][0];
delay(1);
cl4=0;
cl1=cl2=cl3=cl5=1;
P1=pattern[rw][1];
delay(1);
cl5=0;
cl1=cl2=cl3=cl4=1;
P1=pattern[rw][2];
delay(1);
}
//------4th Column---------//
for(i=0;i<15;i++)
{

cl2=0;
cl1=cl3=cl4=cl5=1;
P1=pattern[rw][0];
delay(1);
cl3=0;
cl1=cl2=cl4=cl5=1;
P1=pattern[rw][1];
delay(1);
cl4=0;
cl1=cl2=cl3=cl5=1;
P1=pattern[rw][2];
delay(1);
cl5=0;
cl1=cl2=cl3=cl4=1;
P1=pattern[rw][3];
delay(1);
}
//------5th Column---------//
for(i=0;i<100;i++)
{
cl1=0;
cl2=cl3=cl4=cl5=1;
P1=pattern[rw][0];
delay(1);
cl2=0;
cl1=cl3=cl4=cl5=1;
P1=pattern[rw][1];
delay(1);
cl3=0;
cl1=cl2=cl4=cl5=1;
P1=pattern[rw][2];
delay(1);
cl4=0;
cl1=cl2=cl3=cl5=1;
P1=pattern[rw][3];

delay(1);
cl5=0;
cl1=cl2=cl3=cl4=1;
P1=pattern[rw][4];
delay(1);
}
}