Lab Microprocessors

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 45

Lab 1: Interfacing Of ROM in Proteus

Conducted on: Submitted on:


Instructor: Lab Engineer:
1)
2)
3)

Aim
Interfacing memory chip, ROM in Proteus.

Requirements:

74HC163 (counter), 27C512 (ROM)

Theory

In digital systems, there are two types of memories: 1. RAM 2. ROM

ROM:

Read-only memory is a memory device in which permanent binary information is stored. The number of
words in a ROM is determined from the k address input lines needed to specify the 2k words.

Task:

Write some content in ROM memory by a binary format file, apply the counter outputs to the address
lines of ROM and read the contents via Memory chip.
Lab 2: Interfacing Of RAM in Proteus

Conducted on: Submitted on:


Instructor: Lab Engineer:
1)
2)
3)

Aim
Interfacing memory chip, RAM in Proteus.

Requirements:

74HC163 (counter), 27C512 (ROM), RAM (IS61C1024AL-12JI) ,Multiplexer (74HC157).

Theory

In digital systems, there are two types of memories: 1. RAM 2. ROM

RAM:

Memory unit: Stores binary information in groups of bits called words. Memory word: group of 1’s
and 0’s and may represent a number, character(s), instruction, or other binary-coded information. Most
computer memories use words that are multiples of 8 bits (byte ). 32-bit word ‡ 4 bytes. Each word in
memory is assigned an address 0 up to 2k – 1 (k = # of address lines).
To transfer a new word to be stored into memory:

1. Apply the binary address of the word to address lines.

2. Apply the data bits that must be stored in memory to the data input lines.

3. Activate the write input.

To transfer a stored word out of memory:

1. Apply the binary address of the word to address lines.

2. Activate the read input.

Task:

Design a schematic that loads data from ROM to RAM starting from FFFFH,0000H,0001H to FFFEH

At FFFEH it stops loading.

Give a ram address you should see the data of corresponding address will be shown in the output of
RAM

That means data from ROM has been saved to RAM.


Lab 3: Design Of Arithmetic Logic Unit

Conducted on: Submitted on:

1)
2)
3)

Instructor: Lab Engineer:

Aim
Design and Implementation of 4 bit Arithmetic Logic Unit.

Requirements:

Ful adders, Multiplexers, Registers, etc.

Theory

An Arithmetic Logic Unit (ALU) is a circuit that does arithmetic, such as addition, subtraction,
set‐less‐than, bit‐wise AND, bit‐wise OR, etc. The ALU you will design is a combinational circuit that
operates on 4‐bit numbers. It has two inputs A and B, and an output Y. It has four operations that can be
selected: add, subtract, set‐less‐than, and bit‐wise AND. To select the operation, the ALU has a 2‐bit
select input Sel which selects as follows

 Sel = 00: operation = add


 Sel = 01: operation = sub
 Sel = 10: operation = set‐less‐than
 Sel = 11: operation = bit‐wise AND
Your circuit will also have three 4‐bit registers: RegA, RegB, and RegY. They are all synchronized with
the clock signal. Mux1 Mux2 will select the operands, the result is to be stored in the registers R1 and
R2.

Block Diagram:
Lab 4: Address Decoding

Conducted on: Submitted on:


Instructor: Lab Engineer:
1)
2)
3)

Aim
Implementing address decoding using NAND gates.

Requirements:

74HC163 (counter), 27C512 (ROM),RAM ICs, NAND IC

Theory

With the information of desired address locations, It is the job of a decoder circuitry to locate the memory
chip to where desired data is to be stored. We look at the use of NAND gate IC as decoders:

The Address Information is taken from the ROM ICS, we will be needing a cascade of ROM ICs to make
a 20 bit address bus for the RAM ICs.
TASK:

Make the following circuitry to implement address decoding.

of RAM1

Similarly ,

Of RAM2
Lab 6: Assembly language Instructions
(Branching, Time delay loops )

Conducted on: Submitted on:


Instructor:

Lab Engineer:
1)
2)
3)

Aim
Introduction to branching and time delay loops in Assembly Language.

Theory
Chapter 3 of PIC Microcontroller and embedded systems by MAZIDI

Equipment
PC with MPLABX installed

Task Breakdown Marks


1) Nested loops using unconditional and conditional branch (level 3) 5
2) Toggle pins of port D after interval of 1sec 5
Explanation
DECFSZ instruction and looping
The DECFSZ (decrement file Reg skip zero) instruction is a widely used instruction supported
across all PIC families of microcontrollers from PIC 12 to PIC 18. It has the following format:

DECFSZ file Reg , d ;decrement file Reg and skip next instruction if 0

In this instruction, the file Reg is decremented, and if its content is zero, it skips the next
instruction. By placing the "GOTO target" instruction right below it we can create a loop. The
target address of the "GOTO target" instruction is the beginning of the loop

Example 1
Write program to (a) clear WREG, and (b) add 3 to WREG ten times and place the result
in SFR of PORTB. Use the DECFSZ instruction to perform looping

COUNT EQU Ox25 ; use loc 25H for counter


MOVLW d ‘Lo’ ; WREG = 10 (decimal) for counter
MOVWF COUNT ; load the counter
MOVLW 0 ;WREG = 0

