You are on page 1of 28

ECNG2005 labs (P)

Lab 4

March 3, 2010

Lab 4-p. 1
ID#

You should complete the pre-lab before coming to your lab session. Your Teaching Assistant/Demonstrator
may refuse to allow you into your lab session if your pre-lab is incomplete, or if you are unduly
late.
You will have 12 hours in the lab to complete the exercises. All answers should be written on this
lab-sheet in PEN. Please do not attach any extraneous pieces of paper unless SPECIFICALLY asked
to do so.
Please bring your PIC16F877 datasheet book (provided in ECNG2006) to the lab with
you. You may need to refer to it in order to complete the pre-lab and lab exercises.
Your lab submissions will be checked for unwarranted collusion, and unreferenced use
of Intenet-available/other resources.
At the end of this unit the student will be able to:
produce programs to interface external peripherals/devices to the PIC16F877 microcontroller.
produce programs for communication between a microprocessor and a PC using
the parallel and/or serial port, given UART and/or PPC datasheets/details, and
respective language/instruction sets.
This lab exercise will look at four tasks commonly performed during embedded application programming: multiplexing, interrupts, master/slave communication, PC communication.

Pre-Lab
Instruction Timing
The clock cycle time for the PIC16F877 is determined by the oscillator frequency;
1
Q = TOSC = fOSC
The instruction cycle time for the PIC16F877 is 8 clock cycles; however due to pipelining it appears
as 4 clock cycles. Tinstruction = 4 TOSC
1. (a) For a 4MHz oscillator, what will be actual PIC16F877 instruction cycle time?

. 1 mark

(b) For a 4MHz oscillator, what wil PIC16F877 instruction cycle time appear to be?

Timers(Counters)
The timer (also known as the counter) is a type of peripheral. A basic timer has two inputs
increment, and reset, and two outputs current count and overow signal.
Timers(Counters) overow and generates a signal when the value held matches the overow value.
Where no overow value is specied, this is the maximum value that the timer can hold (i.e. 8 bits
0xFF).
Counter/Timers take signals either from an external source, or from the instruction cycle time
Tinstruction . The bit-width of the timer(counter) will determine how many times it can be incremented after a reset, before it overows. For example a 4-bit timer must be incremented 24 = 16
times after it is reset, in order to overow.
c
DECE,
UWI, St. Augustine, Trinidad

. 1 mark

ECNG2005 labs (P)

March 3, 2010

Lab 4-p. 2

A timer whose increment input is connected to a xed frequency, will overow at regular intervals,
determined by the input frequency and the overow value.
In the PIC16F877 most timers can use a frequency equivalent to the eective instruction cycle time
as the increment input.
Sometimes the frequency of the increment input signal is reduced by a preliminary stage (pre-scaler)
which overows at a small scale factor. The value held by the timer is then incremented on the
rising/falling edge of the scaled signal. The value of the scale factor is determined by setting bits
in the timer conguration register. Available scale factors are always powers of 2(i.e. 1:2; 1:4; 1:8).
2. (a) What value will Timer0 overow at?

(b) What pre-scale factors are available for Timer0?

1 mark

1 mark

(c) Write a 1 millisecond Timer0-based delay routine for the PIC16F877 named StartDly1ms
which will congure and initialize Timer0 so that it will overow 1 millisecond later. You
6 marks
should assume a 4MHz clock.

3. Now some general questions about parallel and serial communication:


(a) Using your knowledge of digital electronics, explain how a multiplexer works.

(b) Complete the following sentence for any of the PIC16F877 ports: PORT
multiplexed with

1 mark

pins are
.
1 mark

(c) Dierentiate between using an external multiplexor (as in Digital Electronics) and multiplexed pins in the PIC16F877. Can we use two peripherals at the same time if they
1 mark
have multiplexed pins?

c
DECE,
UWI, St. Augustine, Trinidad

ECNG2005 labs (P)

March 3, 2010

Lab 4-p. 3

(d) Use an example to highlight the dierence(s) between parallel and serial communications. 1 mark

(e) What do the terms master and slave mean in the context of parallel and serial com1 mark
munication protocols?

(f) What do the terms synchronous and asynchronous mean in the context of parallel and
1 mark
serial communication protocols?

(g) For the following communication peripherals on the PIC16F877:


i.
ii.
iii.
iv.

classify the peripheral as either facilitating serial or parallel communication,


indicate whether it acts as master and/or slave during communication,
indicate whether simplex and/or duplex communication is supported,
identify at least one non-communication peripheral, with which its pins are multiplexed.

Peripheral i ii
iii
iv
MSSP
USART
PSP
PORTB
Simplex

c
DECE,
UWI, St. Augustine, Trinidad

4 marks

ECNG2005 labs (P)

March 3, 2010

Lab 4-p. 4

