You are on page 1of 34

These are the rules or procedures to be followed in coding and testing the assignments.

first learn the sequence of compiling on the keil software.

after the code is properly compiled without any errors try and test it on the simulator.

Give a new project name for every assignment. carryout the test on the target board.

be familiar to make the correct connections on the board,down load the code and test them.

please practice c code bracketing with indentation to make the code readable and debuggable.

understand the use of watch variable window./disassembly view.use functions as much as possible.

be stringent in declaring variables. understand the use of reg52.h carryout the excercises and submit the
softcopy(optional) of the answers at the end of the course.or mail to sharma.ramgopal@gmail.com

Tools needed for microcontroller based development

integrated development environment for application software development.

compiler,debugger,simulator etc.these are cross platform tools and facilitate simulation only.

The developed code needs to be downloaded and tested on the target microcontroller.

Downloding means writing in to the code memory of the microcontroller in the target board.

This is called a programmer. Ther are stand alone programmers and in system programmers.(ISP).

ISP's are more useful as the the newer packaging implies " solder first,code next" as these are IC
baseless.

There are many different types of programmers available on the net.

The basic difference is their interface with the PC. Basically, there are three types of programmers.

Serial Port based, Parallel Port based, USB Port based.

Of these serial port based and parallel port based programmers are easy to make in minimal cost. As the
serials and parallel ports are very old technologies they are fast disappearing from PCs.

Most laptops are not having them and even desktops are dropping them. Therefore, its no surprise that
your PC does not has serial ports. The future is USB. so you should have a usp isp programmer.
In this course we use a usb based programmer and the serial port is also usb basedwithg appropriate
drivers installed in the pc or laptop.

we provide the minimal and most useful data as handouts and the ppts are available in a dvd

which you can transfer to PC/laptop using a usb based pendrive.

89S52 pin diagram. Important and useful.


Lower 128 bytes of internal RAM
We now look at c programs for laboratory excercises.

Lab Assignment no. 1

1. Write a c program to do the following


a. A=N1+N2
b. B=N1-N2
c. C=N1*N2
d. D=N1/N2
e. E=N1%N2
f. GCF of N1 AND N2
g. LCM of N1 AND N2
Where N1 and N2 are two unsigned characters, assign initial values for N1 and
N2. Test in simulator .
2. Repeat problem number 1 where,
N1 is in PORT1
N2 is in PORT2
N3 is O/P to PORT3
N0 is an input which identifies the output function to be performed and test it in the
simulator.
3. Write a C program to switching ON and OFF individual LEDs. with software

