Unit IV Interfacing I-O Devices
Unit IV Interfacing I-O Devices
Unit IV
Interfacing I/O Devices
1.
/*
Q. Draw the diagram to interface LED to pin P0.7of 89C51. Write 'C' program to blink the
LED
Sol:
Blink Led on Pin P0.7
*/
#include<reg51.h>
sbit led=P0^7;
void delay(unsigned int);
void main() {
while(1) {
led=0;
delay(25);
led=1;
delay(25); }
}
void delay(unsigned int t) {
unsigned int i, j;
for(i=0; i<t; i++)
for(j=0; j<1275; j++) {}
}
2.
/*
Q.Draw the interfacing diagram of key and LED to89C51 mic on pins P1.0 and P2.0
respectively.Write a C language program to read the statusof key and display it on LED.
Solution:
interface--- key to P1.0
led to P2.0
read status of key and display on led... Interfacing
rfacing Diagram
Diagram-
*/
Embedded C Program-
#include<reg51.h>
sbit key=P1^0;
sbit led=P2^0;
void main()
{
P1=0xFF; //p1 port intialized as input port.
P2=0x00; //p2 port intialized as output port.
key=0;
led=0;
while(1)
{
if(key==1) //checking status of key
{
led=1; //if key is ON turn ON led
FTCCOERP Page1|4
Interfacing I/O Devices | U-4
}
else
{
Embedded C Program-
#include<reg51.h>
unsigned char cc[ 10]={0x7E
0x7E, 0x30, 0x6D, 0x79, 0x33, 0x5B, 0x5F, 0x70
0x70, 0x7F, 0x7B};
unsigned char ca[ 10]={0x81
0x81, 0xCF, 0x92, 0x86, 0xCC, 0xA4, 0xA0, 0x8F
0x8F, 0x80, 0x84};
unsigned char x;
void delay(int);
void main() Interfacing Diagram
Diagram-
{
P2=0x00; //CC 7 segment display connected
P3=0x00; //CA 7 segment display connected
while(1)
{
P3=0xFF;
for(x=0; x<10; x++)
{
P2=cc[ x];
delay(100);
}
P2=0x00;
for(x=0; x<10; x++)
{
P3=ca[ x];
delay(100);
}
}
}
void delay(int t)
{
unsigned int i, j;
for(i=0; i<t; i++)
for(j=0; j<1275; j++)
{}
}
4.
/*
Q. Draw the interfacing of relay with 89C51 microcontroller.
Write C language program to make relay ON/ OFF
after certain delay
Solution:
relay (rel)===>P2.3
*/
FTCCOERP Page2|4
Interfacing I/O Devices | U-4
#include<reg51.h>
sbit rel=P2^3;
void delay(int);
void main()
{
P2=0x00; //P2 port is initialized as output port
while(1)
{
rel=1;
delay(100);
rel=0;
delay(100);
}
}
void delay(int t)
{
unsigned int i, j;
for(i=0; i<t; i++)
for(j=0; j<1275; j++)
{}
}
5.
/*
Q. Draw the circuit diagram to interface LCD with 89C51.
Write 'C' program to send letters 'FTC COER Sangola'
to the LCD display.
Solution:
P2.0==>RS(0=command reg; 1=data reg)
P2.1==>RW(0=write operation; 1=read operation)
P2.2==>EN(low->high->low)
P3==>data pins D0-D7
0x01=clear display
0x0f=display on cursor blink
0x80=force cursor to begin on 1st line
0xc0=force cursor to begin on 2nd line Interfacing Diagram
Diagram-
0x38=2lines and 5x7 matrix enable
*/
Embedded C Program-
#include <REGX51.H>
sbit rs=P2^0;
sbit rw=P2^1;
sbit en=P2^2;
sfr lcdata=0xB0;
char msg[ 16]="FTC
"FTC COER Sangola";
Sangola"
void delay(int);
void lcommand(unsigned
unsigned char);
char
void ldata(unsigned char);
void main() {
unsigned char x;
lcommand(0x38);
lcommand(0x01);
lcommand(0x0e);
while(1)
{
lcommand(0x01);
FTCCOERP Page3|4
Interfacing I/O Devices | U-4
FTCCOERP Page4|4