4. Next some questions about serial communication protocols:


(a) What is a UART? In your own words, provide a brief description of its function.

1 mark

(b) When establishing a connection across an RS232 link, we must set the baud rate, ow
control setting, parity, # data bits and # stop bits for the UART. In your own
2 marks
words (and using diagrams if needed), explain each of these terms.

(c) What would you expect to happen if two UARTS were congured dierently, and they
2 marks
tried to communicate using RS232? Explain your answer.

(d) Peripherals can be polled by the CPU or they can interrupt the CPU when they need
service(use of an interrupt vector). Explain the highlighted terms in the context of the
4 marks
sentence, and list the relative advantages/disadvantages of the two methods.

c
DECE,
UWI, St. Augustine, Trinidad

ECNG2005 labs (P)

March 3, 2010

Lab 4-p. 5

(e) Explain how you would congure an interrupt from the RB0/INT peripheral such that
no polling is done and an interrupt vector is used. State what needs to be done in the
5 marks
interrupt service routine(how would you save/restore context?).

(f) The PIC16f877 has only one interrupt vector location. How then would you deal with
multiple interrupts? What if they ag simultaneously? How will you set your interrupt
priority?
5 marks

(g) Read the PIC16F877 datasheet (9.1, 10.2) and identify the data, conguration, state
and control bits/registers for the MSSP operating in SPI master mode, and the USART
5 marks
operating in asynchronous mode .

Peripheral Conguration Control


USART
MSSP

State

Data

5. We will now answer a couple of questions to simple codes and encryption


(a) The system board we will use in Lab 4 contains multiple 7 segment displays all connected
to PortD. However the connection of PORTD pins to segments is slightly dierent from
the layout in Lab 3.
Lab 3 circuit

Lab 4 circuit

RD7
RD2

RD6
RD1

RD3

RD5
RD4

RD0

_
RD6
|_| RD1 RD0 RD5
RD2
RD4
|_|. RD3 RD7

Use the patterns for displaying digits and characters provided in the helpsheet on page
7 in Lab 3, and determine the PORTD output required to display each of the following
4 marks
characters:

c
DECE,
UWI, St. Augustine, Trinidad

ECNG2005 labs (P)

March 3, 2010

Lab 4-p. 6

Char ASCII Code Lab3 PORTD Lab4 PORTD


7
a
g
u
(b) Identify and explain a simple algorithm for transforming one set of codes to the other.

4 marks

(c) BONUS: 5 marksUse the following website(http://www.cs.usask.ca/resources/tutorials/


csconcepts/1999_3/lessons/L3/SimpleEncryption.html) to investigate the encryption/decryption technique appropriate to your Group Letter. Write routine which will
implement the encryption/decrption algorithms on a null-terminated string stored in
consecutive memory locations.
M : Reverser
C : Shifter
P/H : Flipper
R : Caeser

c
DECE,
UWI, St. Augustine, Trinidad

ECNG2005 labs (P)

March 3, 2010

Lab 4-p. 7

Helpsheet: Connecting to eb506 Bluetooth Modules from the PC


Please ensure that your circuit has been checked BEFORE attempting to power-up the board.
Write down the Bluetooth connection ID (see sticker under the module) for the eb506.
Ensure that your circuit board has the eb506 module securely mounted, and then power up
the board powered up.
Output a logic-high on the eb506 Mode pin, so that it is ready to receive connections. You
may do this in your program, or use the watch window in the debugger.
Insert the BlueTooth USB module in the PC. Ensure that it is recognised by the Bluetooth
Communication Software (BlueSoleil). Try another USB port if it is not recognised.
In the Bluetooth Communication software, Discover Devices (F5).
Select your module from the list of discovered devices - check the ID number if you are unsure.
Right click on your module to discover the services oered.
You may require a security connection password. Use the default: 0000.
Start the Serial port service service and note the COM port.
Proceed to open Hyperterminal for the COM port that the Bluetooth connection is on.

HelpSheet: Hyperterminal and the PC serial port


Hyperterminal is the terminal emulation program supplied with the Windows Operating system.
It may be used to communicate across any serial channel, including the RS232 communications
ports (COMn:).
To start Hyperterminal:
Click on the Start menu, and then select:
Programs>Accesories>Communications>Hyperterminal.
Click cancel on the box that is presented. You should now have a window entitled New
Connection.
Choose File, Properties, this will bring up a communications box.
Choose the relevant COM port from the connect using box.
Click the Congure button this will bring up a dialogue box which will allow you to choose
the baud rate, data bits, parity and stop bits, and the ow control. Once you have chosen
your settings, click OK twice. The settings should now appear in the status bar at the bottom
of the window.
Click Call> Call to start communicating. (Youll need to enter a name)
Click Call> Disconnect to stop communicating.
If you change the settings while connected, you must disconnect and reconnect for them to
take eect.
c
DECE,
UWI, St. Augustine, Trinidad

