0% found this document useful (0 votes)
28 views103 pages

MIC Manual Te

The document is a laboratory journal for microcontroller experiments at Rajiv Gandhi College of Engineering, detailing various experiments including memory transfer, LED interfacing, and waveform generation using DAC. Each experiment includes aims, apparatus, software used, and theoretical background along with programming examples. The journal serves as a practical guide for students in the Electronics and Telecommunication Engineering department during their semester V lab sessions.

Uploaded by

sagarsabale7766
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)
28 views103 pages

MIC Manual Te

The document is a laboratory journal for microcontroller experiments at Rajiv Gandhi College of Engineering, detailing various experiments including memory transfer, LED interfacing, and waveform generation using DAC. Each experiment includes aims, apparatus, software used, and theoretical background along with programming examples. The journal serves as a practical guide for students in the Electronics and Telecommunication Engineering department during their semester V lab sessions.

Uploaded by

sagarsabale7766
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/ 103

DEPARTMENT OF ELECTRONICS &

TELECOMMUNICATION ENGINEERING

LABORATORY JOURNAL
FOR
MICROCONTROLLERS
SEMESTER - V

NAME :____________________________________

ROLL NO.: ____ EXAM SEAT NO.: ___________

RAJIV GANDHI COLLEGE OF ENGINEERING

1
INDEX
Sr. Date of
Name of the Experiment Remark
No. performance

1 Programs On Memory Transfer

2 Interfacing Of Led’s (Flashing, Hex Counter


,Bcd Counter)

3 Waveform Generation Using DAC

4 LCD Interfacing using 89v51


microcontroller

5 Interfacing Of Stepper Motor With 89v51


microcontroller

6 Interfacing Of Led, Relay, Buzzer with PIC


18f4520

7 Interfacing Of Keypad with PIC 18f4520

8 Generation Of Square Wave using PIC


18f4520

9 PC To PC Communication using PIC 18f4520

10 ADC Interfacing using PIC 18f4520

Subject Incharge HOD

Date:

2
Experiment No.1
TITLE: -PROGRAMS ON MEMORY TRANSFER
Aim: -To perform internal to internal and internal to external memory block transfer.
Software:-Resonance (RIDE) software(version 3.13).
Theory:-
Addressing Modes of 8051 Microcontroller
When the microcontroller executes an instruction, it performs specific function on
data.The data must be moved or copied to destination location. This ways by which these
addresses location are specified are called as addressing modes
1. Direct Addressing Mode
This is another way of addressing an operand. Here the address of the data (source data )
is given as operand. Let’s take an example.
MOV A, 04H
Here 04H is the address of register 4 of register bank#0. When this instruction is
executed, what ever data is stored in register 04H is moved to accumulator. In the picture
below we can see, register 04H holds the data 1FH. So the data 1FH is moved to
accumulator.
2. Indirect Addressing Mode
In this addressing mode, the instruction specifies a register which contains address of an
operand I .e the register holds the actual address that will be used in the data move
operation.This address may be 8bit or a 16 bits address
The R0 and R1 of each register bank can be used as an index or pointer register. Ro and
R1 point to the contents in the RAM.
The @ sing indicates the register acts as a pointer to memory location.
Example
MOV A,@R1; copy the contents of memory location, whose address is specified in R1
register of selected bank to the accumulator
3. Register addressing Mode
Each register bank consists of registers R0 to R7. To access these registers there are
special instructions. In the instruction Opcode, 3 bits are reserved for specifying one of

3
the eight registers from then selecting the register bank; the user has to modify two bits in
the PSW.
Example:
MOV A, R2: copy the data from register R2 of the selected register bank to register A.
4. Register Specific addressing Mode
In this addressing mode the instructions refer to a specific register such as accumulator or
data pointer DPTR.
Example:
DA A : Decimal adjusts Accumulator for addition.
RR A : Rotate the contents of accumulator to right
SWAP A : Swap the nibbles within the accumulator.
5. Immediate Addressing Mode
This method is the simplest method to get the data. In this addressing mode the source
operand is a constant rather than a variable. As the data source is a part of the instruction
it is immediately available.
The # sign indicates that the data followed is immediate operand.
Example:
MOV A, #30H ; copy 30H Immediately to accumulator.
MOV P1, #0FFH ; copy FFH Immediately to port 1.
MOV DPTR, #1234H immediately to data pointer (DPTR).
DATA TRANSFER INSTRUCTIONS
1. MOV <dest-byte>,<src-byte>
This instruction copies the contents of the source location to the destination location. The
contents of source location are unchanged
It is most flexible operation. It allows fifteen combinations of source and destination
addressing modes
Depending on the types of transfer the number of bytes required may be 1,2 or 3 and
number of cycle required are 1 or 2

4
2. MOV A, Rn
Example
MOV A, R1 ; This instruction will copy the contents of register R1 of the selected
register bank to the accumulator.
3. MOV A, direct
Example
MOV A , 40H ;The instruction will copy the contents from memory location whose
address is 40H to the accumulator.
4. MOV A, @Ri
Example
MOV A, @R0 ; This instruction will copy the contents of memory location whose
address is specified in the register R0 of the select bank to accumulator.
5. MOV A, #data
Example
MOV A,#31H ; This instruction will move the data 31H immediately to the accumulator.
6. MOV RN, A
Example
MOV R5, A ; This instruction will copy the contents of the accumulator to register R5 of
selected register bank.
7. MOV RN, direct
Example
MOV R3, 30H ; This instruction will copy the contents from address 30H to the register
R3 of selected register bank.
8. MOV Rn, #data
Example
MOV R7,#20H ; This instruction will copy immediate data 20H to the register R7 of the
selected register bank.
9. MOV direct, A
Example
MOV 80H,A ;80 H is the address of port 0. This instruction will copy the contents of
accumulator to the port 0 latch.

5
10. MOV direct, Rn
Example
MOV 30H,R5 ;This instruction will copy the contents of register R5 of the selected
register bank to the memory location whose address is 30H.
11. MOV direct, direct
Example
MOV 20H,30H ; This instruction will copy the contents of memory location whose
address is 30H to the memory location whose address is 20H.
12. MOV direct, @Ri
Example
MOV 20H, @R1 ; This instruction will copy the contents of memory location whose
address is given in R1 register bank to memory location whose address is 20H.
13. MOV direct, #data
Example
MOV 10H, #10H ; This instruction will copy immediate data 10H to memory location
whose address is 10H.
14. MOV @Ri, A
Example
Move @R0,A ; This instruction will copy the contents of accumulator to memory
location whose address is pointed by the R0 register of selected register bank.
15. MOV @Ri, Direct
Example
MOV @R0,30H ; This instruction will copy the contents of accumulator to memory
location whose address is 30H to the memory location pointed by register R0 of selected
register bank.
16. MOVC A, @A+DPTR
Example
MOVC A, @A+DPTR; This instruction will load the accumulator with a code byte or
constant from the program memory.
Hence, It is essential to generate 16 bit address, so that data can be fetched from that
address. The address of the byte fetched is the sum of the original unsigned eight bit

6
accumulator contents and the contents of a sixteen bit base register. The 16 bit base
registers maybe the data pointer or the program counter (pc).
17. MOVX A, @R0
The MOVX instruction transfer data between the accumulator and a byte
of external data memory, hence “X” is depended to MOV.
Depending on whether the indirect address provides to the external data RAM is eight bit
or 16 bit, there are two types of instructions.
In the first type, the contents of register R0 or R1 of current register bank provide an 8 bit
address that is multiplexed with data
18. DEC<byte>
DEC RN
This instruction will decrement the contents of register Rn of the selected registers bank
by 1.
The registers can be any of the registers R0 – R7 of the selected register bank or the
accumulator.
Example:
REC R5
DEC @Ri
Example:
DEC @R0.
16 . DJNZ<byte>,<ret-addr>
DJNZ
This instruction decrements the specified register or memory location by 1 and branches
to the address indicated by the second operand if the resulting value is nonzero.
The destination is calculated by adding the signed relative displacement value in the last
instruction byte to the PC, after incrementing the PC to the first byte of the next
instruction.
The original value of 00H will under flow to FFH if decremented.
It supports two addressing modes: register addressing mode and direct addressing mode.
Example
DJNZ R5, SUB

7
Flow Chart

START

Initially start address of source

Initially starting address of


source block pointer to 30H

Initially starting address of


pointer destined block to 40H

Get byte from source block

Transfer byte destination pointer

Increment source & destination


pointer

Decrement counter

NO IS
Counter=0

STOP

8
Program 1-
$mod51
mov A,10H
mov B,A
mov 20H,A
nop
end
program 2-
$mod51
mov R0,#30H
mov A,@R0
mov R0,#40H
mov @R0,A
nop
end
Program 3-
$mod51
mov DPTR,#1000H
movx A,@DPTR
mov 10H,A
nop
end
Program 4- Internal to internal memory block transfer
$mod51
mov R2,#0AH
mov R0,#30H
mov R1,#40H
Back:mov A,@R0
mov @R1,A
INC R0
INC R1

9
DJNZ R2,Back
nop
end
Program 4- Internal to external memory block transfer
$mod51
mov R2,#0AH
mov R0,#30H
mov DPTR,#1000H
Back:mov A,@R0
movx @DPTR,A
INC R0
INC DPTR
DJNZ R2,Back
nop
end

Conclusion: -
Thus we have performed internal to internal memory block transfer and internal to
external memory block transfer.

10
Experiment No. 2
TITLE: - INTERFACING OF LED’S
(FLASHING, HEX COUNTER , BCD COUNTER)
Aim: - Interfacing of LED with microcontroller 89x516 for turning ON/OFF LED, Hex
Counter, BCD counter.
Apparatus: - 89x516 microcontroller kit, connecting wires.
Software: - Keil software, SST Flash etc
Theory:-
LEDs are human oriented output peripheral. It is used to display result or operand.
To drive a LED, there are two methods.
Method 1:
Connect the cathode of LED to ground. Connect the anode of LED to port pin of 8051,
through a resistor as shown in fig A.

Fig.1: LED driven by 8051 port pin (method 1)


This method requires 8051 to source a huge amount of current required by the LED i.e.
around 20mA.But 8051is not capable of sourcing a current more than 2mA.this will make
the LED glow very dim.
Method 2:
Connect the anode of LED to Vcc through resistor. Connect the cathode of LED to port
8051 as shown in fig B. This method requires 8051 to sink a huge current required by
LED i.e. 20mA.8051 can sink huge currents and hence it makes LED glow brighter.
Hence we will always use method 2, to interface LED.

11
Fig 2: LED driven by 8051 port pin (Method 2)
LED flashing :

Fig 3: LED interfacing using IC 89x516

12
Flowchart for LED Flashing

Start

P0=00H
P2=00H
P3=00H

Delay

P0=0ffH
P2=0ffH
P3=0ffH

Delay

Program for LED Flashing-

ORG 000H
AGAIN: MOV P0,#00H
MOV P2,#00H
MOV P3,#00H
ACALL DELAY
MOV P0,#0FFH
MOV P2,#0FFH
MOV P3,#0FFH
ACALL DELAY
SJMP AGAIN
DELAY:
MOV R0,#0FFH

13
LOOP2: MOV R1, #0FFH
LOOP1: DJNZ R1, LOOP1
DJNZ R0,LOOP2
RET
END