AGAIN

ADDLW 3 ; add 03 to WREG (WREG = sum)


DECFSZ COUNT,F ; decrement counter, skip if count = 0
GOTO AGAIN ; repeat until count becomes 0
MOVWF PORTB ; send sum to PORTB SFR
Notice that the DECFSZ instruction will decrement the counter (file Regloc Ox25), which has 10
in it. It becomes 9. Because it is not zero, it will execute the "GOTO AGAIN" instruction. The
"GOTO AGAIN" goes back to the start of the loop. Next, it decrements, our counter becomes 8,
and, because it is not zero, it executes the GOTO. It goes on like that until the counter becomes
zero. Upon the counter becoming zero, it skips the GOTO, which gets it out of the loop, and
executes the "MOVWF PORTB" instruction. Notice that we use "DECFSZ COUNT, F" and not
"DECFSZ COUNT,W" because we want the count value to change for the next iteration. We
will never get out of the loop if we use ''DECFSZ COUNT, W" because COUNT= 9 and the
decrement value is placed in WREG

Using Condition branches for looping


The BNZ (branch If not zero) instruction uses the zero flag in the status register. Its use is as:

BACK

……….. ;start of the loop


……….. ;body of the loop
……….. ;body of the loop
DECF ;decrement file Reg, Z = 1 if file Reg = 0
BNZ BACK ;branch to BACK if Z = 0

In the last two instructions, the file Reg is decremented; if it is not zero, it branches (jumps) back
to the target address referred to by the label. Prior to the start of the loop, the file Reg is loaded
with the counter value for the number of repetitions. Notice that the BNZ instruction refers to the
Z flag of the status register affected by the previous instruction, DECF.

In the last two instructions, the file Reg is decremented; if it is not zero, it branches (jumps) back
to the target address referred to by the label. Prior to the start of the loop, the file Reg is loaded
with the counter value for the number of repetitions. Notice that the BNZ instruction refers to the
Z flag of the status register affected by the previous instruction

Example 2
Write a program to (a) clear WREG and then (b) add 3 to WREG ten times.Use the zero flag and
BNZ

#include P18f4520.inc

ORG 0H

COUNT EQU Ox25 ; useloc 25H for counter

CLRF TRISE
MOVLW d'10' ; WREG = 10 (decimal) for counter
MOVWF COUNT ; load the counter
MOVLW 0 ; WREG = 0

AGAIN

ADDLW 3 ; add 03 to WREG (WREG sum)


DECF COUNT, F ; Decrement counter
BNZ AGAIN ; repeat until COUNT = 0
MOVWF PORTE ; send sum to PORTE SFR

END

Flow chart of example 2


Example3
Write a program to (a) load the PORTB SFR register with the value 55H, and (b) complement
Port B 700 times.

Because 700 is larger than 255 (the maximum capacity of any register), we use two registers to
hold the count. The following code shows how to use file Reg locations 25H and
26H as a register for counters.

#include P18f4520.inc

ORG 0H
R1 EQU Ox25
R2 EQU Ox26
COUNT_1 EQU d'l0'
COUNT_2 EQU d'70‘

CLRF TRISD
MOVLW Ox55 ;WREG = 55h
MOVWF PORTD ; PORTE = 55h
MOVLW COUNT_1 ;WREG = 10, outer loop count value
MOVWF R1 ;load 10 into lac 25H (outer loop count)
LOP 1 ;WREG = 70, inner loop count value

MOVLW COUNT_2 ;load 70 into loc 26H


MOVWF R2
LOP 2

COMPF PORTD, F ; complement Port B SFR


DECF R2, F ;dec file Reg lac 26 (inner loop)
BNZ LOP 2 ;repeat it 70 times
DECF R1, F ;dec file Reg lac 25 (outer loop)
BNZ LOP 1 ;repeat it 10 times

END

In this program; file Reg location Ox26 is used to keep the inner loop count. In the instruction
"BNZ LOP_2", whenever location 26H becomes 0 it falls through and "DECF Rl, F" is executed.
This instruction forces the CPU to load the inner count with 70 if it is not zero, and the inner
loop starts again. This process will continue until location 25 becomes zero and the outer loop is
finished

Flow chart of example 3.4 is given on next page


CALL instruction
Another control transfer instruction is the CALL instruction, which is used to call a subroutine.
Subroutines are often used to perform tasks that need to be performed frequently. This makes
a program more structured in addition to saving memory space. In this 4-byte (32-bit)
instruction, the 12 bits are used for the opcode and the other 20 bits, A21-AI, are used for the
address of the target subroutine. Just as in the GOTO instruction, the lowest bit of the program
counter is 0 automatically to ensure it lands on an even address. Therefore, CALL can be used to
call subroutines located anywhere within the 2M address space of00000-1 FFFFH. For the
PICI8To make sure that the PIC knows where to come back to after execution of the called
subroutine, the microcontroller automatically saves on the stack theaddress of the instruction
immediately below the CALL. When a subroutine is called, control is transferred to that
subroutine, and the processor saves the PC (program counter) of the next on the stack and
begins to fetch instructions from the new location. After finishing execution of the subroutine,
the instruction RETURN transfers control back to the caller. Every subroutine needs RETURN as
the last instruction.