ECNG2005 labs (P)

March 3, 2010

Lab 4-p. 8

HelpSheet: Frequently Asked Questions: Embedded Application Programming


multiplex ing parallel i/o devices
Q: Why do we use multiplexors when we can just connect each device to dierent pins?
A: Micro-controller (packages) have a limited number of i/o ports (pins). Multiplexing
allows us to control more devices with the same number of pins.
Q: There must be some tradeo ...
A: Yes - the eective response/control time of the devices will be reduced.
interrupts for device i/o handling
Q:
A:
Q:
A:

What is an interrupt?
An interrupt is a
Interrupts! They are so confusing! Wont polling be easier?
Polling only works well when events will ALWAYS occur in a known sequence. If the
sequence is not known, a polling program runs the risk of getting stuck polling for one
event while another one is occurring.

master /slave synchronous serial communications


Q: What does synchronous mean?
A: The word synchronous refers to things that happen at the same time. For communications that means that the sender and the receiver have an agreed clock signal, using
which they send/recieve at agreed times.
Q: So does the slave do all the work (sending and receiving) in master/slave communications?
A: No. The master controls/generates the clock signal which is used to synchronize communication, however they are both sending and receiving - in fact the master may do
more sending/recieving than the slave.
Personal Computer (PC) asynchronous serial communications
Q: Why would anyone want to communicate between the PC and a microcontroller?
A: Lots of reasons ... you may want the PC to act as a storage device, or as a terminal
emulator, or the microcontroller may be providing updated values for a PC program
(e.g. MATLAB).
Q: Havent we been using a serial connection all along?
A: Yes there was a USB serial cable for communication between the ICD and the MPLAB
IDE.
Q: So whats the dierence?
A: This time you will have two serial connections one for MPLAB-ICD and a wireless Bluetooth serial connection between the PC and the PIC16F877 (without passing through
the ICD). In practice, once you have nished developing your application, the ICD cable
will no longer be necessary.
Q: Ok so we are going to the Bluetooth module to the 9-pin RS232 serial port on the PC?
A: No ... it goes in the USB port ... the software will create a virtual serial port e.g. COM7
c
DECE,
UWI, St. Augustine, Trinidad

ECNG2005 labs (P)

March 3, 2010

In the Lab

Lab 4-p. 9
ID#

If you check Labs 1 - 3, you will realise that all assembly language programs have a similar structure.A useful program development technique is to start with a template (template.asm) and make
incremental changes.
You are given a system board containing
four multiplexed 7-segment LED displays,
a 4-DIP switch package and an opto-switch,
a potentometer-based joystick, whose independent axes are connected to an Analog-to-Digital
converter IC ADC082S021,
an eb506 Bluetooth module with antenna,
The system board is connected via a ribbon cable to a daughter-board on which the PIC16F877
is mounted. The pin connection from the PIC16F877 to the system board are shown on page 10.
Check that the connections between the board and the PIC ZIF socket match those indicated; if
there are any dierences, make appropriate corrections to the diagram and/or circuit.
Please let the demonstrator/lab technician check that you have correctly inserted the
ribbon cable, PIC16F877, eb506 module and antenna before you proceed.
The system board should be powered with 7VDC.

c
DECE,
UWI, St. Augustine, Trinidad

ECNG2005 labs (P)

March 3, 2010

pin #
1
2
3

PIC16F877 daughter-board
Label
Descriptor
M CLR
Tied high through 1K Resistor
RA0
Output
RA1
Output

4
5
6
7
8
9
10
11
12
13
14
15

RA2
RA3
RA4
RA5
RE0
RE1
RE2
VDD
VSS
OSC1
OSC2
RC0

16

Lab 4-p. 10
Ribbon cable - motherboard
Descriptor

pin #

Label

21
23

Mux In1A
Mux In1B

multifunction
multifunction
multifunction
multifunction
multifunction
multifunction
multifunction
Power
Ground
Input
not connected
Input

30
28
32
26
34
40
36
11
18/19
10

Pin1
Pin2
Pin3
Pin4
Pin5
Pin6
Pin7
5V
Ground
4MHz

13

STATUS

RC1

Output

17

MODE

17
18

RC2
RC3

multifunction
SCK

38
20

Pin8
A/D SClk

19
20
21
22
23
24
25
26
27
28
29
30

RD0
RD1
RD2
RD3
RC4
RC5
RC6
RC7
RD4
RD5
RD6
RD7

Output
Output
Output
Output
SDI
SDO
TX
RX
Output
Output
Output
Output

31
29
39
37
12
14
9
7
35
25
27
33

31
32
33

VSS
VDD
RB0

Ground
Power
Input

18/19
11
3

