You are on page 1of 7

EXPERIMENT 2: Elementary Input Output Programming

Objectives
Introduction to the Parallel Input/Output (I/O)
Familiarization to Interfacing with digital inputs and outputs such as switches,
LEDs and 7-segment.

Introduction

Overview of the PIC18F4550 Parallel Ports.

A PIC18F4550 microcontroller has 5 I/O ports known as port A, B, C, D and E. The pins of
an I/O port are often multifunctional with one or more peripheral functions e.g. interrupt,
timer, analog to digital and etc. In general, when a peripheral function is enabled, that pin
may not used as an I/O pin.

Note: Please refer to PIC18F4550 datasheet in moodle appendix for detailed functions of
each port and its pins.

Each I/O port has three register for its operation. These register are:
TRIS register (data direction register)
PORT register (reads the voltage levels on the pins device)
LAT register (output latch)

The register names for each port can be derived by adding the port name to these general
register names. For example, port A has TRISA, PORTA and LATA registers.

Reading and Writing the I/O Ports

Data direction needs to be set before the I/O operation. In order to configure an I/O pin for
input, set the associated bit in the TRIS register to 1. To configure an I/O pin for output, set
the associated bit in the TRIS register to 0.

Digital Switch Input

Switch is a commonly used component to give digital signal. PTK40A offers 4 commonly
used push buttons; one is designated for Reset purpose and other 3 as programmable digital
pin. It is being connected in pulled high configuration; the input signal is initially set to 5V
(high). When the switch is pressed, the input signal becomes 0V (low). The opposite
condition happened to the pulled low configuration. Microcontroller can be programmed to
read the status of input pin and determine action based on the status. Besides switches, digital
sensors also provide digital signal to microcontroller which serve the same function as
switches.

EEEB371 E2- 1
Figure 2.1 Connection of push buttons on PTK40A Training Kit

From Figure 2.1, SW1, SW2 and SW3 are being shared with KC1, KC2 and KC3 pins of
keypad respectively. Please do not press any key on the keypad while using the push button
switches. Switches is being pull-up to 5V through resistor, thus if pressed, the corresponding
pin is pulled to Gnd. In program, please check for logic low (0V) if a press is needed.

Digital LED Output

LED is the most basic and commonly used output device in electronic circuit board. It is an
indicator to display the logic status (High or Low) of a specific pin. Like any other output
devices, LED can be active-high or active-low as shown in Figure 2.2 below. In PTK40A,
most output pins of PIC are connected to an LED as indicator and connected in active high
configuration.

EEEB371 E2- 2
Figure 2.2 Connection of LEDs on PTK40A Training Kit

Digital Buzzer Output

Similar to LED, buzzer is a simple output component that can be used as sound indicator. The
buzzer will buzz continuously when power is provided (5V) and will turn off when the power
is being cut off (0V). In other words, this is an active high configuration. Refer to Figure 2.3
for the connection of buzzer with PIC Microcontroller.

Figure 2.3 Connection of Buzzer to RC2 on PTK40A Training Kit

EEEB371 E2- 3
For hardware configuration, set mini jumper on JP10 to activate buzzer as per Figure 2.4.

Figure 2.4 Hardware configuration for Buzzer

7-Segment Display

7-segment is a component that consists seven segments of lights which would display the
number from 0 to 9. For normal application, it can be connected as shown at figure 2.4
PTK40A uses common cathode 7-segment. Microcontroller can display any number (0-9) by
activating the correct segment via providing 5V similar to the LED in previous section; of
course the resistor is needed in between. For example, microcontroller can switch on the
segment a, b, c, d, g and switch off segment f, e to display the number 3. However, to save
the usage of the I/O pins of MCU and to simplify the control commands the 7-segment is
connected with a decoder unit. In PTK40A, 2 CD4511 (BCD decoder) are used to decode 2
units of 7-segment. With that we are able to control 2 units of 7-segment with 6 output pins
from microcontroller.

Note that RE0 is used to enable (low enable) the first 7-Segment and RE1 is used to enable
(low enable) the second 7-Segment. Both BCD decoders are sharing pins RD0-RD3.

Figure 2.5 Connection of 7-Segment on PTK40A Training Kit

EEEB371 E2- 4
Procedure

1. For this experiment, you are required to complete a program to fulfill your given task.

