Sunteți pe pagina 1din 70

/*

assignment 5 - question 1
program to count from 0 to 99 by generating one second delay
using timer0 in polling mode and measure the actual time elapsed
using timer1 in interrupt mode and display it on the LCD
P2 lower nibble to LCD control
P1 to LCD data
*/

#include<REG52.h>

sbit RS=P2^0;
sbit RW=P2^1;
sbit EN=P2^2;

void polsec(unsigned char); //generates one sec delay using timer0 in polling mode
void wrt(unsigned char,unsigned char); //writes the data onto the LCD
void delay(unsigned int); //generates delay
void lcdint(void); //initialises the LCD

unsigned int count=0,value=~50000;

void main(void)
{
unsigned char i,j;
unsigned long int time;
unsigned char a[]="0123456789",u[3];
lcdint();
wrt(a[0],1); //enters the data a[0] into the LCD
wrt(a[1],1);
TMOD=0x11; //both the timers are used in mode 1
EA=1;ET1=1;ET0=1;
TR1=0;
TL1=0;
TH1=0;
TR1=1;
for(i=0;i<=9;i++) //the for loops are to generate the numbers from 00 to 99
{
for(j=0;j<=9;j++)
{
P3=(i*16)+j;
polsec(1); //generates one-second delay
}
}
time=(65536*count)+(256*TH1)+TL1; //to calculate the total time in micro sec
time=time/10000; //time is converted to centi sec(1/100th of a sec)

u[0]=time%10;time=time/10; //these lines are for separating the digits to display them
u[1]=time%10;time=time/10;
u[2]=time%10;time=time/10;
wrt(a[u[2]],1);wrt(a[u[1]],1);wrt(a[u[0]],1); //displaying the total time
while(1);
}
void polsec(unsigned char n)
{
unsigned int value,i;
value=~50000; //to get 50 milli sec delay

for (i=0;i<(20*n);i++) //20*50=1000


{
TR0=0;TF0=0;
TL0=value%256;TH0=value/256; //taking the lower and upper bytes of value
TR0=1; //switching timer0 on
while(!TF0); //wait till timer0 becomes high
TR0=0; //switching timer0 off
}
}
void timer1(void) interrupt 3 //using timer1 in interrupt mode
{
count++; //keep count of the total time in micro sec
}
void wrt(unsigned char n,unsigned char m)
{
RS=m; //decides whether the LCD should be in data or command
mode
RW=0; //to put the LCD in write mode
P1=n; //sending the input to the LCD through P1
EN=1; // this writes the
EN=0; // data into the LCD
delay(5000); //generates delay
}
void delay (unsigned int n)
{
unsigned int i;
for(i=0;i<n;i++);
}
void lcdint(void) //LCD initialisation
{
wrt(0x38,0);
wrt(0x38,0);
wrt(0x38,0);
wrt(0x38,0);
wrt(0x0e,0);
wrt(0x06,0);
wrt(0x01,0);
}
/*
assignment 5 - question 2

program to count from 0 to 99 by generating one second delay


using timer0 in interrupt mode and measure the actual time elapsed
using timer1 in interrupt mode and display it on the LCD
P2 lower nibble to LCD control
P1 to LCD data
*/

#include<REG52.h>

sbit RS=P2^0;
sbit RW=P2^1;
sbit EN=P2^2;
bit flag = 0;

void wrt(unsigned char,unsigned char); //writes the data onto the LCD
void delay(unsigned int); //generates delay
void lcdint(void); //initialises the LCD

unsigned int count=0,co=0,value=~50000,time = 0x00;

void main(void)
{
unsigned long int ime;
unsigned char a[]="0123456789",u[3];
lcdint();
wrt(a[0],1); //enters the data a[0] into the LCD
wrt(a[1],1);
TMOD=0x11; //both the timers are used in mode 1
EA=1;ET1=1;ET0=1;
TR1=0; TL1=0;TH1=0; TR1=1;
TR0=0;TL0=value%256;TH0=value/256; TR0=1;
while(time <= 99) //to count from 0 to 99
{
if(!flag)
{
P3=((time / 10) << 4) + (time % 10); //to split time into 2 parts and send it through P3
time++; //to increment time and count it in micro sec
flag = 1; //to go to interrupt
}
}

ime=(65536*co)+(256*TH1)+TL1; //ime is the total time


ime=ime/1000000; //to convert total time to sec

u[0]=ime%10;ime=ime/10; //to separate the digits


u[1]=ime%10;ime=ime/10;
u[2]=ime%10;ime=ime/10;
wrt(a[u[2]],1);wrt(a[u[1]],1);wrt(a[u[0]],1); //display on LCD
while(1);
}

void timer1(void) interrupt 3 //using timer1 in interrupt mode


{
co++;
}
void wrt(unsigned char n,unsigned char m)
{
RS=m; //decides whether the LCD should be in data or
command mode
RW=0; //to put the LCD in write mode
P1=n; //sending the input to the LCD through P1
EN=1; // this writes the
EN=0; // data into the LCD
delay(5000); //generates delay
}
void delay (unsigned int n)
{
unsigned int i;
for(i=0;i<n;i++);
}
void lcdint(void) //LCD initialisation
{
wrt(0x38,0);
wrt(0x38,0);
wrt(0x38,0);
wrt(0x38,0);
wrt(0x0e,0);
wrt(0x06,0);
wrt(0x01,0);
}

void timer0(void) interrupt 1 //using timer0 in interrupt mode to generate 1 sec delay
{ TR0=0;
TL0=value%256;TH0=value/256;
TR0=1;
count++;
if(count == 20) {
count = 0x00;
flag = 0;
}
}
/*
assignment 5 - question 3

program to count from 0 to 99 by generating one second delay


using timer0 in interrupt mode and measure the actual time elapsed
using timer1 in interrupt mode and display it on the LCD
P2 lower nibble to LCD control
P1 to LCD data
*/

#include<REG52.h>

sbit RS=P2^0;
sbit RW=P2^1;
sbit EN=P2^2;
bit flag = 0;

void wrt(unsigned char,unsigned char); //writes the data onto the LCD
void delay(unsigned int); //generates delay
void lcdint(void); //initialises the LCD

unsigned int count=0,co=0,value=~50000,time;


unsigned char i;

void main(void)
{
unsigned char a[]="0123456789",u[3];
lcdint();
wrt(a[0],1); //enters the data a[0] into the LCD
wrt(a[1],1);
TMOD=0x10; //both the timers are used in mode 1
EA=1;ET1=1;
TR1=1;
for(i=0;i<99;i++)
{
wrt(i,1);
}
time=(65536*co)+(256*TH1)+TL1; //time is the total time
time=time/1000000; //to convert total time to sec

u[0]=time%10;time=time/10; //to separate the digits


u[1]=time%10;time=time/10;
u[2]=time%10;time=time/10;
wrt(a[u[2]],1);wrt(a[u[1]],1);wrt(a[u[0]],1); //display on LCD
while(1);
}

void timer1(void) interrupt 3 //using timer1 in interrupt mode


{
co++;
}
void wrt(unsigned char n,unsigned char m)
{
RS=m; //decides whether the LCD should be in data or
command mode
RW=0; //to put the LCD in write mode
P1=n; //sending the input to the LCD through P1
EN=1; // this writes the
EN=0; // data into the LCD
delay(5000); //generates delay
}
void delay (unsigned int n)
{
unsigned int i;
for(i=0;i<n;i++);
}
void lcdint(void) //LCD initialisation
{
wrt(0x38,0);
wrt(0x38,0);
wrt(0x38,0);
wrt(0x38,0);
wrt(0x0e,0);
wrt(0x06,0);
wrt(0x01,0);
}