Segment g
Segment f
Segment e
Segment d
A/D Dout
A/D Din
RX
TX
Segment c
Segment b
Segment a
Decimal
point
Ground
5V
OPT

34
35
36
37
38
39
40

RB1
RB2
RB3
RB4
RB5
RB6
RB7

Input
Input
Input
Input
Output
Not connected
Not connected
tied to pin 11 (5V power)
tied to pin 11 (5V power)
tied to pin 11 (5V power)
not connected
not connected

1
8
2
4
16

SW1
SW2
SW3
SW4
CS

5
6
15
22
24

OPTE
SWE
On/O
3.3V

LSB active 7-segment


MSB to determine which 7 segment display is active
Bit 0 General Purpose Port
Bit 1 General Purpose Port
Bit 2 General Purpose Port
Bit 3 General Purpose Port
Bit 4 General Purpose Port
Bit 5 General Purpose Port
Bit 6 General Purpose Port
Output from Regulator
System Ground
Oscillator signal
eb506 line - high if serial connection established
eb506 line - high switches into
data mode, low into command
mode
Bit 7 General Purpose Port
Serial clock line on external A/D
converter
Line on 7 segment displays
Line on 7 segment displays
Line on 7 segment displays
Line on 7 segment displays
Data output from external A/D
Data input to external A/D
Serial receive on eb506 module
Serial transmit on eb506 module
Line on 7 segment displays
Line on 7 segment displays
Line on 7 segment displays
Line on 7 segment displays
System Ground
Output from Regulator
Opto-switch output (high when
opto signal is disrupted)
on 4-bit DIP switch
on 4-bit DIP switch
on 4-bit DIP switch
on 4-bit DIP switch
Chip Select line for A/D converter

Enable pin for opto-switch


Enable pin for 4-bit switch
Powers eb506 (0=o, 1=on)
Output from Regulator
not connected

Lab 4: System Board/ Daughter Board Interconnections


c
DECE,
UWI, St. Augustine, Trinidad

ECNG2005 labs (P)

March 3, 2010

Lab 4-p. 11

Timers
1. (a) If the pre-scaler is used with Timer0, with a rate of 1:128, and T0CS is clear, what
2 marks
frequency will the timer overow at (assume 4MHz clock signal)?

(b) Lets use Timer0 to slowly count upwards on the 7-segment LED display.
Use the code on the next page. You will need one of the lookup functions used earlier.
Paste it in the appropriate place in the code. Verify that the code is consistent with the
comments. Check the program in the simulator, then program the PIC16F877 and run
the program.
Does it work as you expected?
Yes
or
No
(Circle your answer)
If it didnt what was the problem? How did you x it?

Show your results to the TA/lecturer who will sign your script.
(c)

4 marks

i. What frequency does the digit change at?


2 marks
Is this frequency what you expected?
Yes
or
No
(Circle your answer)
If it isnt, what was the problem? Use the space below to clarify your thoughts.

ii. What type(s) of delay did we use in the program: busy-wait, instruction-based, or
2 marks
timer based delay(s)? Explain your answer.

c
DECE,
UWI, St. Augustine, Trinidad

ECNG2005 labs (P)


LIST
Cnt
Dlys

mloop

iloop

0x00
main

bsf
STATUS,RP0
movlw
___________
movwf
OPTION_REG
clrf
TRISD
bcf
TRISC,2
BANKSEL PORTB
bsf
PORTC,2
clrf
Cnt

; bank 1
; set T0CS=1, prescaler = 128
;
;
;
;

PortD all ouputs


Port C transistor for 7-segment display is an output
Move back to bank 0
turn on transistor for 7-segment display

movlw
movwf
movf
call
movwf
incf

.250
Dlys
Cnt,W
lookup
PORTD
Cnt,F

; set the number of Delays we want

call
decfsz
goto

Delay
Dlys
iloop

; call 250 Delays

goto
sleep

mloop

; display the current count

; increment the Count

; in case we run past the goto, stop here

; Now for the delay routine


Delay
bcf
INTCON,T0IF
BANKSEL TMR0
clrf
TMR0

Loop

Lab 4-p. 12

p=16F877
INCLUDE <p16F877.inc>
EQU
0x22
EQU
0x23
ORG
nop
goto

main

March 3, 2010

; clear to be sure
;

bsf
bcf
bcf

STATUS,RP0
; switch to bank 1 to access OPTION_REG
OPTION_REG,T0CS ; start timing
STATUS,RP0

btfss
goto

INTCON,T0IF
Loop

bsf
bsf
bcf
return

STATUS,RP0
; switch to bank 1 to access OPTION_REG
OPTION_REG,T0CS ; stop timing
STATUS,RP0

; keep checking for overflow

; place either lookup function here!


END

c
DECE,
UWI, St. Augustine, Trinidad

ECNG2005 labs (P)

March 3, 2010