delay. (use the eight independent LEDS.


4. Write a C program Blink one LED with S/W delay.
5. write a program to blink individual led’s and make the
blinking led to move left to right and right to left continuously.

/*

question 1 and question 2 combined

P2 decides the operations +,-,*,/,%,GCD,LCM

P0 and P1 are the inputs P3 is the output

*/

#include<REG52.h>

unsigned char hcf(unsigned char,unsigned char);

void main(void)

{ while(1)

{ unsigned char n1,n2;

P0=0xff; P1=0xff; P2=0xff; n1=P0; n2=P1;

switch(P2)

{ case 0: P3=n1+n2; break;


case 1: if(n1>n2) P3=n1-n2; else P3=n2-n1; break;

case 2: P3=n1*n2; break;

case 3: if(n2!=0) P3=n1/n2; break;

case 4: if(n2!=0) P3=n1%n2; break;

case 5: P3=hcf(n1,n2); break;

case 6: P3=(n1*n2)/hcf(n1,n2); break;

} end of switch

} end of while

} end of main

unsigned char hcf(unsigned char x,unsigned char y)

{ unsigned char z,temp;

if(x<y) //if x<y then swap x and y

{ temp=x;x=y; y=temp; }

while((x%y)!=0) //using euler's method

{ z=x%y; x=y; y=z; }

return(z);

Code snippet: swap two 8 bit unsigned character variables without using a third variable

And without using addition or subtraction.


Assigment 1Question 3 switching on individual LEDs with a software delay

*/

#include<REG52.h>

void main (void)

{ unsigned char j,temp; unsigned int i;

while(1) { temp=0x01; for (j=0;j<8;j++){ P0=temp; //sending the output through port 0 to LEDs

for (i=0;i<30000;i++); temp=temp<<1; //left shifting to the next LED

} // end of first for loop

}// end of while

}// end of main

Assigment 1Question 5 switching on and off individual LEDs and making it move in both directions
continuouslyP0 is the output which is connected to the LEDs */

#include<REG52.h>

void main(void)

{ unsigned char i; unsigned int j;

while(1){for(i=0x01;i<=0x80;i<<=1) //moving to the left

{P0=i;

for (j=0;j<10000;j++); //delay

for(i=0x80;i>=0x01;i>>=1) //moving to the right

{P0=i;

for (j=0;j<10000;j++); //delay }

}// end of while

} // end of main
Exercise: change unsigned char i to char i and observe the result and explain the reason.

Lab Assignment no. 2

1. Write a C program to count in BCD from 00 to 99.(use the 2 digit


BCD decoded 7 segment display as the output.
2. Write a C program to down count in BCD from 99 to 00.
3. Write a C program for the following.
N is taken from PORT1 through DIP switch.
a. read the value of the dip switch and display it on the individual leds.
b. Take the least significant portion on the dip switch and assign it
To a variable max. write a code to binary count from 00 to max
With software delay.
c. write a code to bcd count from max to 00With software delay.
4. Write a C program to
a. Up count BCD from 0 to N and display on 2 digit dispaly
b. Down count from N to 0 and display on 2 digit dispaly
c. 0 to N and N to 0 Repeat the process.
Where N is taken from PORT1 through DIP switch
5 Write a C program to
a. display all odd numbers between 0 to N continuosly
b display all even numbers between 0 to N continuously
Where N is taken from PORT1 through DIP switch
*Assignment 2Question 1 to count in BCD from 00 to 99 and displayed on 2digit BCD decoded display

*/

#include<REG52.h>

unsigned char binbcd(unsigned char);

void main(void)

{ unsigned int j; unsigned char i;

while(1)

{ for(i=0;i<=99;i++) //to count from 00 to 99

{ P0=binbcd(i); for(j=0;j<=60000;j++);//delay }

}// end of while

}// end of main

unsigned char binbcd(unsigned char n) //converts binary to BCD

{ unsigned char u,t;

u=n%10; t=n/10; t=t<<4; t=t+u; return(t);} // end of function


Assignment 2question 3 to count from 00 to the least significant portion of the dip switch

and display it on the 2 digit BCD decoded 7 segment display

#include<REG52.h>

unsigned char binbcd(unsigned char);

void main(void)

{ unsigned char i,k,h; unsigned int j; P2=0xff;

while(1)

{ i=0x0f&P2; //masking the more significant portion of the port2

for(k=0;k<=i;k++) //value taken through the dip switch

{P0=binbcd(k); for(j=0;j<=10000;j++);}

for(h=i;h>0;h--)

{P0=binbcd(h); for(j=0;j<=10000;j++); }

unsigned char binbcd(unsigned char n) //converts binary to BCD

unsigned char u,t;

u=n%10; t=n/10; t=t<<4; t=t+u; return(t);}


Lab Assignment no. 3

1. Write a C program to clear the 7 segment serial display.


2. Write a C program to make a walking one segment from Segment 0 to segment
15.
3. Write a C program to count 0 to F in the first digit.
4. Write a C program to count and show it on the serial display.
a. 0 to 99 BCD counter
b. 0 to FF binary counter.
c. Up Down counter BCD.

5. Write a C program to
a. upcount binary 0 to N and display on serial display continuously
b downcount binary N to 0 and display on serial display continuously
Where N is taken from PORT1 through DIP switch
Assignment 3Question 2 program to make a walking segment from segment 0 to segment 16

#include<REG52.h>

sbit dat=P3^6; // these pins are connected to the serial display

sbit clk=P3^7;

void main(void)

{unsigned char i,y; unsigned int h;

while(1){ dat=1; for(y=0;y<16;y++) //to clear all the segments

{ clk=0;clk=1;clk=0;} dat=0;clk=0;clk=1;clk=0;

for (h=0;h<10000;h++); // Delay

dat=1;

for(i=0;i<16;i++) //to make the segment walk

{ clk=0;clk=1;clk=0; for (h=0;h<10000;h++); } //Delay

}// end of while

}// end of main


Assignment 3Question 6 upcount binary 0 to N and display on serial display

#include <REG52.h>

sbit dat=P3^6;sbit clk=P3^7;void seven(unsigned char);

unsigned char []={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};

// these the corresponding codes for 0 to F which has to be sent to the serial display

void main (void)

{ unsigned char i, lowernibble, uppernibble ,temp;

unsigned int j;

P2=0xff; temp=P2;

while (1) { for(h=0x00;h<=temp;h++)

{ lowernibble=h&0x0f; upperniblle=h&0xf0;uppernibble=uppernibble>>4;

dat=1; for(j=0;j<32;j++){clk=0;clk=1;clk=0;} // clears the display

seven(a[uppernibble]); seven(a[lowernibble]);

for (j=0;j<30000;j++); }// end first for loop

} //end of while

}// end of main

void seven(unsigned char n) //to display a single digit on the serial display

{ unsigned char temp,mask,i;

temp=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;} // end of for loop

}// end of function


Lab Assignment no. 4

1. Write software to say "GOOD", "BAD" and "Ugly" on the LCD.


2 Write a C program to count and show it on the LCD display.
d. 0 to 9999 BCD counter
e. 0 to FFFF binary counter.
f. Up Down counter BCD.

3. Write a C program to recognize the keyboard and display the key value on
a.7-segment display.
b. serial display
c. LCD
And generate a beep sound whenever a key is pressed.
4. Write a C program that different names on the LCD for
Different keys. For ex: for key1 hello
For ex: for key2 world etc
Assignment 4 question 1, good bad ugly on lcd

include<REG52.h>

sbit RS=P2^0;sbit RW=P2^1;sbit EN=P2^2;void wrt(unsigned char, unsigned char);

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

void main(void)

{ unsigned char a[]="GOOD BAD Ugly",count;

lcdint();

while(1)

{ for(count=0;count<=12;count++)

{ wrt(a[count],1); delay(50000); }

while(1);// EXECUTE ONLY ONCE

}// //end of main

void lcdint(void)

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

void wrt(unsigned char n,unsigned char m)

{ RS=m; RW=0; P1=n; EN=1; EN=0; delay(30000);}

void delay (unsigned int n)

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


Assignment 4 Question 2a 0 to 9999 BCD counter on LCD

#include<REG52.h>

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

void wrt(unsigned char,unsigned char);void delay(unsigned int);void lcdint(void);

void main(void)

{ int i,j,k,l,temp

Unsigned char buffer[10];

lcdint();// initialising the lcd

while(1) { for(i=0;i<=9999;i++)

{ wrt(0x80,0); places the cursor on the first line

temp=i;j=0;
while(temp){buffer[j]=temp%10;buffer[j]=buffer[j]+0x30;temp=temp/10;j++;}

j--; while(j>0){wrt(buffer(j,1));j--}

}// end of first for

} // end of while

} // end of main

void wrt(unsigned char n,unsigned char m) // write n to command area if m=0 else data

{ RS=m; RW=0; P1=n; EN=1; EN=0; delay(5000);EN=1;}

void delay (unsigned int n) // delay

{ for(;n>0;n--);}

void lcdint(void) // used to initialise the LCD sequence should be written into the command space

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

Exercise: understand the different modes of lcd viz, left entry,right entry,inc,dec etc.
Assignment 4 question 3a. Recognize a 4x4 keypad and display on bcd display.

P1 should be connected to the keyboard P2 should be connected to the 7 segment display

#include<REG52.h>

sbit buzz=P0^6;void beep(void);void delay(unsigned int);

unsigned char excite[4]={0xfe,0xfd,0xfb,0xf7};

unsigned char lookup[16]={0xee,0xed,0xeb,0xe7,0xde,0xdd,0xdb,0xd7,

0xbe,0xbd,0xbb,0xb7,0x7e,0x7d,0x7b,0x77};

void main( void )

{ unsigned char value,key,i,j,units,tens,keycode;

while(1) { for (i=0;i<4;i++)

{ P1=excite[i]; delay(5000); value=P1;

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

{ if (value==lookup[j]) break; }

if (i<16)key=j;else key=0xff;

units =key%10;tens=key/10; tens=tens<<4;keycode=units+tens;P2=keycode;

beep();

}//end of first for

}//end of while

}// end of main

void beep(void) // beeps for a delay of time

{ buzz=1; delay(20000); buzz=0;}

void delay (unsigned int n) // delay

{ for(;n>0;n--);}
Lab Assignment no. 5

1. Design a BCD counter which increments every one-second starting from 00


up to 99. When it reaches 99 it should stop. Use timer in the polling mode to
generate one-second delay. Measure actual time elapsed.
2. Design a BCD counter which increments every one-second starting from 00
up to 99. When it reaches 99 it should stop. Use timer in the interrupt mode to
generate one-second delay. Measure actual time elapsed.
3. Design a method measure the time taken for a loop and display
the for loop number and the time taken on the LCD.
4. Measure the ON/OFF time of the 555 oscillator on the board and display the
value on the LCD .
5. Measure the frequency of the 555 oscillator and display on the LCD.
6. Assuming that the 555 oscillators output is from a shaft encoder of 200 pulses
per one rotation, calculate RPM and display on LCD.

*
assignment 5 - question 1 .program a BCD 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. Count value on 1st line on lcd and elapsed time in the 2nd line od
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;

long totaltime;

void main(void)

{ unsigned char i,j, buffer[10],units,tens;

Unsigned long temp;totaltime=0;

lcdint(); TMOD=0x11;//both the timers are used in mode 1

EA=1;ET1=1; TR1=0; TL1=0; TH1=0; TR1=1;

for(i=0;i<=99;i++) //the for loops are to generate the numbers from 00 to 99

{ temp=i:units=i%10+0x30;tens=i/10+0x30; wrt(0x80,0);wrt(tens,1);write(units,1);

polsec(1); //generates one-second delay

TR1=0;/stop elapsed time counter.

totaltime=(65536*count)+(256*TH1)+TL1;//to calculate the total time in micro sec


temp=totaltime; j=0

while(temp){buffer[j]=temp%10;buffer[j]=buffer[j]+0x30;temp=temp/10;j++;}

j--; while(j>0){wrt(buffer(j,1));j--}
while(1); // stops after 99

void polsec()

{ unsigned int value,i; value=~50000; TMOD=0x11;

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

{ TR0=0;TF0=0; TL0=value%256;TH0=value/256; //taking the lower and upper bytes of value

TR0=1; while(!TF0); TR0=0; }

}// end of function

Vod timer1 (void) interrupt3

{totaltime++}

Exercise: change starting and ending values. Also increase or decrease time delay values.

*
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

the coded is same as above but for change in polsec() functionplus one more interruot
generated by timer0 also. Add another global variable called as count1.

void polsec()

{ value=~50000; TMOD=0x11; count1=0;

TL0=value%256;TH0=value/256; //taking the lower and upper bytes of value

while (count1,<0){}

TR0=0;} // end of function

Void timer0 (void) interrupt 1

{ TR0=0; TL0=value%256;TH0=value/256; //taking the lower and upper bytes of value

TR0=1; count1++; }
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;

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); while(!IN); // get rising edge

TR0=1; //timer0 is switched on

while(IN); //measuring the time while t

TR0=0; //switching timer0 off

ontime=(TH0*256)+TL0; //storing the time elapsed in ontime(in micro seconds)

TH0=0;TL0=0; while(!IN); while(IN); //get falling edge

TR0=1; while(!IN);TR0=0; offtime=(TH0*256)+TL0;

Total time=ontime+offtime; freq=10^6/totaltime;

Display (totaltime);display(freq);

Exercise: there are four methods to measure frequency: (a) shown above.(b) frequency is number of
rising edges per second. Use timer to generate one second delay and count the rising edges within that
time.

©; total time is the time betweek two consecutive rising edges. Count time using timer.

(d) connect the input to an interrupt( make it falling edge sensitive), then count the nuber of interrupts
in one second.
Lab Assignment no. 6

1. Send a character and receive a character to/from hyper terminal.


2. Read a character from hyper terminal and display it on LCD.

3.Design a BCD counter which increments every one-second starting from 00 up


to 99.The counters value should be shown on the hyper terminal. When it reaches
99 it should stop. Use timer in the polling mode mode to generate one-second
delay. Measure actual time elapsed.
4.Design a BCD counter which increments every one-second starting from 00 up
to 99. When it reaches 99 it should stop. Use timer in the interrupt mode to
generate one-second delay. The counters value should be shown on the hyper
terminal.
5.Design a method measure the time taken for a loop and display
the for loop number and the time taken on the hyperterminal.
6.Measure the ON/OFF time of the 555 oscillator on the board and display the
value on the hyperterminal. (PC)
7Measure the frequency of the 555 oscillator and display on the PC.
8 .Assuming that the 555 oscillators output is from a shaft encoder of 200 pulses
per one rotation, calculate RPM and display on PC.
9. All the above eight problems have to be tested in polling method for the serial
port and also using serial interrupt.
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

Ecxercise: read a character and send back a character in the serial interrupt mode.
Lab Assignment no. 7

1. Use the (ADC) ICL7135 to measure the input voltage and show it
on LCD.
2. Write a code to generate a ramp using the DAC (DAC0800).
3. Generate analog signal using DAC and Read it from ADC and
display it on LCD.

Lab Assignment no. 8


1. Design single set point temperature controller using temperature sensor
(LM35), ADC and Solid State relay to turn ON/OFF the heater (or Bulb) to
maintain desired temperature. Use the LCD module to show set point , measured
temperature and heater on/off continuosly.

2. Write a C program for a stopwatch using keyboard and LCD.


- First key of first rows for START the watch.
- Second key of the first row to STOP the watch.
- Third key of the first row to PAUSE.
- Fourth key of the first row to continue.
Output should display time in Minutes and Seconds on the LCD.
Use timer for time delay in the polling mode. Wait for TF0 flag to set or use
timer in the interrupt mode.

3. Write a C program for a stopwatch using hyperterminal and LCD.


- S START the watch.- E STOP the watch.- P PAUSE.- C to continue.Output should
display time in Minutes and Seconds on the PC.
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 rising edge
while(!busy); /
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; RW=0;P1=n; EN=1; EN=0; delay(5000);EN=1;
}
void delay (unsigned int n)
{ unsigned int i; for(i=0;i<n;i++);}
void lcdint(void)
{ 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

While(1)

{ measureadcvalue();Decide();Display()}// end of while

====================================================================================
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);

void display(unsigned int);void lcdint(void); unsigned int count=0,value=~50000;

long time;

void main(void)

{ unsigned char val,state,min,sec;state=0; unsigned long int time,ms;

time=0;TMOD=0x10; 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: TR0=0; if(val==0xee)state=1;break;

case 1: TR1=1; if(val==0xde) state=3;

if(val==0xbe) state=2;break;

case 2: TR1=0; if (val=0xee){time=0;state =1;}break;

case3:if(val==0x7e) state=1;break; }// end of switch

display(time);} // end of while

} // end of main

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

{TR1=0;time++;TH1=value/256;TL1=value%256; TR1=1;}
. Write a C program for a stopwatch using hyperterminal and LCD.
- S START the watch.- E STOP the watch.- P PAUSE.- C to continue.Output should
display time in Minutes and Seconds on the PC.

Implement the following:

Let every character from the hyperterminal interrupt the microcontroller.

In the interrupt service function interpret the character. Interpretation means

Modifying the value of a global variable which will control the time or the state of
the machine. The state controls the behavior of the machine.