BCD Counter:
A BCD counter is a special type of a digital counter which can count to ten on the
application of a clock signal. To make a digital counter which counts from 1 to 10, we
need to have the counter count only the binary numbers 0000 to 1001. That is from 0 to 9
in decimal. Digital counters count upwards from zero to some pre-determined count
value on the application of a clock signal. Once the count value is reached, resetting them
returns the counter back to zero to start again. A decade counter counts in a sequence of
ten and then returns back to zero after the count of nine.
LED display is very popular and it can display digits from 0 to 9 in BCD Counter LEDs
are arranged as shown in below fig D.

Fig 4: Interfacing diagram for BCD counter

14
Flowchart for BCD Counter

Start

Initialize Counter A=00

Move count to port 0 LED

Increment count

Adjust result of A to BCD

Delay

Check counter

IS
Counter=9

Stop

15
Program for BCD Counter
BACK:MOV A,#00H
LOOP5: MOV P0,A
ADD A,#01H
DA A
ACALL DELAY
CJNE A,#99,LOOP5
SJMP BACK
DELAY:
MOV R0,#0FFH
MOV R2,#08H
LOOP2: MOV R1, #0FFH
LOOP1: DJNZ R1, LOOP1
DJNZ R0,LOOP2
DJNZ R2,LOOP2
RET
END
HEX Counter:
The hexadecimal number system, also called base-16 or sometimes just hex, is a number
system that uses 16 unique symbols to represent a particular value. Those symbols are 0-
9 and A-F.The number system that we use in daily life is called the decimal or system
and uses the 10 symbols from 0 through 9 to represent a value.

Fig 5: Interfacing diagram for HEX counter

16
Program for Hex Counter-
ORG 0000H
BACK: MOV A,#00H
AGAIN: MOV P0,A
INC A
ACALL DELAY
CJNE A,#0FFH, AGAIN
SJMP BACK
DELAY:
MOV R0,#0FFH
MOV R2,#08H
LOOP2: MOV R1, #0FFH
LOOP1: DJNZ R1, LOOP1
DJNZ R0,LOOP2
DJNZ R2,LOOP2 RET
END

Conclusion:
Thus we have interfaced microcontroller 89x516 with LEDs and observed LED flashing,
BCD counter Hex Counter.

17
Experiment No.3
TITLE: -WAVEFORM GENERATION USING DAC
Aim:- Interface microcontroller 89x516 to DAC and observe different analog signals-1.
Ramp wave 2. Triangular wave 3. Square wave 4. Sine wave
Apparatus:- 89v51 microcontroller kit, connecting wires.
Software:- Keil software, SST Flash etc
Theory:-
DAC 0808
The DAC 0808 is an 8-bit current output monolithic DAC manufactured by the National
semiconductor corporation. It is a 16 - pin IC available in dual in line DIP plastic
package. The analog output is available in the form of current Io. That means Io is
proportional to the 8-bit digital input. The important features of DAC 0808 are as
follows:
Features of DAC 0808:
Fast settling time 150 nsec. typically
Power supply voltage range +-4.5 mW at +-5V
Low power consumption 33 mW at +-5V.
High speed multiplying input slew rate 8 mA/ micro sec.
Interfaces directly with TTL, DTL, and CMOS logic levels.
Pin Configuration and Functional Block Diagram
The pin configuration and functional block diagram of DAC 0808 are as shown in Fig(a)
and Fig(b) respectively.

Fig1: Pin configuration of DAC 0808

18
 The internal block diagram shows that DAC 0808 consists of R-2R ladder along
with current switches and reference current amplifier.
 A1 to A8 are the 8-digital input lines with A1 as the most significant bit and A8 as
the least significant bit.
 The analog output is available in the form of current Io, therefore we need to use an
external current to voltage converter if the analog output in the form of voltage is
required.
 DAC 0808 requires a dual polarity (+-) supply voltage, typically +- 15V, for its
operation. The reference voltage can be either positive or negative.
 An external reference voltage should be applied to either Vref(+) or Vref(-)
depending on the polarity of reference voltage.

Fig2: Functional block diagram of DAC 0808


Interfacing DAC to 0808
Fig(c) shows the interfacing of DAC 0808 to 8051.
- The output of DAC is a current which is converted into voltage using op-amp based
current-to-voltage (I-V) converter.
- The analog output current Iout of the DAC depends on the Iref flowing into the Vref
terminal and the status of the D0 - D7 bits. It is expressed as,
Iout=Iref((D7/2)+(D6/4)+(D5/8)+(D4/16)+(D3/32)+(D2/64)+(D1/128)+(D0/256))
Where,D7=MSB
Iref depends on Vref voltage and the resistors 1Kohm and 1.5Kohm connected.
Iref=Vref/(1K+1.5K)=5V/2.5Kohm=2mA

19
(a) If D0-D7= FFH then
Iout=2mA*((1/2)+(1/4)+(1/8)+(1/16)+(1/32)+(1/64)+(1/128)+(1/256))
= 2mA*(255/256)
=1.99mA
Vout=1.99mA*5Kohm
=9.96V
(b) If D0-D7=80H then
Iout=2mA*((1/2)+(0/4)+(0/8)+(0/16)+(0/32)+(0/64)+(0/128)+(0/256))
=1mA
Vout=1mA*5Kohm=5V
(c) If D7-D0=00H then Iout=0,Vout=0V
DAC is commonly used in waveform generation.
Interfacing Diagram:

Fig 3: Interfacing of DAC 0808 to 89x516RD2

20
Flow chart for Square Wave

Start

Move 00H to port0

Delay

Move 0ffh to port0

Delay

21
Flowchart of Trangular Wave

Start

A=00

P0 A

Increment A

Check content of A
with FF

Is
No
A=0FF?

Yes

P0 A

Decrement A

Check content of
A with 00

IS
No A=00H

22
1. Program for Ramp wave-
ORG 0000H
MOV A,#00H
AGAIN: MOV P0,A
INC A
SJMP AGAIN
END

2. Program for Sinewave :-


ORG 0000H
BACK:MOV DPTR, #TABLE
MOV R0,#13
AGAIN:MOV A,#00H
MOVC A,@A+DPTR
MOV P0,A
INC DPTR
DJNZ R0,AGAIN
SJMP BACK
TABLE:DB 128,192,238,255,238,192,128,64,17,00,17,64,128
END

3. Program for Square Wave


ORG 0000H
AGAIN: MOV P0,#00H
ACALL DELAY
MOV P0,#0FFH
ACALL DELAY
SJMP AGAIN
DELAY: MOV R0,#10H
LOOP2:MOV R1,#10H
LOOP1:DJNZ R1,LOOP1

23
DJNZ R0,LOOP2
RET
END
4. Program for Triangular wave
ORG 0000H
START:MOV A,#00H
LOOP1: MOV P0,A
INC A
CJNE A,#0FFH,LOOP1
MOV A,#0FFH
LOOP2:MOV P0,A
DEC A
CJNE A,#00H,LOOP2
SJMP START
END

Conclusion: -
Thus we have performed the interfacing of 89x516 microcontroller with DAC logic
board and generated square wave, ramp wave, triangular wave and sine wave.

24
Experiment no:4
TITLE: - LCD INTERFACING
Aim: - Interfacing of LCD with microcontroller 89x516.
Apparatus:- 89x516 microcontroller kit, connecting wires.
Software:- Keil software, SST Flash etc
Theory:-
Interfacing of liquid crystal Display (LCD)
LCD displays are widely used because of its low current consumption as compared to
SSD. Also that LCD CAN BE used to display any character as it uses to display any
character as it uses a 5*6 dot matrix to display.
LCD Pin Description
PINS SYMBOL FUNCTION
1 VSS Ground
2 VCC +5v supply
3 VEE Power supply to control contrast
4 RS Should be ‘0’ for instruction and ‘1’ for data
5 R/W Should be ‘0’ to write and ‘1’ to read
6 E Enable display logic
7 D0 Data bus bit0
8 D1 Data bus bit1
9 D2 Data bus bit2
10 D3 Data bus bit3
11 D4 Data bus bit4
12 D5 Data bus bit5
13 D6 Data bus bit6
14 D7 Data bus bit7 (also used as busy pin)

Table: Pin description of LCD

25
(A)RS: Registers Select
 There are two register of LCD viz. Instruction command code register and data
register. The Rs pin is used to select one of these register.
 IF RS=0 the instruction command code register is selected and if RS =1 The data
register is selected allowing user to send data to be displayed on the LCD.
(B) R/w: Read/Write
 R/W pin is used to read or write to LCD.
 IF R/W=0 the user can write information to the LCD and if R/W=1 the user can
read information.
(C) Enable: E (it can be also be denoted by EN)
 The enable pin E. is used to latch the data into the data or command register.
When data is supplied a high-to low (negative edge) is required for LCD to latch
the data.
(D)D0-D7 OR DBO-DB7
 The data pins DO-D7 are used to send to send information to the LCD or read the
contents of LCD internal register.
 To display letter and number we send ASCIT code for letter A-Z, a-z and
numbers 0-9 while making RS=1.
 The ASCII code that is to be displayed is of 8 bits. it is send to the LCD in either
nibbles or bytes i.e.4 or8 bits at a time .
 the two primary modes of operation to send parallel data are 4or 8 bits .
 If four bit mode is used two nibbles of data are sent to do an 8 bit transfer. The
“E” clock is used to initiate the data transfer. Atleast 6 I/O pins must be available
for 4 bit mode.
 In 8 bit mode atleast 10 I/O pins must be used .This mode is used when
application needs speed.
(E) VCC, VSS and VEE
VCC-provides +5 supply
VSS-Ground
VEE-is used for controlling LCD contract.

26
Cursor Addresses for LCDs:

LCD Command:
 The list of commands that can be given to the LCD are as listed in the table.
HEX COMMAND FUNCTION
0x01 Clear display
0x02 Return cursor to home
0x04 Decrement cursor (i.e. shift cursor left)
0x06 Increment cursor (i.e. shift cursor right)
0x05 Shift display right
0x07 Shift display left
0x08 Display off, cursor off
0x0A Display off, cursor on
0x0C Display on, cursor off
0x0E Display on, cursor on
0x0F Display on, cursor on and blinking
0x10 Move cursor on position left
0x14 Move cursor on position right
0x18 Shift entire display left
0x1C Shift entire display right
0x80 Move cursor to beginning of first line
0xC0 Move cursor to beginning of second line
0x38 Initialize 2 line display of 5x7 matrix
FIG. LCD commands

27
Initialization of LCD
The following algorithm is required to initialize and write data to LCD:
 Wait second after power up for display to stabilize.
 Initialize the LCD by giving the instruction 0x38 to command subroutine.
 Wait for 5 msec.
 Issue the command 0x0F to command subroutine or display on, cursor on and
cursor blinking.
 Wait for 5 msec.
 Issue the command 0x01 for clearing display to command subroutine.
 Wait for 5 msec.
 Issue the command oxo6 for making LCD in increment mode i.e. cursor should
increment after every character is written to command subroutine.
 Wait for 5 msec.
 Issue the command 0x80(to command subroutine), to position the cursor at 1 st line
1st character.
 Issue the data character one by one giving their ASCII value using data
subroutine.
Command subroutine
 Give the instruction to the port connected to data bus of the LCD.
 Make RS= ‘0’, TO indicate instruction.
 Make R/W= ‘0‘to indicate write.
 Make E= ‘1’
 Wait for 12usec.
 Make E= ‘0’
 Return.
Data subroutine
 Check if LCD is ready by calling ready subroutine.
 Give the data to the port connected to the data bus of the LCD.
 Make RS= ‘1’, to indicate data
 make R/W=’0’ indicate write