/*
assignment 5 - question 4,question 5 and question 6 combined

program to measure th on-off time 0f 555 oscillator and


display it on the LCD
*/

#include<REG52.h>

sbit IN=P2^7;
sbit RS=P2^0;
sbit RW=P2^1;
sbit EN=P2^2;

void wrt(unsigned char,unsigned char); //writes the data onto the LCD
void delay(unsigned int); //generates delay
void lcdint(void); //initialises the LCD

void main(void)
{
unsigned char a[]="0123456789",u[5],o[5],t[5],r[5],b[]="ON",c[]="OFF",d[]="FREQ",i;
unsigned long ime,time,tim,lime,rpm;
while(1)
{
IN=1; //sets P2^7 as 1
TMOD=0x01; TR0=0;TH0=0;TL0=0; //timer 0 is used in mode 1
while(IN); // to get the
while(!IN); // rising edge
TR0=1; //timer0 is switched on
while(IN); //measuring the time while the signal is high
TR0=0; //switching timer0 off
ime=(TH0*256)+TL0; //storing the time elapsed in ime(in micro seconds)
lcdint(); //initialises LCD

for(i=0;i<2;i++) //to display "ON" on the LCD


wrt(b[i],1);

u[0]=ime%10;ime=ime/10; //separating the digits of ime


u[1]=ime%10;ime=ime/10;
u[2]=ime%10;ime=ime/10;
u[3]=ime%10;ime=ime/10;
u[4]=ime%10;ime=ime/10;

wrt(a[u[4]],1); //displaying the time ime on the LCD


wrt(a[u[3]],1);
wrt(a[u[2]],1);
wrt(a[u[1]],1);
wrt(a[u[0]],1);

TR0=0;TH0=0;TL0=0;

while(!IN); //the following lines to measure off time in a similar way


while(IN);
TR0=1;
while(!IN);
TR0=0;
time=(TH0*256)+TL0;
for(i=0;i<3;i++)
wrt(c[i],1);

o[0]=time%10;time=time/10;
o[1]=time%10;time=time/10;
o[2]=time%10;time=time/10;
o[3]=time%10;time=time/10;
o[4]=time%10;time=time/10;

wrt(a[o[4]],1);
wrt(a[o[3]],1);
wrt(a[o[2]],1);
wrt(a[o[1]],1);
wrt(a[o[0]],1);

wrt(0xc0,0); //to display the contents on the second line of LCD


for(i=0;i<4;i++)
wrt(d[i],1);

lime=time+ime; //on time + off time


tim=(1000000/lime); //frequency calculated in hertz
t[0]=tim%10;tim=tim/10; //separating the digits of frequency tim
t[1]=tim%10;tim=tim/10;
t[2]=tim%10;tim=tim/10;
t[3]=tim%10;tim=tim/10;
t[4]=tim%10;tim=tim/10;

wrt(a[t[4]],1); //displaying frequency on the LCD


wrt(a[t[3]],1);
wrt(a[t[2]],1);
wrt(a[t[1]],1);
wrt(a[t[0]],1);

rpm=tim/60;
r[0]=rpm%10;rpm=rpm/10; //separating the digits of RPM rpm
r[1]=rpm%10;rpm=rpm/10;
r[2]=rpm%10;rpm=rpm/10;
r[3]=rpm%10;rpm=rpm/10;
r[4]=rpm%10;rpm=rpm/10;

wrt(a[r[4]],1); //displaying RPM on the LCD


wrt(a[r[3]],1);
wrt(a[r[2]],1);
wrt(a[r[1]],1);
wrt(a[r[0]],1);

}
}

void wrt(unsigned char n,unsigned char m)


{
RS=m; //decides whether the LCD should be in data or command mode
RW=0; //to put the LCD in write mode
P3=n; //sending the input to the LCD through P1
EN=1; // this writes the
EN=0; // data into the LCD
delay(5000); //generates delay
}

void delay (unsigned int n)


{
unsigned int i;
for(i=0;i<n;i++);
}

void lcdint(void) //LCD initialisation


{
wrt(0x38,0);
wrt(0x38,0);
wrt(0x38,0);
wrt(0x38,0);
wrt(0x0e,0);
wrt(0x06,0);
wrt(0x01,0);
}

/*
assignment 5 - question 4,question 5 and question 6 combined

program to measure th on-off time 0f 555 oscillator and


display it on the LCD
*/

#include<REG52.h>

sbit IN=P2^7;
sbit RS=P2^0;
sbit RW=P2^1;
sbit EN=P2^2;

void wrt(unsigned char,unsigned char); //writes the data onto the LCD
void delay(unsigned int); //generates delay
void lcdint(void); //initialises the LCD

void main(void)
{
unsigned char a[]="0123456789",u[5],o[5],t[5],r[5],b[]="ON",c[]="OFF",d[]="FREQ",i;
unsigned long ime,time,tim,lime,rpm;
while(1)
{
IN=1; //sets P2^7 as 1
TMOD=0x01; TR0=0;TH0=0;TL0=0; //timer 0 is used in mode 1
while(IN); // to get the
while(!IN); // rising edge
TR0=1; //timer0 is switched on
while(IN); //measuring the time while the signal is high
TR0=0; //switching timer0 off
ime=(TH0*256)+TL0; //storing the time elapsed in ime(in micro seconds)
lcdint(); //initialises LCD

for(i=0;i<2;i++) //to display "ON" on the LCD


wrt(b[i],1);

u[0]=ime%10;ime=ime/10; //separating the digits of ime


u[1]=ime%10;ime=ime/10;
u[2]=ime%10;ime=ime/10;
u[3]=ime%10;ime=ime/10;
u[4]=ime%10;ime=ime/10;

wrt(a[u[4]],1); //displaying the time ime on the LCD


wrt(a[u[3]],1);
wrt(a[u[2]],1);
wrt(a[u[1]],1);
wrt(a[u[0]],1);

TR0=0;TH0=0;TL0=0;

while(!IN); //the following lines to measure off time in a similar way


while(IN);
TR0=1;
while(!IN);
TR0=0;
time=(TH0*256)+TL0;
for(i=0;i<3;i++)
wrt(c[i],1);

o[0]=time%10;time=time/10;
o[1]=time%10;time=time/10;
o[2]=time%10;time=time/10;
o[3]=time%10;time=time/10;
o[4]=time%10;time=time/10;

wrt(a[o[4]],1);
wrt(a[o[3]],1);
wrt(a[o[2]],1);
wrt(a[o[1]],1);
wrt(a[o[0]],1);

wrt(0xc0,0); //to display the contents on the second line of LCD


for(i=0;i<4;i++)
wrt(d[i],1);

lime=time+ime; //on time + off time


tim=(1000000/lime); //frequency calculated in hertz
t[0]=tim%10;tim=tim/10; //separating the digits of frequency tim
t[1]=tim%10;tim=tim/10;
t[2]=tim%10;tim=tim/10;
t[3]=tim%10;tim=tim/10;
t[4]=tim%10;tim=tim/10;

wrt(a[t[4]],1); //displaying frequency on the LCD


wrt(a[t[3]],1);
wrt(a[t[2]],1);
wrt(a[t[1]],1);
wrt(a[t[0]],1);

rpm=tim/60;
r[0]=rpm%10;rpm=rpm/10; //separating the digits of RPM rpm
r[1]=rpm%10;rpm=rpm/10;
r[2]=rpm%10;rpm=rpm/10;
r[3]=rpm%10;rpm=rpm/10;
r[4]=rpm%10;rpm=rpm/10;

wrt(a[r[4]],1); //displaying RPM on the LCD


wrt(a[r[3]],1);
wrt(a[r[2]],1);
wrt(a[r[1]],1);
wrt(a[r[0]],1);

}
}