Lab 4-p. 13

2. We can change the rate at which the digits change, by modifying the pre-scaler used by
Timer0.
Re-write the program so that the initial switch settings (RB5,RB4,RB2,RB1) are used to
select the pre-scaler setting for Timer0 (PSA,PS2,PS1,PS0).
The pre-scaler value should remain constant until the PIC16F877 is reset.
Create a new assembly language project for the PIC16F877 in the MPLAB IDE. Compile the
code and ensure that it is built successfully. Simulate it and see if it works, then
PROGRAM the PIC16F877 .
Does it work as you expected?

Yes

or

No

(Circle your answer)

If it didnt what was the problem? How did you x it?

Use the MPLAB IDE and/or command line tools to:


(a) Print your new program, and attach the printout to this script.

4 marks

(b) Record the frequency at which the digit changes for:

4 marks

i.
ii.
iii.
iv.

one switch on,


two switches on,
three switches on,
all switches on.

(c) Does the pre-scaler perform as you expected? Comment on the accuracy and resolution
2 marks
of your results.

Demonstrate to the demonstrator/TA who will sign your script & printout.

c
DECE,
UWI, St. Augustine, Trinidad

ECNG2005 labs (P)

March 3, 2010

Lab 4-p. 14

Multiplexing
3. In this question you will write an assembly language program which will allow you to use
externally multiplexed 7-segment displays. This is similar to Lab 3 where the 7-segment and
LED Bar were connected to the same port pins, but had individual enable lines.
(a) Start with the template program, and modify the program so that ALL 8 segments of
the right-most digit are on. This should be done ONCE before the main loop.
(b) Compile and test your code. Does it work as you expected? If your program didnt work
on the rst try what were the problems? How did you x it?

(c) Write down the nal version of your conguration routine and main loop here.

(d) Did this code work? Yes or No (Circle your answer) If so, demonstrate the working code
10 marks
to your TA/lecturer who will sign your script

c
DECE,
UWI, St. Augustine, Trinidad

ECNG2005 labs (P)

March 3, 2010

Lab 4-p. 15

4. In the pre-lab you were asked to write a routine called StartDly1ms, which would cause
Timer0 to overow 1 millisecond after it is invoked. Add StartDly1ms to your program. In
your main loop, use this routine and data memory locations labeled DelayCnt and Active, to
change the active 7-segment display every 40 milliseconds.
5 marks
(a) Compile and test your code. Does it work as you expected? Is the timing correct? If
your program didnt work on the rst try what were the problems? How did you x it?

Call your TA/lecturer for troubleshooting help/suggestions if you get stuck.


(b) Write down the nal version of your main loop here.

(c) Did this code work? Yes or No (Circle your answer) If so, demonstrate the display
10 marks
change and timing to your TA/lecturer who will sign your script.

c
DECE,
UWI, St. Augustine, Trinidad

ECNG2005 labs (P)

March 3, 2010

Lab 4-p. 16

5. Using the patterns you generated in the pre-lab, create a lookup table routine called pattlkup
which will return the patterns for the characters 7, a, g, and u, when called with indexes 0,
1, 2, and 3 respectively. If the lookup routine is called with an invalid value, it should return
the patterns where ALL segments are activated. Add pattlkup to your program.
5 marks
(a) Modify the main routine so that the string 7agu is displayed from left to right. Hint:
each time the active digit is changed, retrieve and output a new pattern from the lookup
routine.
(b) Compile and test your code. Does it work as you expected? If your program didnt work
on the rst try what were the problems? How did you x it? Call your TA/lecturer for
troubleshooting help/suggestions if you get stuck.

(c) Write down the nal version of your main loop here.

(d) Did this code work? Yes or No (Circle your answer) If so, demonstrate the display
5 marks
change and timing to your TA/lecturer who will sign your script.

c
DECE,
UWI, St. Augustine, Trinidad

ECNG2005 labs (P)

March 3, 2010

Lab 4-p. 17

6. Challenge (Bonus 10 marks): We can display a message on the Seven segment LED display,
by displaying successive characters at regular intervals.
Write a program (either assembly or C) which will display a message which is stored in
memory (a lookup table (assembly), or an array (C)). The rst byte stored in memory is the
number of characters in the message. Each of the following bytes in memory is a byte which
must be sent directly to the Seven Segment LED display module.
You should write your program so that the message can be easily changed for demonstration.
You should assume that messages will always be less than 180 characters long. Test your
program with the following data:
The eective instruction cycle with a 4MHz oscillator is 1@s. d63, 0x1E, 0x2E, 0xDE, 0x00,
0xDE, 0x8E, 0x8E, 0xDE, 0x1A, 0x1E, 0x22, 0x56, 0xDE, 0x00, 0x22, 0x2A, 0x26, 0x1E, 0x0A,
0x38, 0x1A, 0x1E, 0x22, 0x3A, 0x2A, 0x00, 0x1A, 0x5E, 0x1A, 0x18, 0xDE, 0x00, 0xB8, 0x22,
0x1E, 0x2E, 0x00, 0xFA, 0x00, 0x66, 0xAA, 0x2E, 0x4A, 0x00, 0x3A,0x26, 0x1A, 0x22, 0x18,
0x18, 0xFA, 0x1E, 0x3A, 0x0A, 0x00, 0x22, 0x26, 0x00, 0x60, 0x4E, 0x26, 0x01, 0x00
Create a new project for the PIC16F877 in the MPLAB IDE. Compile the code and ensure
that it is built successfully. Simulate it and see if it works, then
PROGRAM the PIC16F877 .
Does it work as you expected?