28
 Make E= ‘1’
 Wait for 12usec.
 Make E= ‘0’
 Return
Ready subroutine
 Make the busy pin (i.e. data bus bit 7) = ‘1’ to program the corresponding point
pin of 8051 as input port.
 Make RS= ‘0’ to indicate instruction.
 Make R/W=’1’, to indicate read.
 Make E=0
 Make E=1
 check if busy pin=’0’.if it is ‘1’,indicates LCD is busy ,hence again make
E=’0’,then E= ‘1’ and check busy pin repeat this until busy pin. repeat this until
 Return.

Interfacing LCD Module with 8051

Fig: Interfacing diagram of 89x516

29
Program:-
ORG 0000H
MOV A,#38H
ACALL COMMAND
MOV A,#0CH
ACALL COMMAND
MOV A,#06H
ACALL COMMAND
MOV A,#01H
ACALL COMMAND
MOV A,#80H
ACALL COMMAND
MOV A,#'P'
ACALL DISPLAY
MOV A,#'R'
ACALL DISPLAY
MOV A,#'E'
ACALL DISPLAY
MOV A,#'C'
ACALL DISPLAY
SJMP $
COMMAND:ACALL READY
MOV P2,A ;MOVE COMMAND BYTE TO PORT 0
CLR P1.0 ;RS=COMMAND
CLR P1.1 ;RW=WRITE
SETB P1.2 ;SET ENABLE
NOP
CLR P1.2 ;CLEAR ENABLE
RET
DISPLAY:ACALL READY
MOV P2,A ;MOVE COMMAND BYTE TO PORT 0

30
SETB P1.0 ;RS=DATA
CLR P1.1 ;RW=WRITE
SETB P1.2 ;SET ENABLE
NOP
CLR P1.2 ;CLEAR ENABLE
RET
READY:MOV R0,#0FFH
LOOP2:MOV R1,#0FFH
LOOP1:DJNZ R1,LOOP1
DJNZ R0,LOOP2
RET
END

Conclusion: -
Thus we have interfaced LCD with microcontroller 89x516 and displayed the message
string on LCD.

31
Experiment No 5
TITLE:- INTERFACING OF STEPPER MOTOR WITH 89x516
Aim: - Interfacing of 89x516 microcontroller with stepper motor using timer.
Apparatus:- 89X516 microcontroller kit, STEPPER MOTOR logic interfacing card,
connecting wires.
Software:- Keil, SST Flash etc.
Theory:-
A stepper motor is a device that translates electrical pulses to mechanical
movement. It is used in applications like robotics, dot-matrix printers, disk drives for
position control. Stepper motors have a permanent magnet rotor surrounded by a stator.
Generally the stepper motors have four stator windings that are paired with
common center tap as shown in fig.1. Such a stepper motor is called as four phase or
unipolar stepper motor. With the help of center tap the current direction in each of the
two coils can be changed when a winding is grounded. This results in a polarity change
of stator.
The stator is responsible for the direction of the rotation by the stator poles. The
stator poles are determined by the current sent through the wire coils. As the direction of
current is changed, the polarity is also changed causing the reverse motion of the stepper
motor.
The stepper motor has 6 leads, 4 leads representing 4 stator windings and 2 leads
for 2 commons for the center tapped leads. On application of power to the stator the rotor
rotates.

32
Fig.1: stator windings configuration

Fig.2: Interfacing of stepper motor with 89x516


There are many sequences for rotation. Each sequence has a different degree of precision.
Normal four step sequence has 1.8° angle per step whereas eight step sequence has 0.9°
angle per step.

33
Table 1. shows four stepping sequence whereas Table 2. Shows eight stepping sequence.
Although we can start with any sequence i.e. clockwise or anticlockwise. If we start with
the sequence as 1,2,3,4 then motor will rotate in clockwise direction(RRC) and if we start
with the sequence as 4,3,2,1 then motor will rotate in anticlockwise direction(RLC).
Table 1. Four step sequence(Full stepping sequence)
STEP WINDING A1 WINDING A2 WINDING A3 WINDING A4
1 1 0 0 1
2 1 1 0 0
3 0 1 1 0
4 0 0 1 1
Table 2 . Eight step sequence(Half stepping sequence)
STEP WINDING A1 WINDING A2 WINDING A3 WINDING A4
1 1 0 0 1
2 1 0 0 0
3 1 1 0 0
4 0 1 0 0
5 0 1 1 0
6 0 0 1 0
7 0 0 1 1
8 0 0 0 1
Sample programs:
1) An ALP to rotate stepper motor continuously on clockwise direction.
MOV A,#99H
LABEL: MOV P1, A
RRC A
ACALL DELAY
SJMP LABEL
DELAY: MOV R3,#100H
BACK: DJNZ R3, BACK
RET

34
2) An ALP to rotate stepper motor in clockwise direction for 180° angle.
Asuume, 4 step mode. Hence for 4 step mode , 1 pulse=1.8° rotation.
Therefore, count=180°/1.8°=(100)d=(64)h
MOV R1,#64H
LABEL1:MOV DPTR,#CODE
MOV R0,#04H
LABEL2:CLR A
MOV A,@A+DPTR
MOV P1,A
ACALL DELAY
DJNZ R1, LABEL3
SJMP LABEL4
LABEL3:INC DPTR
DJNZ R0,LABEL2
SJMP LABEL1
LABEL4:STOP
CODE:DB 09H,0CH,06H,03H
DELAY:MOV R3,#100H
BACK:DJNZ R2,BACK
RET
Timer Mode Register (TMOD):-
Both timers 0 and 1 use the same register, called TMOD (timer mode), to set the
various timer operation modes TMOD is a 8-bit register,
The lower 4 bits are for Timer 0 and the upper 4 bits are for Timer 1.
In each case, the lower 2 bits are used to set the timer mode and the upper 2 bits to
specify the operation.
(MSB) (LSB)
GATE C/T M1 M0 GATE C/T M1 M0
TIMER1 TIMER0
Fig.4.: TMOD Register

35
GATE:- Gating control when set.
Timer /Counter is enabled only while the INTX pin is high and TRX control pin is set .
When cleared, the timer is enabled whenever the TRX control bit is set.
C/T:- Timer or counter selected
Cleared for timer operation (input from input system clock). Set for counter operation
(input from TX input pin).
M1 :- Mode Bit 1
M0 :- Mode Bit 0
M1 M0 MODE Operation Mode
13-bit timer mode
0 0 0 8-bit timer/counter THx with TLx as 5-bit
prescaler
16-bit timer mode
0 1 1 16-bit timer/counter THx and TLx are
cascaded; there is no prescaler
8-bit auto reload
8-bit auto reload timer/counter; THx holds
1 0 2
a value which is to be reloaded TLx each
time
1 1 3 Split timer mode

Interrupts Enable Register:-


The interrupt can be enabled by software in order to respond microcontrollers. And the
register which is responsible for enabling and disabling the interrupts is called interrupt
enable register.To enable an interrupt, we take the following steps:
D7 D0
EA --- ET2 ES ET1 EX1 ET0 EX0
Fig.4: Interrupt Enable Register

36
EA IE.7 Disables all interrupts.

__ IE.6 Not implemented, reserved for future use.


ET2 IE.5 Enables or disables timer 2 overflow or capture interrupt (8952).
ES IE.4 Enables or disables the serial port interrupt.
ET1 IE.3 Enables or disables timer 1 overflow interrupt.
EX1 IE.2 Enables or disables external interrupt 1.
ET0 IE.1 Enables or disables timer 0 overflow interrupt.
EX0 IE.0 Enables or disables external interrupt 0.

Interrupt Vector Table For The 89v51:-


Interrupt ROM Location Pin Flag Clearing
Reset 0000 9 Auto
External HW (INT0) 0003 P3.2 (12) Auto
Timer 0 (TF0) 000B Auto
External HW (INT1) 0013 P3.(13) Auto
Timer 1 (TF1) 001B Auto
Serial COM (RI and TI) 0023 Programmer clears it.

Theoretical Calculation:-
For Timer:-
As we are using timer 0 and mode 1 so from the diagram of TMOD Register the value
that to be loaded in TMOD can be calculated as follows.
GATE C/T M1 M0 GATE C/T M1 M0
TIMMER 1 TIMER 0

0 0 0 0 0 0 0 1

Therefore TMOD = 01H

For Interrupt :-

37
We know that the interrupt register can be configured from the following diagram.
EA --- ET2 ES ET1 EX1 ET0 EX0

Here we are using timer 0 mode without any external interrupt. And also we know that
EA always equal to 1 to respond the microcontrollers.
Therefore value of interrupt register can be as follows.
1 0 0 0 0 0 1 0

Therefore IE = 82H

38
Flow chart:

Start

Mov 63H to Accumulator

Enable Timer Interrupt

Initialize Timer in mode 1

Load count in TH0 & TL0

Start Timer

Wait for in Interrupt

39
Flow chart for ISR

Start

Transfer Code to stepper

Motor

Generate Next code

Load count in TL0 & TH0

Start timer

RET

40
Program:-
ORG 0000H
LJMP 0030H
ORG 000BH
ACALL STEP
RETI
ORG 0030H
MOV R0,#04H
MOV DPTR,#TABLE
MOV IE,#82H
MOV TMOD,#01H
MOV TL0,#80H
MOV TH0,#80H
SETB TR0
HERE:SJMP HERE
STEP:
MOV A,#00H
MOVC A,@A+DPTR
MOV P1,A
INC DPTR
DJNZ R0,AHEAD
MOV R0,#04H
MOV DPTR,#TABLE
AHEAD: MOV TL0,#50H
MOV TH0,#80H
SETB TR0
RET
TABLE: DB 0C3H, 93H, 33H, 63H
END

41
Conclusion: -
Thus we have performed the interfacing of 89x516 microcontroller with
STEPPER MOTOR using timer to see the rotation of STEPPER MOTOR..

42
EXPERIMENT NO. 6
TITLE:- INTERFACE OF LED, RELAY AND BUZZER WITH PIC
18F4520
AIM:- To write a program for interfacing Relay, Buzzer, LED and switch with
PIC18F4520
OBJECTIVE:-
• When button1 is pressed relay and buzzer is turned on and LED start changing
from left to right.
• When button 2 is pressed relay and buzzer is turned off and LED start changing
from right to left.
APPARATUS:- PIC 18F4520 Trainer Kit, MPLAB Software, PC.
THEORY:-
Electromechanically Relay:-
Relay is an electrically controllable switch widely used in industrial, control, automobile
and appliances. It allows the isolation of two separate section of system with two
different voltage source for e.g. 5V system can be isolated from 120V system by placing
a relay between them.
The electromagnetic relay have three component the coils, spring and contact as shown in
figure. A digital 5 volt on left side and 12 volt motor on the right side without physical
contact between them. When current flows through the coil a magnetic field is created
around the coil which causes the armature to be attracted to the coil .The armature contact
acts like a switch and closes or opens in the circuit hence coil not energized, a spring
pulls the armature to its normal state of open or closed state. There are all types of relays
for all kinds of applications. In choosing relays to the following characteristics need to be
considered.

