Professional Documents
Culture Documents
Interfacing Programs
Interfacing Programs
Interfacing Programs
#include <8051.h>
#include <stdio.h>
Void main()
{
Unsigned char a ;
A=0xFF;
While (1)
{
P2=a; send the data to port2
Delay(100);
A=~a
}
}
#include <8051.h>
#include <stdio.h>
Void main()
{
Unsigned char a ;
While (1)
{
For ( a=0;a<0xFF;a++)
{
P2 = a;
Delay(4)
}
For (a=0xFF; a<0;a--)
{
P2 = a;
Delay(4)
}
}
}
#include <8051.h>
#Include <stdio.h>
BIT AD_OE P3.6
BIT AD_SC P3.5
BIT AD_EOC P3.3
BIT AD_ALE P3.4
Bit ad0 P3.0; channel selection bits (address of channel)
Bit ad1 P3.1
Bit ad2 P3.2
Bit ad3 P1.0
Bit ad4 P1.1
Void main ()
{
P2 = 0XFF;
P3 = 0X08;
AD_ALE = 0;
AD0=0; ADDRESS TO SELECT CHANNEL IN0
AD1=0;
AD2=0;
AD_ALE = 1;
WHILE(1)
{
AD_OE=1;
AD_SC=0;
DELAY(100);
AD_SC=1;
DELAY(100);
AD_SC=0;
}
P2= A;
}
Interfacing LCD
Algo:
1. Initialise LCD
2. Send command code to LCD
3. Send data to LCD
4. Stop
#include <8051.h>
#include <stdio.h>
Bit reg_sel P2.3
Bit read_write P2.4
Bit enable P2.5
Void data_write();
Void cmd_write();
Void main()
{
Temp= 0x38;
Cmd_write();
Temp= 0x0C;
Cmd_write();
Temp= 0x01;
Cmd_write();
Temp= 0x06;
Cmd_write();
Temp= 0x80;
Cmd_write();
P0=0x0f;
Data_write();
}
Void cmd_write()
{
Reg_sel = 0;
Read_write = 0;
Enable = 1;
Enable = 0;
}
Void data_write()
{
Reg_sel = 1;
Read_write = 0;
Enable = 1;
Enable = 0;
}
Stepper motor
#include <8051.h>
#include <stdio.h>
# define phasea 0x03; data for stepper motor excitation
# define phaseb 0x09
# define phasec 0x0C
# define phased 0x06
Void main()
{
While(1)
{
P2 = phasea;
Delay(50);
P2 = phaseb;
Delay(50);
P2 = phasec;
Delay(50);
P2 = phased;
Delay(50);
}
}
Traffic light controller
Algo
1. Allow traffic on the road from north.
2. Allow traffic on the road from east.
3. Allow traffic on the road from south.
4. Allow traffic on the road from west.
5. Goto step 1
#include<reg51.h>
Void main()
{
Long int I;
For(i=0;i<=500;i++)
{
While(1)
{
//north
P1=0xd0;
P3=0x81;
Delay(50);
P1=0x84;
Delay(20);
//east
P1=0x0b;
P3=0x81;
Delay(50);
P1=0x21;
Delay(20);
//south
P1=0x81;
P3=0x0b;
Delay(50);
P1=0x21;
Delay();
//west
P1=0x81;
P3=0xd0;
Delay(50);
P1=0x84;
Delay();
}
}