0% found this document useful (0 votes)
95 views

17EC3652 Lab Manual

The document is a lab manual for a Microcontrollers lab course. It outlines the course details including objectives, outcomes, list of experiments for both 8051 and ARM microcontrollers. It provides block diagrams and descriptions of the 8051 architecture including registers, addressing modes, instruction sets for arithmetic and logical operations. The experiments cover basic programming, peripherals interfacing for each microcontroller platform.

Uploaded by

Mmk Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views

17EC3652 Lab Manual

The document is a lab manual for a Microcontrollers lab course. It outlines the course details including objectives, outcomes, list of experiments for both 8051 and ARM microcontrollers. It provides block diagrams and descriptions of the 8051 architecture including registers, addressing modes, instruction sets for arithmetic and logical operations. The experiments cover basic programming, peripherals interfacing for each microcontroller platform.

Uploaded by

Mmk Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

17EC3652 (VR17) Microcontrollers Lab

Lab Manual
For
17EC3652

MICRO CONTROLLERS LAB


III/IV B Tech ECE

DEPARTMENT OF
ELECTRONICS & COMMUNICATION ENGINEERING

VELAGAPUDI RAMAKRISHNA
SIDDHARTHA ENGINEERING COLLEGE
(AUTONOMOUS)
(Sponsored by Siddhartha Academy of General & Technical Education)
VIJAYAWADA – 520 007

1
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

17EC3652: MICROCONTROLLERS LAB

Course Category: Programme Core Credits: 1.5


Course Type: Practical Lecture - Tutorial -Practice: 0-0-3
Prerequisites: 17EC3602: Continuous Evaluation: 30
Microcontrollers Semester end Evaluation: 70
Total Marks: 100

Course Upon successful completion of the course, the student will be able to:
outcomes
CO1 Develop assembly language programs on 8051 and ARM.
CO2 Interface the peripherals to 8051 and ARM.
Contribution of PO PO PO PO PO PO PO PO PO PO PO PO PS PS
Course a b c d e f g h i j k l PO PO
1 2
Outcomes
towards
achievement of CO1 H H
Program
Outcomes
(L – Low, M -
Medium, H – CO2 H H
High)
Course Content List of lab Exercises:
Experiments Based on 8051:
1. Basic programs on Microcontrollers.
2. Programs on Serial Communication.
3. Programs on Interrupt Mechanism.
4. Programs on Timer/Counter concepts.
5. Programs on LCD Display interfacing.
6. Programs on Traffic Light Control.
Experiments Based on ARM:
1. Basic programs on ARM
2. Interfacing ADC and DAC.
3. Interfacing LED and PWM.
4. Interfacing real time clock and serial port.
5. Interfacing keyboard and LCD.
NB: A minimum of 10(Ten) experiments (5 from each section) have to be performed and
recorded by the candidate to attain eligibility for External Practical Examination
E-resources and 1. http://www.datasheetarchive.com/8051-datasheet.html - (8051
other digital datasheet).
material 2. www.engenuics.com – ARM Cortex-M3 Assembly language.

2
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

8051 ARCHITECTURE BLOCK DIAGRAM

Fig: Functional block diagram of the 80C51s


On-chip hardware features of the Atmel 8051 microcontrollers. Included in this
description are:

 The port drivers and how they function both as ports and, for Ports 0 and 2, in bus
operations
 The Timer/Counters
 The serial Interface
 The Interrupt System
 Reset
 The reduced Power Modes

Special Function Registers:

A map of the on-chip memory area called SFR (Special Function Register) space is
shown in Figure. SFRs marked by parentheses are resident in the microcontrollers which
have the Timer2 feature. Note that not all of the addresses are occupied. Read accesses to
these addresses will in general return random data.

3
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

Accumulator ACC is the Accumulator register. The mnemonics for accumulator-specific


instructions, however, refer to the accumulator simply as A.
B Register The B register is used during multiply and divide operations. For other
instructions it can be treated as another scratch pad register.
Program Status Word The PSW register contains program status information as
detailed in Table

PSW: Program Status Word Register

4
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

Stack Pointer The Stack Pointer register is 8 bits wide. It is incremented before data is
stored during PUSH and CALL executions. While the stack may reside anywhere in on-
chip RAM, the Stack Pointer is initialized to 07H after a reset. This causes the stack to
begin at location 08H.
Data Pointer The Data Pointer (DPTR) consists of a high byte (DPH) and a low byte
(DPL). Its intended function is to hold a 16-bit address. It may be manipulated as a 16-bit
register or as two independent 8-bit registers.
Ports 0 to 3 P0, P1, P2 and P3 are the SFR latches of Ports 0, 1, 2 and 3, respectively.
Serial Data Buffer The Serial Data Buffer is actually two separate registers, a transmit
buffer and a receive buffer register. When data is moved to SBUF, it goes to the transmit
buffer where it is held for serial transmission. (Moving a byte to SBUF is what initiates
the transmission.) When data is moved from SBUF, it comes from the receive buffer.
Timer Registers Register pairs (TH0, TL0), (TH1, TL1), and (TH2, TL2) are the 16-bit
counting registers for Timer/Counters 0, 1, and 2, respectively.
Control Registers Special Function Registers IP, IE, TMOD, TCON, T2CON, SCON,
and PCON contain control and status bits for the interrupt system, the timer/counters, and
the serial port. They are described in later sections.

CPU Timing
X1 Mode (Standard Mode): A machine cycle consists of 6 states (12 oscillator periods).
Each state is divided into a Phase 1 half, during which the Phase 1 clock is active, and a
Phase 2 half, during which the Phase 2 clock is active. Thus, a machine cycle consists of
12 oscillator periods, numbered S1P1 (State 1, Phase 1), through S6P2 (State 6, Phase 2).
Each phase lasts for one oscillator period. Each state lasts for two oscillator periods.
Typically, arithmetic and logical operations take place during Phase 1 and internal
register-to-register transfers take place during Phase 2.

80C51 Fetch/Execute Sequences

5
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

Arithmetic Instructions

The menu of arithmetic instructions is listed in below Table. The table indicates the
addressing modes that can be used with each instruction to access the <byte> operand.
The execution times listed in the above Table assume a 12 MHz clock frequency and X1
mode. All of the arithmetic instructions execute in 1 μs except the INC DPTR instruction,
which takes 2 μs, and the Multiply and Divide instructions, which take 4 μs.

One of the INC instructions operates on the 16-bit Data Pointer. The Data Pointer is used
to generate 16-bit addresses for external memory, so being able to increment it in one 16-
bit operation is a useful feature.

The MUL AB instruction multiplies the Accumulator by the data in the B register and
puts the 16-bit product into the concatenated B and Accumulator registers.

The DIV AB instruction divides the Accumulator by the data in the B register and leaves
the 8-bit quotient in the Accumulator, and the 8-bit remainder in the B register.

Logical Instructions
Table shows the list of logical instructions. The instructions that perform Boolean
operations (AND, OR, Exclusive OR, NOT) on bytes perform the operation on a bit-by
bit basis. That is, if the Accumulator contains 00110101B and <byte> contains
01010011B, then ANL A,<byte> will leave the Accumulator holding 00010001B. The
addressing modes that can be used to access the <byte> operand are listed in Table.
Boolean operations can be performed on any byte in the internal Data Memory space
without going through the Accumulator. The XRL <byte>, # data instruction, for
example, offers a quick and easy way to invert port bits, as in

6
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

XRL P1, #OFFH


If the operation is in response to an interrupt, not using the Accumulator saves the
time and effort to stack it in the service routine.
The Rotate instructions (RL A, RLC A, etc.) shift the Accumulator 1 bit to the left
or right. For a left rotation, the MSB rolls into the LSB position. For a right rotation, the
LSB rolls into the MSB position.
The SWAP A instruction interchanges the high and low nibbles within the
Accumulator. This is a useful operation in BCD manipulations.