43
The contact can be normally open (NO) or normally closed (NC)
A) In the NC type, contacts are closed when the coil is not energized in
B) in NO the contact are open when the coil is un energized.
There can be one or more contact for example we have SPST,SPDT and DPDT relays.
These voltage required is from few volts to 50 volts and current is few mA to 20A to
energized the relay below which the coil will not be energized.
The minimum voltage is called “pull in voltage”, if coil voltage is 5V and the coil
resistance is 500 ohm therefore we need minimum of 10mA i.e. 5V/500 ohm=10mA.
The Ac/Dc Max range is from few volts to 100 of volts and current from few amperes to
40 amperes or more.
Single Pole Single Throw (SPST)
This type of relay has total of four terminal out of which two terminal can be connected
or disconnected. The other two terminal are needed for the coil.

44
Single Pole Double Throw (SPDT)
This relay has a total of five terminal out of these two are the coil terminals. A common
terminal is also included which connected to either of two terminal.

Double Pole Single Throw (DPST)


This relay has total six terminals. These are further divided into two pairs.
Thus they can act as two SPST switch are actuated by a single coil out of six terminal two
of them are coil terminal

Double Pole Double Throw (DPDT)


These is the biggest of all it has mainly eight row terminal out of these two rows are
designed to be over terminals they are designed to act as two SPDT relays which are
actuated by a single coil.
Relay Applications:-
• Relay are used to realize the logic functions. They play very important role in
providing safety critical logic.
• Relay are used to provide time delay function they are used to time the delay open
and delay closed of contact.

45
• The relay are used to control high voltage circuit which with the help of low voltage
signals similarly they are used to control high current circuit with the help of low current
signal.
• They are also used as protective relays by this function all the faults during transmission
and reception can be detected and isolated.
Working of Switch:-
A microcontroller can’t make any decision on controlling something in the outside world
without sensing something about it the simplest thing for microcontroller to check the
status of switch. It is open or closed. Switches can sense if a door is open or close a limit
switch can detect is a part of machine has reached a certain purposes but they can be in
only one of two states on closed or off.
Fig shows the common ways to interface switch to auc i/p P0 uses P1 as a pull up. If
switch 1 is open P0 will be high and read as logical one when switch is closed pin P0 is
shorted to ground. Or 0V and P0 will read as logical 0.
R1 and R2 as a pull down resistor when switch is open, R1 is pulled low and read as
logical a closed switch is open R1 is pulled low R2 closed in the voltage at R1 to the Vcc
level at the point R1 will read as logical 1
Switch Bounce:-
An unfortunate character of switch is that they don’t switch clearly. The mechanical
component of switch vibrate back and for making a number of momentary contact before
final setting down to final state.
A microcontroller to operate thousands of times faster than mechanical switch. It would
see each of the such pulses as individual switch opening and close. The switch is used
for counting event the microcontroller would be many times the turn every in another
application the switch might be used to turn a light or a motor on and off.

46
Interfacing Diagram:-

47
FLOWCHART:-
START

Declare pin from port to relay, switch, buzzer and LED

Set I/O direction pins

No
Ye
Is SW2 =1 & s

SW1=0 ?
?

Relay on Relay on

Buzzer on Buzzer on

LED glowing from left to Left LED glowing from left to right

48
PROCEDURE:-
1. Connect +12V supply to universal (PIC) board.
2. Connect the Serial cable to the pc
3. Switch on the power supply
4. Open the MPLAB IDE
5. Go to Project Project WizardNextSelect PIC18F4520 ICNext select the
Microchip C18 Tool suite  Browse the particular folder where you have to create
project file  Next tab Next tab Finish tab
6. Go to File  New  Write the C program in this new window.
7. Go to File  Save the file with .C extension (e.g. led.C)
8. Right click on the Source files  add led.C file in to the source file.
9. Right click on the Linker script  add 18F4520.lkr file the path is given below
C:\MCC18\LKR\18F4520.lkr.
10. Go to Project Build options project add 3 files here
Go to show directories for
Select Include search path New  browse C:\MCC18\h
Select Library search path New  browse C:\MCC18\lib
Select Linker-script search path New  browse C:\MCC18\lkr
Click on Apply OK
11. Project  build all  build succeeded message will display on window.
To Create Hex File
12. Go to File (Mplab 8.30 Software) Go to Export Write Start address 0x400 and
end address 0x7ffe Unclick all three below option Click on Ok Save that file in
to created folder(Which is created at the time of save writing program)  Hex file is
created
Program Download
13. Open ANI1310ui Software  Turn On The Kit. Press Reset Key on
BoardClick On Red Square Button Green window is displayed. Go to File
Open created Hex file folder Hex file is display Click on down arrow
(Write Devices) wait for write complete. Click on Green arrow to run the
program.

49
PROGRAM AND RESULT: -
#INCLUDE<P18F4520.H>
//ID MSDELAY(UNSIGNED INTITIME);
//ID RIGHT(VOID);
//ID LEFT(VOID);
//EFINE SW2 PORTBBITS.RB0
//EFINE SW1 PORTBBITS.RB1
//EFINE RELAY PORTBBITS.RB2
//NCLUDE<P18F4520.H>
#PRAGMA CONFIG OSC=HS
#PRAGMA CONFIG PWRT=OFF
#PRAGMA CONFIG WDT=OFF
#PRAGMA CONFIG DEBUG=OFF, LVP=OFF
VOIDMSDELAY(UNSIGNED INTITIME);
VOID RIGHT(VOID);
VOID LEFT(VOID);
#DEFINE SW2 PORTBBITS.RB0
#DEFINE SW1 PORTBBITS.RB1
#DEFINE RELAY PORTEBITS.RE2
UNSIGNEDINTI,DATA;
VOID MAIN()
{
TRISD=0X00;
TRISE=0X00;
PORTD=0X00;
ADCON1=0X0F;
TRISBBITS.TRISB0=1;
TRISBBITS.TRISB1=1;
TRISBBITS.TRISB2=0;
SW1=SW2=1;
RELAY=0;

50
WHILE(1)
{
IF (SW2==1 & SW1==0)
{
RELAY=0;
RIGHT();
RELAY=0;
}
IF (SW1==1 & SW2==0)
{
RELAY=1;
LEFT();
RELAY=1;
}
}
}
VOID RIGHT(VOID)
{
RELAY=0;
PORTD=0X00;
RELAY=0;
WHILE(SW2 !=0)
{
DATA=0X01;
FOR(I=0;I<8;I++)
{
RELAY=0;
PORTD=DATA;
DATA=DATA<<1;
IF (SW2==0)
{RELAY=1;LEFT();}

51
MSDELAY(5);
}
}
}
VOID LEFT(VOID)
{
RELAY=1;
PORTD=0X00;
RELAY=1;
WHILE(SW1 !=0)
{
DATA=0X80;
FOR(I=0;I<8;I++)
{
RELAY=1;
PORTD=DATA;
DATA=DATA>>1;
IF (SW1==0)
{RELAY=0;RIGHT();}
MSDELAY(5);
}
}
}
VOIDMSDELAY (UNSIGNED INTITIME)
{
INTI,J;
FOR(I=0;I<ITIME;I++)
FOR(J=0;J<1275;J++);

52
CONCLUSION:-
Hence we interfaced LED , Relay and Buzzer to Pic18 microcontroller. When
button1 is pressed relay and buzzer is turned on and LED start changing from left
to right. When button 2 is pressed relay and buzzer is turned off and LED start
changing from right to left.

53
54
EXEPERIMENT NO 07
TITLE:-KEYPAD INTERFACING
AIM: -Interface 4*4 keypad and LCD with PIC microcontroller.
THEORY:-
Interface 4*4 keypad matrix to PIC microcontroller.
At the lowest level keyboards are organize in a matrix of rows and columns. The CPU
accessesbetween rows & columns through parts therefore with two 8-bits parts. An 8*8
matrix of keys can be converted to a microprocessor when a key is pressed a row
&column make a constant. Otherwise there is a one connection between rows & columns.
There is no connection In IBMPC keyboards. A single microcontroller takes care of
hardware & software interfacing of the keyboard in such a system. The program stored in
the ROM of microcontroller scan the keys continuously identify which one has been
achieved and presentsit to the mother board.
In programming for keyboard interfacing we must two processor.
1. Key press detection &
2. Key identification.
There are two ways by which the PIC18 can perform key press detection.
1.Interrupt method &
2.Scanning method.
1. Interrupt method for key press detection:
Fig. shows a 4*4 matrix keyboard converted to part B. the rows are connected to PORTB
low(RBB-RBD) and the columns are connected to PORTBhigh (RB7-RB9) which is the
PORTB channel interrupt.

The four pins of the PORT(RB7-RB4) can cases as interrupt. When any changes are
detected an any one of them they are referred to as “PORTB-change interrupt” to
distinguish them for the JNT0-JNT2 interrupt which are also located on port(RB0-
RB2)PORTB change interrupt has a single interrupt flag called RBIF & is located in
INTCON. Register also theRBI3 bit for enabling the PORTB- change interrupt any
changes interrupt any changes on the RB7-RB4 PIN will cause an interrupt indicating a
key press.

55
Examine program,which goes through the steps:-
1. To make sure that the proceeding key has been reduced are O/P to all rows at
once & the column are read &checked repeatedly with all the column are high
when all columns are found to be High. The program waits for a short amount of
time before it goes to the next stage of waiting for key to be pressed
2. To see if any key is pressed the columns are connected for the PORTB. Changes
interrupt. Therefore any key press causes an interrupt.
a. Every that the first key press detection was not resources to spike noise &
b. Wait 20 ms to the same key press from being interrupted as multiple key press
fig. shows the keyboard debounce.
3. To detect which row the key press belongs to the microcontroller grovels row at
the time reading the corner each time if it finds that all columns are HIGH this
means that the key press cannot belong to that row, therefore it ground the next
row & column until it finds the row that the key press belongs to it sets up the
starting address for the loop up table holding the scan code (or the ASCIIvalue)
for that row& goes to the nextstage to identify the key.
4. To identify the key press the microcontroller rotates the column bits, one bit at the
time in to the carry may & checks to see if it is low open finding the zero it puts
out the ASCITcode for that lookup table otherwise it increment the pointer to
paint to the next element of the loop up table.
2. Scanning method for key press detection:-
Another method for key press detection is by scanning. In this method the detection is by
the microcontroller grounds all rows by providing to the O/P latch then it read the
columns if the data read from the column are equal to 1111, no key has been pressed &
process continue a until a key press is detected. If one of the column a zero however this
method that a key pressed has occurred. After key press is detected microcontroller will
go through the process of identifying the key staring with the key staring with the top
row. Microcontroller ground it by providing a row to the threat row only their it read the
column if the data is read is all at no key in that row is activated & that the process is
moved to the next row is identified of the row is which the key has been pressed key

56
belong to. This should be easy since the microcontroller knows the time which row &
column are being accessed.

57
SCANNING METHOD

58
PROCEDURE:-

1) Connect +12V supply to universal (PIC) board.


2) Connect the Serial cable to the pc
3) Switch on the power supply
4) Open the MPLAB IDE
5) Go to Project Project WizardNextSelect PIC18F4520 ICNext select the
Microchip C18 Tool suite  Browse the particular folder where you have to create
project file  Next tab Next tab Finish tab
6) Go to File  New  Write the C program in this new window.
7) Go to File  Save the file with .C extension (e.g. led.C)
8) Right click on the Source files  add led.C file in to the source file.
9) Right click on the Linker script  add 18F4520.lkr file the path is given below
C:\MCC18\LKR\18F4520.lkr.
10) Go to Project Build options project add 3 files here
Go to show directories for
Select Include search path New browse C:\MCC18\h
Select Library search path New browse C:\MCC18\lib
Select Linker-script search path New browse C:\MCC18\lkr
Click on Apply OK
11) Project  build all  build succeeded message will display on window.
To Create Hex File
12) Go to File (Mplab 8.30 Software) Go to Export Write Start address 0x400 and
end address 0x7ffe Unclick all three below option Click on Ok Save that file in to
created folder(Which is created at the time of save writing program)  Hex file is
created
Program Download
13) Open ANI1310ui Software  Turn On The Kit. Press Reset Key on BoardClick
on Red Square Button  Green window is displayed. Go to File Open created Hex
file folder Hex file is display Click on down arrow (Write Devices) wait for write
complete. Click on Green arrow to run the program.