Stack
The stack is read/write memory (RAM) used by the CPU to store some very critical information
temporarily. This information usually is an address, but it could be data as well. The CPU needs
this storage area because there are only a limited number of registers. The stack in the PIC18 is
21-bit because the programcounter is 21-bit. This means that it is used for the CALL instruction
to make sure that the PIC knows where to come back to after execution of the called
subroutine.
In the PIC, the CPU uses the stack to save the address of the instruction just below the CALL
instruction. This is how the CPU knows where to resume when it returns from the called
subroutine. Consider following example

Example
Toggle all the bits of the SFR register of Port B by sending to it the values 55H and AAH
continuously. Put a time delay in between each issuing of data to Port B.

MYREG EQU 0x08


ORG 0
BACK MOVLW 0x55
MOVWF PORTB
CALL DELAY
MOVLW 0xAA
MOVWF PORTB
CALL DELAY
GOTO BACK
ORG 300H
DELAY MOVLW 0xFF
MOVWF MYREG
AGAIN NOP
NOP
DECF MYREG, F
BNZ AGAIN
RETURN
END
The following points should be noted for the program in Example:
I. Notice the DELAY subroutine. Upon executing the first "CALL DELAY", the address of the
instruction right below it, "MOVLW OxAA", is pushed onto the stack, and the PIC starts to
execute instructions at address 000300H.
2. In the DELAY subroutine, first the counter MYREG is set to 255 (MYREG = FFH); therefore, the
loop is repeated 256 times. When MYREG becomes 0,control falls to the RETURN instruction,
which pops the address from the topof the stack into the program counter and resumes
executing the instructionsafter the CALL.
Delay calculation Example

MYREG EQU 0x08


ORG 0
BACK MOVLW 0x55
MOVWF PORTB
CALL DELAY
MOVLW 0xAA
MOVWF PORTB
CALL DELAY
GOTO BACK
ORG 300H
DELAY MOVLW 0xFA
MOVWF MYREG
AGAIN NOP
NOP
NOP
DECF MYREG, F
BNZ AGAIN
RETURN
END
We have the following machine cycles for each instruction of the DELAY subroutine:
Instruction cycle
DELAY MOVL 1
MOVWF 1
AGAIN NOP 1
NOP 1
NOP 1
DECF 1
BNZ 2
RETURN 1
Therefore, we have a time delay of [(250 x 6) + 1 + 1 + I] x 111s = 1503 11s.
Task 1
Write program (using unconditional branches) for nested loop (level 3) for toggling pin RD1
10000 times

Task 2
Write program (using conditional branches) for nested loop (level 3) for toggling pin RD1 10000
times

Task 3
Create a delay of 1 sec assuming an oscillator frequency of 4MHz. Toggle pins of port D (in an
infinite loop) after every 1 sec.
Lab 6: Arithmetic Instructions

Conducted on: Submitted on:


Instructor:

Lab Engineer:
Group Members

1)
2)
3)

Aim
Understanding following topics

1) Unsigned Nos Arithmetic

2) Signed Nos Arithmetic

Theory
Chapter 5 of PIC Microcontroller and embedded systems by MAZIDI.

Equipment
PC with MPLABX installed

Tasks Marks
1) Multiple byte addition 3
2) Multiple byte subtraction (unsigned numbers) 3
3) Multiple byte subtraction (signed numbers) 4
Explanation
ADDLW
The ADDLW instruction has the following format: ADDLW K ; ADD literal value K to WREG The
ADD instruction tells the CPU to add the literal value K to register WREG and put the result back
in the WREG register. Notice that in ADDLW, first comes the letter L (literal) and then the letter
W (WREG), which means "add a literal value to WREG," the destination. To add two numbers
such as 25H and 34H, one can do the following:
MOVLW 25H ; load 25H into WREG
ADDLW 34H ; add value 34 to W (W = W + 34H)
Executing the above lines results in WREG = 59H (25H + 34H = 59H)

ADDWF
Instruction "ADDWF fileReg, d" allows the addition ofWREG andindividual bytes residing in RAM
locations of the file register. Notice that WREGmust be involved because memory-to-memory
arithmetic operations are neverallowed in PIC Assembly language. To calculate the sum of any
number of operands, the carry flag should be checked after the addition of each operand.

ADDWFC
When adding two 16-bit data operands, we need to be concerned with the propagation of a
carry from the lower byte to the higher byte. This is called multibyteaddition to distinguish it
from the addition of individual bytes. The instruction ADDWFC (ADDW and fileReg with carry) is
used on such occasions.

Example
Write a program to add two 16-bit numbers. The numbers are 3CE7H and 3B8DH. Assume that
file Reg location 6 = (8D) and location 7 = (3B). Place the sum in file Reg locations 6 and 7;
location 6 should have the lower byte.

MOVLW 0xE7
ADDWF 0x6,F
MOVLW 0x3C
ADDWFC 0x7,F

SUBLW K (WREG = K- WREG)