Data Transfers

Internal RAM:

Table shows the menu of instructions that are available for moving data around
within the internal memory spaces, and the addressing modes that can be used with each
one. With a 12 MHz clock, all of these instructions execute in either 1 or 2 μs.
The MOV <dest>, <src> instruction allows data to be transferred between any two
internal RAM or SFR locations without going through the Accumulator. Remember the
Upper 28 bytes of data RAM can be accessed only by indirect, and SFR space only by
direct addressing.
In all 8051 devices, the stack resides in on-chip RAM, and grows upwards. The
PUSH instruction first increments the Stack Pointer (SP), then copies the byte into the
stack. PUSH and POP use only direct addressing to identify the byte being saved or
restored, but the stack itself is accessed by indirect addressing using the SP register.

7
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

The XCH A, <byte> instruction causes the Accumulator and addressed byte to exchange
data. The XCHD A, @ Ri instruction is similar, but only the low nibbles are involved in
the exchange.
Data Transfer Instructions that Access Internal Data Memory Space

External RAM

Table shows a list of the Data Transfer instructions that access external Data Memory.
Only indirect addressing can be used. The choice is whether to use a one-byte address,
@Ri, where Ri can be either R0 or R1 of the selected register bank, or a two byte address,
@DPTR.

Data Transfer Instructions that Access External Data Memory Space

Boolean Instructions:

8051 devices contain a complete Boolean (single-bit) processor. The internal RAM
contains 128 addressable bits, and the SFR space can support up to 128 other addressable
bits. All of the port lines are bit-addressable, and each one can be treated as a separate
single-bit port. The instructions that access these bits are not just conditional branches,
but a complete menu of move, set, clear, complement, OR and AND instructions. These

8
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

kinds of bit operations are not easily obtained in other architectures with any amount of
byte-oriented software. The instruction set for the Boolean processor is shown in Table

Jump Instructions :

The table lists a single “JMP addr” instruction, but in fact there are three -SJMP, LJMP,
AJMP -which differ in the format of the destination address. JMP is a generic mnemonic
which can be used if the programmer does not care how the jump is encoded. The SJMP
instruction encodes the destination address as relative offset, as described above. The
instruction is 2 bytes long, consisting of the opcode and the relative offset byte. The jump
distance is limited to range of -128 to + 127 bytes relative to the instruction following the
SJMP.

The LJMP instruction encodes the destination address as a 16-bit constant. The
instruction is 3 bytes long, consisting of the opcode and two address bytes. The
destination address can be anywhere in the 64K Program Memory space. The AJMP
instruction encodes the destination address as an 11-bit constant. The instruction is 2
bytes long, consisting of the opcode, which itself contains 3 of the 11 address bits,
followed by another byte containing the low 8 bits of the destination address. When the
instruction is executed, these 11 bits are simply substituted for the low 11 bits in the PC.
The high 5 bits stay the same. Hence the destination has to be within the same 2K block
as the instruction following the AJMP.
Above Table shows a single “CALL addr” instruction, but there are two of them –
CALL and ACALL -which differ in the format in which the subroutine address is given
to the CPU. CALL is a generic mnemonic which can be used if the programmer does not
care which way the address is encoded.

9
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

The LCALL instruction uses the 16-bit address format, and the subroutine can be
anywhere in the 64K Program Memory space. The ACALL instruction uses the 11-bit
format, and the subroutine must be in the same 2K block as the instruction following the
ACALL. In any case the programmer specifies the subroutine address to the assembler in
the same way: as a label or as a 16-bit constant. The assembler will put the address into
the correct format for the given instructions.
Subroutines should end a RET instruction, which returns execution following the
CALL. RETI is used to return from an interrupt service routine. The only difference
between RET and RETI is that RETI tells the interrupt control system that the interrupt in
progress is done. If there is no interrupt in progress at the time RETI is executed, then the
RETI is functionally identical to RET
Table shows the list of conditional jumps available to the Atmel 8051 user. All of
these jumps specify the destination address by the relative offset method, and so are
limited to a jump distance of -128 to + 127 bytes from the instruction following the
conditional jump instruction. Important to note, however, the user specifies to the
assembler the actual destination address the same way as the other jumps: as a label or a
16-bit constant.

Table Conditional Jumps in Atmel 8051 Devices

There is no Zero bit in the PSW. The JZ and JNZ instructions test the Accumulator data
for that condition

10
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

Instruction Set Summary:

11
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

12
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

13
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

Instructions That Affect Flag Settings:

14
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

Port Structures and Operation


All four ports in the 80C51 are bidirectional. Each consists of a latch (Special Function
Register P0 through P3), an output driver, and an input buffer. The output drivers of Ports
0 and 2, and input buffers of Port 0, are used in accesses to external memory. In this
application, Port 0 outputs the low byte of the external memory address, time-multiplexed
with the byte being written or read. Port 2 outputs the high byte of the external memory
address when the address is 16 bits wide. Otherwise the Port 2 pins continue to emit the
P2 SFR content.
All the Port 3 pins, and (in the case of Timer2) two Port 1 pins are multifunctional. They
are not only port pins, but also serve the functions of various special features as listed
below:

I/O Configurations:
Below Figure shows a functional diagram of a typical bit latch and I/O buffer in each of
the four ports. The bit latch (one bit in the port’s SFR) is represented as a Type D flip-
flop, which will clock in a value from the internal bus in response to a “write to latch”
signal from the CPU. The Q output of the flip-flop is placed on the internal bus in
response to a “read latch” signal from the CPU. The level of the port pin itself is placed
on the internal bus in response to a “read pin” signal from the CPU. Some instructions
that read a port activate the “read latch” signal, and others activate the “read latch” signal,
and others activate the “read pin” signal.

15
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

As shown in Figure, the output drivers of Ports 0 and 2 are switchable to an