void wrt(unsigned char n,unsigned char m)


{
RS=m; //decides whether the LCD should be in data or command mode
RW=0; //to put the LCD in write mode
P3=n; //sending the input to the LCD through P1
EN=1; // this writes the
EN=0; // data into the LCD
delay(5000); //generates delay
}

void delay (unsigned int n)


{
unsigned int i;
for(i=0;i<n;i++);
}

void lcdint(void) //LCD initialisation


{
wrt(0x38,0);
wrt(0x38,0);
wrt(0x38,0);
wrt(0x38,0);
wrt(0x0e,0);
wrt(0x06,0);
wrt(0x01,0);
}

/*
assignement 6 question 1

program to send and receive a to/from the hyperterminal


*/

#include<REG52.h>

void main(void)
{
unsigned char k;
TMOD=0x20; //timer1 in mode 2
SCON=0x50; //necessary for hyperterminal communication
TH1=-3;TR1=1; //to set the baud rate at 9600 bps
while(1)
{
while(!RI); //wait till a key is pressed so that the receive buffer becomes high
RI=0; //make RI low
k=SBUF; //store the value present in the buffer in k
SBUF=k; //put the value of k in the transmit buffer
while(!TI); //wait till the character is sent so that TI becomes high
TI=0; //set TI as 0
}
}
/*
assignement 6 question 2

program to read a character from hyperterminal and display it on LCD


*/

#include<REG52.h>

sbit RS=P2^0;
sbit RW=P2^1;
sbit EN=P2^2;

void wrt(unsigned char,unsigned char); //writes the data onto the LCD
void delay(unsigned int); //generates delay
void lcdint(void); //initialises the LCD

void main(void)
{
unsigned char k;
TMOD=0x20; //timer1 in mode 2
SCON=0x50; //necessary for hyperterminal communication
TH1=0xfd; //to set the baud rate at 9600 bps
TR1=1;
lcdint();
while(1)
{
while(!RI); //wait till a key is pressed so that the receive buffer becomes high
RI=0; //make RI low
k=SBUF; //store the value present in the buffer in k
wrt(k,1); //display key on LCD
}
}

void wrt(unsigned char n,unsigned char m)


{
RS=m; //decides whether the LCD should be in data or command mode
RW=0; //to put the LCD in write mode
P3=n; //sending the input to the LCD through P1
EN=1; // this writes the
EN=0; // data into the LCD
delay(5000); //generates delay
}

void delay (unsigned int n)


{

for(;n;--n);
}

void lcdint(void) //LCD initialisation


{
wrt(0x38,0);
wrt(0x38,0);
wrt(0x38,0);
wrt(0x38,0);
wrt(0x0e,0);
wrt(0x06,0);
wrt(0x01,0);
}
/*
assignment 6 - question 3

program to count from 0 to 99 by generating one second delay


using timer0 in polling mode and measure the actual time elapsed
using timer1 in interrupt mode and display it on the hyperterminal
*/

#include<REG52.h>

void polsec(unsigned char); //generates one sec delay using timer0 in polling mode

unsigned int count=0,value=~50000;

void main(void)
{
unsigned char i,j;
unsigned long int time;
unsigned char u[3],a[]="0123456789";
TMOD=0x01; //timer0 is used in mode 1 and timer1 in mode 0
EA=1;ET1=1;ET0=1;
TR0=0; //setting all values to zero
TL0=0;
TH0=0;
TR0=1; //switching timer0 on
for(i=0;i<=9;i++) //the for loops are to generate the numbers from 00 to 99
{
for(j=0;j<=9;j++)
{
TMOD=0x21; //timer0 is used in mode 1 and timer1 in mode 2
SCON=0x50; //necessary for hyperterminal communication
TH1=-3;TR1=1; //to set the baud rate at 9600 bps
SBUF=0x0c;while(!TI);TI=0; //put the value 0x0c in the transmit buffer
SBUF=a[i];while(!TI);TI=0; //put the value of a[i] in the transmit buffer
SBUF=a[j];while(!TI);TI=0; //put the value of a[j] in the transmit buffer
TR1=0; //switch off timer1
polsec(1); //generates one-second delay
}
}TR0=0; //switch off timer0
time=(65536*count)+(256*TH0)+TL0; //calculate total time and storing it in time(in microseconds)
time=time/100; //converts time into 1/10th of a millisecond

u[0]=time%10;time=time/10; //these lines are for separating the digits to display them
u[1]=time%10;time=time/10;
u[2]=time%10;time=time/10;

TMOD=0x21;
SCON=0x50;
TH1=-3;TR1=1;
SBUF=0x0c;while(!TI);TI=0;
SBUF=(a[u[2]]);while(!TI);TI=0;
SBUF=(a[u[1]]);while(!TI);TI=0;
SBUF=(a[u[0]]);while(!TI);TI=0;
TR1=0;
while(1);
}

void polsec(unsigned char n)


{
unsigned int value,i;
value=~50000; //to get 50 milli sec delay

for (i=0;i<(20*n);i++) //20*50=1000


{
TR0=0;TF0=0;
TL0=value%256;TH0=value/256; //taking the lower and upper bytes of value
TR0=1; //switching timer0 on
while(!TF0); //wait till timer0 becomes high
TR0=0; //switching timer0 off
}
}

void timer0(void) interrupt 1 //using timer1 in interrupt mode


{
count++; //keep count of the total time in micro sec
}

/*
assignment 6 - question 4

program to count from 0 to 99 by generating one second delay


using timer0 in interrupt mode and measure the actual time elapsed
using timer1 in interrupt mode and display it on the hyperterminal
*/

#include<REG52.h>

bit flag = 0;
unsigned int count=0,value=~50000,

void main(void)
{
unsigned int time = 0x00;
TMOD=0x21; //timer0 is used in mode 1 and timer1 in mode 2
SCON=0x50; //necessary for hyperterminal communication
TH1=-3;TR1=1; //to set the baud rate at 9600 bps
EA=1;ET0=1;
TR0=0;TL0=value%256;TH0=value/256;
TR0=1; //switching timer0 on
while(time <= 99) //to count from 0 to 99
{
if(!flag)
{
flag = 1;
SBUF=0x0c; //put the value 0x0c in the transmit buffer
while(!TI);TI=0; //wait till the character is sent so that TI becomes high and then set it as zero
SBUF=time / 10 + 0x30; //put the first digit of the total time in the transmit buffer
while(!TI);TI=0; //wait till the character is sent so that TI becomes high and then set it as zero
SBUF= time%10 + 0x30; //put the second digit of the total time in the transmit buffer
while(!TI); //wait till the character is sent so that TI becomes high
time++; //increment time
TI=0; //set TI as zero
}
}
while(1);
}