59
INTERFACING DIAGRAM:-

60
PROGRAM AND RESULT: -
// ====4X4 KEYBOARD AND 7 SEGMENT DISPLAY =====
// FOR COL PORTC0 TO PORTC3 & ROW PORTC4 TO PORTC7
// 7 SEGMENT CONNECTION PORTB0 TO PORTB3
// PORT LINES FOR 7 SEGMENT USING PORTD
#INCLUDE <P18F4520.H>
#PRAGMA CONFIG OSC=HS
#PRAGMA CONFIG PWRT=OFF
#PRAGMA CONFIG WDT=OFF
#PRAGMA CONFIG DEBUG=OFF, LVP=OFF
#PRAGMA CONFIG FCMEN=ON
#PRAGMA CONFIG XINST=OFF
#DEFINE LDATA PORTD
#DEFINE RS PORTEBITS.RE0
#DEFINE RW PORTEBITS.RE1
#DEFINE EN PORTEBITS.RE2
VOIDDELAY_MS(UNSIGNED INTMS_COUNT);
VOIDDELAY_US(UNSIGNED INTUS_COUNT);
VOIDLCD_INIT(VOID);
VOIDLCD_CMDWRITE(CHAR CMD);
VOIDLCD_DATAWRITE(CHAR DATA);
//VOID LCD_DISPLAYSTRING(CHAR *STRING_PTR);
//CHAR HEXDIGIT(UNSIGNED N);
//VOID CHARTOHEX(UNSIGNED CHAR C);
#DEFINE R1 PORTBBITS.RB0
#DEFINE R2 PORTBBITS.RB1
#DEFINE R3 PORTBBITS.RB2
#DEFINE R4 PORTBBITS.RB3
#DEFINE C1 PORTBBITS.RB4
#DEFINE C2 PORTBBITS.RB5
#DEFINE C3 PORTBBITS.RB6
#DEFINE C4 PORTBBITS.RB7

VOID MAIN(VOID)
{
TRISE=0X00;
TRISD=0X00;
ADCON1=0X0A;

61
TRISB=0X0F;
PORTB=0X0F;
LCD_INIT();
LCD_CMDWRITE(0X80);
LCD_DATAWRITE(' ');
DELAY_US(10);
LCD_CMDWRITE(0X81);
LCD_DATAWRITE('K');
DELAY_US(10);
LCD_CMDWRITE(0X82);
LCD_DATAWRITE('E');
DELAY_US(10);
LCD_CMDWRITE(0X83);
LCD_DATAWRITE('Y');
DELAY_US(10);
LCD_CMDWRITE(0X84);
LCD_DATAWRITE(':');
DELAY_US(10);
WHILE(1)
{
LCD_CMDWRITE(0X85);
C1=0;C2=C3=C4=1;
IF(R1 == 0){LCD_DATAWRITE ('F');} // DISPLAY 0
IF(R2 == 0){LCD_DATAWRITE ('B');} // DISPLAY 4
IF(R3 == 0){LCD_DATAWRITE ('7');} // DISPLAY 8
IF(R4 == 0){LCD_DATAWRITE ('3');} // DISPLAY C
C2=0;C1=C3=C4=1;
IF(R1 == 0){LCD_DATAWRITE ('E');} // DISPLAY 1
IF(R2 == 0){LCD_DATAWRITE ('A');} // DISPLAY 5
IF(R3 == 0){LCD_DATAWRITE ('6');} // DISPLAY 9
IF(R4 == 0){LCD_DATAWRITE ('2');} // DISPLAY D
C3=0;C1=C2=C4=1;
IF(R1 == 0){LCD_DATAWRITE ('D');} // DISPLAY 2
IF(R2 == 0){LCD_DATAWRITE ('9');} // DISPLAY 6
IF(R3 == 0){LCD_DATAWRITE ('5');} // DISPLAY A
IF(R4 == 0){LCD_DATAWRITE ('1');} // DISPLAY E
C4=0;C1=C2=C3=1;
IF(R1 == 0){LCD_DATAWRITE ('C');} // DISPLAY 3

62
IF(R2 == 0){LCD_DATAWRITE ('8');} // DISPLAY 7
IF(R3 == 0){LCD_DATAWRITE ('4');} // DISPLAY B
IF(R4 == 0){LCD_DATAWRITE ('0');} // DISPLAY F
}
}
VOIDLCD_INIT()
{
LCD_CMDWRITE(0X38);
DELAY_US(10);
LCD_CMDWRITE(0X0E);
DELAY_US(10);
LCD_CMDWRITE(0X06);
DELAY_US(10);
LCD_CMDWRITE(0X01);
DELAY_US(10);
}
VOIDLCD_CMDWRITE(CHAR CMD)
{
LDATA=CMD;
RS=0; //COMMAND
RW=0; //WRITE OPERATION
EN=1; //SEND HIGH TO LOW PULSE AT ENABLE PIN
DELAY_US(10);
EN=0;
DELAY_MS(1);
}
VOIDLCD_DATAWRITE(CHAR DATA)
{
LDATA=DATA;
RS=1;
RW=0;
EN=1;
DELAY_US(10);
EN=0;
DELAY_MS(1);
}
VOIDDELAY_US(UNSIGNED INTUS_COUNT)
{

63
WHILE(US_COUNT!=0)
{
US_COUNT--;
}
}
VOIDDELAY_MS(UNSIGNED INTMS_COUNT)
{
WHILE(MS_COUNT!=0)
{
DELAY_US(112); //DELAY_US IS CALLED TO GENERATE 1MS
DELAY
MS_COUNT--;
}
}

CONCLUSION:-
In this practical we studied-
1. Interfacing the key to the PIC 18fxx microcontroller by two Method.
a. Interrupts method for key press detection.
b. Scanning method for key press detection.
2. Detection of key & display the key the LCD screen.
3. Also the matrix keyboard connection to parts & keyboard de bouncing.

64
EXPERIMENT NO 08
TITLE:- GENERATION OF SQUARE WAVE
AIM: -To generate square wave using timer and interrupt
APPARATUS: -Universal PIC board, 10 pin FRC cable 12V, power supply, computer.
THEORY:-
1) Basic Register oftheTimer:
Many of the PIC18 timers are 16 bits wide because the PIC18 has an 8-bit architecture,
each 16 bit timer is accessed as to separate as to separate register of low byte
(TMRXL)and high byte (TMRHX). Each timer also has TCON (timer control) register
for setting mode of operation next we discuss each separately.
Timers 0 Registers and Programming:
Timer 0 can be used as an 8-bit or a 16-bit timer. The 16 bit registers of timer 0 is
accessed as low byte and higher byte as shown in figure. The low byte register is called
as TMRO(timer 0 low byte). The high byte register is referred to as TMROH (Timer 0
high byte). These register can be accessed like any other spherical function register for
example the instruction. “MOVFF TMROL” moves the value in register into TMROL,
the low byte of timer 0. These register can also be read like any other register e.g.
“MOVFF TMR0L, PORTB”copies TMROL (low byte of timer 0) to port B

D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0


Figure: Timer0 high and low registers
TOCON (Timer 0 Control Register):
Each timer has a control register called T0CON to set the various timer operation modes.
TOCON is an 8-bit register used for control to timer 0. The bits for T0CON are as shown
in figure
TMR0ON TM8BIT T0CS T0SE PSA T0PS2 T0PS1 T0PS0

1)TMR0ON:
D7 Timer 0 on and off control bit
1= Enable (start)timer0

65
0=stop timer 0
2) TO8BIT: D6
Timer 0 8-bit/16bit selector bit
1=Timer 0 is configured as an 8-bit timer counter
0= Timer 0 is configured as an16-bit timer/ counter
3) TOCS: D5
Timer o clock source select bit
1= external clock RA4/ T0CKI pin
0=Internal clock fosc/4 from external oscillator
4)TOSE: D4Timer 0 source edge select bit
1= Increment on high to lo transition on TOCKI pin
0=Increment on lo to high transition on TOCKI pin
5) PSA: TIMER O prescaler assignment bit
1= Timer 0 prescaler is not assigned. Timer 0 clock input bypasses scalar.
0= Timer o prescaler is assigned. Timer 0 clock input comes from prescalar output.
6)TOPS2-TOPS0:
Timer 0 prescaler select bits
T0PS2 T0PS1 T0PS0 Timer prescaler selector
0 0 0 1:2
0 0 1 1:4
0 1 0 1:8
0 1 1 1:16
1 0 0 1:32
1 0 1 1:64
1 1 0 1:128
1 1 1 1:256

66
T1CON (Timer 1 Control) Resister

RD16 - T1CKPS1 T1CKPS0 T10S0EN T1SYNC TMR1CS TMR1ON

RD16:- D7: 16 bit read / write enable bit


1 = Timer 16 bit is accessible in one 16-bit operation
0 = Timer 1 16-bit is accessible in two 8-bit operation
T1CKPS 2:- D5,D4 Timer1 prescalar selector
T1CKPS0:-
D5 D4 TIMER1 prescaler selection
0 0 1:8
0 1 1:4
1 0 1:2
1 1 1:1

T1OSCEN:- D3: Timer 1 oscillator enable bit


1 = Timer 1 oscillator is enable
0 = Timer 0 oscillator is shut off
T1SYNC:- D2: Timer 1 synchronization used only when TMR1CS =1 for counter mode
to synchronize external clock i/p
If TMR1CS = 0 this bit is not used.
TMR1CS:- D1: Timer clock source select bit
1 = external clock from pin RCO(1 T0CLK)
0 = internal clock (F0sc/4 from XTA1)
TMR1ON:- D0: Timer 1 on and off control bit
1 = enable (start) timer 1
0 = stop Timer 1
T2CON (Timer 2 control) Register
TOUTPS3 TOUTPS2 TOUTPS1 TOUTS0 TMR2S0 T2CKPS1 T2CKPS0

67
TOUT PS 3:- TOUT PS0:- D6-D3 Timer 2 o/p post scale select bit
D6:D3 SCALE SELECTION
0000 1:1 post scale Value
0001 1:2 post scale Value
0010 1:3 post scale Value
0011 1:4 post scale Value

TMR2ON:- D2: Timer2 on and off control bit


1 = Enable (start) Timer 2
0 = Enable (stop) Timer 2
T2CK PS1:T2CK PS0:-
D1 and D2 Timer 2 clock prescalar select binary
T2CLK PS1 T2CLK PS0 PRESCALER SELECT BIT
0 0 Prescalar 1
0 1 Prescalar 4
1 x Prescalar 16

T3CON (Timer 2 control) Register


RD16 T3CCP2 T3CKPS1 T3CKPS0 T3CCP1 T3CYNC TMR3C1 TMR3ON

RD16:- D7: 16 Bit read / write enable


1 = timer3 16 bit is accessible in one 16 bit operation
0 = timer3 16 bit is accessible in two 8 bit operation