internal ADDR and ADDR/DATA bus by an internal CONTROL signal for use in
external memory accesses. During external memory accesses, the P2 SFR remains
unchanged, but the P0 SFR gets 1s written to it.
Port 0 differs in not having internal pull-ups. The pull-up FET in the P0 output
driver (see Figure) is used only when the Port is emitting 1’s during external memory
accesses. Otherwise the pull-up FET is off. Consequently P0 lines that are being used as
output port lines are open drain. Writing a 1 to the bit latch leaves both output FETs off,
so the pin floats. In that conditions it can be used as a high-impedance input. Because
Ports 1, 2, and 3 have fixed internal pull-ups they are sometimes called
“quasibidirectional” ports. When configured as inputs they pull high and will source
current in the data sheets) when externally pulled low. Port 0, on the other hand, is
considered “true” bidirectional, because when configured as an input it floats.
Timer/Counters:
80C51 Microcontrollers implement two general purpose, 16-bit timers/ counters. They
are identified as Timer 0 and Timer 1, and can be independently configured to operate in
a variety of modes as a timer or as an event counter. When operating as a timer, the
timer/counter runs for a programmed length of time, and then issues an interrupt request.
When operating as a counter, the timer/counter counts negative transitions on an external
pin. After a preset number of counts, the counter issues an interrupt request.
The various operating modes of each timer/counter are described in the following
sections.
Timer/Counter Operations:
A basic operation consists of timer registers THx and TLx (x= 0, 1) connected in cascade
to form a 16-bit timer. Setting the run control bit (TRx) in TCON register turns the timer
on by allowing the selected input to increment TLx. When TLx overflows it increments
THx; when THx overflows it sets the timer overflow flag (TFx) in TCON register. Setting
the TRx does not clear the THx and TLx timer registers. Timer registers can be accessed
to obtain the current count or to enter preset values. They can be read at any time but TRx
bit must be cleared to preset their values, otherwise the behavior of the timer/counter is
unpredictable.
The C/Tx# control bit (in TCON register) selects timer operation, or counter
operation, by selecting the divided-down peripheral clock or external pin Tx as the source
for the counted signal. TRx bit must be cleared when changing the mode of operation,
otherwise the behavior of the timer/counter is unpredictable. For timer operation (C/Tx#
= 0), the timer register counts the divided-down peripheral clock. The timer register is
incremented once every peripheral cycle (6 peripheral clock periods). The timer clock
rate is FPER / 6, i.e. FOSC / 12 in standard mode or FOSC / 6 in X2 mode.
For counter operation (C/Tx# = 1), the timer register counts the negative
transitions on the Tx external input pin. The external input is sampled every peripheral
cycle. When the sample is high in one cycle and low in the next one, the counter is
incremented. Since it takes 2 cycles (12 peripheral clock periods) to recognize a negative
transition, the maximum count rate is FPER / 12, i.e. FOSC / 24 in standard mode or
FOSC / 12 in X2 mode. There are no restrictions on the duty cycle of the external input
signal, but to ensure that a given level is sampled at least once before it changes, it should
be held for at least one full peripheral cycle.
In addition to the “timer” or “counter” selection, Timer 0 and Timer 1 have four
operating modes from which to select which are selected by bit-pairs (M1, M0) in

16
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

TMOD. Modes 0, 1, and 2 are the same for both timer/counters. Mode 3 is different. The
four operating modes are described below.

Mode 0 (13-bit Timer):


Mode 0 configures timer 0 as a 13-bit timer which is set up as an 8-bit timer (TH0
register) with a modulo 32 prescaler implemented with the lower five bits of the TL0
register (see Figure 2-9). The upper three bits of TL0 register are indeterminate and
should be ignored. Prescaler overflow increments the TH0 register. As the count rolls
over from all 1’s to all 0’s, it sets the timer interrupt flag TF0. The counted input is
enabled to the Timer when TR0 = 1 and either GATE = 0 or INT0 = 1. (Setting GATE =
1 allows the Timer to be controlled by external input INT0, to facilitate pulse width
measurements). TR0 is a control bit in the Special Function register TCON, GATE is in
TMOD.
The 13-bit register consists of all 8 bits of TH0 and the lower 5 bits of TL0. The
upper 3 bits of TL0 are indeterminate and should be ignored. Setting the run flag (TR0)
does not clear the registers.
Mode 0 operation is the same for Timer 0 as for Timer 1. Substitute TR0, TF0 and
INT0 for the corresponding Timer 1 signals in Table 2-10. There are two different GATE
bits, one for Timer 1 (TMOD.7) and one for Timer 0 (TMOD.3).

Timer/Counter x (x = 0 or 1) in Mode 0

Mode 1 (16-bit Timer):


Mode 1 is the same as Mode 0, except that the Timer register is being run with all 16 bits.
Mode 1 configures timer 0 as a 16-bit timer with the TH0 and TL0 registers connected in
cascade (see Figure 2-10). The selected input increments the TL0 register.
Timer/Counter x (x = 0 or 1) in Mode 1

Mode 2 (8-bit Timer with Auto-Reload):


Mode 2 configures timer 0 as an 8-bit timer (TL0 register) that automatically reloads from
the TH0 register. TL0 overflow sets TF0 flag in the TCON register and reloads TL0 with
the contents of TH0, which is preset by software. When the interrupt request is serviced,
hardware clears TF0. The reload leaves TH0 unchanged. The next reload value may be
changed at any time by writing it to the TH0 register.

17
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

Mode 2 operation is the same for Timer/Counter 1.

Timer/Counter x (x = 0 or 1) in Mode 2

Mode 3 (Two 8-bit Timers):


Mode 3 configures timer 0 so that registers TL0 and TH0 operate as separate 8-bit timers.
This mode is provided for applications requiring an additional 8-bit timer or counter. TL0
uses the timer 0 control bits C/T0# and GATE0 in the TMOD register, and TR0 and TF0
in the TCON register in the normal manner. TH0 is locked into a timer function (counting
FPER /6) and takes over use of the timer 1 interrupt (TF1) and run control (TR1) bits.
Thus, operation of timer 1 is restricted when timer 0 is in mode 3.

Timer/Counter 0 in Mode 3: Two 8-bit Counters

18
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

EXPERIMENT 1a:

Write a Program to find sum of the values and simulate on keil compliler. Assume that
RAM locations 40-44 have the values. At the end R6 should have the low byte and R7 the
high byte.

Procedure:

1. Write the assembly language program for this task in the keil compiler.
2. Compile it and dump the hex file as per the procedure given above.
3. Place this microcontroller in the trainer kit and fix it properly.
4. Power up the trainer kit from the mains supply

Program:

ORG 00H
MOV R0, #40H ;load pointer
MOV R2, #5 ;load counter
CLR A ;A=0
MOV R7, A ;clear R7
AGAIN: ADD A, @R0 ;add the byte pointer to by R0
JNC NEXT ;if CY=0 don't accumulate carry
INC R7 ;keep track of carries
NEXT: INC R0 ;increment pointer
DJNZ R2, AGAIN ;repeat until R2 is zero
MOV R6, A
END

EXPERIMENT 1B:

Write a Program to convert packed BCD to two ASCII numbers, place them in R2 and R6
and simulate on keil compliler.

Procedure:

1. Write the assembly language program for this task in the keil compiler.
2. Compile it and dump the hex file as per the procedure given above.
3. Place this microcontroller in the trainer kit and fix it properly.
4. Power up the trainer kit from the mains supply

Program:

ORG 00H
MOV A, #29H ;A=29H, packed BCD
MOV R2, A ;keep a copy of BCD data in R2
ANL A, #0FH ;mask the upper nibble(A=09)
ORL A, #30H ;make it an ASCII, A=39H (`9')
MOV R6, A ;save it (R6=39H ASCII char)
MOV A, R2 ;A=29H;get the original data
ANL A, #0F0H ;mask the lower nibble(A=20)

19
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

RR A ;rotate right
RR A ;rotate right
RR A ;rotate right
RR A ;rotate right,(A=02)
ORL A, #30H ;A=32H,ASCII char.'2'
MOV R2, A ;save ASCII char in R2
END

Viva Questions:

1. What is the Packed BCD number


2. Explain Packed BCD to Unpacked BCD number
3. Explain BCD addition
4. Define Auxiliary carry
5. Draw PSW
6. What are the flags effected by logical operations

20
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

EXPERIMENT 2a: Serial transmission

Write a program for RS 232 serial transmission and simulate on keil compiler.

Procedure:

1. Write the assembly language program for this task in the keil compiler.
2. Compile it and dump the hex file as per the procedure given above.
3. Place this microcontroller in the trainer kit and fix it properly.
4. Power up the trainer kit from the mains supply.
5. In order to establish communication between PC and Microcontroller, Serial cable
RS232 and voltage converter MAX232 IC are used. This section is available on
the system board.
6. For serial communication, Port 3 (pins 10 and 11 of Microcontroller) are used as
reception and transmission pins respectively.
7. The pins 10 and 11 of Microcontroller are connected to the pins 12 and 11 of
MAX232 IC respectively.
8. Connect one end of the serial cable to the processor and the other end to the DB 9
pin provided on the system board.
9. Check the connections before switching on the mains.
10. An LED is provided in the power supply section of the trainer kit to indicate if the
board is powered or not.
11. After the board is powered up, the message written in the program will be
displayed in the PC in the Hyperterminal window.
12. According to the program written in the microcontroller, the message can be seen
in the Hyperterminal window in the PC.

Program:

ORG 00H

MOV TMOD,#20H ;to select timer 1 in mode 2 (auto reload)


MOV SCON,#50H ; to select serial communication in mode 1
; and receiver enable
TH1,#0FDH ; to set the baud rate to 9600

DPTR,#MSG
BACK: CLR A
MOVC A,@A+DPTR ;to load the message into accumulator
; character by character
JZ NEXT
ACALL TRANSMIT ;call subroutine for transmission
INC DPTR
SJMP BACK

NEXT: SJMP NEXT ;termination of the program

21
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

/*TRANSFERRING DATA SERIALLY*/

TRANSMIT: SETB TR1 ; to start the timer

MOV SBUF,A ; load data into sbuf


JNB TI,$ ;wait for the transmission to be completed
CLR TI ;clear ti for next transmission
RET

MSG: DB "VRSEC",0

END

EXPERIMENT 2b: Serial reception

Write a program for RS 232 serial reception and simulate on keil compiler.

Procedure:

1. Write the assembly language program for this task in the keil compiler.
2. Compile it and dump the hex file as per the procedure given above.
3. Place this microcontroller in the trainer kit and fix it properly.
4. Power up the trainer kit from the mains supply.
5. In order to establish communication between PC and Microcontroller, Serial cable
RS232 and voltage converter MAX232 IC are used. This section is available on
the system board.
6. For serial communication, Port 3 (pins 10 and 11 of Microcontroller) are used as
reception and transmission pins respectively.
7. The pins 10 and 11 of Microcontroller are connected to the pins 12 and 11 of
MAX232 IC respectively.
8. Connect one end of the serial cable to the processor and the other end to the DB 9
pin provided on the system board.
9. Check the connections before switching on the mains.
10. An LED is provided in the power supply section of the trainer kit to indicate if the
board is powered or not.
11. After the board is powered up, open the Hyperterminal window in the PC.
12. According to the program written in the microcontroller, the characters entered in
the Hyperterminal window in the PC can be seen on the LCD display i.e., the
microcontroller receives the data from PC.

22
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

Program:

RS11 EQU P2.0 ; assigning names to the port pins as


; mentioned
RW11 EQU P2.1
EN EQU P2.2

ORG 00H

MOV DPTR, #CMD ; Initialization of LCD Commands


BACK1: CLR A
MOVC A,@A+DPTR
JZ NEXT
ACALL COMMAND
INC DPTR
SJMP BACK1

NEXT: MOV TMOD, #20H ;to select timer 1 in mode 2 (auto reload)
MOV SCON, #50H ; to select serial communication in mode 1
; and receiver enable
MOV TH1, #0FDH ; to set the baud rate to 9600
BACK: SETB TR1 ; to start the timer
JNB RI, $ ;wait for the character to be loaded into sbuf
CLR RI ; clear ri for the next reception
MOV A, SBUF ; To Move The Received Character Into A
ACALL DATA1 ; Call Subroutine For Display On The Lcd
JMP BACK

COMMAND: MOV P1, A


CLR RS11
CLR RW11
SETB EN
ACALL DELAY
CLR EN
RET

DATA1: MOV P1, A


SETB RS11
CLR RW11
SETB EN
ACALL DELAY
CLR EN
RET

DELAY: MOV R6,#255


HERE: MOV R7,#255
DJNZ R7,$
DJNZ R6, HERE
RET

23
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

CMD: DB 38H, 0EH, 01H, 06H, 80H


END

Viva Questions:

1. What are the advantages of serial communication over parallel communication


2. List out different types of serial communication protocols
3. What are the advantages of communication protocol

24
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

EXPERIMENT 3: Interrupt Mechanism

Write a program reads data from P1 and writes it to P2 continuously while giving a copy
of it to the serial COM port to be transferred serially and simulate on keil compiler.

Procedure:
1. Write the assembly language program for this task in the keil compiler.
2. Compile it and dump the hex file as per the procedure given above.
3. Place this microcontroller in the trainer kit and fix it properly.
4. Power up the trainer kit from the mains supply.

Program:
ORG 00H
LJMP MAIN
ORG 23H
LJMP SERIAL ;Jump to serial interrupt ISR
ORG 30H

MAIN: MOV P1, #0FFH ; make P1 an input port


MOV TMOD, #0x20H ; timer1, mode 2 (auto reload)
MOV TH1, #0xFDH ; 9600 baud rate
MOV SCON, #0x50H ; 8-bit, 1 stop, REN enabled
MOV IE, #90H ; enable serial interrupt
SETB TR1 ; start timer1

BACK: MOV A, P1 ; read data from port1


MOV SBUF,A ; give a copy to SBUF
MOV P2, A ; send it to P2
SJMP BACK ; stay in loop indefinitely

ORG 100H
SERIAL: JB TI, TRANS ; Jump if TI is high
MOV A, SBUF ; Otherwise due to receive
CLR RI ; clear RI since CPU does not
RETI ; return from interrupt

TRANS: CLR TI ; clear TI since CPU does not


RETI ; return from ISR

END

Viva Questions:

1. What is the advantage of Interrupt over Polling mechanism


2. Define Interrupt service mechanism
3. Define Polling
EXPERIMENT 4 a: Timer concepts

Write a program to generate 5K Hz square wave on P1.5 (8051 board) and simulate on
keil compiler.

25
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

Procedure:
1. Write the assembly language program for this task in the keil compiler.
2. Compile it and dump the hex file as per the procedure given above.
3. Place this microcontroller in the trainer kit and fix it properly.
4. Power up the trainer kit from the mains supply.
5. Now connect the Port pin P1.5 of Microcontroller) to the out put device (CRO)
6. Check the connections before switching on the mains.
7. After the board is powered up, the out put will be displayed on the CRO.
8. According to the program written in the microcontroller, the signal will be seen on
the CRO.

