Presentation is loading. Please wait.

Presentation is loading. Please wait.

I/O Interfacing and Programming

Similar presentations


Presentation on theme: "I/O Interfacing and Programming"— Presentation transcript:

1 I/O Interfacing and Programming
Welcome to . . . Seminar 5: I/O Interfacing and Programming Reprinted with permission of the copyright owner, Microchip Technology Incorporated ©2000, 2001 and All rights reserved. No further reprints or reproductions may be made without Microchip Technology Inc.’s prior written consent.

2 Objectives: At the end of this seminar, you should be able to:
Initialize the input/output ports Send the data to output ports Read the data from input ports

3 PIN Diagram of PIC18F4520

4 PIC18F4520 I/O Ports I/O Ports: Facilitate the microcontroller in communicating with the outside world 5 I/O ports (Port A, B, C, D, E) 36 I/O pins RE3 is Input only The rest of the I/O pins are bi-directional  hence, we need to program the direction of I/O port pins before we use them!

5 PIC18F4520 I/O Ports Port A (8 pins : RA0 – RA7)
Port B (8 pins : RB0 – RB7) Port C (8 pins : RC0 – RC7) Port D (8 pins : RD0 – RD7) Port E (4 pins : RE0 – RE3) All ports except RE3 can be used for digital I/O . RE3 is input only. Ports A,B & E can also be used for analog input.

6 Registers Each port has 3 registers associated with it.
TRIS (Data Direction) PORT (Read/Write DATA from or to I/O pin ) LAT (Data Latch register for read-modify-write) In order to use a port, we need -to set the data direction in the TRIS REGISTER -to read data from/write to the PORT REGISTER

7 PORTx and TRISx There are five I/O ports in PIC18F They are Ports A, B, C, D and E. The data registers for these ports are named PORTA, PORTB, PORTC, PORTD and PORTE. The direction of the individual pins in the port is configured by programming the TRISA, TRISB, TRISC, TRISD and TRISE. A ‘1’ in the TRISx bit sets the corresponding pin of the port as input whereas a ‘0’ sets the pin as output.

8 Ports A, B & E I/O pins in Ports A, B and E can either be used as an analog input pin or digital I/O pin. Indicate their use as analog/ digital pins by writing to the ADCON1 register.

9 ADCON1 Port A, Port B and Port E can be used for digital I/O as well as analog input. Programming ADCON1 will set Port A , Port B or /and Port E as analog or digital. A 0x0F to ADCON1 will initialize ALL the pins of Ports A, B and E as digital. User can also choose to configure some pins as analog while others as digital. For such applications, please refer to the PIC18F4520 Concise Data Book for different binary values to be written to ADCON1.

10 Using I/O Ports Identify the Port to be used
If Port A, B and/or E is used, configure them as digital I/O pins by Setting ADCON1 Configure the direction of each I/O pin Using TRISx (E.g Port A use TRISA) Read from or Write to the pin or port Using PORTx

11 Example 1 Configure port A such that pins RA<5:4> are inputs and pins RA<3:0> are outputs Bit <5:4> of TRISA = ‘1’ Bit <3:0> of TRISA = ‘0’ The remaining bits (Bits 6 & 7) can be ‘0’ or ‘1’ Solution: ADCON1 = 0x0F; // Port A&E as digital I/O TRISA = 0b ; //RA0-3 as o/p, RA4-5 as i/p

12 Example 2 TRISB = 0b11010110 ; ADCON1 = 0x0f; Solution:
Configure Port B pins 1, 2, 4, 6 and 7 as inputs and pins 0, 3 and 5 as output pins. Solution: TRISB = 0b ; ADCON1 = 0x0f;

13 Example 3 Solution: ADCON1 = 0x0F; // Port A&E as digital I/O
Given the circuit below, configure the directions of the I/O pins used. RC1 RC0 RC2 RC3 RC4 RC5 RC6 RC7 RA1 RA0 RA2 RA3 RA5 RA4 Solution: ADCON1 = 0x0F; // Port A&E as digital I/O TRISA = 0b ; // Port A pin 0-5 are outputs TRISC = 0b ; //RC0,2,3,6 as o/p, RC1,4,5,7 as i/p