void timer0(void) interrupt 1 //using timer0 in interrupt mode to generate 1 sec delay
{
TR0=0;
TL0=value%256;TH0=value/256;
TR0=1;
count++;
if(count == 20)
{
count = 0x00;
flag = 0;
}
}
/*
assignment 6 - question 5

program to count from 0 to 99 using a for loop and measuring the total time
using timer1 in interrupt mode and display it on the hyperterminal
*/

#include<REG52.h>

unsigned int count=0,value=~50000;

void main(void)
{
unsigned int time;
unsigned char t[3],a[]="TOTAL TIME=";
TMOD=0x21; //timer0 is used in mode 1 and timer1 in mode 2
SCON=0x50; //necessary for hyperterminal communication
TH1=-3;
TR1=1; //to set the baud rate at 9600 bps

{
unsigned char i;
TR0=1; //switching timer0 on
for(i=0;i<=99;i++)
{
SBUF=i/10+0x30;
while(!TI);
TI=0;
SBUF=i%10+0x30;
while(!TI);
TI=0;
}
TR0=0; //switching timer0 off
time=(65536*count)+(256*TH0)+TL0; //calculate total time and storing it in time(in microseconds)
t[0]=time%10; //separating the digits of the total time
t[1]=(time/10)%10;
t[2]=(time/100)%10;
for(i=0;i<11;i++)
{
SBUF=a[i];
while(!TI);
TI=0;
}
for(i=0;i<3;i++) //displaying the total time on the hyper terminal
{
SBUF=t[i]+0x30;
while(!TI);
TI=0;
}
}while(1);
}
void timer0(void) interrupt 1 //using timer1 in interrupt mode
{
count++; //keep count of the total time in micro sec
}

/*
assignment 6 - question 6,question 7 and question 8 combined

program to measure the on-off time,the frequency and the RPM of 555 oscillator and
display it on the hyperterminal
*/

#include<REG52.h>
sbit IN=P2^7;
void main(void)
{
unsigned char u[5],o[5],t[5],b[]="ON",c[]="OFF",d[]="FREQ",i,e[]="RPM",r[5];
unsigned long ime,time,tim,rpm;
while(1)
{
IN=1;
TMOD=0x21; //timer0 is used in mode 1 and timer1 in mode 2
SCON=0x50; //necessary for hyperterminal communication
TR0=0;TH0=0;TL0=0;
TR1=0;TH1=0xfd;TR1=1; //to set the baud rate at 9600 bps
while(IN); // to get the
while(!IN); // rising edge
TR0=1; //switching timer0 on
while(IN);
TR0=0; //switching timer0 off
ime=(TH0*256)+TL0; //calculating total on-time ime
u[0]=ime%10;ime=ime/10; //separating the digits of ime
u[1]=ime%10;ime=ime/10;
u[2]=ime%10;ime=ime/10;
u[3]=ime%10;ime=ime/10;
u[4]=ime%10;ime=ime/10;
TR0=0;TH0=0;TL0=0;
while(!IN); // to get the
while(IN); // falling edge
TR0=1; //switching timer0 on
while(!IN);
TR0=0; //switching timer0 off
time=(TH0*256)+TL0; //calculating the total off-time time
o[0]=time%10;time=time/10; //separating the digits of time
o[1]=time%10;time=time/10;
o[2]=time%10;time=time/10;
o[3]=time%10;time=time/10;
o[4]=time%10;time=time/10;
tim=(1000000/(time+ime)); //calculating the frequency tim
t[0]=tim%10;tim=tim/10; //separating the digits of tim
t[1]=tim%10;tim=tim/10;
t[2]=tim%10;tim=tim/10;
t[3]=tim%10;tim=tim/10;
t[4]=tim%10;tim=tim/10;
rpm=tim/60; //calculating the RPM rpm
r[0]=rpm%10;rpm=rpm/10; //separating the digits of rpm
r[1]=rpm%10;rpm=rpm/10;
r[2]=rpm%10;rpm=rpm/10;
r[3]=rpm%10;rpm=rpm/10;
r[4]=rpm%10;rpm=rpm/10;
for(i=0;i<2;i++) //displaying on-time,off-time,frequency and rpm
{
SBUF = b[i];while(!TI);TI=0;
}
for(i=5;i>0;i--)
{
SBUF=u[i-1]+0x30;while(!TI);TI=0;
}
SBUF=0x0a;while(!TI);TI=0;
SBUF=0x0d;while(!TI);TI=0;
for(i=0;i<3;i++)
{
SBUF=c[i];while(!TI);TI=0;
}
for(i=5;i>0;i--)
{
SBUF=o[i-1]+0x30;while(!TI);TI=0;
}
SBUF=0x0a;while(!TI);TI=0;
SBUF=0x0d;while(!TI);TI=0;
for(i=0;i<4;i++)
{
SBUF=d[i];while(!TI);TI=0;
}
for(i=5;i>0;i--)
{
SBUF=t[i-1]+0x30;while(!TI);TI=0;
}
SBUF=0x0a;while(!TI);TI=0;
SBUF=0x0d;while(!TI);TI=0;
for(i=0;i<3;i++)
{
SBUF = e[i];while(!TI);TI=0;
}
for(i=5;i>0;i--)
{
SBUF=r[i-1]+0x30;while(!TI);TI=0;
}
SBUF=0x0a;while(!TI);TI=0;
SBUF=0x0d;while(!TI);TI=0;
SBUF=0xc0;while(!TI);TI=0;
}
}

/*
assignment 7 - question 1
program to use the ADC to measure the input voltage and
display it on LCD
*/
#include<REG52.h>

sbit clk = P3^4;


sbit busy = P3^5;
sbit RS=P2^0;
sbit RW=P2^1;
sbit EN=P2^2;

void wrt(unsigned char,unsigned char); //displays on LCD


void delay(unsigned int); //generates delay
void lcdint(void); //initialises LCD

void main(void)
{
unsigned int value;
unsigned char a[4];
clk=1;busy=1;
TMOD=0x05; //uses timer0 in mode 1 in counter mode
TR0=0; //switching off timer0
lcdint(); //initialises LCD
while(1)
{
TL0=0;TH0=0;
while(busy); // to get the
while(!busy); // rising edge
TR0=1; //switching on timer0
while(busy);
TR0=0; //switching off timer0

value=(((TH0*256)+TL0)-10000); //calculating the total time and storing it in value


value=value/10;
a[0]=value%10;value=value/10; //separating the digits of value and storing it in array a[]
a[1]=value%10;value=value/10;
a[2]=value%10;value=value/10;
a[3]=value%10;value=value/10;
wrt(0x80,0);
wrt((a[3]+0x30),1); //displaying value on LCD
wrt((a[2]+0x30),1);
wrt((a[1]+0x30),1);
wrt((a[0]+0x30),1);
}
}
void wrt(unsigned char n,unsigned char m)
{
RS=m; //RS decides whether the LCD should be in control mode or in data mode
RW=0; //RW decides whether the LCD should be in read mode or in write mode
P1=n; //sending the input to LCD through P1
EN=1; // sending high to low pulse
EN=0; // on EN writes the data into the LCD
delay(5000);
}
void delay (unsigned int n)
{
unsigned int i;
for(i=0;i<n;i++);
}
void lcdint(void) //LCD initialisation
{
wrt(0x38,0);
wrt(0x38,0);
wrt(0x38,0);
wrt(0x38,0);
wrt(0x0e,0);
wrt(0x06,0);
wrt(0x01,0);
}
/*
assignment 7 - question 2
program to generate a ramp using DAC

*/