Program:

ORG 00H
MOV TMOD, #10H ;timer 1, mode 1(16-bit)
AGAIN: MOV TL1, #34H ;TL1=34H,low byte of timer
MOV TH1, #76H ;TH1=76H,Hi byte (7634H=Timer value)
SETB TR1 ;start the timer 1
BACK: JNB TF1, BACK ;stay till timer rolls over
CLR TR1 ;stop timer 1
CPL P1.5 ;comp. p1.5 to get hi, lo
CLR TF1 ;clear timer flag 1
AGAIN: SJMP AGAIN ;reload timer since mode 1
END

Viva Questions:

1. Explain the Timer circuit with one example


2. What are the applications of Timer circuit
3. How the timer circuit is used to synchronize the different tasks

26
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

EXPERIMENT 4b: Counter concepts


Write a program on counter to count events on P3.5, send the count to P2 and
simulate on keil compiler.

Procedure:
1. Write the assembly language program for this task in the keil compiler.
2. Compile it and dump the hex file as per the procedure given above.
3. Place this microcontroller in the trainer kit and fix it properly.
4. Power up the trainer kit from the mains supply.

Program:

ORG 00H

MOV TMOD, #60H ; counter 1, mode 2, C/T=1 external pulses


MOV TH1, #00H ; clear TH1
SETB P3.5 ; make T1 input
AGAIN: SETB TR1 ; start the counter
BACK: MOV A, TL1 ; get copy of count TL1
MOV P2, A ; display it on port 2
JNB TF1, BACK ; keep doing it if TF=0
CLR TF1 ; stop the counter1
CLR TF1 ; make TF=0
SJMP AGAIN

END

Viva questions:

1. Explain the counter circuit with one example


2. What are the applications of counter circuit
3. How the counter circuit is used to generate the delay element

27
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

EXPERIMENT 5:

Develop necessary interfacing circuit to display the given data on LCD display and
program to display a message on the LCD and simulate on keil compiler.

Procedure:
1. Write the assembly language program for this task in the keil compiler.
2. In order to display any message on the LCD, first the LCD should be initialized.
This is done by writing some commands in the programs. LCD contains both data
and commands.
3. 3 Pins for commands and 8 pins for data of LCD are connected to the
microcontroller port pins in the program.
4. Compile it and dump the hex file as per the procedure given above.
5. Place this microcontroller in the trainer kit and fix it properly.
6. Power up the trainer kit from the mains supply.
7. Now connect the Port 1(pins 1 to 8 of Microcontroller) to the data pins of LCD
provided on the system board using 8-pin connector.
8. Similarly connect the Port 2(pins 21, 22, 23 of microcontroller) to the command
pins of LCD provided on the system board using 3-pin connector.
9. Check the connections before switching on the mains.
10. An LED is provided in the power supply section of the trainer kit to indicate if the
board is powered or not.
11. After the board is powered up, the message will be displayed on the LCD.
12. According to the program written in the microcontroller, the message can be seen
on the LCD provided on the system board.

Program:

RS11 EQU P2.0 ; assigning names to the port pins


RW1 EQU P2.1
EN EQU P2.2

ORG 00H ; start of the program

MOV A,#38H ;initialize LCD 2 lines,5*7 matrix


ACALL COMMAND ; call command subroutine
MOV A,#0EH ;display on, cursor on
ACALL COMMAND
MOV A,#01H ;clear LCD
ACALL COMMAND
MOV A,#06H ;Shift Cursor Right
ACALL COMMAND
MOV A,#84H ;cursor at line 1, position 4
ACALL COMMAND
ACALL DELAY ;give LCD sometime

MOV A,#'V' ;display letter V


ACALL DATA1 ;call data subroutine
ACALL DELAY ;call delay subroutine
MOV A,#'R' ;display letter R

28
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

ACALL DATA1
MOV A,#'S' ;display letter S
ACALL DATA1
MOV A,#'E' ;display letter E
ACALL DATA1
MOV A,#'C' ;display letter C
ACALL DATA1
SJMP $ ;stay here

COMMAND: ;send command to lcd


MOV P1,A ;send command to port
CLR RS11 ;RS=0 for command
CLR RW1 ;RW=0 fro write operation

SETB EN ;EN=1 for high pulse


ACALL DELAY ;give LCD some time
CLR EN ;EN=0 for high to low pulse
RET

DATA1: ;send data to LCD


MOV P1,A ;send data to port
SETB RS11 ;RS=1 for data
CLR RW1 ;RW=0 for write operation
SETB EN ;EN=1 for high pulse
ACALL DELAY ;give LCD some time
CLR EN ;EN=0 for high to low pulse
RET

DELAY: MOV R5,#255 ;R5=255


HERE: MOV R6,#255 ;R6=255
DJNZ R6,$ ;stay here until R6=0
DJNZ R5,HERE
RET
END

Viva Questions:

1. What is the function of enable pin in LCD


2. Define LCD
3. Write command word to shift the cursor left
4. Write command word to start display from 6th postion

29
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

EXPERIMENT 6:

Write a program for Traffic Light Control and simulate on keil compiler

Procedure:

1. Write the assembly language program for this task in the keil compiler.
2. Compile it and dump the hex file as per the procedure given above.
3. Place this microcontroller in the trainer kit and fix it properly.
4. Power up the trainer kit from the mains supply.

Program:

ORG 00H

MOV DPTR, #TRE ; assign address of control word to DPTR


MOV R2, #0C
MOVX @DPTR, A
PUSH DPH
PUSH DPL
MOV DPTR, #CNTL PORT ; assign address of control port to DPTR
MOVX @DPTR, A
POP DPL
POP DPH
INC DPTR
LOOP1: MOVX @DPTR, A
PUSH DPH
PUSH DPL
MOV DPTR, #PORTA
MOVX @DPTR, A
POP DPL
POP DPH
INC DPTR
MOVX @DPTR, A
PUSH DPH
PUSH DPL
MOV DPTR, #PORTB
MOVX @DPTR, A
LCALL DELAY ; calling delay subroutine
POP DPL
POP DPH
INC DPTR
DJNZ R2, LOOP1
SJMP START
DELAY: MOV R7, #10H ; delay subroutine
LOOP4: MOV R5, #0FFH
LOOP3: MOV R6, #0FFH
LOOP2: NOP
NOP
DJNZ R6, LOOP2

30
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

DJNZ R5, LOOP3


DJNZ R7, LOOP4
RET

TRE: 8601 21H, 09H, 10H, 00H (SOUTH WAY)


8605 0CH, 09H, 80H, 00H (EAST WAY)
8609 64H, 08H, 00H, 04H (NOURTH WAY)
860D 24H, 03H, 02H, 00H (WEST WAY)

END

Viva questions:

1. How could modify the algorithm for 5 road junction


2. How could modify the algorithm for 3 road junction

31
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

EXPERIMENT 7: Basic programs on ARM


Aim: Write an assembly ‘C’ code
i. Add two numbers
ii. To print the numbers from 1-10
iii. Swap two numbers without using third variable
Proceedure:
1. Write the assembly C program for this task in the keil compiler.
2. Compile it and dump the hex file as per the procedure given above.
3. Place this microprocessor in the trainer kit and fix it properly.
4. Power up the trainer kit from the mains supply.

Program:

i. #include<stdio.h>

int sum(int, int);


int main() {
int a, b;

printf("Enter the two Numbers: ");


scanf("%d %d", &a, &b);

printf("Addition of two num. is : %d", add(a, b));


return(0);
}

int add(int num1, int num2) {


int i;
for (i = 0; i < num2; i++)
num1++;
return num1;
}

ii. #include<stdio.h>

void printNumber(int value) {


int i;
printf("%d\n", value);
i = value + 1;

if (i > 10)
return;
printNumber(i);
}

void main() {
printNumber(1);
}

iii. #include<stdio.h>

32
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

int main() {
int a, b;

printf("\nEnter value for num1 & num2 : ");


scanf("%d %d", &a, &b);

a = a + b;
b = a - b;
a = a - b;

printf("\nAfter swapping value of a : %d", a);


printf("\nAfter swapping value of b : %d", b);

return (0);
}
Output:
Enter value for num1 & num2: 10 20

After swapping value of a: 20


After swapping value of b: 10

iv. #include<stdio.h>
#include<stdlib.h>
int main(){
int a[10],n,i;
system ("cls");
printf("Enter the number to convert: ");
scanf("%d",&n);
for(i=0;n>0;i++)
{
a[i]=n%2;
n=n/2;
}
printf("\nBinary of Given Number is=");
for(i=i-1;i>=0;i--)
{
printf("%d",a[i]);
}
return 0;
}
Output:
Enter the number to convert: 5
Binary of Given Number is=101

Viva Questions:
1. What is ARM?

33
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

2. Difference between microcontroller and ARM?


3. What is RISC
4. Difference between RISC and CISC

34
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

EXPERIMENT 8: Interfacing ADC and DAC with ARM