T3CCP:T3CCP1:- Timer 3 and timer 1 to CCPX enable bit


0 0 = Timer 1 is the clock source for compare of CCP mode
0 1 = Timer 3 is the clock compare of the CCP2
1 X = Timer 3 is the clock source for compare 1 capture of the module

68
T3CKPS1:T3CKPSO:-
T3CKI PS1 T3CKI PS0 TIMER3 PRESCALER
SLECT BIT
0 0 1:1 Prescalar Value
0 1 1:2 Prescalar Value
0 0 1:4 Prescalar Value
1 1 1:8 Prescalar Value

T1SYNC:-D2
Timer3 external clock input synchronize control bit used only when TMR3CS=1 &
Clock comes from external source.
1 = Do not synchronize external clock input
0 = synchronize external clock input
TMR3CS:-D1
Timer 3 clock Source select bit
1 = External clock from pin T1OSIONT1 CKI
0 = Internal clock (F0SC14)
TMR3ON D0:-
1 = Enable (start) Timer 1.
0 = Stop Timer 1.
2] Timer delay calculation for XTAL= 10 MHz,With no prescalar

( FFFF-YYXX+1) X 0.4 us. Control YYXX value of the


Where XXXX are the TMROH TMROL,TMROH Resister to decimal
TMROL (Initial) value respectively. number then (65536-NNNNN)
Notice the YYXX value is in hex. X 0.4us.

69
PROCEDURE:-
1) Connect +12V supply to universal (PIC) board.
2) Connect the Serial cable to the pc
3) Switch on the power supply
4) Open the MPLAB IDE
5) Go to Project Project WizardNextSelect PIC18F4520 ICNext select the
Microchip C18 Tool suite  Browse the particular folder where you have to create
project file  Next tab Next tab Finish tab
6) Go to File  New  Write the C program in this new window.
7) Go to file  save the file with .C extension (e.g. led.C)
8) Right click on the Source files  add led.C file in to the source file.
9) Right click on the Linker script  add 18F4520.lkr file the path is given below
C:\MCC18\LKR\18F4520.lkr.
10) go to Project Build options project add 3 files here
Go to show directories for
Select Include search path new  browse C:\MCC18\h
Select Library search path new  browse C:\MCC18\lib
Select Linker-script search path new browse C:\MCC18\lkr
Click on Apply OK
11) Project  build all  build succeeded message will display on window.
To Create Hex File
12) Go to File (Mplab 8.30 Software) Go to Export Write Start address 0x400 and
end address 0x7ffe Unclick all three below option Click on Ok Save that file
in to created folder(Which is created at the time of save writing program)  Hex file
is created
Program Download
13) Open ANI1310ui Software  Turn On The Kit. Press Reset Key on BoardClick
on Red Square Button  Green window is displayed. Go to File Open created
Hex file folder Hex file is display Click on down arrow (Write Devices) wait for
write complete. Click on Green arrow to run the program.

70
FLOWCHART:-

START

Initialize the port A as the output port

Square bit =1 & delay

Square bit =0 & delay

Start timer 0 by setting TMR0ON=1

Stop timer 0 by making T1R0ON=0

Reset interrupt control resister

STOP

INTERFACING DIAGRAM:-

71
PROGRAM: -
#INCLUDE <P18F4520.H>
#PRAGMA CONFIG OSC=HS
#PRAGMA CONFIG PWRT=OFF
#PRAGMA CONFIG WDT=OFF
#PRAGMA CONFIG DEBUG=OFF, LVP=OFF
#PRAGMA CONFIG FCMEN=ON
#PRAGMA CONFIG XINST=OFF
//----------------------------------------------------------------------------
VOID MAIN (VOID);
VOIDINTERRUPTHANDLERLOW (VOID);
UNION
{
STRUCT
{
UNSIGNED TIMEOUT:1; //FLAG TO INDICATE A TMR0 TIMEOUT
UNSIGNED NONE:7;
} BIT;
UNSIGNED CHAR BYTE;
} FLAGS;
EXTERN VOID _STARTUP(VOID); //THIS IS DEFINED IN THE C RUNTIME
#PRAGMA CODE RESET_VECTOR_SECTION=0X400
VOIDRESET_VECTOR(VOID)
{
_ASMGOTO _STARTUP _ENDASM
}
//----------------------------------------------------------------------------
// HIGH PRIORITY INTERRUPT VECTOR
#PRAGMA CODE LOW_VECTOR_SECTION=0X418
VOIDLOW_VECTOR(VOID)
{

72
_ASMGOTOINTERRUPTHANDLERLOW _ENDASM
}
//----------------------------------------------------------------------------
// HIGH PRIORITY INTERRUPT ROUTINE
#PRAGMA CODE
#PRAGMA INTERRUPT INTERRUPTHANDLERLOW
VOIDINTERRUPTHANDLERLOW ()
{
IF (INTCONBITS.TMR0IF)
{ //CHECK FOR TMR0 OVERFLOW
INTCONBITS.TMR0IF = 0; //CLEAR INTERRUPT FLAG
FLAGS.BIT.TIMEOUT = 1; //INDICATE TIMEOUT
LATDBITS.LATD0 = !LATDBITS.LATD0; //TOGGLE LED ON RB0
}
}
//----------------------------------------------------------------------------
// MAIN ROUTINE
VOID MAIN ()
{
PORTD=0;
FLAGS.BYTE = 0;
INTCON = 0X20; //DISABLE GLOBAL AND ENABLE TMR0
INTERRUPT
INTCON2 = 0X84; //TMR0 HIGH PRIORITY
RCONBITS.IPEN = 1; //ENABLE PRIORITY LEVELS
TMR0H = 0; //CLEAR TIMER
TMR0L = 0; //CLEAR TIMER
T0CON = 0X84; //SET UP TIMER0 - PRESCALER 1:32(FOR 1:8
T0CON = 0X82)
INTCONBITS.GIEH = 1; //ENABLE INTERRUPTS
TRISD = 0;

73
WHILE (1)
{
IF (FLAGS.BIT.TIMEOUT == 1)
{ //TIMEOUT?
FLAGS.BIT.TIMEOUT = 0; //CLEAR TIMEOUT INDICOR
//LATBBITS.LATB = LATBBITS.LATB0; //COPY LED STATE FROM RB0
TO RB7
}
}
}

CONCLUSION:-
Hence we studied the use of timer to generate square wave using pic18 microcontroller.

74
EXPERIMENT NO 9
TITLE:- PC TO PC COMMUNICATION
AIM: Interfacing Serial Port with PC Both Side Communications.
APPARATUS: -Universal PIC board, 10 pin FRC cable, 12V power supply, Computer.
SOFTWARE REQUIRED: MPLAB Software
THEORY:
Basic Of Serial Communication:-
Serial communication is used for transferring data between two system located at
distance of hundreds of feet to millions of miles apart. The fact in a single line data is
used in serial communication instead of the 8-bit data line of parallel communication
make serial transfer not only much cheaper but also enables two computer located in two
different cities to communicate over the telephone
For serial communication to work the byte of data must be converted to serial bit
using a parallel to serial out shift register then it can be transmitted over single data line.
This must be serial in to parallel out shift at the out shift register to receive the serial data
and pack them into a byte. When the distance is short the digital data can be transferred
as it is an a simple wire and required no modulation for long distance data transfer using
communication line such as a telephone however serial data communication and is
required a modern to modulate (convert from 0s and 1s to audio tones) and demodulate
(convert from audio tones )
Serial data communication uses two method asynchronous and synchronous the
synchronous method transfer a block data at the time where as the asynchronous method
transfer a single byte at a time.

Fig.1. Serial versus Parallel data Transfer

75
Start and Stop Bits:-
Asynchronous serial data communication is widely used for character oriented
transistor while the asynchronous method each character is placed between start and stop
bits this is called forming in data forming from asynchronous communication. The data
such as ASCII forming from asynchronous communication the data such as ASCII
character are placed between a start bit and a stop bit. The start bit is always one bit and a
stop bit be one or two bits is high.

Fig.2 .Start and Stop bits


Ex from figure: 8-bit binary 01000001 is frames between the start bit and a single stop
bit. When there is no transfer the signal is 1(high) which is referred to as mark the 0(low)
is referred to as space. The transmission begins with a start bit followed by D0 the LSB
then the rest of the bits followed the MSB(D7) and finally the one stop bit indicates the
end of the character A.

In a asynchronous serial communication peripheral chip and modem can be programmed


for data 7 or 8 bits wide this is in addition to the no of stop bits. The parity bit of
character byte is included in the data frame in order to maintain data integrity. This
means that for each character (7 or 8)bits depending on the system. We have single parity
bit in addition to start and stop bits. The parity bit is even or odd. In case of an odd parity
bit the no of 1s in data bits including parity bit is odd. Similar in an even parity bit system
the total no of bits includeing parity bits is even.

76
Fig.3. DB 9 connector
IBM PC DB9 Signal
PIN DESCRPTION
1 Data Serial Detect (DCD)
2 Recived Data (RXD)
3 Transmitted Data(TXD)
4 Data Terminal Ready(DTR)
5 Signal Ground(GND)
6 Data Set Reddy(DSR)
7 Request To Send(RTS)
8 Clear to Send(CTS)
9 Ring Indicator(RI)

Null Modem Connection:-

Fig.4. Null Modem Connection

1.DTR(data terminal ready):-


When the term is turned on after going through a self test it sends out signal DTR to
indicate that it is ready for communication. If there is something wrong with the

77
communication port this signal will not be used to inform the moderm that the computer
is alive and this is an active pin from DTE (PC communication port) and input to the
modem.
2.DSR(data set ready):-
when the DCE (modem) is turned on and gone through the self test it assert DSR to
indicate that it is ready to communciate. Thus it is an output from modem(DCE)and i/p
to PC (DTE)this is an active low signal. If for any reasion the modem cant make a
connection to the telephone the signal remain inactive, indicate the pcor term that it cant
accepts or send data.
3.RTS(request to send):
when the DTE device such as PC has a byte to transmitt it assert RTS to signal the
modem that it is has a byte of data to transmit RTS is an active low output from DTE and
an input to modem.
4.CTS(clear to send)
In response to RTS when the moderm has room to store that it is to indicate that it can
recieve the data now this input signal to the DTE is used by DTE to start transmission
5.DCD :
signal to inform that a volid carrier has been detected and that contact between it and the
other is valid carrier has been detected therfore DCD is an output form the moderm and
an input to the PC (DTE)
6.RI(ring indicator):-
An output from the moderm(DCE)and an input to PC (DTE) incicates that the telephone
is ringing.
MATRIX 232:-
The matrix 232 converts from RS232 voltage level to TTL voltage level and vice versa.
One advantage of the max 232 chip is that it is use a +5v power source which is the same
as the source voltage for the PIC18. In other words with a single +5v power supply we
can power both the PIC18 and max232 with no need for the dual power supply that are
common in many older system.

78
The max 232 has two sets of line transfering and receiving data. The line drivers used
for Tx are called T1 and T2 while the line drivers for Rx are designed R1 and R2 in main
application only one of each is used.

Fig.5. a) Inside MAX 232 b) Its Connection to PIC 18