#include<REG52.h>

void delay(unsigned int); //generates delay

void main(void)
{
unsigned char i;
while(1)
{
for(i=0;i<0xff;i++) //loop to generate rising edge of ramp
{
P2=i;
delay(10000);
}
for(i=0xff;i>0;i--) //loop to generate falling edge of ramp
{
P2=i;
delay(10000);
}
P2=0x00;
for(i=0;i<10;i++) //loop to generate flat portion of ramp
delay(10000);
}
}
void delay (unsigned int n)
{
unsigned int i;
for(i=0;i<n;i++);
}
/*
assignment 7 - question 3
program to generate analog signal using DAC and read it from ADC
and display it on LCD
*/

#include<REG52.h>

sbit clk = P3^4;


sbit busy = P3^5;
sbit RS=P2^0;
sbit RW=P2^1;
sbit EN=P2^2;

void wrt(unsigned char,unsigned char); //displays on LCD


void delay(unsigned int); //generates delay
void lcdint(void); //initialises LCD

void main(void)
{
unsigned int value,i;
unsigned char a[4];
clk=1;busy=1;
TMOD=0x05; //uses timer0 in mode 1 in counter mode
TR0=0; //switching off timer0
lcdint(); //initialises LCD
while(1)
{

for(i=0;i<0xff;i++) //loop to generate rising edge of ramp


{
P0=i;
TL0=0;TH0=0;
while(busy); // to get the
while(!busy); // rising edge
TR0=1; //switching on timer0
while(busy);
TR0=0; //switching off timer0
value=(((TH0*256)+TL0)-10000); //calculating the total time and storing it in value
value=value/10;
a[0]=value%10;value=value/10; //separating the digits of value and storing it in array a[]
a[1]=value%10;value=value/10;
a[2]=value%10;value=value/10;
a[3]=value%10;value=value/10;
wrt(0x80,0);
wrt((a[3]+0x30),1); //displaying value on LCD
wrt((a[2]+0x30),1);
wrt((a[1]+0x30),1);
wrt((a[0]+0x30),1);
delay(50000);
}
for(i=0xff;i>0;i--) //loop to generate falling edge of ramp
{
P0=i;
TL0=0;TH0=0;
while(busy);
while(!busy);
TR0=1;
while(busy);
TR0=0;
value=(((TH0*256)+TL0)-10000);
value=value/10;
a[0]=value%10;value=value/10;
a[1]=value%10;value=value/10;
a[2]=value%10;value=value/10;
a[3]=value%10;value=value/10;
wrt(0x80,0);
wrt((a[3]+0x30),1);
wrt((a[2]+0x30),1);
wrt((a[1]+0x30),1);
wrt((a[0]+0x30),1);
delay(50000);
}

for(i=0;i<10;i++) //loop to generate flat portion of ramp


{
P0=0x00;
TL0=0;TH0=0;
while(busy);
while(!busy);
TR0=1;
while(busy);
TR0=0;
value=(((TH0*256)+TL0)-10000);
value=value/10;
a[0]=value%10;value=value/10;
a[1]=value%10;value=value/10;
a[2]=value%10;value=value/10;
a[3]=value%10;value=value/10;
wrt(0x80,0);
wrt((a[3]+0x30),1);
wrt((a[2]+0x30),1);
wrt((a[1]+0x30),1);
wrt((a[0]+0x30),1);
delay(50000);
}
}
}
void wrt(unsigned char n,unsigned char m)
{
RS=m; //RS decides whether the LCD should be in control mode or in data mode
RW=0; //RW decides whether the LCD should be in read mode or in write mode
P1=n; //sending the input to LCD through P1
EN=1; // sending high to low pulse
EN=0; // on EN writes the data into the LCD
delay(5000);
}
void delay (unsigned int n)
{
unsigned int i;
for(i=0;i<n;i++);
}
void lcdint(void) //LCD initialisation
{
wrt(0x38,0);
wrt(0x38,0);
wrt(0x38,0);
wrt(0x38,0);
wrt(0x0e,0);
wrt(0x06,0);
wrt(0x01,0);
}
/*
Assignment 8
Question 1
TEMPERATURE CONTROLLER

temperature sensor - 10 mV/C

*/

#include<REG52.h>

sbit RS=P2^0; // control bits of LCD


sbit RW=P2^1;
sbit EN=P2^2;

void wrt(unsigned char,bit);


void delay(unsigned int);
void lcdint(void);

sbit clk = P3^4; // connected to ADC


sbit busy = P3^5;

void main(void)
{
unsigned char a[4];
unsigned int value;
clk=1;busy=1;
TMOD=0x05; // timer 0 in counter mode
TR0=0;
lcdint(); // initialising the LCD

while(1)
{
TL0=0;TH0=0;
while(busy); // to get the rising edge of busy
while(!busy);
TR0=1;
while(busy);
TR0=0;
value=(((TH0*256)+TL0)-10000); // to get the value of time which is proportional to the voltage

a[0]=value%10;value=value/10; // separating the digits of value and storing it in array a[]


a[1]=value%10;value=value/10;
a[2]=value%10;value=value/10;
a[3]=value%10;value=value/10;

wrt(0x80,0);
wrt((a[3]+0x30),1); // displaying the value on LCD
wrt((a[2]+0x30),1);
wrt((a[1]+0x30),1);
wrt((a[0]+0x30),1);

}
}

void wrt(unsigned char n,bit m)


{
RS=m; // selects whether LCD is in data or control mode
RW=0; // selects whether LCD is in read or write mode
P0=n; // sneding the input to LCD through P0
EN=1; // sending a high to low pulse to EN bit
EN=0;
delay(1000);
}
void lcdint(void) // LCD initialisation
{
wrt(0x38,0);
wrt(0x38,0);
wrt(0x38,0);
wrt(0x38,0);
wrt(0x0e,0);
wrt(0x06,0);
wrt(0x01,0);
}
void delay (unsigned int n)
{
for(;n>0;n--);
}

/*
assignment 8 - question 2

stop watch using keyboard and LCD

*/

#include<REG52.h>

sbit RS=P2^0;
sbit RW=P2^1;
sbit EN=P2^2;

void wrt(unsigned char,unsigned char); //displays on LCD


void delay(unsigned int); //generates delay
void lcdint(void); //initialises LCD

unsigned int count=0,value=~50000;


void main(void)
{
unsigned char val,state,min,sec;
unsigned long int time,ms;
TMOD=0x10; //timer1 in mode 1
EA=1;ET1=1;
TL1=0;TH1=0;
TR1=0; //switching off timer1
lcdint();
while(1)
{
P1=0xfe;delay(1000);val=P1;
switch(state)
{
case 0:
if(val==0xee) //row 0 column 0
{
TR0=0; //switching off timer0
state=1;
}
break;
case 1: TR1=1;
if(val==0xde) //row 0 column 1
{
state=3;
}
else if(val==0xbe) //row 0 column 2
{
state=2;
}
break;
case 2: TR1=0;
if(val==0x7e) //row 0 column 3
{
state=1;
}
break;
case 3: TR1=0;
if(val==0xee) //row 0 column 0
{
state=0;
}
break;
};
if(state==0) //init
{
count=0;TH1=0;TL1=0;
wrt(0x80,0);
wrt(0x30,1);
wrt(0x30,1);
wrt(':',1);
wrt(0x30,1);
wrt(0x30,1);
wrt(':',1);
wrt(0x30,1);
wrt(0x30,1);
}
else if(state==1) // run mode
{
time=((65536*count)+(TH1*256)+TL1); //calculating total time(in micro sec) elapsed and storing it in time
ms=time/1000; //converting the time into milli sec
ms=ms%1000;
time=time/1000000; //converting time to sec
min=time/60; //calculating minutes
sec=time%60; //calculating seconds
wrt(0x80,0);
wrt((min/10+0x30),1); //displaying minutes
wrt((min%10+0x30),1);
wrt(':',1);
wrt((sec/10+0x30),1); //displaying sec
wrt((sec%10+0x30),1);
wrt(':',1);
wrt((ms/100+0x30),1); //displaying milli sec
wrt(((ms/10)%10+0x30),1);
wrt((ms%10+0x30),1);
}
}
}
void timer1(void) interrupt 3 //timer1 in interrupt mode
{
count++; //incrementing count
}

