Microcontroller Technology Oct 11 Page 1 Welcome to... Seminar 5: I/O Interfacing and Programming...
-
Upload
nicholas-sutton -
Category
Documents
-
view
215 -
download
2
Transcript of Microcontroller Technology Oct 11 Page 1 Welcome to... Seminar 5: I/O Interfacing and Programming...
Microcontroller Technology Oct 11Page 1
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.
Microcontroller Technology Oct 11Page 2
Objectives:
At the end of this seminar, you should be able to:Initialize the input/output ports Send the data to output portsRead the data from input ports
Microcontroller Technology Oct 11Page 3
PIN Diagram of PIC18F4520
Microcontroller Technology Oct 11Page 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
Microcontroller Technology Oct 11Page 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.
Microcontroller Technology Oct 11Page 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
Microcontroller Technology Oct 11Page 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.
Microcontroller Technology Oct 11Page 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.
Microcontroller Technology Oct 11Page 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.
Microcontroller Technology Oct 11Page 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
Microcontroller Technology Oct 11Page 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’
ADCON1 = 0x0F; // Port A&E as digital I/O
TRISA = 0b11110000; //RA0-3 as o/p, RA4-5 as i/p
Solution:
Microcontroller Technology Oct 11Page 12
Configure Port B pins 1, 2, 4, 6 and 7 as inputs and pins 0, 3 and 5 as output pins.
TRISB = 0b11010110 ;ADCON1 = 0x0f;
Example 2
Solution:
Microcontroller Technology Oct 11Page 13
Example 3Given the circuit below, configure the directions of the I/O pins used.
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
RC1RC0
RC2RC3RC4RC5RC6RC7
RA1RA0
RA2RA3
RA5RA4
Microcontroller Technology Oct 11Page 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.
Microcontroller Technology Oct 11Page 15
PIC18F4520
Vcc
Vcc
J6
S3
S2
RB0/INT0
RA4
RB1
RB2
RB3
RD3
RD2
RD1
RD0
E
R/W
RS
Vcc
VEE
GND
GND1
RA3
RA2
RA1
DB7
DB6
DB5
DB4
DB3
DB2
DB1
DB0
LCD16 Char X 2 lines
Vcc
RX
TX
RC7
RC6
Vcc
Vcc
RA0
J9Piezo BuzzerRC2
Exercise 1
Microcontroller Technology Oct 11Page 16
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
Solution
Exercise 1
Microcontroller Technology Oct 11Page 17
Project Time!
Let us try to initialize the I/O ports for our Electronic Queueing System
Microcontroller Technology Oct 11Page 18
Project: Queueing System
Hardware design
PIC18F4520
RB1
16x2 linesLCD Module
RC0RC1RC2RC3RC4RC5RC6RC7
D0D1D2D3D4D5D6D7
RE1RE2
E RS RW+5V
5V
RA1
RA0
RA2
+5.0V
RB0
Alarm
Q_request
Button 1
Button 2
Button 3
Microcontroller Technology Oct 11Page 19
/* 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 */
Microcontroller Technology Oct 11Page 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;
Microcontroller Technology Oct 11Page 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;
Microcontroller Technology Oct 11Page 22
Example 4
Write a program segment to configure entire PORTC as output port and write 0x0F to PORTC.
TRISC = 0b00000000; // entire Port C as output
PORTC = 0x0F;
Solution
Microcontroller Technology Oct 11Page 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
LEDSolution
TRISD = 0b11111011; // RD2 as o/p
PORTD = 0x00; // or PORTDbits.RD2 = 0
Microcontroller Technology Oct 11Page 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 = 0b10000001; // RD1 - 6 as o/p
PORTD = 0b01100010;
Microcontroller Technology Oct 11Page 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 = 0b10000001; // RD1 - 6 as o/p
PORTD = ~PORTD;
Microcontroller Technology Oct 11Page 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;
Microcontroller Technology Oct 11Page 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;
Microcontroller Technology Oct 11Page 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 = 0b11111111; // entire Port C as input
x = PORTC;
Solution
Microcontroller Technology Oct 11Page 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
Microcontroller Technology Oct 11Page 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 BTurn ON or OFF the LED corresponding to switch being CLOSED or OPEN
PIC18F4520
RB3
LED R
+V
SW
RB2
Microcontroller Technology Oct 11Page 31
Exercise 4
#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
}
Solution
Microcontroller Technology Oct 11Page 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.
Microcontroller Technology Oct 11Page 33
#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
}
}
Exercise 5
Solution
Microcontroller Technology Oct 11Page 34
Summary
You have just learnt:I/O ports of PIC18F4520Configuring of I/O pinReading from and Writing to I/O PinI/O Programming
We will learn more programming techniques next week on how to write a looping program.
Microcontroller Technology Oct 11Page 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
Microcontroller Technology Oct 11Page 36
PIC18F4520
RA4
RA3
RE2
RD7
RD6
RD5
RD4
RD3
RD2
RD1 g
a
f
be
c
d
d
c
e
b
f
a
g
5V
RC0RC1
+5V
M
RC2
RC3
RC4
TIME
+5V
SPEED
+5V
POWER ON
OFF
RB0RB1RB2RB3RB4RB5RB6RB7
16x2 linesLCD Module
E RS RW
D0D1D2D3D4D5D6D7
RE0
RE1
Speaker
Motor
HIGH
MID
LOW
+V
Microcontroller Technology Oct 11Page 37
ADCON1 = 0x0F;
TRISA = 0bXX100111; //X can be 1 or 0
TRISB = 0x00;
TRISC = 0b11100000;
TRISD = 0b00000001;
TRISE = 0bXXX0X111;
Solution to Question 1:
/*TRISE bit4 must be written as 0 so that PORTD will be in General Purpose I/O mode */
Microcontroller Technology Oct 11Page 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
Microcontroller Technology Oct 11Page 39
Solution to Question 2:
#include <p18f4520.h> // Directive
void main () { // Function ADCON1 = 0x0F; // PORTA as digital IO
TRISA = 0xFF; // PORTA as i/pTRISB = 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 } }
Microcontroller Technology Oct 11Page 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 ?