Documente Academic
Documente Profesional
Documente Cultură
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
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
#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
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
}
}
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
#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
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
/*
assignment 5 - question 4,question 5 and question 6 combined
#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
TR0=0;TH0=0;TL0=0;
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);
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;
}
}
/*
assignment 5 - question 4,question 5 and question 6 combined
#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
TR0=0;TH0=0;TL0=0;
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);
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;
}
}
/*
assignement 6 question 1
#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
#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
}
}
for(;n;--n);
}
#include<REG52.h>
void polsec(unsigned char); //generates one sec delay using timer0 in polling mode
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);
}
/*
assignment 6 - question 4
#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>
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>
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
*/
#include<REG52.h>
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>
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)
{
*/
#include<REG52.h>
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
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);
}
}
/*
assignment 8 - question 2
*/
#include<REG52.h>
sbit RS=P2^0;
sbit RW=P2^1;
sbit EN=P2^2;
/*
assignment 8 - question 3
*/
#include<REG52.h>
void delay(unsigned int); //generates delay
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;
}
}
}
Connect P0 to LCD
Connect P2 lower nibble to LCD control
Connect P1 lower nibble to i2c
*/
#include<REG52.h>
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;
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;
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);
void disp(void)
{
wrt(0x80,0);
wrt(huns+0x30,1);
wrt(tens+0x30,1);
wrt(units+0x30,1);
}
/*
Assignment 9 - Question 2
#include<REG52.h>
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;
}
}
}
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
#include<REG52.h>
#define START 0
#define STOP 3
#define ANTI 1
#define CLK 2
#define GET 4
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;
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;
}
}
}
*/
#include<REG52.H>
void get(void);
void main(void) {
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;
}
}
}
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)
{
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;
}
}
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
#include<REG52.h>
#define START 0
#define STOP 3
#define ANTI 1
#define CLK 2
#define GET 4
#define PAUSE 5
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 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;
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;}
}
}