8a: Aim: To interface ADC with LPC 1768.
Algorithm:
Step 1: Start the program
Step 2: Declare the variable to hold ADC converted value
Step 3: Function is declared and defined for ADC initialization
Step 4: Configure PO.23 as ADC input using PINSELI
Step 5: Declare the variable ADC data
Step 6: Select clock for ADC to start of conversion
Step 7: Check end of conversion and read result
Step 8: Return the 12 bit result to main function
Step 9: Call the functions TargetResetInit(), ADC_Init(), InitUART0()
Step 10: Display ADC test
Step 11: Read the ADC value to convert
Step 12: Convert to volts using adc=(5*adc)/1023
Step 13: Display the result on UART
Step 14: Stop
Program:
#include "LPC17xx.h"
#include "stdio.h"
#include "UART0.h"
#include "target.h"
#define PCADC 0x00001000
char adcreading[16] ; // Variable to hold the ADC converted Value
//static inline void __enable_irq() { asm volatile ("cpsie i"); }
void ADC_Init (void) // ADC initialation function
{
LPC_SC->PCONP |= PCADC ;
LPC_PINCON->PINSEL1 = (LPC_PINCON->PINSEL1 | 0x00004000 ); // P0.23 is
configured as ADC Input
}
unsigned int ADC_GetAdcReading ()
{
unsigned int adcdata;
LPC_ADC->ADCR =0x01200301; // Select AD0.0, Select clock for ADC, Start of
conversion,
while(!((adcdata = LPC_ADC->ADGDR) & 0x80000000)) // Check end of conversion
(Done bit) and read result
{
}
return((adcdata >> 4) & 0x3ff) ; // Return 12 bit result
}
int main (void)
{
unsigned int delay;
float adc;
TargetResetInit();
ADC_Init() ; // Initiate ADC Setting
InitUart0();
printf("ADC Test"); // Display ADC Test

35
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

while(1)
{
adc = ADC_GetAdcReading(); // Read AD0.7 reading
adc=(5*adc)/1023;// Convertion of ADC value in Volts
sprintf(adcreading,"\n ADC0 CH1= %.2f V",adc); // Convert result into ASCII to
display it on LCD
UART0Puts(adcreading) ; // Display result on UART
for(delay=0;delay<60000;delay++);
}
}
Peripheral Interface:
To give analog input from Potentiometer R15 present in Analog Input region on
ADTV1.1. Analog input range is from 0 to 1023.
Output:
You can see output on Hyper Terminal.
Therefore Open Hyper Terminal. Go to Start->All Programs->Accessories-
>Communications-
>Hyper Terminal->Assign the Respective port-> Settings. Do proper settings (Baud Rate:
19200,
Data Bits: 8, Stop Bits: 1, Echo: Off, Parity: None, Com Port: Com 1 (if other choose it)).
Click on OK. Go to Port -> Open. If required Reset the ADT board. Now vary the R15
POT and hence see the change in voltage on Hyper Terminal.
Result:
Thus interfacing of ADC with LPC1768 is executed and verified successfully.

8b: Aim: To interface DAC with LPC1768.


Algorithm :
Step 1: Start the program
Step 2: Declare the variable to store output of DAC
Step 3: Call Target Reset Init() function
Step 4: Clock selection for DAC is done
Step 5: Select the DAC output pin
Step 6: Call delay for sometime
Step 7: Aout value is shifted right by 6bits for the loop to count from 0 to 1024
Step 8: Call Delay
Step 9: Again the loop count for 1024 times
Step 10: Aout value is again shifted right by 6times
Step 11: Stop
Program:
#include "LPC17xx.h"
#include <stdio.h>
#include "target.h"
unsigned int delay,AoutValue = 0 ;
// Variable to store the output value of DAC
int main (void) // Main function
{
TargetResetInit(); //LPC_SC->PCLKSEL0 |= 0x00400000;
//Peripheral clock selection for DAC as PCLK_peripheral = CCLK
LPC_PINCON->PINSEL1 |= 0x00200000 ; // to use P0.26 as DAC output pin.
while(1) // Infinite for loop for continues operation

36
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

{
for(AoutValue = 0 ; AoutValue < 1024 ; AoutValue ++) // For loop to count the 1024
times
{
for(delay=0;delay<1000;delay++);
LPC_DAC->DACR = AoutValue << 8 ; //AoutValue is shifted right by 6 bits
} //End of nested for loop
for(AoutValue = 1024 ; AoutValue > 0 ; AoutValue --) // For loop to count the 1024
times
{
for(delay=0;delay<1000;delay++);
LPC_DAC->DACR = AoutValue << 8 ; // AoutValue is shifted right by 6 bits
}
} // End of outer For loop
} // End of Main Function
Connect one pin of oscilloscope to TP2/DAC and another to GND.
Output:
You can see ramp wave on oscilloscope.
Note: Keep S11.2 switch in OFF position, after execution of program

Viva Questions:

1. What is ADC?
2. What is DAC?
3. Applications od ADC and DAC
4. What is successive approximation technique

37
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

EXPERIMENT 9: Interfacing LED and PWM

Aim: To interface LED and PWM (RGB) with LPC1768


Proceedure:
Program:
#include "LPC17xx.h"
#include <stdint.h>
#include "PWM.h"
#include "lpc_types.h"
unsigned int PWM_Init( enum color Color, unsigned int cycle )
{
if(Color == RED)
{
LPC_PINCON->PINSEL4 = 0x00000001 ; // Set GPIOs for all PWM pins on PWM2
LPC_PWM1->LER = LER0_EN | LER1_EN; // Loading the Latch enable resister
LPC_PWM1->PCR = PWMENA1; //Enabling PWM2 by setting PWM control register
}
else if (Color == GREEN)
{
LPC_PINCON->PINSEL4 = 0x00000004 ; //set GPIOs for all PWM pins on PWM4
LPC_PWM1->LER = LER0_EN | LER2_EN ; //Loading the Latch enable resister
LPC_PWM1->PCR = PWMENA2 ; // Enabling PWM4 by setting PWM control register
}
else if(Color == BLUE)
{
LPC_PINCON->PINSEL4 = 0x0000010 ; //set GPIOs for all PWM pins on PWM6
LPC_PWM1->LER = LER0_EN | LER3_EN ; //Loading the Latch enable resister
LPC_PWM1->PCR = PWMENA3 ; // Enabling PWM6 by setting PWM control register
}
else if(Color == ALL)
{
LPC_PINCON->PINSEL4 = 0x00000015 ; //set GPIOs for all PWM pins on PWM0
LPC_PWM1->LER = LER0_EN | LER2_EN | LER2_EN | LER3_EN;
// Loading the Latch enable resister
LPC_PWM1->PCR = PWMENA1 | PWMENA2 | PWMENA3;
// Enabling PWM2,PWM4,PWM6 by setting PWM control
register
}
LPC_PWM1->TCR = TCR_RESET; // Counter Reset
LPC_PWM1->PR = 0x00; // count frequency : Fpclk
LPC_PWM1->MCR = PWMMR0I; // interrupt on PWMMR0, reset on PWMMR0, reset
// TC if PWM0 matches
LPC_PWM1->MR0 = cycle; // set PWM cycle
LPC_PWM1->MR1 = cycle * 5/6;
LPC_PWM1->MR2 = cycle * 5/6; // cycle * 2/3;
LPC_PWM1->MR3 = cycle * 5/6 ; // cycle * 1/2;
LPC_PWM1->TCR = TCR_CNT_EN | TCR_PWM_EN; // counter enable,
PWM enable
return (1);
}

38
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

Peripheral Interface:
Keep S4.3 switch in ON position.
Output:
You can see RED,BLUE,GREEN color blinking on RGB Led.
Note: Keep S4.3 switch in OFF position, after execution of program
Result:
Thus interfacing of LEDS and PWM(RGB) with LPC1768 is executed and verified
successfully

Viva Questions:
1. Define PWM?
2. What is duty cycle?
3. Difference between timer and counter
4. What is PWMMR?

39
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

EXPERIMENT 10: Interfacing real time clock and serial port