Yes

or

No

(Circle your answer)

If it didnt what was the problem? How did you x it?

Attach a printout of the program to the script.


Did this code work?

Yes

or

No

(Circle your answer)

Demonstrate to the demonstrator/TA who will sign your script & printout.

c
DECE,
UWI, St. Augustine, Trinidad

ECNG2005 labs (P)

March 3, 2010

Lab 4-p. 18

7. The multiple seven segment displays were externally multiplexed, but the pins/ports of the
PIC16F877 can also be internally multiplexed. This can sometimes cause a problem, as the
peripheral will not work if it is not congured to use the pins, or if another peripheral is
using the pins. A simple example for your circuit is PortB: the pins are shared RB0 - RB5
between the opto-switch, the DIP switches, and the external A/D converter. In this question
you will write a program which will allow you to read the value on the four DIP switches,
without being aected by, or interfering with the settings for, the other devices. To do this
we will need to use masks and bit-wise logical operations to set, clear and read bits on a port
independently of each other.
(a) What mask will you use for pins RB1 to RB4 of PortB? How will you use this mask to
congure ONLY these PORTB pins as inputs? How will you use this mask to extract
ONLY the values on those pins, from PORTB?
5 marks

(b) Start with the template program, and modify the program so that the DIP switches are
congured as inputs and ALL 8 segments of the right-most digit are on. This should
be done ONCE before the main loop. Within the main loop, the active display should
change based on the current switch setting i.e. the rightmost segment should light if the
5 marks
rightmost switch is on.
(c) Compile and test your code. Does it work as you expected? What assumptions/choices
did you have to make? If your program didnt work on the rst try what were the problems? How did you x it?

c
DECE,
UWI, St. Augustine, Trinidad

ECNG2005 labs (P)

March 3, 2010

Lab 4-p. 19

(d) Write down the nal version of your conguration routine and main loop here.

(e) Did this code work? Yes or No (Circle your answer) If so, demonstrate the working code
to your TA/lecturer who will sign your script.
10 marks

c
DECE,
UWI, St. Augustine, Trinidad

ECNG2005 labs (P)

March 3, 2010

Lab 4-p. 20

8. (Challenge: Bonus 15 marks) Start with the program from Question 1d, and modify it so that
the switches determine which of the 7-segment displays is active i.e if the switches are all ON,
the seven segment displays show the pattern 7agu and if any switch is OFF the corresponding
seven segment display is o.
(a) Compile and test your code. Does it work as you expected? What assumptions/choices
did you have to make? If your program didnt work on the rst try what were the problems? How did you x it?

(b) Write down the nal version of your conguration routine and main loop here.

(c) Did this code work? Yes or No (Circle your answer) If so, demonstrate the working code
to your TA/lecturer who will sign your script.

c
DECE,
UWI, St. Augustine, Trinidad

ECNG2005 labs (P)

March 3, 2010

Lab 4-p. 21

9. We would like to write a program for the PIC16F877, which communicates with the PC over
an RS232 link via the eb506. Our rst attempt will simply echo the character typed at the
PC, back to the PC.
(a) Fill in appropriate settings, and register/ag names in the code below.
LIST
INCLUDE
ORG
nop
goto
ORG

5 marks

P=16F877
<P16F877.inc>
0x00
main
0x10

main
call
call
clrw
goto
Cnfg
bsf
______
bcf
bcf
bsf

Cnfg
UART_Cnfg
mainlp
STATUS,RP0
TRISC,0
TRISC,1
STATUS,RP0
PORTC,1

;
;
;
;
;

Go to Bank 1
configure eb506 STATUS line as input
configure eb506 MODE line as output
Go to Bank 0
set eb506 in data tx mode

waitconn
______
goto
return

PORTC,0
waitconn

; wait until the serial connection is established.

UART_Cnfg
bsf
movlw
movwf
movlw
movwf
bcf
movlw
movwf
return

STATUS,RP0
_________
SPBRG
_________
TXSTA
STATUS,RP0
_________
RCSTA