Ex- T1 and R1 are used together for Tx and Rx of PIC18 and sec set is left unused. T1
line driver has desting of T1 in and T1 out on pin no 11 and 14 respectively. The T1 in
pin is the TTL side that is connected to Tx pin of microcontroller while T1 out is RS232
side that is connected to Rx pinof RS 232DB connector.
The R1 line driver has distination of R1in and R1out on pin no 13 and 12 respectively
the R1in pin no 13 is RS232 side that connected to Tx pin of RS232 DB connector and
R1out pin12 is TTL side that is connected to Rx pin of microcontroller null modem
connection where Rx for one Tx for other max232 requires 4 capacitor ranging from 1 to
22 µf. The most widely used value for these capacitor is 22µf.
TXREG is another 8 bit registor used for serial communication in the PIC 18 for a byte
of data to the transmitted via the Tx pin it must be placed in the TXREG register which is
an special function SFR and can be accessed like any other register in the PIC 18

TXSTA (transmit status and control register):


CSRC TX9 TXEN SYNC 0 BRGH TRMT TX9D

TX9 D6:-9 bit transmit enable


1=select 9 bit transmission

79
0= select 0 bit transmission
TXEN D5:- transmit enable
1= transmit enable
0= transmit disable
We turn on and turn off this bit in order to start or stop data transfer
SYNC D4:- USART mode select
1= synchronous
0= asynchronous
BRGH D2- high baud rate select
1= high speed
0= low speed (default)
RCSTA (Receive status and control register):
Similarly when the bit are received serially via the RX pin the PIC 18 reframes them by
byte of data received and then placing it in the RCPEG register
SPEN RX9 SREN CREN ADDEN FERR OERR RX9D

SPEN D7 serial port enable bit


1= serial port enable when makes
Tx and RX plus as serial port pins
0= serial port disable
RX9 D6 9bit receives enable bit
1= select 9bit reception
0= select 8bit reception
SREN D6 single receive enable bit
1= enable constantreceiver
0= disable constant receiver
ADDEN D3 address dated enable bit
Free D2 framing error bit
1= framing error
0 = no framing error
OERR D1= over up error bit

80
1= overrun error
0=no over run error
SPBRG value for various baud rates (Fosc = 4Mhz, BRGH=0)
BAUD RATE DECIMAL VALUE SPBRG(HEX VAL.)
19200 2 2
9600 5 5
4800 12 OC
2400 25 19
1200 51 33

FLOWCHART:-
1) TRANSMIT BIT 2)RECEIVED BIT

START START

Initialize Ports, USART


Call Open USART Initialize LCD, Enable
Function Serial Port and Receiver

Call USART function


to Transmit message Wait To Receive Bit Word
‘PREC’

Wait Until Word Save Value


Transmitted

STOP
Place Character in Buffer

81
PROCEDURE:
1) Connect +12V supply to universal (PIC) board.
2) Connect the Serial cable to the pc
3) Switch on the power supply
4) Open the MPLAB IDE
5) Go to ProjectProject WizardNextSelect PIC18F4520 ICNext select the
Microchip C18 Tool suite  Browse the particular folder where you have to create
project file  Next tab Next tab Finish tab
6) Go to File  New  Write the C program in this new window.
7) Go to File  Save the file with .C extension (e.g. led.C)
8) Right click on the Source files  add led.C file in to the source file.
9) Right click on the Linker script  add 18F4520.lkr file the path is given below
C:\MCC18\LKR\18F4520.lkr.
10) go to Project Build options project add 3 files here
Go to show directories for
Select Include search path new browse C:\MCC18\h
Select Library search path new browse C:\MCC18\lib
Select Linker-script search path new browse C:\MCC18\lkr
Click on Apply OK
11) Project  build all  build succeeded message will display on window

82
INTERFACING DIAGRAM:-

LCD

83
PROGRAM:
1) Program for serial transmission:
#INCLUDE<P18F4520.H>
#INCLUDE<USART.H>
VOID DELAY(UNSIGNED INT VALUE);
#PRAGMA CONFIG OSC=HS
#PRAGMA CONFIG PWRT=OFF
#PRAGMA CONFIG WDT=OFF
#PRAGMA CONFIG DEBUG=OFF, LVP=OFF
#PRAGMA CONFIG FCMEN=ON
#PRAGMA CONFIG XINST=OFF

VOIDSENDDATA(VOID);
VOID MAIN(VOID)
{
OSCCON=0B01100000;
ADCON1=0X7F;
OPENUSART(USART_TX_INT_OFF &USART_RX_INT_ON
&USART_ASYNCH_MODE &USART_EIGHT_BIT &USART_CONT_RX
&USART_BRGH_LOW,12);
//4800 BOUD RATE
WHILE(1)
{
PUTRSUSART ("PREC");
DELAY(25);
}
}
VOID DELAY(UNSIGNED INT VALUE)
{
INTI,J;
FOR(I=0;I<=VALUE;I++)

84
FOR(J=0;J<=1000;J++);
}

2)PROGRAM FOR SERIAL RECEPTION


#INCLUDE <P18F4520.H>
#INCLUDE <USART.H>
#PRAGMA CONFIG OSC=HS
#PRAGMA CONFIG PWRT=OFF
#PRAGMA CONFIG WDT=OFF
#PRAGMA CONFIG DEBUG=OFF, LVP=OFF
#PRAGMA CONFIG FCMEN=ON
#PRAGMA CONFIG XINST=OFF
#DEFINE LDATA PORTD
#DEFINE RS PORTEBITS.RE0
#DEFINE RW PORTEBITS.RE1
#DEFINE EN PORTEBITS.RE2
VOIDDELAY_MS(UNSIGNED INTMS_COUNT);
VOIDDELAY_US(UNSIGNED INTUS_COUNT);
VOIDLCD_INIT(VOID);
VOIDLCD_CMDWRITE(CHAR CMD);
VOIDLCD_DATAWRITE(CHAR DATA);
VOIDLCD_DISPLAYSTRING(CHAR *STRING_PTR);

UNSIGNED CHAR DATA;


VOID MAIN()
{
TRISD = 0X00;
TRISEBITS.TRISE0=0;
TRISEBITS.TRISE1=0;
TRISEBITS.TRISE2=0;
ADCON1=0X7F;

85
OPENUSART(USART_TX_INT_OFF &USART_RX_INT_ON
&USART_ASYNCH_MODE &USART_EIGHT_BIT
&USART_CONT_RX &USART_BRGH_LOW,12);//4800 BOUD RATE
PUTRSUSART ("\R\NPRESS ANY KEY..");
LCD_INIT();
LCD_CMDWRITE(0X80);
LCD_DATAWRITE(' ');
DELAY_US(10);
LCD_CMDWRITE(0X81);
LCD_DATAWRITE('R');
DELAY_US(10);
LCD_CMDWRITE(0X82);
LCD_DATAWRITE('C');
DELAY_US(10);
LCD_CMDWRITE(0X83);
LCD_DATAWRITE('V');
DELAY_US(10);
LCD_CMDWRITE(0X84);
LCD_DATAWRITE(' ');
DELAY_US(10);
LCD_CMDWRITE(0X85);
LCD_DATAWRITE('D');
DELAY_US(10);
LCD_CMDWRITE(0X86);
LCD_DATAWRITE('A');
DELAY_US(10);
LCD_CMDWRITE(0X87);
LCD_DATAWRITE('T');
DELAY_US(10);
LCD_CMDWRITE(0X88);
LCD_DATAWRITE('A');

86
DELAY_US(10);
LCD_CMDWRITE(0X89);
LCD_DATAWRITE(':');
DELAY_US(10);
WHILE(1)
{
IF(PIR1BITS.RCIF==1)
{
DATA=READUSART();
LCD_CMDWRITE(0X8A);
LCD_DATAWRITE(DATA);
}
}
}
VOIDLCD_INIT()
{
LCD_CMDWRITE(0X38);
DELAY_US(10);
LCD_CMDWRITE(0X0E);
DELAY_US(10);
LCD_CMDWRITE(0X06);
DELAY_US(10);
LCD_CMDWRITE(0X01);
DELAY_US(10);
}
VOIDLCD_CMDWRITE(CHAR CMD)
{
LDATA=CMD;
RS=0; //COMMAND
RW=0; //WRITE OPERATION
EN=1; //SEND HIGH TO LOW PULSE AT ENABLE PIN

87
DELAY_US(10);
EN=0;
DELAY_MS(1);
}
VOIDLCD_DISPLAYSTRING(CHAR *STRING_PTR)
{
WHILE(*STRING_PTR)
LCD_DATAWRITE(*STRING_PTR++);
}
VOIDLCD_DATAWRITE(CHAR DATA)
{
LDATA=DATA;
RS=1;
RW=0;
EN=1;
DELAY_US(10);
EN=0;
DELAY_MS(1);
}
VOIDDELAY_US(UNSIGNED INTUS_COUNT)
{
WHILE(US_COUNT!=0)
{
US_COUNT--;
}
}
VOIDDELAY_MS(UNSIGNED INTMS_COUNT)
{
WHILE(MS_COUNT!=0)
{

88
DELAY_US(112); //DELAY_US IS CALLED TO GENERATE 1MS
DELAY
MS_COUNT--;
}
}

CONCLUSION:
Hence we have observed pc to pc communication using pic18 microcontroller. Output of
the serial communication is observed on LCD.

89
90
EXPERIMENT NO 10
TITLE:-ADC INTERFACING
AIM:-Interface analog voltage 0-5V sto internal ADC and display value on LCD.
APPARATUS:- Universal PIC board,10 pin FRC cable,12v power supply.
THEORY:-
ADC devices
Analog to digital are among the most widely used devices for data acquisition. Digital
computer uses binary(discrete) values,but in the physical world everything is
analog(continues). Temperature, pressure (wind or liquid), humidity and velocity are a
few examples of physical quantities that we deal with every day.A physical quantity is
converted to electrical(voltage, current) signal using a device called a
transducer.Transducer are also referred to as sensor,sensor for
temperature,velocity,pressure,light and many other natural quantities produce an output
that is voltage or current.

ADC CPU DISPLAY


SENSOR

Fig: Microcontroller connect to sensor via ADC

Resolution v/s step size for ADC (Vref = 5V)


N-bit No.of steps Step size(mV)
8 256 5/256=19.53
10 1024 5/1024=4.88
12 4096 5/4096=1.2
16 65536 5/65536=0.076

Resolution:-ADC has n-bit resolution;where n can be 8,10,12,16 or even 24 bits. The


higher resolution ADC provides a smaller step size,where step size is the smallest change
that can be resized by an ADC.

91
Conversion Timer:-Conversion time is defined as the time it takes the ADC to convert
the analog input to digital number .The conversion time is calculated by the clock source
connected to the ADC in addition the method used for data conversion and technology
used in the fabrication of the ADC chip such as MOS or TTL technology.

Vref relation to Vin range for an 10 bit ADC:

Vref (V) Vin (V) Step size (mV)


5.00 0 to 5 5.00/1.024=4.88
4.096 0 to 4.096 4.096/1.024=4
3 0 to 3 3/1.024=2.93
2.56 0 to 0.56 0.56/1.024=2.5
2.048 0 to 2.048 2.048/1.024=2
1.28 0 to 1.28 1.28/1.024=1.25
1.024 0 to 1.024 1.024/1.024=1

Digital data output:-


In 8 bit ADC we have on 8 bit digital data output of D0-D7 while in to bit ADC
the data output is D0-D9. To calculate the output voltage we used the following formula.
Dout=Vin/StepSize
where,
Dout=Digital data output.
Vin=Analog input voltage and step size.
is the smaller change, which is Vref /256 for an 8 bit ADC.