Aim :
To interface Real Time Clock on Serial UART with LPC1768.
Algorithm :
Step 1: Start the program
Step 2: Enable Divisor latch access ,set 8 bit word length, stop bit no parity, disable break
transmission
Step 3: Set UPB bus clock divisor
Step 4: Set UART0 divisor latch(LSB)
Step 5: Set UART0 divisor latch(MSB)
Step 6: Clear TxFIFO and enable Rx and TxFIFOS
Step 7: Wait until Trasnsmit holding register is empty
Step 8: Store to transmit holding register.
Step 9: Wait until Trasnsmit holding register is empty
Step 10: Then Store to transmit holding register.
Step 11: Wait until there’s a character table read
Step 12: Then read from the receiver buffer register.
Program:
#include "LPC17xx.h"
#include "target.h"
#include "UART0.h"
#include <stdio.h>
#include <string.H>
#include "I2C_RTC.h"
#include "Timer.h"
char MAIN_u8buffer1[20];
char MAIN_u8buffer2[20];
#define PCF8523 1
#ifdef PCF8523
#define OFFSET 3
#endif
#ifdef DS1307
#define OFFSET 0
#endif
#define CONTROL_ADDR0 0
#define CONTROL_ADDR1 1
#define CONTROL_ADDR2 2
#define SECONDS_ADDR 3
#define MINUTES_ADDR 4
#define HOURS_ADDR 5
#define DATE_ADDR 6
#define DAY_ADDR 7
#define MONTH_ADDR 8
#define YEAR_ADDR 9
#define BYTE0 0x00
#define BYTE1 0x00
#define BYTE2 0x88
unsigned char MAIN_u8buffer[20] ;
volatile unsigned int delay,i;

40
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

unsigned char date, mon, year;


