EXPERIMENT 4: Parallel Input/Output Objectives Introduction to the Parallel Input/Output (I/O) Familiarization to Interfacing Components' List: 1. Protoboard 2. 4 x pushbutton 3. 4 x 330Ω resistor 4. 4 x 4.7kΩ resistor 5. 4 x LED Pre lab 1. Design a program loop to create a time delay of 0.5sec and named it as delay05s. This program loop is to be run/ clocked by a 20MHz crystal oscillator. 2. Complete the Program 4.1 below. Introduction Overview of the PIC18F458 Parallel Ports. A PIC18F458 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. 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.
Port A is a 7-bit wide, bidirectional port. Table 4.1: PORTA function Port B is an 8-bit wide, bidirectional port. Table 4.2: PORTB function Port C is an 8-bit wide, bidirectional port. Table 4.3: PORTC function
Port D is an 8-bit wide, bidirectional port. Table 4.4: PORTD functions Port E is a 3-bit wide, bidirectional port. Table 4.5: PORTE functions Procedure 1. For this experiment, you will construct a simple circuit and complete a program with your design. The program being identifies which of four (4) switches is closed and each LED will turn ON according to the button pressed. SW1 corresponds to the LED at pin RB0, SW2 to RB1, SW3 to RB2 and SW4 to RB3. The LEDs should only turn ON as long as the switches are pressed. 2. The input and output external circuits are shown in Figure 4.1. 3. Build the output and input circuits on a breadboard
Figure 4.1 Circuit diagram of the switch and LED connection 4. Write the program below using the MPLAB #include <p18f458.inc> TEMP EQU 0X10 ORG 0X00 GOTO START START CLRF TRISB,A ; OUTPUT PORT B MOVLW 0XF0 MOVWF TRISD,A CLRF PORTB,A AGAIN CLRF PORTB BTFSS PORTD,7,A RCALL LED3 BTFSS PORTD,6,A RCALL LED2 BTFSS PORTD,5,A RCALL LED1 BTFSS PORTD,4,A RCALL LED0 BRA AGAIN LED3 MOVLW 8 MOVWF PORTB,A LED2. (Complete the program) LED1. (Complete the program) LED0. (Complete the program) delay05s (write the delay s program here) Program 4.1 5. Assemble the program using the MPLAB and download the program using the bootloader (MicroC). To do so, double-click the MikroC shortcut at the desktop.
6. Under Tool menu, select mikrobootloader. Figure 4.2 Select mikrobootloader 7. Click Connect. Once Connected message is displayed at the History Window, click Open HEX file to browse to your hex file and then click Start bootloader to download your program into the PIC. Figure 4.3 mikrobootloader menu 8. Execute and test your program on the board. Press the switches randomly. Write your observation in the worksheet.
9. By using the same circuit, write, assemble and execute the program below. Write your observation. #include <p18f458.inc> TEMP EQU 0X10 ORG 0X00 GOTO START START CLRF TRISB,A ; OUTPUT PORT B MOVLW 0XF0 MOVWF TRISD,A CLRF PORTB,A AGAIN CLRF PORTB BTFSS PORTD,7,A RCALL DO_SW3 BTFSS PORTD,6,A RCALL DO_SW2 BTFSS PORTD,5,A RCALL DO_SW1 BRA AGAIN DO_SW1 SETF PORTB CLRF PORTB DO_SW2 MOVLW 5 MOVWF 0X01 MOVLW 1 MOVWF TEMP LOOP2 DCFSNZ 0X01,F,A MOVFF TEMP,PORTB RLNCF TEMP,F,A BRA LOOP2 DO_SW3 MOVLW 0X5 MOVWF PORTB RCALL DELAY05s MOVLW 0XA MOVWF PORTB delay05s (write the delay s program here) Program 4.2 10. Draw the flowchart of the program for Program 4.2. 11. Switch off PIC18 board and logoff.
Special instructions on how to use Proteus 1. For this experiment, you may use the existing Proteus circuit design provided by your lab instructor to verify your source code if the hardware is not working/faulty. 2. Go to Start>All Programs>Proteus 7 Professional>ISIS 7 Professional. 3. Open the design file named Lab4.DSN provided by your lab instructor. 4. The circuit will appear in the workspace.
5. To run the simulation, you need to load the hex file first by right-clicking on the microcontroller and then click edit properties. 6. At the Edit Component menu: a) Under the Program File tab, select your hex file by clicking on the browse icon. b) Under the Processor Clock Frequency, select your desired clock frequency. c) Under Advanced Properties, select Enable Watchdog Timer and then select No. d) Finally, click OK. 7. Finally, click on the Play button at the bottom of the workspace to run the simulation.