In subtraction, the PIC microcontrollers use the 2's complement method. In this method the PIC
uses adder circuitry to perform the subtraction command. Assuming that the PIC is executing a
simple subtract instruction and that C = 0 prior to the execution of the instruction, one can
summarize the steps of the hardware of the CPU in executing the SUBLW instruction for
unsigned numbers as follows:
l. Take the 2's complement of the subtrahend (WREG operand).
2. Add it to the minuend (K operand).

SUBWFB (dest = file Reg- W- Borrow) subtract with borrow


This instruction is used for multibyte numbers and will take care of the borrowof the lower
byte. If C = 0 prior to executing the SUBWFB instruction, it also subtracts 1 from the result.

Example
Write a program to subtract two 16-bit numbers. The numbers are 2762H- 1296H. Assume
fileReg location 6 = (62) and location 7 = (27). Place the difference in file Reglocations 6 and 7;
loc 6 should have the lower byte.
MOVLW 0x96
SUBWF 0x6,F
MOVLW 0x12
SUBWFB 0x7,F

TASK 1
Save Two unsigned nos of size n bytes on location starting from address 10H and 20H
respectively (n=7) add them and save result on location starting from address 30H.

TASK 2
Save Two signed nos of size n bytes on location starting from address 10H and 20H
respectively (n=7) add them and save result on location starting from address 30HDo
this two time, in one iteration result should be valid (no overflow) and in second
iteration result should be invalid(overflow occurs). Use overflow flag to check validity of
result.
Lab 7: Register Indirect addressing Mode

Conducted on: Submitted on:

Instructor:

Lab Engineer:
Group Members
1)
2)
3)

Aim
Understanding Register indirect Addressing mode

Theory
Chapter 5 and 6 of PIC Microcontroller and embedded systems by MAZIDI.

Equipment
PC with MPLABX installed

Tasks Marks
4) Taking two’s compliment of n byte no 5
5) subtracting n byte no 5
Explanation
Register Indirect Addressing Mode
In the register indirect addressing mode, a register is used as a pointer to the data RAM
location. In the PICI8, three registers are used for this purpose: FSRO, FSRI, and FSR2. FSR
stands for file select register and must not be confused with SFR (special function register).
The FSR is a 12-bit register allowing access to the entire 4096 bytes of data RAM space in
the PIC18. We use LFSR (load FSR) to load the RAM address. In other words, when FSRx are
used as pointers, they must be loaded first with the RAM addresses as shown below

LFSR 1, Ox40 ;loadFSRl with Ox40

LFSR 2, Ox6F ;load FSR2 with Ox6F

Because FSRO, FSRl, and FSR2 are 12-bit registers they cannot fit into the SFR address space
unless they are split into pieces of an 8-bit size. That is exactly what PIC I 8 has done. The
FSR registers have the low-byte and high-byte parts called FSRxL and FSRxH, as shown in the
SFR table of Table 6-1. In Table 6-1 we see FSROL and FSROH, representing the low and high
parts of the 12-bit FSRO register. Note that the FSRxH is only 4-bit and the upper 4 bits are
not used. Another register associated with the register indirect addressing mode is the INDF
(indirect register). Each of the FSRO, FSRl, and FSR2 registers has an INDF register associated
with it, and these are called INDFO, INDFI, and INDF2. When we move data into INDFx we
are moving data into a RAM location pointed to by the FSR. In the same way, when we read
data from the INDF register, we are reading data from a RAM location pointed to by the
FSR. This is shown below.

LFSR 0, Ox30 ;FSRO = 30H RAM location pointer


MOVWF INDFO ;copy contents of WREG into RAM
;location whose address is held by
;12-bit FSRO register

Auto-increment option for FSR


Because the FSR is a 12-bit register, it can go from 000 to FFFH, which covers the entire 4K
RAM space of the PIC18. Using the "INCF FSROL, p" instruction to increment the pointer can
cause a problem when an address such as 5FFH is incremented. The instruction "INCF
FSROL, F" will not propagate the carry into the FSRIH register. The PIC 18 gives us
theoptions of auto-increment and auto-decrement for FSRn to overcome this problem.

Example
Write a program to copy a block of 5 bytes of data from RAM locations starting at 30Hto RAM
locations starting at 60H.

COUNTREG EQU 0x10


CNTVAL EQU D'5'
MOVLW CNTVAL
MOVWF COUNTREG
LFSR 0,0x30
LFSR 1,0x60
B3 MOVF POSTINC0,W
MOVWF POSTINC1
DECF COUNTREG,F
BNZ B3

TASK 1
Save an n(n=7) byte no is save on RAM starting 10H(MSB), find two’s complement of no
and place it on RAM staring from 20H(MSB)

TASK 2
Save two n(n=7) byte no on RAM starting 10H(MSB) and 20H(MSB) respectively ,
subtract operand 1 from operand 2 first take two’s complement of minuend using code
of task 1 and then use addition command
Lab 8: Pic Programming in C

Conducted on: Submitted on:


Instructor:

1)
2)
3)

Lab Engineer:
Group Members

Aim
Understanding writing codes in C for PIC 18F microcontroller

Theory
1) Chapter 7 of PIC Microcontroller and embedded systems by MAZIDI.
2) Data sheet of PIC microcontroller
Equipment
PC with MPLABX and XC8 compiler installed.