92
PIC 18F408 ADC feature programming:-
The ADC peripheral of the PIC 18 has following characteristics,
a.It is 10 bit ADC.
b.It can have 5 to 15 channel of analog input channel depending on the family number.
c.The Converted output binary data is held by two special function register called
ADRSEL(Address result low) and ADRESH(Address result high).

ADC Interfacing:-
a.ADRESH register gives us 16 bit and the ADC data out is only 10bit wide,6bit of the
16 are unused. We have the option of making either the upper 6 bits or the lower 6 bits
unused.
b.We have the option of using Vdd(Vcc) the voltage source of the PIC 18 chip itself as
the Vref or connecting it to an external voltage source for the Vref.
c.The conversion time is dictated by the fuseof crystal frequency connected to oscillators
pins while the fuse for PIC18 can be as high as 40MHz, the conversion time cannot be
shorter than 1.6ms.
d.It allows the implementation of the differential Vref voltage using the Vref(+) and
Vref(-) pins where,

Vref=Vref(+)-Vref(-)

ADCON 0 Register:-
1.The ADCON 0 register is used to set the conversion time and select the analog input
channel among other things.
2.In order to reduces the power consumption of the PIC 18 the ADC feature is turned off
when the microcontroller is powered up.
3.We turn on the ADC with the ADON bit of ADCON0 register.
4.The other important bit is the GO/DONE bit.
5.We use this bit to start conversion and monitor it to see if conversion has ended.
6.The conversion time is set with the ADCS bits.

93
7.While ADCS1/ADCS0 are held by the ADCON 0 register ADCS2 is part of the
ADCON1 register.

ADCS1 ADCS0 CHS2 CHS1 CHS0 GO/DONE - ADON

ADCS2 ADCS1 ADCS0 Conversion clock source


0 0 0 fosc/2
0 0 1 fosc/8
0 1 0 fosc/32
0 1 1 Intenal / RC used for circuit
1 0 0 fosc/ 4
1 0 1 fosc/ 16
1 1 0 fosc/ 64
1 1 1 Intenal / RC used for circuit

CHS2 CHS1 CHS0 Channel Selection


0 0 0 CHAN 0 (AN0)
0 0 1 CHAN 1 (AN1)
0 1 0 CHAN 2 (AN2)
0 1 1 CHAN 3 (AN3)
1 0 0 CHAN 4 (AN4)
1 0 1 CHAN 5 (AN5/NOT implement)
1 1 0 CHAN 6 (AN6/NOT implement)
1 1 1 CHAN 7 (AN7/NOT implement)

94
GO/DONE ADC conversion status bit
1=ADC conversion is in progress this is used as start conversion,which means after the
conversion is complete. It will go low to indicate the end of conversion.
0=ADC conversion is complete and digital data is available in register ADRESH and
ADRESL.
ADON A/D on bit:-
0=ADC port of the PIC18 is off and consume no power. This is the default and we should
leave it off for application in which ADC is not used.
1=ADC feature is powered up.
ADCON1 register:-
a.Another major register of the PIC 18's ADC feature is ADCON 1.
b.The ADCON 1 register is used to select the Vref. among other things.
c.After the ADC conversion is complete,the result sits in register ADRESL (ADC result
low byte)and ADRESH(ADC result high byte).
d.The ADFM bit of the ADCON 1 is used for making it right justified or left justified
because we need only 10 bit of the 16.

ADEM ADCS2 - - PCFG3 PCFG2 PCFG1 PCFG0

ADEM ADC result format select bit


1=Right justified the 10 bit result is in the ADRSEL register and the lower 2bit of
ADRESH.That means the 6 most significant bit of the ADRESH register are all 0s.
0=Left justified the 10 bit result is in the ADRESH register and the upper 2 bit of
ADRESL that means the 6 least significant bit of the ADRSEL register are all 0s.

ADCS 2 A/D clock select bit 2


This bit along with the ADCS 1 and ADCS 0 bit of the ADCON 0 register decide the
conversion clock for the ADC. The default value for ADCS 2 is 0 which means setting
the ADCS 0 and ADCS 1 value ADCON 0 can give us clocks conversion of
Fosc/2,Fosc/8,Fosc/32.

95
FLOWCHART:-

START

Function declaration

Port initialization

Select channel 0 AD0 as analog input

Call LCD

ADCON0=0x01

Start conversion of ADC

Is end of
conversion
?

Convert result into Hex

Display digital data on LCD

96
PROCEDURE:-
PROCEDURE:-
1) Connect +12V supply to universal (PIC) board.
2) Connect the Serial cable to the pc
3) Switch on the power supply
4) Open the MPLAB IDE
5) Go to ProjectProject WizardNextSelect PIC18F4520 ICNext select the
Microchip C18 Tool suite  Browse the particular folder where you have to create
project file  Next tab Next tab Finish tab
6) Go to File  New  Write the C program in this new window.
7) Go to File  Save the file with .C extension (e.g. led.C)
8) Right click on the Source files  add led.C file in to the source file.
9) Right click on the Linker script  add 18F4520.lkr file the path is given below
C:\MCC18\LKR\18F4520.lkr.
10) go to Project Build options project add 3 files here
Go to show directories for
Select Include search path new browse C:\MCC18\h
Select Library search path new browse C:\MCC18\lib
Select Linker-script search path new browse C:\MCC18\lkr
Click on Apply OK
11) Project  build all  build succeeded message will display on window.
To Create Hex File

12) Go to File (Mplab 8.30 Software) Go to Export Write Start address 0x400 and
end address 0x7ffe Unclick all three below option Click on Ok Save that file
in to created folder(Which is created at the time of save writing program)  Hex file
is created
Program Download

13) Open ANI1310ui Software  Turn On The Kit. Press Reset Key on BoardClick
on Red Square Button Green window is displayed. Go to File Open created Hex

97
file folder Hex file is display Click on down arrow (Write Devices) wait for write
complete. Click on Green arrow to run the program
INTERFACING DIAGRAM:-

98
PROGRAM:-
#INCLUDE <P18F4520.H>
#PRAGMA CONFIG OSC=HS
#PRAGMA CONFIG PWRT=OFF
#PRAGMA CONFIG WDT=OFF
#PRAGMA CONFIG DEBUG=OFF, LVP=OFF
#PRAGMA CONFIG FCMEN=ON
#PRAGMA CONFIG XINST=OFF

/*===== FUNCTION DECLARATION============*/


VOID DELAY(UNSIGNED INT VALUE);
VOIDLCDCMD(UNSIGNED CHAR VALUE);
VOIDLCDDATA(UNSIGNED CHAR VALUE);
VOIDLCDINIT(VOID);
VOIDLCDWRITE(VOID);
VOID DISPLAYHEX1(CHAR DATA);
VOID DISPLAYHEX2(CHAR DATA);
CHARCONVERTHEX(CHAR DATA);
/*===== PORT PIN DECLARATION============*/
#DEFINE LDATA PORTD
#DEFINE RS PORTEBITS.RE0
#DEFINE RW PORTEBITS.RE1
#DEFINE EN PORTEBITS.RE2
/*===== VARIABLE DECLARATION============*/
UNSIGNED CHAR TEMP1,TEMP2;
INT RESULT; //TO STORED 10 BIT RESULT
VOID MAIN()
{
TRISD = 0X00; // PORTD AS OUTPUT
TRISE=0X00; // PORTE AS OUTPUT
TRISABITS.TRISA0=1; //ADC CHANNEL 0

99
ADCON1 = 0X0C; //AN0 AS ANALOGIPUT
ADCON2=0X86; //RIGHT JUSTIFIED,FOSC/64
LCDINIT();
LCDWRITE();
WHILE(1)
{
ADCON0 = 0X01; //CHANNEL 0 SELECTED
ADCON0BITS.GO = 1; //START CONVERGEN
WHILE(ADCON0BITS.DONE ==1); //WAIT FOR CONVERGEN END
RESULT=ADRESH; //MSB OF 10 BIT RESULT
RESULT=(RESULT<<8)|ADRESL; //MSB + LSB OF 10 BIT RESULT
DISPLAYHEX1(RESULT >> 8); //CONVERT 8 BIT(MSB) RESULT TO HEX &
DISPLAY
DISPLAYHEX2(RESULT); //CONVERT 8 BIT(LSB) RESULT TO
HEX & DISPLAY
}

}
VOID DISPLAYHEX1(CHAR DATA)
{
UNSIGNED CHAR TEMP = DATA >> 4; //ROTATE TEMP LEFT BY 4
TEMP1 = CONVERTHEX(TEMP); //CONVERT 4 BIT RESULT INTO HEX
LCDCMD(0X89); //LOCATION TO DISPLAY 1ST HEX
VALUE
LCDDATA(TEMP1);
TEMP = (DATA & 0X0F);
TEMP2 = CONVERTHEX(TEMP); //CONVERT 4 BIT RESULT INTO HEX
LCDCMD(0X8A); //LOCATION TO DISPLAY 2ND HEX VALUE
LCDDATA(TEMP2);
}
VOID DISPLAYHEX2(CHAR DATA)

100
{
UNSIGNED CHAR TEMP = DATA >> 4;
TEMP1 = CONVERTHEX(TEMP); //CONVERT 4 BIT RESULT INTO
HEX
LCDCMD(0X8B); //LOCATION TO DISPLAY 3RD
HEX VALUE
LCDDATA(TEMP1);
TEMP = (DATA & 0X0F);
TEMP2 = CONVERTHEX(TEMP); //CONVERT 4 BIT RESULT INTO HEX
LCDCMD(0X8C); //LOCATION TO DISPLAY 4TH HEX VALUE
LCDDATA(TEMP2);
}
CHARCONVERTHEX(CHAR DATA)
{
SWITCH(DATA)
{
CASE 0X0A:
RETURN 'A';
CASE 0X0B:
RETURN 'B';
CASE 0X0C:
RETURN 'C';
CASE 0X0D:
RETURN 'D';
CASE 0X0E:
RETURN 'E';
CASE 0X0F:
RETURN 'F';
DEFAULT:
RETURN (DATA |0X30);
}

101
}
VOID DELAY(UNSIGNED INT VALUE)
{
INTI,J;
FOR(I=0;I<=VALUE;I++)
FOR(J=0;J<=400;J++);
}
VOIDLCDCMD (UNSIGNED CHAR VALUE)
{
LDATA=VALUE;
RS=0;
RW=0;
EN=1;
DELAY(10);
EN=0;
}
VOIDLCDDATA (UNSIGNED CHAR VALUE)
{
LDATA=VALUE;
RS=1;
RW=0;
EN=1;
DELAY(10);
EN=0;
}
VOIDLCDINIT(VOID)
{
LCDCMD(0X38);
DELAY(10);
LCDCMD(0X0E);
DELAY(10);

102
LCDCMD(0X01);
DELAY(10);
LCDCMD(0X06);
DELAY(10);
LCDCMD(0X80);
DELAY(10);
}
VOIDLCDWRITE(VOID)
{
LCDCMD(0X80);
LCDDATA('A');
LCDCMD(0X81);
LCDDATA('D');
LCDCMD(0X82);
LCDDATA('C');
LCDCMD(0X84);
LCDDATA('I');
LCDCMD(0X85);
LCDDATA('N');
LCDCMD(0X86);
LCDDATA('=');
LCDCMD(0X87);
LCDDATA('0');
LCDCMD(0X88);
LCDDATA('X');
}

CONCLUSION:-
Thus we have concluded that interface analog voltage 0-5v to internal ADC and display
value on LCD.

103

You might also like