B M S Institute of Technology Yelahanka, Bangalore-64: Laboratory Manual
B M S Institute of Technology Yelahanka, Bangalore-64: Laboratory Manual
B M S Institute of Technology
Yelahanka, Bangalore-64
IV SEMESTER
LABORATORY MANUAL
BRANCH :
BATCH :
PEO2 Demonstrate ability to work in multidisciplinary teams and engage in lifelong learning.
After the successful completion of the course, the graduate will be able to,
CO-PO MAPPING
CO/PO PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12
CO1 3 3 3 3 3 3
10ESL47 CO2 3 3 3 3 3 3
CO3 3 3 3 3 3 3
Course Outcomes
At the end of the course, students will be able to,
CO1 Write assembly programs and run simulation to carryout data transfer, arithmetic, logical
and code conversion programs for 8051 microcontroller
CO2 Write C-programs and interface various modules to 8051 microcontroller and conduct
experiments.
CO3 Write assembly programs and run simulation to carryout data transfer, arithmetic, logical
programs forMSP430 microcontroller
CONTENTS
PAGE
Sl.
Content NO.
No.
Introduction and microcontroller basics 7
VTU SYLLABUS
I. PROGRAMMING
1. Data Transfer - Block move, Exchange, Sorting, Finding largest element in an array.
2. Arithmetic Instructions - Addition/subtraction, multiplication and division, square, Cube
– (16 bits Arithmetic operations – bit addressable).
3. Counters.
4. Boolean & Logical Instructions (Bit manipulations).
5. Conditional CALL & RETURN.
6. Code conversion: BCD – ASCII; ASCII – Decimal; Decimal - ASCII; HEX - Decimal
and Decimal -HEX .
7. Programs to generate delay, Programs using serial port and on-Chip timer / counter.
II. INTERFACING:
Write C programs to interface 8051 chip to Interfacing modules to develop single chip
solutions.
8. Simple Calculator using 6 digit seven segment displays and Hex Keyboard interface to
8051.
9. Alphanumeric LCD panel and Hex keypad input interface to 8051.
10. External ADC and Temperature control interface to 8051.
11. Generate different waveforms Sine, Square, Triangular, Ramp etc. using DAC interface
to 8051; change the frequency and amplitude.
Microcontroller Basics
The Intel 8051 is an 8-bit microcontroller which means that most available
operations are limited to 8 bits. All the ICs from a generic model in general are
called 8051 because they can all be programmed using 8051 assembly
language, and they all share certain features (although the different models all
have their own special features).
Some of the features that have made the 8051 popular are:
8051 models may also have a number of special, model-specific features, such
as UARTs, ADC, OpAmps, etc...
Typical applications
8051 chips are used in a wide variety of control systems, telecom applications,
robotics as well as in the automotive industry. By some estimations, 8051
family chips make up over 50% of the embedded chip market.
Basic Pins
PIN 9: PIN 9 is the reset pin which is used reset the microcontroller’s internal
registers and ports upon starting up.
PINS 18 & 19: The 8051 has a built-in oscillator amplifier hence we need to
only connect a crystal at these pins to provide clock pulses to the circuit.
PIN 40 and 20: Pins 40 and 20 are VCC and ground respectively. The 8051
chip needs +5V 500mA to function properly, although there are lower powered
versions like the Atmel 2051 which is a scaled down version of the 8051 which
runs on +3V.
PINS 29, 30 & 31: As described in the features of the 8051, this chip contains
a built-in flash memory. In order to program this we need to supply a voltage
of +12V at pin 31. If external memory is connected then PIN 31, also called
EA/VPP, should be connected to ground to indicate the presence of external
memory. PIN 30 is called ALE (address latch enable), which is used when
multiple memory chips are connected to the controller and only one of them
needs to be selected. We will deal with this in depth in the later chapters. PIN
29 is called PSEN. This is "program select enable". In order to use the external
memory it is required to provide the low voltage (0) on both PSEN and EA
pins.
Ports
PORT P3 (Pins 10 to 17): PORT P3 acts as a normal IO port, but Port P3 has
additional functions such as, serial transmit and receive pins, 2 external
interrupt pins, 2 external counter inputs, read and write pins for memory
access.
PORT P0 (pins 32 to 39) PORT P0 can be used as a general purpose 8 bit port
when no external memory is present, but if external memory access is required
then PORT P0 acts as a multiplexed address and data bus that can be used to
access external memory in conjunction with PORT P2. P0 acts as AD0-AD7.
Oscillator Circuits
The 8051 requires the existence of an external oscillator circuit. The oscillator
circuit usually runs around 12MHz, although the 8051 (depending on which
specific model) is capable of running at a maximum of 40MHz. Each machine
cycle in the 8051 is 12 clock cycles, giving an effective cycle rate at 1MHz
(for a 12KHz clock) to 3.33MHz (for the maximum 40MHz clock).
The 8051 starts executing program instructions from address 0x00 in the
program memory.
Direct Memory
The 8051 has 256 bytes of internal addressable RAM, although only the first
128 bytes are available for general use by the programmer. The first 128 bytes
of RAM (from 0x00 to 0x7F) are called the Direct Memory, and can be used
to store data.
A and B Registers
The A register is located in the SFR at memory location 0xE0. The A register
works in a similar fashion to the AX register of x86 processors. The A register
is called the accumulator, and by default it receives the result of all arithmetic
operations. The B register is used in a similar manner, except that it can
receive the extended answers from the multiply and divide operations. When
not being used for multiplication and Division, the B register is available as an
extra general-purpose register.
Introduction
Microcontroller used is Atmel AT89S8252
8051 based Full Static CMOS controller with Three-Level Program, Memory
Lock, 32 I/O lines, 3 Timers/Counters, 9 Interrupts Sources, SPI, Watchdog
Timer, 2 DPTRs, 8K Flash Memory, 2k EEPROM, 256 Bytes On-chip RAM
3. Start Project – New Project, and select the CPU from the device database (Database-
Atmel- AT89S8252).On clicking ‘OK’, the following option is displayed. Choose No.
4. Create a source file (using File->New), type in the assembly or C program and save this
(filename.asm / filename.c) and add this source file to the project using either one of the
following two methods. (i) Project-Components, Environment and Books->add files->
browse to the required file -> OK “OR”
(ii) right click on the Source Group in the Project Window and the Add Files to Group
option.
5. Build the project; using Project -> Build Project. Vision translates all the user
application and links. Any errors in the code are indicated by – “Target not created” in the
Build window, along with the error line. Debug the errors. After an error free build, go to
debug mode
6. Now user can enter into Debug mode with Debug- Start / Stop Debug session dialog or
by clicking in the icon.
7. The program is run using the Debug-Run command & halted using Debug-Stop
Running. Also the (reset, run, halt) icons can be used. Additional icons are
8. If it is an interface program the outputs can be seen on the LCD, CRO, motor, led status,
etc. If it is a part A program, the appropriate memory window is opened using View ->
memory window (for data RAM & XRAM locations), Watch window (for timer program),
serial window, etc.
Note: To access data RAM area type address as D:0020h.
Similarly to access the code region, say 1000h location type in C:1000H.
AIM : To write an assembly language program to transfer n =10 bytes of data from
location 8035h to location 8041h (without overlap).
APPARATUS REQUIRED: 8051 board with 89S8252 microcontroller, PC loaded with
Keil Micro vision 3.
PROCEDURE: 1.Program is edited and is compiled with Keil compiler.
2. After successful compilation, debugging is started.
3. The memory window is accessed and the input data are entered and the output is
seen as shown in the result.
ORG 0000H
SJMP 30H
ORG 30H
MOV DPH,#80H
MOV R0,#35H //source address
MOV R1,#41H //destination address
MOV R3,#05H //count
BACK: MOV DPL, r0
MOVX A,@dptr
MOV DPL, R1
MOVX @dptr,A
INC R0
INC R1
DJNZ R3, BACK
HERE: SJMP HERE
END
RESULT:
Before Execution: 10 locations X:8035h are filled up with data.
After Execution: 10 locations X:8041h are filled up with data from 8035h.
Algorithm
1. Initialize registers to hold count data & also the source & destination addresses.
2. Get data from source location into accumulator and transfer to the destination
location.
3. Decrement the count register and repeat step 2 till count is zero.
Note: For data transfer with overlap start transferring data from the last location of
source array to the last location of the destination array.
INC R1
DJNZ R3, BACK
HERE: SJMP HERE
END
RESULT:
Before Execution: 5 locations at X:0027h & X:0041h are filled up with data.
Algorithm
1. Initialize registers to hold count data (array size) & also the source & destination
addresses.
2. Get data from source location into accumulator and save in a register.
3. Get data from the destination location into accumulator.
4. Exchange the data at the two memory locations.
5. Decrement the count register and repeat from step 2 to 4 till count is zero.
Algorithm
1. Store the elements of the array from the address 9000h
2. Initialize a pass counter with array size-1 count (for number of passes).
3. Load compare counter with pass counter contents & initialize DPTR to point to the
start address of the array (here 9000h).
4. Store the current and the next array elements pointed by DPTR in registers B and r2
respectively.
5. Subtract the next element from the current element.
6. If the carry flag is set (for ascending order) then exchange the 2 numbers in the
array.
7. Decrement the compare counter and repeat through step 4 until the counter becomes
0.
8. Decrement the pass counter and repeat through step 3 until the counter becomes 0.
RESULT:
Before Execution:Unsorted Array at 9000h
4) Write an assembly language program to find the largest element in a given string of
n = 6 bytes at location 4000h. Store the largest element at location 4062h.
AIM: To write an assembly language program to find the largest element in a given
string of n = 6 bytes at location 4000h. Store the largest element at location 4062h.
APPARATUS REQUIRED: 8051 board with 89S8252 microcontroller, PC loaded with
Keil Micro vision 3, 9V- 1A adapter.
PROCEDURE: 1.Program is edited and is compiled with Keil compiler.
2. After successful compilation, debugging is started.
3. The memory window is accessed and the input data are entered and the output is
seen as shown in the result.
ORG 0000H
SJMP 30H
ORG 30H
MOV R3,#6 //length of the array
MOV DPTR,#4000H //starting address of the array
MOVX A,@DPTR
MOV r1,a
NEXTBYTE: INC DPTR
MOVX A,@DPTR
CLR C //reset borrow flag
MOV R2,A //next number in the array
SUBB A,R1 //OTHER Num - PREVIOUS LARGEST no.
JC skip // JNC for smallest element
MOV A,r2 //UPDATE larger number in r1
MOV R1,A
skip:DJNZ R3,NEXTBYTE
MOV DPL, #62H //LOCATION OF THE RESULT-4062H
MOV A,R1 //LARGEST NUMBER
MOVX @DPTR,A //STORE AT #4062H
OVER: SJMP OVER
END
Algorithm
1. Store the elements of the array from the address 4000h
2. Store the length of the array in r3 and set it as counter.
3. DPTR is loaded with starting address of the array.
4. Store the first number of the array in r1 (r1 is assigned to hold the largest number).
5. Increment DPTR.
6. Subtract the number pointed by DPTR from the contents of r1 (to compare whether
the next array element is larger than the one in r1).
7. If the element pointed by DPTR is larger then load the larger number into r1.
8. Decrement the counter and repeat steps through 5 until the counter becomes 0.
9. Store the largest number in r1 in address 4062h
RESULT:
Before Execution:
SJMP LAST
CKDIV: CJNE R1,#03,OTHER
DIV AB //Quotient in A & remainder in B
SJMP LAST
OTHER:MOV A,#00
MOV B,#00
LAST: INC R0
MOVX @R0,A
INC R0
MOV A,B
MOVX @R0,A
HERE:SJMP HERE
END
Algorithm
1. Store the condition x in r1.
2. Load the first and second numbers to A and B registers respectively
3. Compare the contents of r1 and perform the operations add, sub, etc accordingly.
4. Store the result present in A and B registers to the appropriate memory locations.
To get square make the D1 bit of data memory 20h high, say FF,02,06,etc. The bit address is
01. Similarly bit address 78h correspond to D0 bit 0f data ram location 2Fh.
Algorithm
1. Store the eight bit number x in A, r0 & B registers.
2. Multiply A and B registers to obtain the square (say SQH:SQL) of the number x.
3. Check if bit 01 is set. If set go to end (storing the result), else do the cube operations.
4. The high part of the square result (SQH) is stored on the stack.
5. Multiply the low part of the square result (SQL) with x (partial cube result).
6. Store the low part of the above result at 9001h & the high part in R2.
7. Retrieve the high part of the square result (SQH) stored on the stack & multiply with
x.
8. Add the low part of the above result (SQH*X) with R2 and store in 9002h.
9. Add the high part (SQH*X) with the resulting carry and store in 9003.
III. COUNTERS
A) ASSEMBLY PROGRAM ILLUSTRATING HEX UP/DOWN COUNTERS.
//counter program - hex/binary counters
1) Write an ALP to implement (display) an eight bit up/down binary (hex) counters on
watch window.
AIM: To write an ALP to display an eight bit up/down binary (hex) counters on watch
window.
APPARATUS REQUIRED: 8051 board with 89S8252 microcontroller, PC loaded with
Keil Micro vision 3.
PROCEDURE: 1.Program is edited and is compiled with Keil compiler.
2. After successful compilation, debugging is started.
3. The memory window is accessed and the input data are entered and the output is seen as
shown in the result.
Note: to run this program, after selecting DEBUG session in the main menu use
View-> Watch& call Stack window, in the Watches select watch 1(or 2) and
press F2 and enter a (for accumulator A)
ORG 0H
SJMP 30H
ORG 0H
MOV a,#00
BACK: ACALL DELAY
INC a //dec a for binary down counter
JNZ BACK
HERE:SJMP HERE
Algorithm:
1. Move 00 to A register
2. Call the delay subroutine for 1 second, in delay program move FFH to registers r1,
r2 and r3, loop and decrement until 0.
3. Increment A register(decrement for down counter)
DJNZ r3,$
DJNZ r2, DECR
DJNZ r1, DECR1
RET
END
Algorithm:
4. Move 00 to A register
5. Call the delay subroutine for 1 second (in delay program move FFH to registers r1,
r2 and r3, loop and decrement until 0).
6. Increment A register(add 99h for down counter)
7. Decimal adjust accumulator for the BCD up/down counter.
RESULT: Accumulator A is incremented in BCD from 00, 01, 02…09, 10, 11,…99.
1) Two eight bit numbers NUM1 & NUM2 are stored in external memory locations 8000h
& 80001h respectively. Write an ALP to compare the 2 nos.
Reflect your result as: if NUMI<NUM2, SET LSB of data RAM 2F (bit address 78H)
IF NUM1>NUM2, SET MSB OF 2F(7FH). if NUM1 = NUM2-Clear both LSB & MSB of
bit addressable memory location 2Fh.
AIM: To illustrate Boolean and logical instructions for the given conditions.
APPARATUS REQUIRED: 8051 board with 89S8252 microcontroller, PC loaded with
Keil Micro vision 3.
PROCEDURE: 1.Program is edited and is compiled with Keil compiler.
2. After successful compilation, debugging is started.
3. The memory window is accessed and the input data are entered and the output is seen as
shown in the result.
ORG 0000H
SJMP 30H
ORG 30H
MOV DPTR,#8000H
MOVX A,@DPTR
MOV R0,A
INC DPTR
MOVX A,@DPTR
CLR C
SUBB A,R0
JZ EQUAL
JNC BIG
SETB 78H
SJMP END1
BIG:SETB 7FH
SJMP END1
EQUAL:CLR 77H
CLR 7FH
END1:SJMP END1
END
Algorithm:
1. Store the elements of the array from the address 4000h
2. Move the first number in r0 and the second number in register A respectively
3. Clear carry flag and subtract the two numbers, if the carry flag is 0(if the nos are
equal), Clear both LSB & MSB of bit addressable memory location 2Fh
4. If the carry bit is set then Set MSB of 2F(7FH), else LSB of data RAM 2F (bit
address 78H).
Algorithm:
1. Point to the data RAM register 20h and store the condition x.
2. Point to 21h and 22h and move the first number to A register.
3. Compare the contents of r1 and perform the operations accordingly.
4. The result will be stored in 23H register.
The above program can also be written as shown below (using indirect addressing)
ORG 0000H
SJMP 30H
ORG 30H
mov r0,#20h
MOV A,@R0 //ON CHIP DATA RAM-DONOT USE MOVX
MOV R1,A //X IN R1
INC R0
MOV A,@R0 //A -NUM1
INC R0 // R0 POINTS TO NUM2
CJNE R1,#0,CKOR
ANL A, @R0
SJMP END1
CKOR:CJNE R1,#01,CKXOR
ORL A, @R0
SJMP END1
CKXOR:CJNE R1,#02,OTHER
XRL A, @R0
SJMP END1
OTHER: CLR A
END1:INC R0
MOV @R0,A //STORE RESULT
HERE:SJMP HERE
END
ORG 00H
SJMP 30h
ORG 30h
MOV R0,20H //R0-X
CJNE R0,#0,CK1
MOV C,08H //LSB OF NUM1 (21H) - BIT ADDRESS -08
ANL C,10H //LSB OF NUM2 (22H) - BIT ADDRESS -10
SJMP LAST
CK1:CJNE R0,#1,CK2
MOV C,0FH //MSB OF NUM1 (21H) - BIT ADDRESS -0F
ANL C,17H //MSB OF NUM2 (22H) - BIT ADDRESS -17
SJMP LAST
CK2:CJNE R0,#2,CK3
CPL 0FH
MOV C,0FH //MSB OF NUM1 (21H) - BIT ADDRESS -0F
SJMP LAST
CK3:CLR C
LAST:MOV 1FH,C //RES IS MSB OF 23H LOCATION -1FH
HERE:SJMP HERE
END
RESULT: 20h = 00 => AND OF LSBs=1 (hence 80 in 23h location)
20h = 01 =>complement of MSB of 21h location. Hence 21h is changed to A1 and 23h
location has 80h
Before Execution After Execution
Algorithm:
1. Move the condition X (from 20h location) into R0 register.
2. If X=0; then move LSB bit of 21h to carry flag and ‘AND’ Carry flag with LSB bit
of 22h. Goto step5
3. If X=1; then move MSB bit of 21h to carry flag and ‘OR’ Carry flag with MSB bit
of 22h. Goto step5
4. If X=0; then complement MSB bit of 21h and move it to carry flag. Goto step5
5. Store Carry flag at MSB bit of 23h location.
V. CODECONVERSION PROGRAMS
Algorithm
1. Move the decimal data to be converted from external memory 40h to accumulator.
2. AND A reg with 0f0h and obtain the upper MSB of the decimal digit and swap the
LSB and MSB of accumulator to bring the same to units place.
3. Move 0ah to B register and multiply with A reg to convert to hex value, store the
converted tens value in r1
4. Get the LSB of the decimal number and add to the converted tens value
5. point to the next memory location and store the result (hexadecimal).
Algorithm
1. Move the hex data to be converted to accumulator.
2. Move 10 to B register and divide with A reg to convert to ascii value
3. Store the converted LSB value in r7
4. Repeat the step 2 to obtain the converted MSB value
5. Store the same in r6.
Algorithm :
//Converts the BCD byte in A into two ASCII characters.
1. Move the BCD data to be converted to accumulator.
2. Get the lower nibble(BCD digit) & ADD (or ORL) with 30h
3. Store the converted ASCII value
4. Get the higher nibble(tens BCD digit) & ADD (or ORL) with 30h
5. Store the converted ASCII value.
RESULT: The BCD code 2C at D:0050h is converted to 2 ASCII codes-43h(for 0B) & 32h
(for 02) Another Example-BA
Algorithm :
//Converts the hexadecimal byte in A into two ASCII characters.
1. Move the hexadecimal data to be converted to accumulator.
2. Get the lower nibble & call ASCII routine
3. Store the converted ASCII value
4. Get the higher nibble & call ASCII routine
5. Store the converted ASCII value
ASCII subroutine
1. If digit greater than 09,(for A-F) add 07h & 30h
2. Else (i.e., for 0-9) add only 30h
3. return
Note: For this program the input data should be only in the range 30h-39h & 41h to 46h.
Algorithm :
//Converts the ASCII characters into hexadecimal number.
1. Move the ASCII character to be converted to accumulator.
2. If character is greater than 41h,(for A-F), then subtract 07h & 30h
3. Else (i.e., for 0-9) subtract only 30h
Store the converted hexadecimal number.
RESULT: yE is printed on the serial window each time the program is executed.
Note-to use result of this program, after selecting DEBUG session in the main menu use
View-> serial window #1. On running & halting the program, the data is seen in the serial
window.
transmission is set by the baud rate which is done with the help of timer 1. (Refer Ayala).
Timer1 must be programmed in mode 2 (that is, 8-bit, auto reload).
Baud rate Calculation: Crystal freq/ (12*32) = (11.0592MHz)/(12*32) = 28800.
Serial communication circuitry divides the machine cycle frequency(11.0592MHz)/(12) by
32 before it is being used by the timer to set the baud rate.
To get 9600, 28800/3 is obtained by loading timer1 with -3 (i.e., FF – 3 = FD) for further
clock division. For 2400 baud rate, 28800/12 => -12 = F4 in TH1.
Algorithm:
1. Initialize timer 1 to operate in mode 2 by loading TMOD register.
2. load TH1 with -3 to obtain 9600 baud.
3. Initialize the asynchronous serial communication transmission (SCON) register.
4. Start timer1 to generate the baud rate clock.
5. Transmit the characters “y” & “E” by writing into the SBUF register and waiting for
the TI flag.
RESULT: Accumulator A is incremented in binary from 00, 01,02…09,0A, 0B, …, 0F, 10,
11, …FF every 1 second (for 33MHz clock setting & every 3 seconds for 11.0598MHz)
Algorithm:
1. Set up timer0 in mode 2 operation
2. Load TH1 with 118 to generate an interrupt every 0.05msec.
3. Reset registers a, r1 & r0.
4. Repeat step 4 continuously
5. On interrupt; ISR at 000B location goes to step 6
6. disable timer0
7. Update r1 & r0
8. Check if 20000 interrupts (=1 sec) over. Yes –increment accumulator a.
9. Enable timer & return from ISR.
Timerdelay = 12*(257-delay)/frequency
Timerdelay=0.05msec
Delay=256-((timerdelay * frequency)/12) =256-(0.05*10 -3 * 33*106)/12
=256-137.5 =118.5 //loaded in TH0
To get 1sec delay
1/0.05msec = 200*100 in the ISR
(assuming 33 MHZ crystal frequency. For 11 MHz, the calculations change).
Features of Embedded C
• C is a simple programming language and so very easy to code.
• Embedded C has most features of C-language with more stress on certain bit
manipulative instructions.
• This feature makes it easy to write program for μC and μP.
• Keil is a versatile software with a cross compiler that will convert the C program to
assembly language and thus the program can be executed on the desired target (say
8051).
a.Square waveform
b.Triangular Waveform
c.Ramp waveform
d.Sine waveform
8
0
5 P0 DAC CRO
1
μC
P0=0xff;
delay(500);
P0=0x00;
delay(1500);
}
}
#include <REG51.H>
main()
{
unsigned char i=0;
P0 = 0x00; /* P0 as Output port */
while(1)
{
for(i=0;i<0xff;i++){ /* Generate positive slope */
P0 = i;
}
for(i=0xfe;i>0x00;i--) /* Generate negative slope */
{P0 = i;
P1 = i;
}
}
}
#include <REG51.H>
main ()
{ Unsigned char i=0;
P0 = 0x00; /* P0 as Output port */
while (1)
{
for (i=0;i<0xff;i++) /* Generate ON pulse */
{
P1 = i;
P0 = i;
}
}
}
#include <REG51xD2.H>
main()
{
a)STEPPER MOTOR
• Configure P0 as output.
• Apply the initial excitation of 11 to motor coils through P0.
• Rotate right/left by one bit for clockwise /anti clockwise direction.
PS
8051µC
Stepper
Motor Stepper
P0
Interface Motor
Card
PS
8051µC
DC Motor
P0 Interface DC
FRC 8pin Card Motor
Cables
P3.2 (start)
P3.3 (cw)
#include<reg51xd2.h>
sbit start=P3^3;
sbit cw=P3^2;
main()
{
while(1)
{while(start&&cw)
{
P0=0xc0;
}
while(start&&(!cw))
{
P0=0x50;
}
P0=0x00;
}
}
PS
8051µC
P0 4X4 Keyboard
FRC 26pin Interface
Cable Card/LCD
Display
a) LCD Display
rd=0;
en=1;
en=0;
return;
}
void delay(unsigned char X){
for (;X>0;X--);
return;}
main ()
{
for (i=0;i<5;i++){
delay(500);
command(C[i]);
}
for (i=0;i<5;i++){
delay(500);
display(D[i]);
}
while(1);
}
#include<reg51.h>
Unsigned char Temp;
Main(){
While(1){
P0=0x0f;
Temp=P0;
P0=0xf0;
Temp=Temp+P0;
P2=Temp;
}
}
PC
8051µC
Heat
switch
a) TEMPERATURE CONTROLLER
#include<reg51.h>
sbit rd=P1^1;
sbit wr=P1^2;
sbit hswitch=P3^0;
unsigned char mntr=0xA5;
void delay (unsigned int x){
for(;x>0;x--);
return;
}
main (){
P1=0x00;
wr=1;
delay (50000);
rd=1;
if(P2<mntr){
hswitch=1;
}
else hswitch=0;
}
b)VOLTAGE CONVERSION USING ADC
#include<reg51.h>
sbit rd=P1^1;
sbit wr=P1^2;
void delay (unsigned int x){
for(;x>0;x--);
return;
}
main (){
P1=0x00;
wr=1;
delay (50000);
rd=1;
}
V. Elevator
PS
8051µC
P0 4X4 matrix
FRC 26pin keypad and
Cable LCD Display
#include <reg51.h>
unsigned char temp,value=0x80,press;
int request,i,prefloor;
char getkey(){
P0=0xf0;
temp=P0;
if(temp==0x70)request=3;
else if(temp==0xb0)request=2;
else if(temp==0xd0)request=1;
else if(temp==0xe0)request=0;
//else request=0;
return temp;
}
void delay(){
unsigned char j,k,l;
for(j=0;j<0xff;j++){
for(k=0;k<0xff;k++){
for(l=0;l<0x0f;l++);
}
}
}
main(){
while(1){
press=getkey();
if(press!=0xf0){
//value=0x80;
i=prefloor-request;
prefloor=request;
if(i<0){
for(;i!=0;i++){
value=value>>1;
P2=value;
delay();
}
}
if(i>0){
for(;i!=0;i--){
value=value<<1;
P2=value;
delay();
}
}
}
}
}
Algorithm
• Read the numbers n1 and n2 from keyboard and display them on seven segment.
• Read the operand from the keypad if key pressed is B (+), C(-),D(*),E(/) then
respective operation is performed.
• Result is displayed on 2 digit seven segment display.
• If any time the key pressed value returned as 10h then clear the LCD.
PS
PS
8051µC Keypad
P0
FRC 26pin
Cable
7 Seg
Display
#include <REG51.H>
//--------------Funtion declaration
void display(unsigned char); //LED Display Routine
void convert_display(unsigned char);
void MSDelay (unsigned int); //Delay Routine
void Clear (void);
unsigned char getkey (); //KeyBoard Routine
//---------------The message
unsigned char idata keypad[4][4]= { '1','2','3','/',
'4','5','6','*',
'7','8','9','-',
'C','0','C','+',};
unsigned char idata disp[10] = { 0x40,0xcf,0xa4,0x30,0x19,0x12,0x02,0xf8,0x00,0x10 };
void main()
{
unsigned char Data1,Data2,Ans,Funct;
Clear();
while(1)
{
do
{
getkey (); //get data from matrix key pad
Clear();
Data1 = key;
key = key & 0xF0;
}while (key!=0x30);
display(disp[Data1&0x0F]);
MSDelay(10);
do
{
getkey (); //get data from matrix key pad
Funct = key;
key = key & 0xF0;
}while (key!=0x20);
display(0xff);
MSDelay(10);
do
{
getkey (); //get data from matrix key pad
Data2 = key;
key = key & 0xF0;
}while (key!=0x30);
display(disp[Data2&0x0F]);
MSDelay(110);
Clear();
break;
}
case ('-'):
{
Ans = Data1 - Data2;
convert_display(Ans);
break;
}
case ('*'):
{
Ans = Data1 * Data2;
convert_display(Ans);
break;
}
case ('/'):
{
Ans = Data1 / Data2;
convert_display(Ans);
break;
}
}
}
}
P2 = 0xff;
do {
P2 = 0x0f; //Wait untill all keys are released
colloc = P2;
colloc &= 0x0f;
} while (colloc != 0x0f);
do {
do
{
MSDelay (1);
colloc = P2;
colloc &= 0x0f;
} while (colloc == 0x0f); //Check whether any ket is pressed
MSDelay (1);
colloc = P2;
colloc &= 0x0f;
//Confirm whether any ket is pressed after delay
} while (colloc == 0x0f); //to aviod spikes
while(1)
{
P2 = 0xfE; //get the row presses
colloc = P2;
colloc &= 0xf0;
if (colloc != 0xf0)
{
rowloc = 0;
break;
}
P2 = 0xfd;
colloc = P2;
colloc &= 0xf0;
if (colloc != 0xf0)
{
rowloc = 1;
break;
}
P2 = 0xfb;
colloc = P2;
colloc &= 0xf0;
if (colloc != 0xf0)
{
rowloc = 2;
break;
}
P2 = 0xf7;
colloc = P2;
colloc &= 0xf0;
rowloc = 3;
break;
}
//get the coloum presses
if (colloc == 0xe0) key = (keypad[rowloc][0]);
else if (colloc == 0xd0) key = (keypad[rowloc][1]);
else if (colloc == 0xb0) key = (keypad[rowloc][2]);
else key = (keypad[rowloc][3]);
return(key);
}
4. Block transfer:
;-------------------------------------------------------------------------------
RSEG CODE ; Assemble to Flash memory
;-------------------------------------------------------------------------------
RESET mov.w #SFE(CSTACK),SP ; Initialize stackpointer
StopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; Stop WDT
SetupP5 bis.b #002h,&P5DIR ; P1.0 output
SetupDMA0 movx.a #1400h,&DMA0SA ; Start block address
movx.a #1420h,&DMA0DA ; Destination block address
mov.w #0010h,&DMA0SZ ; Block size
mov.w
#DMADT_5+DMASRCINCR_3+DMADSTINCR_3+DMAEN,&DMA0CTL; Rpt, inc
bis.w #DMAEN,&DMA0CTL ; Enable DMA0
;
Mainloop bis.b #02h,&P5OUT ; P5.1 = 1, LED on
bis.w #DMAREQ,&DMA0CTL ; Trigger block transfer
bic.b #02h,&P5OUT ; P5.1 = 0, LED off
jmp Mainloop ; Again
;
;-------------------------------------------------------------------------------
COMMON INTVEC ; Interrupt Vectors
;-------------------------------------------------------------------------------
ORG RESET_VECTOR ; POR, ext. Reset, Watchdog
DW RESET
END
PUBLIC main ; make the main label visible outside this module
ORG 0FFFEh
DC16 init ; set reset vector to 'init' label
ADD.B #01,R9
ADD.W R9,R10
AND.B #000Fh,R6
MOV.W #0002h,R4
MOV.W #00011000000b,R15
QUESTION BANK
1.Write an assembly language program to transfer ______ bytes of data from location
_____ to location ______ (without overlap).
2. Write an assembly language program to exchange n = ____ bytes of data at
location______ and at location _______.
3. Write an assembly language program to sort an array of n= _______bytes of data in
ascending order stored from location __________(use bubble sort algorithm).
4. Write an assembly language program to find the largest element in a given string of n
=______ bytes at location _______. Store the largest element at location ______.
5. Write an ALP to perform the following: If x=0-perform w + v; else if x=1-perform w-v;
else if x=2-perform w*v; else if x=3-perform w/v, where w & v are eight bit numbers.
6. Write an ALP to illustrate square and cube operations.
7. Write an ALP to implement (display) an eight bit up (hex) counters on watch window.
8. Write an ALP to implement (display) an eight bit down (hex) counters on watch
window.
9. Write an ALP to implement (display) an eight bit up/down BCD counters on watch
window.
10. Two eight bit numbers NUM1 & NUM2 are stored in external memory locations 8000h
& 80001h respectively. Write an ALP to compare the 2 nos.
Reflect your result as: if NUMI<NUM2, SET LSB of data RAM 2F (bit address 78H)
If NUM1>NUM2, SET MSB OF 2F(7FH). if NUM1 = NUM2-Clear both LSB & MSB of
bit addressable memory location 2Fh.
11. 3 eight bit numbers X, NUM1 & NUM2 are stored in internal data RAM locations 20h,
21h & 22H respectively. Write an ALP to compute the following.
If X=0; then NUM1 (AND) NUM2, if X=1; then NUM1 (OR) NUM2,
if X=2; then NUM1 (XOR) NUM2, else RES =00, RES is 23H location.
12. Write an ALP to implement decimal to hex conversion.
13. Write an ALP to implement hex to decimal conversion.
14. Write an ALP to implement BCD to ASCII conversion.
15. Write an ALP to implement ASCII to BCD conversion.
16. Conduct an experiment to configure 8051 microcontroller to transmit characters (yEs) to
a PC using the serial port and display on the serial window.
VIVA QUESTIONS
1. What is microcontroller?
2. What is the difference between microcontroller and microprocessor?
3. What is the difference between microcontroller and microcomputer?
4. Compare 8051 and MSP430 microcontrollers
5. Explain the role of watchdog timer in MSP430
6. Explain the clock system of MSP430
7. Explain the application areas of low power embedded systems.
8. List the peripherals of the microcontroller 89S8252.
9. List the special function registers of 8051 and their function
10. What is the difference between bit and byte addressable SFRs?
11. List the applications of microcontrollers.
12. What are arithmetic instructions?
13. What are logical instructions?
14. What are interrupts? List the interrupts of 8051 with their priority.
15. What do you mean by baud rate?How do to set the baud rate?
16. What is the difference between MOV and MOVC instruction?
17. What is the difference between CALL and JUMP instruction?
18. What are assembler directives?
19. What is a cross compiler?
20. What are the features of embedded C?
21. What is stack? List the instructions related to stack
22. What do you mean by data memory and code memory?
23. List out the types of memories. What do you mean by flash memory?
24. What are internal and external memories? Which instruction is used to access
external memory.
25. What is the difference between timer and counter?
.
**********