Tasks Marks
6) Binary to decimal conversion 4
7) BCD to ASCII conversion 4

Explanation
Reason behind C programming of pic
The following are some of the major reasons for writing programs in C instead of Assembly.
I. It is easier and less time consuming to write in C than in Assembly.
2. C is easier to modify and update.
3. You can use code available in function libraries.
4. C code is portable to other microcontrollers with little or no modification.

Example
Following C code toggles all pins of PORTC and PORTD continuously with an approximate
delay of 250ms

#include <P18F458.h>
voidMSDelay(unsigned int);
void main(vd)
{
TRISC = 0;
TRISD = 0;
while(1)
{
PORTC = 0x55;
PORTD = 0x55;
MSDelay(250);
PORTC = 0xAA;
PORTD = 0xAA;
MSDelay(250);
}
}
voidMSDelay(unsigned intitime)
{
unsignedinti; unsigned char j;
for(i=0;i<itime;i++)
for(j=0;j<165;j++);
}
Task1
Write a Cl8 program to convert packed BCD to ASCII and display the bytes on PORTB and
PORTC

Task2
Write a C18 program to convert FD hex to decimal and display the digits on PORTB, PORTC,
and PORTD
Lab 9: Introduction to hardware

Conducted on: Submitted on:


Instructor:

Lab Engineer:
Group Members

1)
2)
3)

Aim
At the end of lab student should be able to program microcontroller IC.

Theory
Chapter 8 of PIC Microcontroller and embedded systems by MAZIDI.

Equipment
PC with MPLABX and proteus installed, PICKit3, PIC 18F452, +5v Battery or power
supply, 20 MHz Crystal, 10kΩ Resistor, 1uF Capacitor, 5mm LED, Breadboard, Breadboard Wire,
Battery Holder

TaskMarks
8) Toggle LED connected to PORTD.1 after every 1sec 10
Explanation
Make connection of microcoxntroller with accessories as shown in figure 1 and figure 2

Figure 1: Schematics of circuit

Figure 2: sample circuit snap

Task
Write a program to toggle pin RD1 after every 1 sec. Connect LED to RD1. (first run simulation in
proteus)
Lab 10: Generating square wave using timer

Conducted on: Submitted on:

Instructor:

Lab Engineer:
Group Members

1)
2)
3)

Aim
At the end of lab student should be able to understand working of timers

Theory
Chapter 9 of PIC Microcontroller and embedded systems by MAZIDI.

Equipment
PC with MPLABX and proteusinstalled,PICKit3, PIC 18F452, +5v Battery or power
supply, 20 MHz Crystal, 10kΩ Resistor, 1uF Capacitor, 5mm LED, Breadboard, Breadboard
WireSIPs, Battery Holder, Oscilloscope

TaskMarks
9) Generating square wave of variable frequency using timers 10
Explanation
Timer0 16-bit timer programming
The following are the characteristics and operations of 16-bit mode:
1. It is a 16-bit timer; therefore, it allows values of 0000 to FFFFH to be loaded into the
registers TMROH and TMROL.
2. After TMROH and TMROL are loaded with a 16-bit initial value, the timer must be
started. This is done by “BSF TOCON, TMROON" for Timer 0.
3. After the timer is started, it starts to count up. It counts up until it reaches its limit of
FFFFH. When it rolls over from FFFFH to 0000, it sets HIGH a flag bit called TMROIF
(timer interrupt flag, which is part of the INTCON register). This timer flag can be
monitored. When this timer flag is raised, one option would be to stop the timer.
4. After the timer reaches its limit and rolls over, in order to repeat the process, the
registers TMROH and TMROL must be reloaded with the original value, and the TMROIF
flag must be reset to 0 for the next round.

Steps to program Timer 0 in 16-bit mode

To generate a time delay using the Timer mode 16, the following steps are taken:
1. Load the value into the TOCON register indicating which mode (8-bit or 16-bit) is to be
used and the selected prescaler option.
2. Load register TMROH followed by register TMROL with initial count values.
3. Start the timer with the instruction "BSF T OCON, TMROON".
4. Keep monitoring the timer flag (TMROIF) to see if it is raised. Get out of the loop
when TMROIF becomes high.
5. Stop the timer with the instruction "BCF T OCON, TMROON".
6. Clear the TMROIF flag for the next round.
7. Go back to Step 2 to load TMROH and TMROL again. To clarify the above steps, see
Example 9-3. To calculate the exact time delay and the square wave frequency
generated on pin PB5, we need to know the XTAL frequency
TMROL, because the value for TMROH is kept in a temporary register and written to
TMROH when TMROL is loaded. This will prevent any error in counting ifthe TMROON
flag is set HIGH.