void wrt(unsigned char n,unsigned char m)


{
RS=m; //RS decides whether the LCD should be in control mode or in data mode
RW=0; //RW decides whether the LCD should be in read mode or in write mode
P1=n; //sending the input to LCD through P1
EN=1; // sending high to low pulse
EN=0; // on EN writes the data into the LCD
delay(5000);
}

void lcdint(void) //LCD initialisation


{
wrt(0x38,0);
wrt(0x38,0);
wrt(0x38,0);
wrt(0x38,0);
wrt(0x0e,0);
wrt(0x06,0);
wrt(0x01,0);
}

void delay (unsigned int n)


{
unsigned int i;
for(i=0;i<n;i++);
}

/*
assignment 8 - question 3

stop watch using hyperterminal and LCD

*/

#include<REG52.h>
void delay(unsigned int); //generates delay

unsigned int count=0,value=~50000;

void main(void)
{
unsigned char val,state,min,sec;
unsigned long int time;
TMOD=0x21; //timer0 in mode 1 and timer1 in mode 2
EA=1;ET0=1;
TL0=0;TH0=0;
TR0=0; //switching off timer0
SCON=0x50; //necessary for serial communication
TH1=0xfd; //set baud rate at 9600 baud
TR1=1; //switching on timer1
while(1)
{
P2=0xfe;delay(1000);val=P2;
switch(state)
{
case 0:
if(val==0xee) //row 0 column 0
{
TR0=0; //switching off timer0
state=1;
}
break;
case 1: TR1=1;
if(val==0xde) //row 0 column 1
{
state=3;
}
else if(val==0xbe) //row 0 column 2
{
state=2;
}
break;
case 2: TR1=0;
if(val==0x7e) //row 0 column 3
{
state=1;
}
break;
case 3: TR1=0;
if(val==0xee) //row 0 column 0
{
state=0;
}
break;
};
if(state==0) //init
{
count=0;TH0=0;TL0=0;
SBUF=(0xc0);while(!TI);TI=0;
SBUF=0x30;while(!TI);TI=0;
SBUF=0x30;SBUF=':';while(!TI);TI=0;
SBUF=0x30; while(!TI);TI=0;
SBUF=0x30;while(!TI);TI=0;
}
else if(state==1) // run mode
{
time=((65536*count)+(TH0*256)+TL0); //calculating total time and storing it in time
time=time/1000000; //converting time to sec
min=time/60; //calculating minutes
sec=time%60; //calculating seconds

SBUF=0xc0;while(!TI);TI=0;
SBUF=(min/10+0x30);while(!TI);TI=0; //displaying time on hyperterminal
SBUF=(min%10+0x30);while(!TI);TI=0;
SBUF=':';while(!TI);TI=0;
SBUF=(sec/10+0x30);while(!TI);TI=0;
SBUF=(sec%10+0x30);while(!TI);TI=0;
}
}
}

void timer0(void) interrupt 1 //timer0 in interrupt mode


{
count++;
}

void delay (unsigned int n)


{
unsigned int i;
for(i=0;i<n;i++);
}
/*
Assigment 9
Question 1

DOCTOR'S TOKEN DISPLAY

Connect P0 to LCD
Connect P2 lower nibble to LCD control
Connect P1 lower nibble to i2c

Input: Give the initial token number through hyperterminal


Press '+' for increment
Press '-' for decrement
Press 'b' for buzz

*/

#include<REG52.h>

sbit RS=P2^0; //pin connects to control of lcd


sbit RW=P2^1;
sbit EN=P2^2;
sbit BUZ=P1^2; // pin connected to buzzer

sbit dat=P3^6; // pin for seven segment display


sbit clk=P3^7; // pin for seven segment display

void lcdint(void); //used for lcd display


void wrt(unsigned char,bit); //used for lcd display
void delay(unsigned int); //used for lcd display
void disp(void);
void seven(unsigned char);

unsigned char welc[]="WELCOME",i,huns,tens,units,count,val,cc,j;


bit flag;

void main(void)
{
BUZ=1;
TMOD=0x21; // setting timer1 in 8-bit auto reload mode for serial comm.
SCON=0x50; // mode 1
TH1=-3; // setting baud rate =9600
TR1=1; // start timer1
TR0=0;
EA=1;
ET0=1;

lcdint(); // initialise LCD

wrt(0x80,0); // display welcome message


for(i=0;i<7;i++)
wrt(welc[i],1);

wrt(0x80,0);
for(i=0;i<7;i++)
wrt(' ',1);
wrt(0x80,0);

SBUF='T';
while(!TI);TI=0;
SBUF=':';
while(!TI);TI=0;

while(!RI);RI=0; // input the initial value of token number


huns=SBUF;
SBUF=huns;
while(!TI);TI=0;

while(!RI);RI=0;
tens=SBUF;
SBUF=tens;
while(!TI);TI=0;

while(!RI);RI=0;
units=SBUF;
SBUF=units;
while(!TI);TI=0;

huns-=0x30;
tens-=0x30;
units-=0x30;

disp();

while(1)
{
while(!RI);RI=0;
val=SBUF;
if(val=='+') // increment the token number if input is '+'
{
units++; // increment units digit
if(units>9)
{
units=0;
tens++; // if units digit exceeds 9,then make units digit 0 & increment tens digit
if(tens>9)
{
tens=0;
huns++; // if tens digit exceeds 9,then make tens digit 0 & increment huns digit
if(huns>9)
{
huns=tens=units=0;
}
}
}
wrt(0x80,0);

wrt(huns+0x30,1); // display on LCD


wrt(tens+0x30,1);
wrt(units+0x30,1);
}
else if(val=='-') // decrement the token number if input is '-'
{
units--;
if(units==255)
{
units=9;
tens--; // if units digit goes less than 0,then make units digit 9 &
decrement tens digit
if(tens==255)
{
tens=9;
huns--; // if tens digit goes less than 0,then make tens digit 9 &
decrement huns digit
if(huns==255)
{
huns=tens=units=9;
}
}
}
wrt(0x80,0);

wrt(huns+0x30,1); // display on LCD


wrt(tens+0x30,1);
wrt(units+0x30,1);
}
else if(val=='b') // buzz if input is 'b'
{
TR0=0;
TH0=0x4b;
TL0=0xfc;
TR0=1;
dat=1;
for(j=0;j<32;j++)
{
clk=0;clk=1;clk=0;
}
seven(huns); // display on seven segment display
seven(tens);
seven(units);
}
}
}