; Go to Bank1
; Set Baud rate to 9600

btfss
goto

_________,RCIF
mainlp

movf
movwf

_________,W
_________

; 8-bit transmit, transmitter enabled,


; asynchronous mode, low speed mode
; Go to Bank
; 8-bit receive, receiver enabled,
; serial port enabled

mainlp
; wait on char

nchar
; transmit what you rx (NB reading clears the flag)

waittx
_____
goto
goto
sleep
END

_________,TXIF ; flag set when character is gone from buffer


waittx
mainlp

c
DECE,
UWI, St. Augustine, Trinidad

ECNG2005 labs (P)

March 3, 2010

Lab 4-p. 22

(b) Compile, download and start your code - it will pause awaiting connection. Start Hyperterminal (as per helpsheet) and choose the required communication settings. Connect to
the communication channel, and the program on the PIC16F877 should continue. Test
the program by typing in the HyperTerminal window. Does it work as you expected?
Make changes until it does. If your program didnt work on the rst try what were the
problems? How did you x it?

(c) Did this code work? Yes or No (Circle your answer) If so, demonstrate the working code
to your TA/lecturer who will sign your script.
5 marks

c
DECE,
UWI, St. Augustine, Trinidad

ECNG2005 labs (P)

March 3, 2010

Lab 4-p. 23

10. (Challenge: Bonus 8 marks) Indirect addressing allows us to access data whose address is
stored at another memory location. This is particularly useful when dealing with multi-byte
information, which must be processed one byte at a time; null-teminated ASCII strings being
a key example.
In the pre-lab you were asked to write a routine which will decrypt a null-terminated string.
Name your routine decrypt which starts processing a string stored at data memory location
StrIn, and outputs a null terminated string which starts at data memory location StrOut.
We can modify the code from question 3, so that instead of just repeating what it received,
the PIC16F877 stores the received characters until the user hits the return key, generates the
decrypted string using decrypt and then transmits out the decrypted string.
(a) If we limit the string to a maximum of 15 characters; then 16 bytes needs to be allocated
for each null-teminated string. Use the CBLOCK directive to specify the locations
StrIn and StrOut which start each block of 16 memory locations.
Modify mainlp so that it will repeatedly:
(b) Store characters starting from StrIn at successive locations, until either the RETURN
key is pressed (ASCII code 0x13) or 15 characters have been received. Use a data
memory location named NxtIn to store the address where the next incoming character
will be stored.
(c) Place a null character (ASCII code 0x00) after the last received character.
(d) Use your decrypt routine to generate StrOut.
(e) Transmit the characters of StrOut one at a time, until either the null-character is encountered, or 15 characters have been transmitted. Use a data memory location named
NxtOut to store the address of the next character to be transmitted. Compile and test
your code. Does it work as you expected? What assumptions/choices did you have to
make? If your program didnt work on the rst try what were the problems? How did
you x it?

Print the nal version of your program and attach it to this script. Did this code work?
Yes or No (Circle your answer) If so, demonstrate the working code to your TA/lecturer
who will sign your attached printout AND your script.

c
DECE,
UWI, St. Augustine, Trinidad

ECNG2005 labs (P)

March 3, 2010

Lab 4-p. 24

11. (a) So far we have been polling. Let us convert the polling program to an interrupt driven
one. We can rewrite the main part of the code as:
(b) congure and control
loop:
- do something else
goto loop

(c) and then write an interrupt service routine, and handlers for the individual interrupts.
Let us start with the interrupt subroutine. Choose appropriate locations to store _W,
3 marks
_STATUS and _PCLATH, so that the code will work in all banks. Justify your choices.
_W
_STATUS
_PCLATH
isr

EQU
EQU
EQU

_______
_______
_______

; save all registers


movwf
_W
; Copy W to TEMP register
swapf
STATUS,W
; Swap status to be saved into W
clrf
STATUS
; bank 0, clears IRP,RP1,RP0
movwf
_STATUS
; Save status to bank zero STATUS_TEMP register
movf
PCLATH, W
; Only required if using pages 1, 2 and/or 3
movwf
_PCLATH
; Save PCLATH into W
clrf
PCLATH
; Page zero, regardless of current page
; no need to clear flags as they change automatically for this device
btfsc
PIR1, RCIF
goto
rx_handler

rtn_isr
movf
movwf
swapf

_PCLATH, W
PCLATH
_STATUS,W

movwf
swapf
swapf

STATUS
_W,F
_W,W

; Restore PCLATH
; Move W into PCLATH
; Swap STATUS_TEMP register into W
; (sets bank to original state)
; Move W into STATUS register
; Swap W_TEMP
; Swap W_TEMP into W
; all registers restored

retfie

c
DECE,
UWI, St. Augustine, Trinidad

ECNG2005 labs (P)

March 3, 2010