Example
Assuming that XTAL = I0 MHz, write a program to generate a square wave with a period of 10
ms on pin PORTB.3.
For a square wave with T = I0 ms we must have a time delay of 5 ms. Because XTAL= I0 MHz,
the counter counts up every 0.4 11s. This means that we need 5 msI 0.4 uS =12,500 clocks.
65,536- 12,500 = 53,036 = CF2CH. Therefore, we have TMROH = CF and TMROL = 2C.
Code
BCF TRISB,3
MOVLW 0x08
MOVWF T0CON
HERE MOVLW 0xCF
MOVWF TMR0H
MOVLW 0x2C
MOVWF TMR0L
BCF INTCON,TMR0IF
CALL DELAY
BTG PORTB,3
BRA HERE
DELAY BSF T0CON,TMR0ON
AGAIN BTFSS INTCON,TMR0IF
BRA AGAIN
BCF T0CON,TMR0ON
RETURN

Task
Receive 8 bit value from port B and move to TMROH and TMROL and generate square wave.
Calculate all possible frequency (255 frequencies) you can generate using this method by
writing Matlab code.
10) Run code in proteus simulation
11) Run code on hardware and observe output on oscilloscope.

Optional
1) Suppose beside value of timer you are also receiving 3 bit value for pre scalar. Now
again calculate all possible frequency you can generate using pre scaler and timer
values
2) Write code for generating square wave of varying duty cycle
Lab 11: LCD Interfacing

Conducted on: Submitted on:

Instructor:

Lab Engineer:
Group Members
1)
2)
3)

Aim
At the end of lab student should be able to understand working of LCD and using it for
displaying information.

Theory
Chapter 12of PIC Microcontroller and embedded systems by MAZIDI.

Equipment
PC with MPLABX and proteus, Matlab and Vspd/Vspe installed PICKit3, PIC18 generic
board or pic dem2 plus demonstration board, Serial cable, LCD

TaskMarks
1) Display name and registration on LCD 10
Explanation
LCD pin descriptions
LCD (Liquid Crystal Display) display module to PIC microcontroller? LCD is a passive component
that is it does not make any light but just modifies the light passing through it for alphanumeric
displays. LCD is exclusively manufactured to be used withmicrocontrollers, which means that
it cannot be triggered by usual IC circuits Details of particular LCD are given in its data sheet.
Text book may also be consulted here only brief description is given

Control pins
(R/S , R/W , E pins)
The control pin RS determines if the data transfer between the LCD module and an external
microcontroller are actual character data or command/status. When the microcontroller needs
to send commands to LCD or to read the LCD status, it must be pulled low. Similarly, this must
be pulled high if character data is to be sent to and from the LCD module. The direction of data
transfer is controlled by the R/W pin. If it ispulled Low, the commands or character data is
written to the LCD module. And, when it is pulled high, the character data or status information
from the LCD registers is read. Here, we will use one way data transfer, i.e., from
microcontroller to LCD module, so the R/W pin will be grounded permanently. The enable pin
(E) initiates the actual data transfer. When writing to the LCD display, the data is transferred
only on the high to low transition of the E pin.

Power supply pins


(power, ground , contrast pins)
Although most of the LCD module data sheets recommend +5V d.c. supply for operation, some
LCDs may work well for a wider range (3.0 to 5.5 V). The Vdd pin should be connected to the
positive power supply and Vss to ground. Pin 3 is Vee, which is used to adjust the contrast of
the display. In most of the cases, this pin is connected to a voltage between 0 and 2V by using a
preset potentiometer. Data pins Pins 7 to 14 are data lines (D0-D7). Data transfer to and from
the display can be achieved either in 8-bit or 4-bit mode. The 8-bit mode uses all eight data
lines to transfer a byte, whereas, in a 4- bit mode, a byte is transferred as two 4-bit nibbles. In
the latter case, only the upper 4 data lines (D4-D7) are used. This technique is beneficial as this
saves 4 input/output pins of microcontroller.

Circuit Diagrams
Connect LCD to microcontroller according to following connections. Basic connections of
microcontroller are not shown
Task
Make circuit according to the above diagram and write a program to display your name (in first line) and
registration no(in second line).(First run simulation in proteus)
Lab 12: Serial communication

Conducted on: Submitted on:


Instructor:

Lab Engineer:
Group Members

1)
2)
3)

Aim
At the end of lab student should be able to understand serial communication basic
concepts and implement serial communication protocol between PIC18 and MATLAB

Theory
Chapter 10 of PIC Microcontroller and embedded systems by MAZIDI.

Equipment
PC with MPLABX , Proteus, Matlab and Vspd/Vspe installed ,PICKit3, pic 18 generic
board with serial port or pic dem2 plus demonstration ,Serial cable

Task BreakdownMarks
12) Receiving serial data using Simulink block 5
13) Receiving serial data using Matlab code 5
Explanation
Programming the PIC18 to transfer data serially
In programming the PIC 18 to transfer character bytes serially, the following steps must be
taken:
I. The TXSTA register is loaded with the value 20H, indicating asynchronous mode with 8-bit
data frame, low baud rate, and transmit enabled.
2. Make TX pin ofPORTC (RC6) an output for data to come out of the PIC.
3. The SPBRG is loaded with value to set the desired baud rate for serial data transfer.
4. SPEN bit in the RCSTA register is set HIGH to enable the serial port of the PIC18.
5. The character byte to be transmitted serially is written into the TXREG register.
6. Monitor the TXIF bit ofthe PIRI register to make sure UART is ready for next byte.
7. To transfer the next character, go to Step 5.

Importance of the TXIF flag