TASK 1: The task is to control the LEDs using SW1 (RB0) and SW2 (RB1). When
SW1 is pressed, LEDs RD0-RD7 should rotate from left to right with 1 second
interval and when SW2 is pressed, LEDs RD0-RD7 should blink with 1 second
interval. The LEDs should perform the specified tasks as long as each one of those
switches is pressed.

2. Write and complete the program below using MPLAB


#include<p18F4550.inc>

;include assembler directives here

;include makro here

org 0x00
goto start
org 0x08
retfie
org 0x18
retfie

;main program
start SETF TRISB,A ;configure portB as input
CLRF TRISD,A ;configure portD as output
CLRF PORTD,A ;initialize portD to turn OFF
AGAIN BTFSS PORTB,0 ;check SW1 condition
BRA FIRST
BTFSC PORTB,1 ;check SW2 condition
BRA AGAIN
SETF PORTD,A ;execute if SW2 is pressed
CALL DELAY1S
CLRF PORTD,A
CALL DELAY1S
BRA AGAIN
FIRST BSF PORTD,7,A ;execute if SW1 is pressed
CALL DELAY1S
MOVLW 0x07
MOVWF PRODL,A
LOOP RRNCF PORTD,F,A
CALL DELAY1S
DECFSZ PRODL,F,A
BRA LOOP
CLRF PORTD,A
BRA AGAIN
NOP

DELAY1S ;1sec delay for 20Mhz


;complete the program
RETURN

END

3. Execute and test your program on the board. Press the switches randomly. Write your
observation in the worksheet.

EEEB371 E2- 5
4. TASK 2: The next task is to turn ON and OFF the buzzer 5 times with 1 second
interval when SW3 is pressed. Write, complete and execute the program below. Write
your observation.

#include<p18F4550.inc>

;include assembler directives here

;include makro here

org 0x00
goto start
org 0x08
retfie
org 0x18
retfie

;main program
start SETF TRISB,A ;configure portB as input
BCF TRISC,2,A ;configure RC2 as output
BCF PORTC,2,A ;initialize RC2 to turn OFF
AGAIN BTFSC PORTB,2 ;check SW3 condition
BRA AGAIN
;complete the program
;to turn ON & OFF
;the buzzer 5 times
;when SW3 is pressed
BRA AGAIN
NOP

DELAY1S ;1sec delay for 20Mhz


;complete the program
RETURN

END

5. Draw the flowchart of the program for the program above.

EEEB371 E2- 6
6. TASK 3: The subsequent task is to display number 1 until 9 on the 7-Segment with 1
second interval between each number display when SW1 is pressed. Write, complete and
execute the program below. Write your observation.

#include<p18F4550.inc>

;include assembler directives here

;include makro here

org 0x00
goto start
org 0x08
retfie
org 0x18
retfie

;main program
start SETF TRISB,A ;configure portB as input
CLRF TRISE,A ;configure portE as output
CLRF TRISD,A ;configure portD as output
CLRF PORTD,A ;initialize portD to turn OFF
CLRF PRODH,A ;initialize PRODH to hold the value of number to be displayed
BSF PORTE,0,A ;disable first 7 segment and enable the second 7 segment
AGAIN BTFSC PORTB,0 ;check SW1 condition
BRA AGAIN
MOVLW 0x0A ;execute task when SW1 is pressed
MOVWF PRODL,A
LOOP MOVFF PRODH,PORTD
CALL DELAY1S
INCF PRODH,F,A
DECFSZ PRODL,F,A
BRA LOOP
CLRF PRODH,A
CLRF PORTD,A
BRA AGAIN
NOP

DELAY1S ;1sec delay for 20Mhz


;complete the program
RETURN

END

7. TASK 4: The final task to write a complete program which will display number 0 until 9
with 1 second interval between each number display when SW2 is pressed and upon
reaching number 9, the buzzer will ON and OFF 3 times with 1 second interval between
each beep. Write your observation. Copy and Paste ASM file to Word Doc and print.
Make sure that the ASM file is properly commented.

8. Turn off the PTK40A power supply, rearrange the USB cable back into the Training Kit.
Exit from MPLAB and PICKit 2 programmer. Shutdown your PC and rearrange your
workstation before you leave.

EEEB371 E2- 7

You might also like