14 Exercise 1 Refer to the schematic diagram shown in the next slide. Write a C program code segment to initialize ALL the input/output ports.

15 Exercise 1 PIC18F4520 Vcc J6 S3 S2 RB0/INT0 RA4 RB1 RB2 RB3 RD3 RD2
R/W RS VEE GND GND1 RA3 RA2 RA1 DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0 LCD 16 Char X 2 lines RX TX RC7 RC6 RA0 J9 Piezo Buzzer RC2

16 Exercise 1 Solution ADCON1 = 0x0F; // Port A, B &E as digital I/O
TRISA = 0b ; //RA1,2 & 3 as o/p, RA0 & 4 as i/p TRISB = 0b ; // lower 4-bits as o/p TRISC = 0b ; // RC2 & 6 as o/p, RC7 as i/p TRISD = 0b ; // lower 4-bits as o/p

17 Project Time! Let us try to initialize the I/O ports for our Electronic Queueing System

18 Project: Queueing System
Hardware design PIC18F4520 RB1 16x2 lines LCD Module RC0 RC1 RC2 RC3 RC4 RC5 RC6 RC7 D0 D1 D2 D3 D4 D5 D6 D7 RE1 RE2 E RS RW +5V 5V RA1 RA0 RA2 +5.0V RB0 Alarm Q_request Button 1 Button 2 Button 3

19 /* Initialise the IO ports */
ADCON1 = 0x0F; /* PORTA, B & E as digital IO */ TRISA = 0xFF; /* PORTA as digital input ports – for buttons*/ TRISB = 0b ; /* RB0 as output pin for alarm */ TRISC = 0x00; /* PORTC as digital output data ports for LCD */ TRISE = 0b ; /* RE1 & RE2 as digital output pin for LCD control */

20 Output Operation To send data to the whole output port, you can use this instruction: PORTx = [DATA]; This instruction writes [DATA] to the PORTx Example: PORTA = 0x0F;

21 Output Operation To send data to the individual output pins, you can use this instruction: PORTxbits.Rxn = [DATA]; This instruction writes [DATA] to the PORTx bit n. Example: PORTCbits.RC4 = 1;

22 Example 4 Write a program segment to configure entire PORTC as output port and write 0x0F to PORTC. Solution TRISC = 0b ; // entire Port C as output PORTC = 0x0F;

23 Example 5 Solution TRISD = 0b11111011; // RD2 as o/p
Assume that a LED is tied to bit 2 of PORT D as shown in the circuit below. Write a program segment to initialize bit 2 of Port D as output and turn on the LED. PIC18F4520 RD2 R +V LED Solution TRISD = 0b ; // RD2 as o/p PORTD = 0x00; // or PORTDbits.RD2 = 0

24 Exercise 2 Given the circuit as shown, write a program segment to turn on LEDs 1, 2 and 6, and turn off LEDs 3, 4 and 5 LED2 RD1 RD2 RD3 RD4 RD5 RD6 PIC18F4520 LED1 LED3 LED4 LED5 LED6 Solution TRISD = 0b ; // RD1 - 6 as o/p PORTD = 0b ;

25 Exercise 3 Given the circuit as shown, write a program segment to toggle all the LEDs LED2 RD1 RD2 RD3 RD4 RD5 RD6 PIC18F4520 LED1 LED3 LED4 LED5 LED6 Solution TRISD = 0b ; // RD1 - 6 as o/p PORTD = ~PORTD;

26 Input Operation The whole input port can be read using the instruction: A = PORTx; This instruction reads the contents of PORTx into a register file A Example: data = PORTB;

27 Input Operation Individual input pins can be read using the instruction: A = PORTxbits.Rxn; This instruction reads the contents of PORTx pin n into a register file A Example: data = PORTBbits.RB2;

28 Example 6 Write a program segment to configure the entire PORT C as input port and read in the value of PORT C. Solution TRISC = 0b ; // entire Port C as input x = PORTC;