Lab 4-p. 25

(d) Next we have the individual handlers. We would like to transmit as soon as we receive,
so the transmit handler can be empty for now.
rx_handler
movf
movwf
goto

; on rx, tx the next character


________,W ; read the byte to clear the flag
__________
rtn_isr

(e) Now for the associated main loop, ll in the blanks in the following code.
LIST P=16F877
INCLUDE <P16F877.inc>
ORG
0x00
nop
goto
main
ORG
0x04
goto
isr
ORG
0x10
main
call
call
goto

Cnfg
UART_Cnfg
otherloop

Cnfg
bsf
______
bcf
bcf
bsf

STATUS,RP0 ; Go to Bank 1
TRISC,0 ; configure eb506 STATUS line as input
TRISC,1 ; configure eb506 MODE line as output
STATUS,RP0 ; Go to Bank 0
PORTC,1 ; set eb506 in data tx mode

waitconn
______
goto
return
UART_Cnfg
bsf
movlw
movwf
movlw
movwf
bsf
bcf
movlw
movwf
bsf
bsf
bsf
return
otherloop
goto
sleep
END

PORTC,0 ; wait until the serial connection is established.


waitconn

STATUS,RP0
; Go to Bank1
_________
; Set Baud rate to 9600
SPBRG
_________
; 8-bit transmit, transmitter enabled,
TXSTA
; asynchronous mode, low speed mode
PIE1, RCIE
; enable receive interrupts
STATUS,RP0
; Go to Bank 0
__________
; 8-bit receive, receiver enabled,
RCSTA
; serial port enabled
INTCON, ____
; enable peripheral & general interrupts
______, PEIE
RCSTA , CREN

otherloop

(f) Type in all your code, compile and run it. Test your program by using HyperTerminal.
Does it work as you expected? If not, why not, and/or what did you do to x it?

c
DECE,
UWI, St. Augustine, Trinidad

5 marks

ECNG2005 labs (P)

March 3, 2010

Lab 4-p. 26

(g) Print the nal version of your program and attach it to this script. Did this code work?
Yes or No (Circle your answer) If so, demonstrate the working code to your TA/lecturer
4 marks
who will sign your attached printout AND your script.

c
DECE,
UWI, St. Augustine, Trinidad

ECNG2005 labs (P)

March 3, 2010

Lab 4-p. 27

Application(bonus)
12. (Challenge: Bonus 7 marks) For the challenge question that you have done for question 4,
convert it to an interrupt driven approach.
(a) Compile and test your code. Does it work as you expected? What assumptions/choices
did you have to make? If your program didnt work on the rst try what were the problems? How did you x it?

(b) Print the nal version of your program and attach it to this script. Did this code work?
Yes or No (Circle your answer) If so, demonstrate the working code to your TA/lecturer
who will sign your attached printout AND your script.

c
DECE,
UWI, St. Augustine, Trinidad

ECNG2005 labs (P)

March 3, 2010

Lab 4-p. 28

13. (Challenge: Bonus 20 marks) Write,simulate and then implement a program that increments and switches between displays when an encoder strip is passed through the
opto-switch. The demonstration of the program would be done using the student ID
card. The card would be used to trigger the count and switch to the next seven segment display. The count should be recurring 0-f and this must be synchronized with
the switching of the displays 1 through 4. Every time the card passes through the opto
switch the progam is required to count and switch only once. HINT: a delay may be
required to account for noise in the circuit board
Compile and test your code. Does it work as you expected? What assumptions/choices
did you have to make? If your program didnt work on the rst try what were the problems? How did you x it?

Print the nal version of your program and attach it to this script. Did this code work?
Yes or No (Circle your answer) If so, demonstrate the working code to your TA/lecturer
who will sign your attached printout AND your script.
Total marks 149.
This exercise is worth 9% of your ECNG2007 lab mark.
PLAIGIARISM DECLARATION:
For the purposes of this exercise, unauthorised collaboration is any form of collaboration which does NOT fall into one of the following
categories:
verbal or written discussion/clarication of question and/or related concepts
assistance in troubleshooting circuitry and/or using/operating the IDE/debugger
Department of Electrical and Computer Engineering
PLAGIARISM Plagiarism is the presentation by a student of an assignment which has in fact been copied in whole or in part from another
students work, or from any other source (e.g. published books or periodicals), without due acknowledgement in the text.
COLLUSION Collusion is the presentation by a student of an assignment as his or her own which is in fact the result in whole or part of
unauthorised collaboration with another person or persons.
DECLARATION I declare that this assignment is my own work and does not involve plagiarism or collusion. I have read and understood
University Examination Regulations 73,75,76 and 79 regarding cheating.
Signed:

Date:

(Department of Electrical and Computer Engineering)

c
DECE,
UWI, St. Augustine, Trinidad

You might also like