unsigned char hh,mm,ss ;
unsigned int idate, imon, iyear ;
unsigned int ihh,imm,iss ;
char rtcdate[16] ;
char rtctime[16] ;
int main(void)
{
TargetResetInit();
InitUart0();
I2C_Init();
TIMER_Init();
printf("Clock:\nPress 'S' (to set RTC) Within 5 seconds, any other to discard") ;
for(delay=0; delay<=800000;delay++)
{
if (UART0_IsReady())
break;
}
if(UART0_IsReady() && (UART0getchar())=='S')
{
printf("\nEnter date and time in dd mm yy hh mm ss format\n") ;
scanf("%02x%02x%02x%02x%02x%02x",&idate,&imon,&iyear,&ihh,&imm,&iss);
date = idate ;
mon = imon ;
year = iyear ;
hh = ihh ;
mm = imm ;
ss = iss ;
printf("You entered:\n%02x-%02x-%02x
%02x:%02x:%02x\n",date,mon,year,hh,mm,ss) ;
printf("Press Y to store these values to RTC, any other to discard\n") ;
if (UART0getchar() == 'Y')
{
I2C_WriteToRTC(CONTROL_ADDR0,BYTE0,1) ;
I2C_WriteToRTC(CONTROL_ADDR1,BYTE1,1) ;
I2C_WriteToRTC(CONTROL_ADDR2,BYTE2,1) ;
I2C_WriteToRTC(DATE_ADDR,date,1) ;
I2C_WriteToRTC(MONTH_ADDR,mon,1) ;
I2C_WriteToRTC(YEAR_ADDR,year,1) ;
I2C_WriteToRTC(SECONDS_ADDR,ss,1) ;
}
else;
}
printf("\n");
while(1)
{
if (!(I2C_ReadFromRTC(OFFSET, MAIN_u8buffer, 7)))
printf("Main Buffer %s \n",MAIN_u8buffer);
printf("\r\nClock : ");
printf("Date : %02x",MAIN_u8buffer[DATE_ADDR-OFFSET]& 0x3F);

41
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

printf("-%02x",MAIN_u8buffer[MONTH_ADDR-OFFSET]& 0x1F);
printf("-%02x",MAIN_u8buffer[YEAR_ADDR-OFFSET]);
printf(" Time : %02x",MAIN_u8buffer[HOURS_ADDR-OFFSET]& 0x1F);
printf(":%02x",MAIN_u8buffer[MINUTES_ADDR-OFFSET]& 0x7F);
printf(":%02x",MAIN_u8buffer[SECONDS_ADDR-OFFSET]& 0x7F);
while(1)
{
ss = MAIN_u8buffer[SECONDS_ADDR-OFFSET] & 0x7F;
if (!I2C_ReadFromRTC(OFFSET, MAIN_u8buffer, 7))
printf("\nMemory Read error..$");
if (ss != (MAIN_u8buffer[SECONDS_ADDR-OFFSET] & 0x7F))
break;
}
} }
Peripheral Interface:
No switches are there to turn ON/OFF I2C RTC
Output:
You can see output on Hyper Terminal.
Therefore Open Hyper Terminal. Go to Start->All Programs->Accessories-
>Communications-
>Hyper Terminal->Assign the Respective port-> Settings. Do proper settings (Baud Rate:
19200,
Data Bits: 8, Stop Bits: 1, Echo: Off, Parity: None, Com Port: Com 1 (if other choose it)).
Click on OK. Go to Port -> Open. If required Reset the ADT board.On terminal after
reset, press “Shift-S’ to set DD MM YY HH MM SS format and press enter. To save the
RTC press “Shift-Y”,now you can see the RTC getting updated.
Result:
Thus interfacing of real time clock on serial UART with LPC1768 is executed and
verified
successfully.

Viva questions:

1. What is UART?
2. Define baud rate
3. What does RTC do?
4. Application of RTC

42
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

EXPERIMENT 11: Interfacing LCD and keyboard with ARM

Aim: Write an assembly C program to interface LCD and keyboard with ARM
Proceedure:
1. Write the assembly C program for this task in the keil compiler.
2. Compile it and dump the hex file as per the procedure given above.
3. Place this microcontroller in the trainer kit and fix it properly.
4. Power up the trainer kit from the mains supply.

Program:
#include <LPC17xx.H>
#include "interfaces.h"

void delay(unsigned int x) /* Delay routine */


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

unsigned char getKey()


{
unsigned char i,j,indx;
unsigned int key,key1;
while(1)
{
indx = 0x00;
for(i=1;i<=0x08;i<<=1)
{
LPC_GPIO0->FIOSET2 = i<<3;
delay(1000);
key = LPC_GPIO0->FIOPINH & 0x0780 ;
key >>= 7;

if(key != 0)
{
delay(0xFF64);

do
{
key1 = (LPC_GPIO0->FIOPINH & 0x0780 )>> 7;

}while(key1 !=0);

delay(0xFF64);
delay(0xFF64);
delay(0xFF64);
delay(0xFF64);
delay(0xFF64);
for(j=1;j<=4;j++)

43
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

{
key >>= 1;
if(key == 0)
{
key = indx+j;
switch(key)
{
case 1: key = 0x0F;break;
case 2: key = 0x0B;break;
case 3: key = 0x07;break;
case 4: key = 0x03;break;
case 5: key = 0x0E;break;
case 6: key = 0x0A;break;
case 7: key = 0x06;break;
case 8: key = 0x02;break;
case 9: key = 0x0D;break;
case 10: key = 0x09;break;
case 11: key = 0x05;break;
case 12: key = 0x01;break;
case 13: key = 0x0C;break;
case 14: key = 0x08;break;
case 15: key = 0x04;break;
case 16: key = 0x00;break;

}
return key;
}
}
}
LPC_GPIO0->FIOCLR2 = i<<3;
indx += 4;
}
}
}

main()
{
unsigned char key;

Sys_Init();
LPC_PINCON ->PINMODE1 = 0x003FC000;
GLCD_DisplayString(4,0,__FI,"OnBoard KEYPAD Demo");

LPC_GPIO0->FIODIRH |= 0x0078; //Output

GLCD_DisplayString(5,0,__FI,"KeyPressed :");

while(1)
{

44
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

key = getKey();
GLCD_DisplayChar(5,12,__FI,key<=9?key+0x30:'A'+key-0x0A);
}
}

Viva questions:
1. LCD interfacing can be done in how many bit mode?
2. What are key matrices?
3. What does RS pin do in LCD board?
4. LCD device consists of how many pins and what are they?

45
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

EXPERIMENT ON I2C COMMUNICATION (Beyond syllabus)

Write a program for I2C bus communication and simulate on keil compiler

Procedure:

1. Write the assembly language program for this task in the keil compiler.
2. Compile it and dump the hex file as per the procedure given above.
3. Place this microcontroller in the trainer kit and fix it properly.
4. Power up the trainer kit from the mains supply.

Program:

ORG 00H

MOV SCON, #0x40 ; to set SCON register


MOV TMOD, #0x20 ; to set the TMOD register
ACALL I2CSTSRT ; to initialize the I2C communication
MOV A, #0x9E
ACALL I2CWRITE ; to send the command word
MOV A, #0x40
ACALL I2CWRITE ; to send the command word
ACALL I2CREAD ; to read the data
ACALL CONV ; to convert ASCII to Packed BCD
MOV A, #0x9F
ACALL I2CWRITE ; to send the command word
ACALL I2CREAD ; to read the data
ACALL CONV ; to convert ASCII to Packed BCD
SETB TR1 (88.6)
MOV SBUF, #0x61h ; serial buffer is initialized with ASCII value
ACALL SOUT ; transmitting data
MOV SBUF, #0x64h
ACALL SOUT
MOV SBUF, #0x63h
ACALL SOUT
MOV SBUF, #0x3Ah
ACALL SOUT
MOV SBUF, R7
ACALL SOUT
MOV SBUF, R6
ACALL SOUT
LJMP NEXT
SETB 0x90.5
SETB 0x90.4
HERE: JNB 0x90.4, HERE
CLR 0x90.5
CLR 0x90.4
RET
MOV R5, #0x08
JNB 0xE0.7 HERE1

46
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

SETB 0x90.5
SJMP HERE2
CLR 90.5
SETB 0x90.4
RLC A
CLR 0x90.4
DJNZ R5, HERE1
SETB 0x90.5
SETB 0x90.4
HERE3: JNB 0x90.4, HERE3
CLR 0x90.4
RET
MOV R5, #0x08
MOV A, #0x00
CLR C
BACK1: SETB 0x90.4
SETB 0x90.5
RLC A
CLR 0x90.4
DJNZ R5, BACK1
MOV R0, A
CLR 0x90.5
SETB 0x90.4
NOP
CLR 0x90.4
RET
MOV B, A
ANL A, # 0x0F
CJNE A, #0x0A, N1
N1: JC N2
ADD A, #0x0A
N2: A, #0x2D
MOV R6, A
ANL A, #0x0F
CJNE A, #0x0A, N3
N3: JC N4
ADD A, #0x0A
ADD A, #0x2B
MOV R7, A
RET
JNB 0x98.1, SOUT
CLR TI
RET
END

Viva Questions:

1. What is the data format of I2C protocol


2. What are the applications of I2C protocol
3. Compare I2C over other protocols

47
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

EXPERIMENT ON DATA ACQUISITION (Beyond the syllabus)

Develop necessary interfacing circuit to read data from a sensor and process using the
8051 board. The data has to be displayed on a PC monitor.
Program to read the input from LM35 (Temperature sensor) and process using 8051
board and display on a PC monitor.

Procedure:
1. Write the assembly language program for this task in the keil compiler.
2. Compile it and dump the hex file as per the procedure given above.
3. Place this microcontroller in the trainer kit and fix it properly.
4. Power up the trainer kit from the mains supply.
5. LM35 is used as the temperature sensor to read the temperature value
continuously and convert it into electrical signals.
6. This LM35 is connected to ADC for the analog data conversion into digital
form.LM35 has three terminals. Two pins are given to the power supply and
ground connections. The analog temperature value is available at the second pin.
7. Now this LM35 output pin is connected to the Vin (pin 6 of ADC).
8. A 10K preset is used to adjust the reference voltage according to the input voltage
applied to ADC. The two terminals of preset are connected to the power supply
and ground. The third pin is connected to the Vref/2 (pin 9 of ADC).
9. The command pins (RD, WR and INTR) of ADC are connected to the Port 2 (pins
26, 27 and 28 of Microcontroller).
10. The data pins (D0-D7) of ADC are connected to the Port 1 (pins 1 to 7 of
Microcontroller).
11. In order to establish communication between PC and Microcontroller, Serial cable
RS232 and voltage converter MAX232 IC are used. This section is available on
the system board.
12. For serial communication, Port 3 (pins 10 and 11 of Microcontroller) are used as
reception and transmission pins respectively.
13. The pins 10 and 11 of Microcontroller are connected to the pins 12 and 11 of
MAX232 IC respectively.
14. Connect one end of the serial cable to the processor and the other end to the DB 9
pin provided on the system board.
15. Check the connections before switching on the mains.
16. An LED is provided in the power supply section of the trainer kit to indicate if the
board is powered or not.
17. After the board is powered up, the message and the room temperature will be
displayed continuously on the HyperTerminal window of the PC.

Program:

RD1 BIT P2.5 ;assigning names to the


WR1 BIT 2.6 ;port pins
INTR BIT P2.7

ORG 00H
MOV TMOD,# 20H ;to select timer 1 in mode 2 (auto reload)
MOV SCON,# 50h ; to select serial communication in mode 1

48
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

; and receiver enable


MOV TH1, #0FDH ; to set the baud rate to 9600
MOV DPTR, #MSG
BACK: CLR A
MOVC A,@A+DPTR
JZ START
ACALL TRANSMIT ;call subroutine for transmission
INC DPTR
SJMP BACK
START:SETB RD1 ;make read pin high
SETB INTR ;make interrupt pin high
CONTINUE: CLR WR1 ;write=0
NOP
NOP
NOP
SETB WR1 ;write=1 low to high to start conversion
JB INTR,$ ;wait for the end of conversion
CLR RD1 ;conversion finished, enable read pin
MOV A, P1 ;read the data
ACALL DISPLAY ;call subroutine to display the data on lcd
SETB RD1 ;make rd=1 for the next round
JMP CONTINUE

DISPLAY: MOV A, #08H ;backspace to display the value continuously


ACALL TRANSMIT ;at the same location
MOV A, #08H
ACALL TRANSMIT
MOV A, #08H
ACALL TRANSMIT
MOV A, P1
MOV R2, #3
MOV R1, #40H
LOOP: MOV B, #10
DIV AB
MOV @R1,B
INC R1
DJNZ R2,LOOP
MOV R2,#3
LOOP1:DEC R1
MOV A,@R1
ADD A,#30H
ACALL TRANSMIT
ACALL DELAY
MOV A,#08H
MOV A,#08H
MOV A,#08H
DJNZ R2, LOOP1

RET

49
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department
17EC3652 (VR17) Microcontrollers Lab

TRANSMIT: SETB TR1 ;START THE TIMER1


MOV SBUF,A ;load data into sbuf
JNB TI,$ ;wait till the transmission is completed
CLR TI ;clear ti for the next transmission
RET

DELAY: MOV R6, #255


HERE: MOV R7, #255
DJNZ R7, $
DJNZ R6, HERE
RET

MSG: DB "TEMPERATURE: ",0

END

Viva Questions:

1. What is the signaling conditioning circuit


2. Define actuator and sensor
3. Define resolution of ADC.

50
Velagapudi Ramakrishna Siddhartha Engineering College ECE Department

You might also like