To understand the importance of the role of TXIF, look at the following sequence of steps that
the PICI8 goes through in transmitting a character via TX:
I. The byte character to be transmitted is written into the TXREG register.
2. The TXIF flag is set to I internally to indicate that TXREG has a byte and will not accept
another byte until this one is transmitted.
3. The TSR (Transmit Shift Register) reads the byte from TXREG and begins to transfer the byte
starting with the start bit.
4. The TXIF is cleared to indicate that the last byte is being transmitted and TXREG is ready to
accept another byte.
5. The 8-bit character is transferred one bit at a time.
6. By monitoring the TXIF flag, we make sure that we are not overloading the TXREG register. If
we write another byte into the TXREG register before the TSR has fetched the last one, the old
byte could be lost before it is transmitted.

Example
Assume a switch is connected to pin RD7. Write a program to monitor its status and send two
messages to the serial port continuously as follows: SW = 0 send "NO" ,SW = I send "YES"
Assume XTAL = 10 MHz, and set the baud rate to 9,600.

Code
BSF TRISD,7
MOVLW 0x20
MOVWF TXSTA
MOVLW D'15'
MOVWF SPBRG
BCF TRISC, TX
BSF RCSTA, SPEN
OVER BTFSS PORTD,7
BRA NEXT
MOVLW high(MESS1)
MOVWF TBLPTRH
MOVLW low(MESS1)
MOVWF TBLPTRL
FN TBLRD*+
MOVF TABLAT,W
BZ EXIT
CALL SENDCOM
BRA FN
NEXT MOVLW high(MESS2)
MOVWF TBLPTRH
MOVLW low(MESS2)
MOVWF TBLPTRL
LN TBLRD*+
MOVF TABLAT,W
BZ EXIT
CALL SENDCOM
BRA LN
EXIT MOVLW 0x20
CALL SENDCOM
GOTO OVER
SENDCOM
S1 BTFSS PIR1, TXIF
BRA S1
MOVWF TXREG
RETURN
MESS1 DB "NO",0
MESS2 DB "YES",0

Task1
Write a program to send linearly increasing 8 bit noin infinite loop from PIC 18 and receive this data in
Matlab.( sawtooth waveform should be displayed in MATLAB)

Task2
Program microcontroller with code given in example and receive this data in Simulink.

1) Run code in proteus simulation


2) Run code on hardware

Serial communication in MATLAB


clearall
closeall
clc
time = 0;
data = 0;
count = 1;
h=stem(time,data,'XDataSource','time','YDataSource','data')
serialPort = 'COM2'; % define COM port #
delete(instrfindall)
s = serial(serialPort,'Baudrate',2400,'InputBufferSize',1)
fopen(s);
timea=0;
i=0;fg=0;
y=1;tic;tt=0;
while (ishandle(h)) %Loop when Plot is Active
time11=cputime
dat = fread(s);%Read Data from Serial as Float
if(~isempty(dat) ) %Make sure Data Type is Correct ds=toc;
ds=toc;
tt=ds+tt
tic;
rt=length(data);
time(1,rt+1)=tt;
data( 1 , rt+1) = dat;

if(length(time) > 200)


time =(time(1,((rt-199):(rt+1))));
data=(data(1,((rt-199):(rt+1))));
axis([time(1,1) time(1,(200)) min(data) max(data) ])
else
axis([time(1,1) time(1,(rt+1)) min(data)
max(data) ])
end
refreshdata(h,'caller')
drawnow;
end
end
Lab 13: Interrupts

Conducted on: Submitted on:

Instructor:

Lab Engineer:
Group Members

1)
2)
3)

Aim
At the end of lab student should be able to understand how to process multiple tasks
using priority interrupt feature of microcontroller

Theory
Chapter 11 of PIC Microcontroller and embedded systems by MAZIDI.

Equipment
PC with MPLABX,proteus, Matlab and Vspd/Vspe installed,PICKit3, PIC18 generic board
with serial port or pic dem2 plus demonstration board,Serial cable

TaskMarks
14) Two Square waves generation with Variable duty cycle and time period
Received serially 10
Explanation
Steps in executing an interrupt
Upon activation of an interrupt, the microcontroller goes through the following steps:
I. It finishes the instruction it is executing and saves the address of the next instruction
(program counter) on the stack.
2. It jumps to a fixed location in memory called the interrupt vector table. The interrupt vector
table directs the microcontroller to the address of the interrupt service routine (ISR).
3. The microcontroller gets the address of the ISR from the interrupt vector table and jumps to
it. It starts to execute the interrupt service subroutine until it reaches the last instruction of the
subroutine, which is RETFIE (return from interrupt exit).
4. Upon executing the RETFIE instruction, the microcontroller returns to the place where it was
interrupted. First, it gets the program counter (PC) address from the stack by popping the top
bytes of the stack into the PC. Then it starts to execute from that address.

Example
This program uses Timer 0 to generate a square wave on pin PORTB.5, while at the same time
data is being transferred from PORTC to PORTO.
ORG 0000H
GOTO MAIN
ORG 0008H
BTFSS INTCON,TMR0IF
RETFIE
GOTO T0_ISR
ORG 00100H
MAIN BCF TRISB,5
CLRF TRISD
SETF TRISC

