You are on page 1of 21

PRACTICAL 1A

#include<reg51.h>

sbit led=P1^0;

void delay(long a);

int main(void)

P1=0x00;

P0=0x00;

P2=0x00;

P3=0x00;

while(1)

led=0;

delay(10000);

led=1;

delay(10000);

void delay(long a)

long i;

for(i=0;i<a;i++);

PRACTICAL 2A

#include<reg51.h>

sbit led=P2^0; // led at PORT 2 pin 0

void Delay(void); // Delay function declaration

void main () // main function

{
led=0; //output PORT

while(1) // infinite loop

led = 1; // LED ON

Delay();

led = 0; // LED OFF

Delay();

void Delay()

TMOD = 0x01; // Timer0 mode1

TH0=0xDC; //initial value for 10ms

TL0=0x00;

TR0 = 1; // timer0 start

while (TF0 == 0); // check overflow condition

TR0 = 0; // Stop Timer

TF0 = 0; // Clear flag

PRACTICAL 2B

MICROCONTROLLER 1 CODE:

#include <reg51.h>

void delay(unsigned int time);

void main()

{
P2 = 0x00;

while(1)

P1 = P3;

delay(1000);

void delay(unsigned int time)

unsigned int i;

for(i=0;i<time;i++);

MICROCONTROLLER 2 CODE:

//#include <reg51.h>

//void delay2(unsigned long time);

//void main()

//{

// P2 = 0x00;

// P3 = 0x00;

// while(1)

// {

// P1 = 0X55;

// delay2(10000);

// P1 = 0xAA;

// delay2(10000);

// }

//}

//void delay2(unsigned long time)


//{

// unsigned long i;

// for(i=0;i<time;i++);

//}

PRACTICAL 3A

#include<reg51.h>

void delay();

void main()

unsigned int i;

while(1)

for(i=0;i<16;i++)

P1=i;

delay();

void delay()

unsigned int i,j,k;

for(i=0;i<8;i++)

for(j=0;j<100;j++)

for(k=0;k<100;k++);

PRACTICAL 3B

PATTERN 1

#include<reg51.h>

sbit led0=P1^0; // led at PORT 1 pin 0

sbit led1=P1^1; // led at PORT 1 pin 1


sbit led2=P1^2; // led at PORT 1 pin 2

sbit led3=P1^3; // led at PORT 1 pin 3

sbit led4=P1^4; // led at PORT 1 pin 4

sbit led5=P1^5; // led at PORT 1 pin 5

sbit led6=P1^6; // led at PORT 1 pin 6

sbit led7=P1^7; // led at PORT 1 pin 7

void Delay2(long value); // Delay function declaration

void main () // main function

P1=0; //output PORT

while(1) // infinite loop

led0 =1; // LED ON

Delay2(1000);

led0 =0; // LED OFF

led1 =1; // LED ON

Delay2(1000);

led1 =0; // LED OFF

led2 =1; // LED ON

Delay2(1000);

led2 =0; // LED OFF

led3 =1; // LED ON

Delay2(1000);

led3 =0; // LED OFF

led4 =1; // LED ON

Delay2(1000);

led4 =0; // LED OFF

led5 =1; // LED ON

Delay2(1000);

led5 =0; // LED OFF

led6 =1; // LED ON


Delay2(1000);

led6 =0; // LED OFF

led7 =1; // LED ON

Delay2(1000);

led7 =0; // LED OFF

void Delay2(long value)

long inc,cnt;

for( inc=0;inc<=value;inc++)

for(cnt=0;cnt<2;cnt++);

PATTERN 2

#include "REG51.h"

#define LED P1 //port defination

long int i;

void main()

while(1) // infinite loop

LED=0xff; // shows 00000001

for(i=0;i<10000;i++);

LED=0x00;

for(i=0;i<10000;i++);

PATTERN 3

#include "REG51.h"

#define LED P1

long int i;
void main()

while(1)

LED=0xAA;

for(i=0;i<4500;i++);

LED=0x55;

for(i=0;i<4500;i++);

PRACTICAL 3C

#include<reg51.h>

sbit LED = P1^0;

void timer0_isr() interrupt 1

TH0 = 0X4B; //ReLoad the timer value

TL0 = 0XFD;

LED =! LED; // Toggle the LED pin

}
void main()

TMOD = 0x01; //Timer0 mode 1

TH0 = 0X4B; //Load the timer value

TL0 = 0XFD;

TR0 = 1; //turn ON Timer zero

ET0 = 1; //Enable TImer0 Interrupt

EA = 1; //Enable Global Interrupt bit

while(1)

// Do nothing

PRACTICAL 4A

#include <reg51.h>

void send(char x);

void main(void)

long loopvar;
// INITIALIZE SERIAL PORT

TMOD = 0x20; // Timer 1 IN MODE 2 -AUTO RELOAD TO GENERATE BAUD RATE

TH1 = 0xFD; // BAUD RATE 9600

SCON = 0x50; // SERIAL MODE 1, 8-DATA BIT 1-START BIT, 1-STOP BIT, REN ENABLED

TR1 = 1;// START TIMER

while(1)

send('B');

for(loopvar=0;loopvar<10000;loopvar++)

{}

send('G');

void send(char x)

SBUF = x; // LOAD DATA TO SERIAL BUFFER REGISTER

while(TI==0); // WAIT UNTIL TRANSMISSION TO COMPLETE

TI=0; // CLEAR TRANSMISSION INTERRUPT FLAG

PRACTICAL 4B

#include<reg51.h>

void delay(unsigned int ms)

{ unsigned int i,j;

for(i=0;i<ms;i++)

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

void main(void)

char number[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};

int i,j;
P2=0x00;

P3=0x00;

while(1)

for(i=0;i<=9;i++)

P2=number[i];

for(j=0;j<=9;j++)

P3=number[j];

delay(50);

PRACTICAL 4C

#include<REG51.h>

void fordelay();

void main()

P2=0x00;

while(1)

P2=0xFF;

fordelay();

P2=0x00;

fordelay();

void fordelay()

{
long i=0;

for(i=0;i<=5000;i++)

{}

PRACTICAL 5A

#include<reg51.h>

void main()

P1=0x00;

while(1)

do

P1 += 0x05; //Gradually increasing the current on the


port pin till it reaches the peak value i.e. 0xFF

while(P1<0xFF);

do

P1 -= 0x05; //Gradually decreasing the current on the port pin


till it reaches the low i.e. 0x00

while(P1>0x00);

PRACTICAL 5B

#include<reg51.h>

void main()

int WaveValue[12]={128,192,238,255,238,192,128,64,17,0,17,64};

int i;

while(1)
{

for(i=0;i<12;i++)

P1=WaveValue[i];

PRACTICAL 6

STEPPER MOTOR BIPOLAR

#include<reg51.h>

#define stepper P1

void delay()

unsigned char i,j,k;

for(i=0;i<6;i++) {

for(j=0;j<255;j++)

for(k=0;k<255;k++);

void main()

while (1) {

stepper = 0x08;

delay();

stepper = 0x02;

delay();

stepper = 0x04;

delay();

stepper = 0x01;

delay();
}

//Clockwise

//void main()

//{

//while (1) {

//stepper = 0x0C;

//delay();

//stepper = 0x06;

//delay();

//stepper = 0x03;

//delay();

//stepper = 0x09;

//delay();

//}

//}

STEPPER MOTOR UNIPOLAR WAVE DRIVE

#include<reg52.h>

#include<stdio.h>

void delay(int);

void main()

do

P2=0x01; //0001

delay(1000);

P2=0x02; //0010

delay(1000);

P2=0x04; //0100
delay(1000);

P2=0x08; //1000

delay(1000);

while(1);

void delay(int k)

int i,j;

for(i=0;i<k;i++)

for(j=0;j<100;j++)

{}

STEPPER MOTOR HALF DRIVE

#include<reg52.h>

#include<stdio.h>

void delay(int);

void main()

do

P2=0x01; //0001

delay(1000);

P2=0x03; //0011

delay(1000);

P2=0x02; //0010
delay(1000);

P2=0x06; //0110

delay(1000);

P2=0x04; //0100

delay(1000);

P2=0x0C; //1100

delay(1000);

P2=0x08; //1000

delay(1000);

P2=0x09; //1001

delay(1000);

} while(1);

void delay(int k)

int i,j;

for(i=0;i<k;i++)

for(j=0;j<100;j++)

{}

STEPPER MOTOR FULL DRIVE

#include<reg52.h>

#include<stdio.h>

void delay(int);

void main()

{
do

P2 = 0x03; //0011

delay(1000);

P2 = 0x06; //0110

delay(1000);

P2 = 0x0C; //1100

delay(1000);

P2 = 0x09; //1001

delay(1000);

while(1);

void delay(int k)

int i,j;

for(i=0;i<k;i++)

for(j=0;j<100;j++)

{}

PRACTICAL 8 TRAFFIC SIGNAL

#include<reg51.h>

//Signal block on west

sbit red_west=P1^2;

sbit yellow_west=P1^1;

sbit green_west=P1^0;

//Signal block on south

sbit red_south=P1^4;
sbit yellow_south=P1^3;

sbit green_south=P2^7;

//Signal block on north

sbit red_north=P1^7;

sbit yellow_north=P1^5;

sbit green_north=P1^6;

//Signal block on east

sbit red_east=P2^0;

sbit yellow_east=P2^1;

sbit green_east=P2^2;

void SignalWait(long time);

void main()

P1=0;

P2=0;

SignalWait(15000);

while(1)

red_west=1;

red_east=1;

red_north=0;

red_south=0;

green_north=1;

green_south=1;

SignalWait(15000);

green_north=0;

green_south=0;

yellow_north=1;

yellow_south=1;

SignalWait(15000);

yellow_north=0;
yellow_south=0;

red_north=1;

red_south=1;

red_west=0;

red_east=0;

green_west=1;

green_east=1;

SignalWait(15000);

green_west=0;

green_east=0;

yellow_west=1;

yellow_east=1;

SignalWait(15000);

yellow_west=0;

yellow_east=0;

void SignalWait(long time)

long inc,cnt;

for(inc=0;inc<=time;inc++)

for(cnt=0;cnt<2;cnt++);

PRACTICAL 9 ELEVATOR

#include <REG51.h>

sbit FloorG = P1^7;

sbit Floor1 = P1^6;

sbit Floor2 = P1^5;

sbit Floor3 = P1^4;

sbit Floor4 = P1^3;


sbit Floor5 = P1^2;

sbit Floor6 = P1^1;

sbit Floor7 = P1^0;

int FloorFlag, CurrentFloor, DisplayValue;

int GROUNDFLOOR = 0;

int GNDFLR = 0x3D;

//0x3F is 0 replaced by G alphabet to display on the 7 segment.

char number[]={0x3D,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};

char motor_lookup[]={0x09,0x08,0x0C,0x04,0x06,0x02,0x03,0x01};

void delay (unsigned int);

void main(){

P0 = P2 = P3 = 0;

CurrentFloor = GROUNDFLOOR;

DisplayValue = GROUNDFLOOR;

FloorFlag = GROUNDFLOOR;

P3 = GNDFLR;

while(1)

if( FloorG == 0 )

FloorFlag = 0;

else if(Floor1 == 0)

{
FloorFlag = 1;

else if(Floor2 == 0)

FloorFlag = 2;

else if(Floor3 == 0)

FloorFlag = 3;

else if(Floor4 == 0)

FloorFlag = 4;

else if(Floor5 == 0)

FloorFlag = 5;

else if(Floor6 == 0)

FloorFlag = 6;

else if(Floor7 == 0)

FloorFlag = 7;

if(CurrentFloor < FloorFlag )

do{

CurrentFloor++;
DisplayValue++;

P3 = number[DisplayValue];

P2 = motor_lookup[DisplayValue];

delay(100000);

}while(( CurrentFloor != FloorFlag ));

if(CurrentFloor > FloorFlag )

do{

CurrentFloor--;

DisplayValue--;

P3 = number[DisplayValue];

P2 = motor_lookup[DisplayValue];

delay(100000);

}while(CurrentFloor != FloorFlag);

void delay(unsigned int delaytime)

unsigned int loopvar;

for(loopvar=0;loopvar < delaytime;loopvar++);

THANK ME LATER!!:)