29 Example 7 Refer to the diagram below, write a program segment, to test whether the bit 3 of PORTC is logic 0. PIC18F4520 +V SW RC3 Solution if (PORTCbits.RC3 == 0) { //continue the task if SW is closed }

30 Exercise 4 An LED is tied to bit 2 of PORT B & the switch SW is connected to bit 3 of PORTB. Write a complete program to: Initialise Port B Turn ON or OFF the LED corresponding to switch being CLOSED or OPEN +V RB2 LED R SW PIC18F4520 RB3

31 Exercise 4 Solution #include <p18f4520.h> //Directive main() {
/* initialize the I/O port */ ADCON1 = 0x0f; // Port B as digital TRISB = 0b ; // RB2 as o/p, all other pins as i/p /* read the input */ if (PORTBbits.RB3 == 0) // if SW is closed, logic ‘0’ PORTBbits.RB2 = 0; // turn ON the LED else // if SW is open, logic ‘1’ PORTBbits.RB2 = 1; // turn OFF the LED }

32 Exercise 5 Refer to the same schematic diagram as in the Exercise 1. Write a complete C program to toggle all the 4 LEDs in every 0.2S. Note: Assume that calling the library function Delay10KTCYx(20) will give a delay of 0.2 sec delay.

33 Exercise 5 Solution #include <p18f4520.h> // Directive
#include<delays.h> // include library delay function void main () { // Function TRISB = 0b ; // Lower 4 bits of Port B as output ADCON1 = 0x0f; PORTB = 0x00; // Initially off all LEDs while(1) { // Endless loop PORTB = ~PORTB; // Toggle Port B content Delay10KTCYx(20); // call library for 0.2sec delay }

34 Summary You have just learnt:
I/O ports of PIC18F4520 Configuring of I/O pin Reading from and Writing to I/O Pin I/O Programming We will learn more programming techniques next week on how to write a looping program.

35 Do-it-yourself Question 1:
Write a ‘C’ program segment to configure all the I/O ports for the circuit shown in next slide.

36 a b d e f c g PIC18F4520 RA4 RA3 RE2 RD7 RD6 RD5 RD4 RD3 RD2 RD1 RC0
5V RC0 RC1 +5V M RC2 RC3 RC4 TIME SPEED POWER ON OFF RB0 RB1 RB2 RB3 RB4 RB5 RB6 RB7 16x2 lines LCD Module E RS RW D0 D1 D2 D3 D4 D5 D6 D7 RE0 RE1 Speaker Motor HIGH MID LOW +V

37 Solution to Question 1: ADCON1 = 0x0F;
TRISA = 0bXX100111; //X can be 1 or 0 TRISB = 0x00; TRISC = 0b ; TRISD = 0b ; TRISE = 0bXXX0X111; Point out that the X in the binary sequence stands for don’t care condition. Emphasize that students should use either 1 or 0 for these don’t care bits while writing programs. /*TRISE bit4 must be written as 0 so that PORTD will be in General Purpose I/O mode */

38 Do-it-yourself Question 2:
Write a complete ‘C’ program to initialize the PIC18F4520 to operate the circuit shown below. Continuously, read the switches from RA0 to RA3 and switch on the corresponding LEDs in Port B, for the closed switches. Assume that RB4 to RB7 are not used. LED3 RB0 RB1 RB2 RB3 PIC18F4520 LED4 LED2 LED1 V+ RA2 RA0 RA1 RA3

39 Solution to Question 2: #include <p18f4520.h> // Directive
void main () { // Function ADCON1 = 0x0F; // PORTA as digital IO TRISA = 0xFF; // PORTA as i/p TRISB = 0b ; // Lower 4 bits of Port B as output PORTB = 0x00; // Initially off all LEDs while(1) { // Endless loop PORTB = ~PORTA; // read from PORTA & complement it // before send to PORTB }

40 To stimulate your thinking…
Take a look at the do-it-yourself question 2. You need to read a switch and then light up an LED based on the switch status. Is it necessary to link the switch and the LED through wire connections? Can we do the linkage through the software alone? If your answer to (b) is “yes”, which hardware component is this flexibility due to ?


Download ppt "I/O Interfacing and Programming"

Similar presentations


Ads by Google