MOVLW 0x08
MOVWF T0CON
MOVLW 0xFF
MOVWF TMR0H
MOVLW 0xF2
MOVWF TMR0L
BCF INTCON,TMR0IF
BSF T0CON,TMR0ON
BSF INTCON,TMR0IE
BSF INTCON,GIE
OVER MOVFF PORTC,PORTD
BRA OVER
T0_ISR
ORG 200H
MOVLW 0xFF
MOVWF TMR0H
MOVLW 0xF2
MOVWF TMR0L
BTG PORTB,5
BCF INTCON,TMR0IF
EXIT RETFIE
END

Notice the following points about Program


1) We must avoid using the memory space allocated to the interrupt vector table. Therefore,
we place all the initialization codes in memory starting at anaddress such as 1 OOH. The GOTO
instruction is the first instruction that the PIC18 executes when it is awakened at address 00000
upon power-on reset (POR). The GOTO instruction at address 00000 redirects the controller
awayfrom the interrupt vector table.
2. In the MAIN program, we enable (unmask) the Timer0 interrupt with instruction "BSF
INTCON, TMROIE" followed by the instruction "BSF INTCON, G IE" to enable all interrupts
globally.
3. In the MAIN program, we initialize the Timer0 register and then enter an infinite loop to keep
the CPU busy. This could be a real-world application being executed by the CPU. In this case,
the loop gets data from PORTC and sends it to PORTD. While the PORTC data is brought in and
issued to PORTD continuously, the TMROIF flag is raised as soon as Timer0 rolls over, and the
microcontroller gets out of the loop and goes to 00008H to execute the ISR associated with
Timer0. At this point, the PIC18 clears the GIE bit (D7 of INTCON) to indicate that it is currently
serving an interrupt and cannot be interrupted again; in other words, no interrupt inside the
interrupt.
4. The ISR for Timer0 is located starting at memory location 00200H because it is too large to fit
into address space 08-17H, the address allocated to high-priority interrupts.
5. In the ISR for Timer0, notice that the "BCF INTCON, TMROIF" instruction is needed before the
RETFIE instruction. This will ensure that a single interrupt is serviced once and is not recognized
as multiple interrupts.
6. RETFIE must be the last instruction of the ISR. Upon execution of the RETFIE instruction, the
PIC18 automatically enables the GIE (D7 of the INTCON register) to indicate that it can accept
new interrupts.

Task 1
You have to generate two square waves S1 and S2 simultaneously using timer 0 and timer 1. Off
time and ON time of square waves are given by user at port D. According to the following truth
table
PIN C0 PIN C.1 Data Type
0 0 S1 ON time
0 1 S1 Off time
1 0 S2 ON time
1 1 S2 Off time

Calculate duty cycle and time period from given on time and off time on paper.

Assign low priority to timer 1 interrupt.(first run simulation in proteus)

Optional
Make following addition to above code

Use serial transmit interrupt and send data to Matlab and plot it in real time.
Lab 14: Data Acquisition using ADC and
Serial

Conducted on: Submitted on:

Instructor:

Lab Engineer:
Group Members
1)
2)
3)

Aim
At the end of lab student should be able to understand how to use ADC and serial port
to plot analog signal in MATLAB

Theory
Chapter 11 and 13 of PIC Microcontroller and embedded systems by MAZIDI.

Equipment
PC with MPLABX, proteus, Matlab and Vspd/Vspe installed,PICKit3,PIC 18 generic board
with serial port or PIC dem2 plus demonstration board Serial cable

Task Marks
2) Digitizing analog sine signal(from function generator) using ADC and plot
in MATLAB 10
Explanation
Steps in programming the A/D converter using polling
To program the AID converter of the PIC18, the following steps must be taken:
1. Turn on the ADC module of the PICI8 because it is disabled upon power-on reset to save
power. We can use the "BSF ADCONO, ADON" instruction.
2. Make the pin for the selected ADC channel an input pin. We use "BSF TRISA, x." or "BSF
TRISE, x" where x is the channel number.
3. Select voltage reference and A/C input channels. We use registers ADCONO and ADCON1.
4. Select the conversion speed. We use registers ADCONO and ADCONI.
5. Wait for the required acquisition time.
6. Activate the start conversion bit of GO/DONE.
7. Wait for the conversion to be completed by polling the end-of-conversion (GO/DONE) bit.
8. After the GO/DONE bit has gone LOW, read the ADRESL and ADRESH registers to get the
digital data output.
9. Go back to step 5.

TASK
1) Apply sine wave of 1HZ 2vpp with 2v offset to AN0 PIN of microcontroller
2) Digitize it using ADC save only 8 bits not 10 bits
3) Send 1 byte data serially to MATLAB
4) In MATLB use previous code of serial reception to plot serial data in real time
5) To ensure that each sample digitized by ADC is sent serially use a register R3 of RAM and
set it if ADC reads sample and gives a byte and clear it if it is sent serially next time
don’t digitize a sample using ADC until R3 is zero i.e. ADC will wait for serial
transmitter if he send current byte then it will digitize next sample
6) First run simulation in proteus

You might also like