I/O Interfacing and Programming

Slides:



Advertisements
Similar presentations
Numbers Treasure Hunt Following each question, click on the answer. If correct, the next page will load with a graphic first – these can be used to check.
Advertisements

Adders Used to perform addition, subtraction, multiplication, and division (sometimes) Half-adder adds rightmost (least significant) bit Full-adder.
1
© 2008 Pearson Addison Wesley. All rights reserved Chapter Seven Costs.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
McGraw-Hill©The McGraw-Hill Companies, Inc., 2003 Chapter 3 Data Transmission.
Chapter 1 The Study of Body Function Image PowerPoint
1 Copyright © 2013 Elsevier Inc. All rights reserved. Chapter 4 Computing Platforms.
Processes and Operating Systems
1 Copyright © 2013 Elsevier Inc. All rights reserved. Chapter 1 Embedded Computing.
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
Author: Julia Richards and R. Scott Hawley
1 Copyright © 2013 Elsevier Inc. All rights reserved. Appendix 01.
1 Copyright © 2013 Elsevier Inc. All rights reserved. Chapter 3 CPUs.
Properties Use, share, or modify this drill on mathematic properties. There is too much material for a single class, so you’ll have to select for your.
Objectives: Generate and describe sequences. Vocabulary:
UNITED NATIONS Shipment Details Report – January 2006.
RXQ Customer Enrollment Using a Registration Agent (RA) Process Flow Diagram (Move-In) Customer Supplier Customer authorizes Enrollment ( )
Document #07-2I RXQ Customer Enrollment Using a Registration Agent (RA) Process Flow Diagram (Move-In) (mod 7/25 & clean-up 8/20) Customer Supplier.
We need a common denominator to add these fractions.
FIGURE 8.1 Process and controller.
1 RA I Sub-Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Casablanca, Morocco, 20 – 22 December 2005 Status of observing programmes in RA I.
Properties of Real Numbers CommutativeAssociativeDistributive Identity + × Inverse + ×
Create an Application Title 1A - Adult Chapter 3.
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
Rhesy S.ppt proRheo GmbH
1 Click here to End Presentation Software: Installation and Updates Internet Download CD release NACIS Updates.
REVIEW: Arthropod ID. 1. Name the subphylum. 2. Name the subphylum. 3. Name the order.
Break Time Remaining 10:00.
Figure 12–1 Basic computer block diagram.
PP Test Review Sections 6-1 to 6-6
TCP/IP Protocol Suite 1 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 2 The OSI Model and the TCP/IP.
Bright Futures Guidelines Priorities and Screening Tables
EIS Bridge Tool and Staging Tables September 1, 2009 Instructor: Way Poteat Slide: 1.
Chapter 3 Basic Logic Gates 1.
Flip-Flops and Registers
Bellwork Do the following problem on a ½ sheet of paper and turn in.
Exarte Bezoek aan de Mediacampus Bachelor in de grafische en digitale media April 2014.
VOORBLAD.
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
1 RA III - Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Buenos Aires, Argentina, 25 – 27 October 2006 Status of observing programmes in RA.
Factor P 16 8(8-5ab) 4(d² + 4) 3rs(2r – s) 15cd(1 + 2cd) 8(4a² + 3b²)
Basel-ICU-Journal Challenge18/20/ Basel-ICU-Journal Challenge8/20/2014.
1..
CONTROL VISION Set-up. Step 1 Step 2 Step 3 Step 5 Step 4.
© 2012 National Heart Foundation of Australia. Slide 2.
Adding Up In Chunks.
Understanding Generalist Practice, 5e, Kirst-Ashman/Hull
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt Synthetic.
Note to the teacher: Was 28. A. to B. you C. said D. on Note to the teacher: Make this slide correct answer be C and sound to be “said”. to said you on.
Model and Relationships 6 M 1 M M M M M M M M M M M M M M M M
25 seconds left…...
Datorteknik TopologicalSort bild 1 To verify the structure Easy to hook together combinationals and flip-flops Harder to make it do what you want.
Analyzing Genes and Genomes
Speak Up for Safety Dr. Susan Strauss Harassment & Bullying Consultant November 9, 2012.
To the Assignments – Work in Progress Online Training Course
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Essential Cell Biology
Intracellular Compartments and Transport
PSSA Preparation.
Essential Cell Biology
Datorteknik TopologicalSort bild 1 To verify the structure Easy to hook together combinationals and flip-flops Harder to make it do what you want.
Immunobiology: The Immune System in Health & Disease Sixth Edition
Energy Generation in Mitochondria and Chlorplasts
Murach’s OS/390 and z/OS JCLChapter 16, Slide 1 © 2002, Mike Murach & Associates, Inc.
Profile. 1.Open an Internet web browser and type into the web browser address bar. 2.You will see a web page similar to the one on.
TCP/IP Protocol Suite 1 Chapter 18 Upon completion you will be able to: Remote Login: Telnet Understand how TELNET works Understand the role of NVT in.
1 Decidability continued…. 2 Theorem: For a recursively enumerable language it is undecidable to determine whether is finite Proof: We will reduce the.
One of the most important feature of the microcontroller is a number of input/output pins used for connection with peripherals. In the case of PIC 167CX,
Presentation transcript:

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 2002. All rights reserved. No further reprints or reproductions may be made without Microchip Technology Inc.’s prior written consent.

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

PIN Diagram of PIC18F4520

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!

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.

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

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.

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.

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.

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

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 = 0b11110000; //RA0-3 as o/p, RA4-5 as i/p

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 = 0b11010110 ; ADCON1 = 0x0f;

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 = 0b11000000; // Port A pin 0-5 are outputs TRISC = 0b10110010; //RC0,2,3,6 as o/p, RC1,4,5,7 as i/p

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.

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

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

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

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

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

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;

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;

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

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 = 0b11111011; // RD2 as o/p PORTD = 0x00; // or PORTDbits.RD2 = 0

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 = 0b10000001; // RD1 - 6 as o/p PORTD = 0b01100010;

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 = 0b10000001; // RD1 - 6 as o/p PORTD = ~PORTD;

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;

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;

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 = 0b11111111; // entire Port C as input x = PORTC;

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 }

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

Exercise 4 Solution #include <p18f4520.h> //Directive main() { /* initialize the I/O port */ ADCON1 = 0x0f; // Port B as digital TRISB = 0b11111011; // 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 }

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.

Exercise 5 Solution #include <p18f4520.h> // Directive #include<delays.h> // include library delay function void main () { // Function TRISB = 0b11110000; // 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 }

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.

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

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

Solution to Question 1: ADCON1 = 0x0F; TRISA = 0bXX100111; //X can be 1 or 0 TRISB = 0x00; TRISC = 0b11100000; TRISD = 0b00000001; 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 */

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

Solution to Question 2: #include <p18f4520.h> // Directive void main () { // Function ADCON1 = 0x0F; // PORTA as digital IO TRISA = 0xFF; // PORTA as i/p TRISB = 0b11110000; // 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 }

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 ?