void wrt(unsigned char n,bit m) // writes data into lcd


{ // if bit m=0 it writes into command space
RS=m; // if bit m=1 it writes into data space
RW=0;
P0=n; // value of n is sent to lcd through port 1
EN=1;
EN=0;
delay(5000);
}
void delay (unsigned int n) // delay using for loop
{
for(;n>0;n--);
}
void lcdint(void) // initialising the lcd
{
wrt(0x38,0);
wrt(0x38,0);
wrt(0x38,0);
wrt(0x38,0);
wrt(0x0e,0);
wrt(0x06,0);
wrt(0x01,0);
}

void timer0(void) interrupt 1


{
TR0=0;
TH0=0x4b;
TL0=0xfc;
TR0=1;
count++;
if(count%4==0)
BUZ=~BUZ;
if(count==100)
{
count=0;
TR0=0;
BUZ=1;
}
}

void disp(void)
{
wrt(0x80,0);
wrt(huns+0x30,1);
wrt(tens+0x30,1);
wrt(units+0x30,1);
}

void seven(unsigned char n) // displaying the value on seven segment display


{
unsigned char a[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
unsigned char temp,mask,i;
temp=a[n];
mask=0x80;
for(i=0;i<8;i++)
{
if(temp&mask)
dat=1;
else
dat=0;
clk=0;clk=1;clk=0;
mask=mask>>1;
}
}

/*
Assignment 9 - Question 2

Program to control stepper motor using keyboard


1st key - start
2nd key - stop
3rd key - clockwise
4th key - anticlockwise

Connect P2 lower nibble to stepper motor driver


connect P1 to keyboard
*/

#include<REG52.h>

#define START 0 //states


#define STOP 3
#define ANTI 1
#define CLK 2
#define GET 4
unsigned char val,state;

void delay(unsigned int); //generates delay

bit run=0,antirun=0; //flags

void main(void)
{
TMOD=0x21; //timer1 in mode 2 and timer0 in mode 1
SCON=0x50; //necessary for serial communication
TH1=0xfd; //setting baud rate at 9600 baud
TR1=1; //switching on timer1
EA=1;
ET0=1;
TR0 = 0;
TH0 = 0x4B; //sets timer0 for 50 ms delay
TL0 = 0xFC;
TR0 = 1; //switching timer0 on

state = GET;
while(1)
{

switch(state)
{

case START:
run=1;
antirun=0;
state=GET;
break;
case STOP:
run=0;
antirun=0;
state=GET;
break;

case ANTI : run=0;


antirun=1;
state=GET;
break;

case CLK: run=1;


antirun=0;
state=GET;
break;
case GET: P1=0xfe;delay(1);val=P1;
switch(val)
{
case 0xee: state = START;
break;
case 0xde: state = STOP;
break;
case 0xbe: state = ANTI;
break;
case 0x7e: state = CLK;
break;
}
break;

}
}
}
void delay(unsigned int n)
{
unsigned int i,j;
for(i=0;i<n;i++)
for(j=0;j<1275;j++);
}
void timer0(void) interrupt 1 //timer0 in interrupt mode
{
TR0 = 0;
TH0 = 0x4B;
TL0 = 0xFC;
TR0 = 1; //for every 50 ms the motor runs according to the flags
if(!antirun&&run)
{
P2=0x0a; //sequence for clockwise rotation of stepper motor
delay(1);
P2=0x06;
delay(1);
P2=0x05;
delay(1);
P2=0x09;
delay(1);
}
else if(!run&&antirun)
{
P2=0x09; //sequence for anticlockwise rotation of stepper motor
delay(1);
P2=0x05;
delay(1);
P2=0x06;
delay(1);
P2=0x0a;
delay(1);
}
}

/*
Assignment 9 - Question 3

Program to control stepper motor using hyperterminal


R - start
S - stop
C - clockwise
A - anticlockwise

Connect P2 lower nibble to stepper motor driver


*/

#include<REG52.h>

#define START 0
#define STOP 3
#define ANTI 1
#define CLK 2
#define GET 4

unsigned char val,state;

void delay(unsigned int); //generates delay

bit run=0,antirun=0; //flags

void main(void)
{
TMOD=0x21; //timer1 in mode 2 and timer0 in mode 1
SCON=0x50; //necessary for serial communication
TH1=0xfd; //setting baud rate at 9600 baud
TR1=1; //switching on timer1
EA=1;
ET0=1;
TR0 = 0;
TH0 = 0x4B; //sets timer0 for 50 ms delay
TL0 = 0xFC;
TR0 = 1; //switching timer0 on

state = GET;
while(1)
{

switch(state)
{

case START:
run=1;
antirun=0;
state=GET;
break;

case STOP:
run=0;
antirun=0;
state=GET;
break;

case ANTI : run=0;


antirun=1;
state=GET;
break;

case CLK: run=1;


antirun=0;
state=GET;
break;
case GET: while(!RI);
RI=0;
val=SBUF;
SBUF = val;
while(!TI)
;
TI = 0;

switch(val)
{
case 'r': case 'R': state=START;
break;
case 's': case 'S': state = STOP;
break;
case 'a': case 'A': state = ANTI;
break;
case 'c': case 'C': state = CLK;
break;
}
break;

}
}
}

void delay(unsigned int n)


{
unsigned int i,j;
for(i=0;i<n;i++)
for(j=0;j<1275;j++);
}
void timer0(void) interrupt 1 //timer0 in interrupt mode
{
TR0 = 0;
TH0 = 0x4B;
TL0 = 0xFC;
TR0 = 1; //for every 50 ms the motor runs according to the flags
if(!antirun&&run)
{
P2=0x0a; //sequence for clockwise rotation of stepper motor
delay(1);
P2=0x06;
delay(1);
P2=0x05;
delay(1);
P2=0x09;
delay(1);
}
else if(!run&&antirun)
{
P2=0x09; //sequence for anticlockwise rotation of stepper motor
delay(1);
P2=0x05;
delay(1);
P2=0x06;
delay(1);
P2=0x0a;
delay(1);
}
}
/*
Assignment 10
Question 1
LED TEST BED

*/
#include<REG52.H>

#define INIT 0 // states


#define DECODE_PACK 1
#define WAIT_PACK 2
#define START 3
#define STOP 4

void get(void);

unsigned int No_blinks, No_blinks_ind, On_time;


unsigned int On_time_ind = 0x000, Off_time = 0x0001, Off_time_ind = 0x0000;
unsigned char Led = 0x01, State = 0x00, Buf[5], Loops = 0x00, Index = 0x00;
bit Mode = 0, flag = 0;

void main(void) {

unsigned char *p, i;

State = INIT; // set initial state


while(1)
{
switch(State)
{
case INIT: TMOD = 0x21; // timer1 in 8 bit auto reload mode and timer 0 in mode 1
IE = 0x92; // interrupt enable
SCON = 0x50; // condition for using hyperterminal
TH1 = -3; // setting baud rate = 9600
TR1 = 1;
P0 = 0x00;
No_blinks = 20; // default values
No_blinks_ind = 0x0000;
On_time = 0x0001;
On_time_ind = 0x0000;
Off_time = 0x0001;
Off_time_ind = 0x0000;
State = WAIT_PACK;
break;

case WAIT_PACK: break; // hold state

case DECODE_PACK: p = Buf;


switch(*p) {
case 's': case 'S': State = START;
break;
case 't': case 'T': State = STOP;
break;
case 'n': case 'N': No_blinks = 0x0000; // reads the packet and stores the value into no_blinks
for(++p, i = 0x00; i < 4; ++p, ++i) {
if(*p >= '0' && *p <= '9') {
No_blinks *= 10;
No_blinks += (*p - '0');
}
}
State = WAIT_PACK;
break;
case 'o': case 'O': On_time = 0x0000; // reads the packet and stores the value into on time
for(++p, i = 0x00; i < 4; ++p, ++i) {
if(*p >= '0' && *p <= '9') {
On_time *= 10;
On_time += (*p - '0');
}
}
State = WAIT_PACK;
break;
case 'f': case 'F': Off_time = 0x0000; // reads the packet and stores the value into off time
for(++p, i = 0x00; i < 4; ++p, ++i) {
if(*p >= '0' && *p <= '9') {
Off_time *= 10;
Off_time += (*p - '0');
}
}
State = WAIT_PACK;
break;
case 'l': case 'L': ++p; // reads the packet and stores the led no into led
if(*p >= '1' && *p <= '8') {
Led = 0x01;
Led <<= (*p - '1') ;
}
State = WAIT_PACK;
break;
case 'm': case 'M': ++p; // set the mode
if(*p == '1')
Mode = 1;
else if(*p == '0')
Mode = 0;
State = WAIT_PACK;
break;
}

break;

case START: P0 = Led; // led is switched on


TH0 = 0x4B; // sets timer0 for 50 ms interrupt
TL0 = 0xFC;
TR0 = 1;
State = WAIT_PACK;
break;

case STOP: P0 = 0x00; // led is switched off and timer0 is turned off
TR0 = 0; // default values are set
No_blinks = 20;
No_blinks_ind = 0x0000;
On_time = 0x0001;
On_time_ind = 0x0000;
Off_time = 0x0001;
Led = 0x01;
Mode = 0;
State = WAIT_PACK;
break;
}
}
}

void timer0_int(void) interrupt 1


{

TR0 = 0;
TH0 = 0x4B;
TL0 = 0xFC;
TR0 = 1;
++Loops; // loops keeps a count of every 50 ms
if(Loops == 20) { // indicates one sec has reached
Loops = 0x00;
get();
}
}

void get(void)
{

if(!Mode || (Mode && (No_blinks_ind < No_blinks))) {


if(!flag) { // if flag bit is 0 then led is on
++On_time_ind; // keeps a count of the on time
if(On_time_ind >= On_time) {
P0 = 0x00;
On_time_ind = 0x0000;
flag ^= 1;
}
}
else { // flag bit is 1 then led is off
++Off_time_ind; // keeps a count of the off time
if(Off_time_ind >= Off_time) {

Off_time_ind = 0x0000;
++No_blinks_ind; // keeps a count of the no blinks
if(Mode && No_blinks_ind >= No_blinks)
P0 = 0x00; // led is switched off when off time is reached
else
P0 = Led;
flag ^= 1;
}
}
}
else { // set to initial values
TR0 = 0;
No_blinks = 20;
No_blinks_ind = 0x0000;
On_time = 0x0001;
On_time_ind = 0x0000;
Off_time = 0x0001;
Led = 0x01;
Mode = 0;
}
}

void ser_int(void) interrupt 4 // timer 1 in interrupt mode


{ // as we use packet based system we have to wait for Z condition

if(RI) {
RI = 0;
Buf[Index] = SBUF;
if(Buf[Index] == 'z' || Buf[Index] == 'Z') {
State = DECODE_PACK;
Index = 0x00;
}
else
++Index;
}
if(TI)
TI = 0;
}

/*
Assignment 10 - Question 2

Program to control stepper motor using GUI

Connect P2 lower nibble to stepper motor driver

start acts as continue


*/

#include<REG52.h>

#define START 0
#define STOP 3
#define ANTI 1
#define CLK 2
#define GET 4
#define PAUSE 5

unsigned char val,state,a,temp,no[4],k=0;


unsigned int steps;

void delay(unsigned int); //generates delay

bit run=0,antirun=0,bittu=0; //flags

void main(void)
{
TMOD=0x21; //timer1 in mode 2 and timer0 in mode 1
SCON=0x50; //necessary for serial communication
TH1=0xfd; //setting baud rate at 9600 baud
TR1=1; //switching on timer1
EA=1;
ET0=1;
TR0 = 0;
TH0 = 0x4B; //sets timer0 for 50 ms delay
TL0 = 0xFC;
TR0 = 1; //switching timer0 on

state = GET;
while(1)
{

switch(state)
{
case START:
run=1; //flag conditions for the motor to run(by default in clockwise
direction)
antirun=0;
bittu=1;
state=GET;
break;

case STOP: //flag conditions for the motor to stop


run=0;
antirun=0;
bittu=0;
steps=0;
state=GET;
break;

case ANTI : run=0; //flag conditions for the motor to run anticlockwise
antirun=1;
state=GET;
break;

case CLK: run=1; //flag conditions for the motor to run clockwise
antirun=0;
state=GET;
break;
case PAUSE: run=0; //flag conditions for the motor to pause
antirun=0;
bittu=0;
state=GET;
break;

case GET: while(!RI);


RI=0;
val=SBUF;
SBUF = val;
while(!TI)
;
TI = 0;
a=0;
switch(val) {
case 'r': case 'R':
while(!(a=='z'))
{ while(!RI);
RI=0;
a=SBUF;}

state=START;
break;
case 's': case 'S': while(!(a=='z'))
{while(!RI);
RI=0;
a=SBUF;}
state = STOP;
break;
case 'a': case 'A': while(!(a=='z'))
{while(!RI);
RI=0;
a=SBUF;}

state = ANTI;
break;
case 'c': case 'C': while(!(a=='z'))
{while(!RI);
RI=0;
a=SBUF;}
state = CLK;
break;
case 'n': case 'N':while(!(a=='d'))
{
while(!RI);
RI=0;
no[k]=SBUF;
no[k]=no[k]-0x30;
k++;
if(k==3){ a='d';}
}
steps=(no[0]*100)+(no[1]*10)+(no[2]*1);
k=0;
while(!(a=='z'))
{
while(!RI);
RI=0;
a=SBUF;
}
state=GET;
break;
case 't': case 'T': while(!(a=='z'))
{
while(!RI);
RI=0;
a=SBUF;
}
state = PAUSE;
break;
case 'm': case 'M':while(!(a=='z')) //setting the mode(continuous or steps)
{
while(!RI);
RI=0;
a=SBUF;
if(a==0x31)
{steps=60000;}

}
state=GET;
break;
}

break;}

}
}

void delay(unsigned int n)


{
unsigned int i,j;
for(i=0;i<n;i++)
for(j=0;j<1275;j++);
}
void timer0(void) interrupt 1
{
TR0 = 0;
TH0 = 0x4B;
TL0 = 0xFC;
TR0 = 1;
if(steps==0)
{
run=0;
antirun=0;
state=STOP;
}
if(!antirun&&run&&bittu)
{
P2=0x0a;
delay(1);
P2=0x06;
delay(1);
P2=0x05;
delay(1);
P2=0x09;
delay(1);
steps--;
}
else if(!run&&antirun&&bittu)
{
P2=0x09;
delay(1);
P2=0x05;
delay(1);
P2=0x06;
delay(1);
P2=0x0a;
delay(1);
steps--;
}
}

S-ar putea să vă placă și