Presentation is loading. Please wait.

Presentation is loading. Please wait.

Microcontroller Technology Oct 11 Page 1 Welcome to... Seminar 5: I/O Interfacing and Programming Reprinted with permission of the copyright owner, Microchip.

Similar presentations


Presentation on theme: "Microcontroller Technology Oct 11 Page 1 Welcome to... Seminar 5: I/O Interfacing and Programming Reprinted with permission of the copyright owner, Microchip."— Presentation transcript:

1

2 Microcontroller Technology Oct 11 Page 1 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.

3 Microcontroller Technology Oct 11 Page 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

4 Microcontroller Technology Oct 11 Page 3 PIN Diagram of PIC18F4520

5 Microcontroller Technology Oct 11 Page 4 PIC18F4520 I/O Ports 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! I/O Ports: Facilitate the microcontroller in communicating with the outside world

6 Microcontroller Technology Oct 11 Page 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.

7 Microcontroller Technology Oct 11 Page 6 Registers TRIS (Data Direction) PORT (Read/Write DATA from or to I/O pin ) LAT (Data Latch register for read-modify- write) Each port has 3 registers associated with it. 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

8 Microcontroller Technology Oct 11 Page 7 PORTx and TRISx There are five I/O ports in PIC18F4520. 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.

9 Microcontroller Technology Oct 11 Page 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.

10 Microcontroller Technology Oct 11 Page 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.

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

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

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

14 Microcontroller Technology Oct 11 Page 13 Example 3 Given the circuit below, configure the directions of the I/O pins used. 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 RC1 RC0 RC2 RC3 RC4 RC5 RC6 RC7 RA1 RA0 RA2 RA3 RA5 RA4

15 Microcontroller Technology Oct 11 Page 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.

16 Microcontroller Technology Oct 11 Page 15 Exercise 1

17 Microcontroller Technology Oct 11 Page 16 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 Solution Exercise 1

18 Microcontroller Technology Oct 11 Page 17 Project Time! Let us try to initialize the I/O ports for our Electronic Queueing System

19 Microcontroller Technology Oct 11 Page 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 RSRW +5V 5V RA1 RA0 RA2 +5.0V RB0 Alarm Q_request Button 1 Button 2 Button 3

20 Microcontroller Technology Oct 11 Page 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 */

21 Microcontroller Technology Oct 11 Page 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;

22 Microcontroller Technology Oct 11 Page 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;

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

24 Microcontroller Technology Oct 11 Page 23 Example 5 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

25 Microcontroller Technology Oct 11 Page 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 ;

26 Microcontroller Technology Oct 11 Page 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;

27 Microcontroller Technology Oct 11 Page 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;

28 Microcontroller Technology Oct 11 Page 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;

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

30 Microcontroller Technology Oct 11 Page 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 if ( PORTCbits.RC3 == 0) { //continue the task if SW is closed } Solution

31 Microcontroller Technology Oct 11 Page 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 PIC18F4520 RB3 LED R +V SW RB2

32 Microcontroller Technology Oct 11 Page 31 Exercise 4 #include //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 } Solution

33 Microcontroller Technology Oct 11 Page 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.

34 Microcontroller Technology Oct 11 Page 33 #include // Directive #include // 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 } Exercise 5 Solution

35 Microcontroller Technology Oct 11 Page 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.

36 Microcontroller Technology Oct 11 Page 35 Question 1: Write a C program segment to configure all the I/O ports for the circuit shown in next slide. Do-it-yourself

37 Microcontroller Technology Oct 11 Page 36

38 Microcontroller Technology Oct 11 Page 37 ADCON1 = 0x0F; TRISA = 0bXX100111; //X can be 1 or 0 TRISB = 0x00; TRISC = 0b ; TRISD = 0b ; TRISE = 0bXXX0X111; Solution to Question 1: /*TRISE bit4 must be written as 0 so that PORTD will be in General Purpose I/O mode */

39 Microcontroller Technology Oct 11 Page 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

40 Microcontroller Technology Oct 11 Page 39 Solution to Question 2: #include // 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 }

41 Microcontroller Technology Oct 11 Page 40 To stimulate your thinking… 1.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. a)Is it necessary to link the switch and the LED through wire connections? b)Can we do the linkage through the software alone? c)If your answer to (b) is yes, which hardware component is this flexibility due to ?


Download ppt "Microcontroller Technology Oct 11 Page 1 Welcome to... Seminar 5: I/O Interfacing and Programming Reprinted with permission of the copyright owner, Microchip."

Similar presentations


Ads by Google