Microcontroller Lecture # 6_edited
Microcontroller Lecture # 6_edited
Chapter 4
Week 6
Biomedical Engineering Department
College of Engineering
Imam Abdulrahman Bin Faisal University, Saudi Arabia 1
5-Arithmetic, Logic
3-Branch, Call and Time 4-PIC I/O Port
Instructions, and
Delay Loop Programming
Programs
2
Today’s lecture
➢List all the ports of the PIC18
3
Number of Ports in PIC18 Family
Members
4
I/O PORT PROGRAMMING IN PIC18
❑In the PIC18 family, there are many ports for I/O operations,
depending on which family member you choose.
❑The PIC18F458 40-pin chip. A total of 33 pins are set aside for
the five ports
1. PORTA
2. PORTB
3. PORTC
4. PORTD
5. PORTE
❑The rest of the pins are designated as Vdd (VCC), VSS (GND).
❑OSC1, OSC2, MCLR (reset), and another set of Vdd and Vss
5
PICF458 Pin Diagram
A B
C C
D D
3/17/2024 Eng.Kamran Hameed 6
I/O port pins and their functions
❑The 18-pin PIC18 has ports A and B
❑The 64-pin version has ports A through F
❑The 80-pin PIC18 has ports A through L
❑The 40-pin PIC18F458 has five ports. They are PORTA,
PORTB, PORTC, PORTD, and PORTE.
❑To use any of these ports as an input or output port, it must be
programmed
❑In addition to being used for simple I/O, each port has some other
functions such as ADC, timers, interrupts, and serial
communication pins
7
Port Pins of PIC18458
❑Not all ports have 8 pins.
❑ The PIC18F458, Port A has 7 pins
❑Ports B, C, and D each have 8 pins
❑Port E has only 3 pins.
❑Each port has three SFRs associated with it
❑They are designated as
1. PORTx,
2. TRISx
3. LATx.
❑For example, for Port B we have PORTB, TRISB, and LATB.
8
Ports' SFR Addresses for PIC18F458
9
TRIS register role in outputting data
❑Each of the Ports A-E in the PIC18F458 can be used for
input or output.
❑The TRISx SFR is used solely for the purpose of making a
given port an input or output port.
❑For example, to make a port an output, we write 0s to
the TRISx register.
❑In other words, to output data to any of the pins of the
Port B, we must first put 0s into the TRISB register to make
it an output port, and then send the data to the Port B SFR
itself.
10
❑The following code will toggle all 8 bits of Port B
forever with some time delay in between "on" and
"off ' states
11
TRIS register role in Inputting data
❑ To make a port an Input port, we must first put 1s
into the TRISx register for that port, and then bring in
(read) the data present at the pins.
12
❑ The following code will get the data present at the pins
of port C and send it to port B indefinitely, after adding
the value 5 to it:
13
Read followed by write I/O operation
❑ Due to the timing issue, we must be careful not to have two I/O
operations one right after the other.
❑ We need NOP to make sure that data is written in WREG before
it is read for outputting. This is called data dependency in
CPU design
❑ This type of data dependency is commonly referred to as RAW
(Read-after-write)
❑ One way to avoid this problem is to use the MOVFF instruction,
which is a 4 byte instruction.
14
Read followed by write I/O operation
15
Port A, B, C and D
❑ Port A occupies a total of 7 pins (RAO-RA6), but for the
PIC18F458, pin A6 is used for the OSC2 pin. A6 is not available if
we use a crystal oscillator to provide frequency to the PIC18 chip
PORT A:
17
❑ For example, the following code will continuously send out to
Port A, B, C, D the alternating values of 55H and AAH:
PORT B:
18
❑ For example, the following code will continuously send out to
Port A, B, C, D the alternating values of 55H and AAH:
PORT C:
19
❑ For example, the following code will continuously send out to
Port A, B, C, D the alternating values of 55H and AAH:
PORT D:
20
Port A,B,C,D as Inputs
❑ In order to make all the bits of Port A, B, C and D an inputs,
TRISA must be programmed by writing 1s to all the bits.
21
Port A,B,C,D as Inputs
PORT A:
PORT B:
22
Port A,B,C,D as inputs
PORT C:
PORT D:
23
Port E
❑ Port E occupies a total of 3 pins (REO-RE2) in the
PIC18F458/4580.
❑ Port E is used for 3 additional analog inputs or simple I/O:
AN5, AN6, and AN7. Just like other ports.
❑ Port E has alternate functions. We will show how to use them
in future chapters.
24
Dual role of Ports A and B
❑ The PIC18 multiplexes an analog-to-digital converter through
Port A to save I/O pins. The alternate functions of the pins for
Port A are shown in Table below. we do not use Port A for simple
I/O functions.
❑ The PIC18 multiplexes some other functions through Port B to
save pins. The alternate functions of the pins for Port
25
Dual role of Ports C and D
❑ The alternate functions of the pins for Port C are shown in
Table 4-5. We will show how to use Port C alternate functions in
future chapters. The alternate functions of the pins for Port D are
shown in Table 4-6
26
Different ways of accessing the entire 8
bits
❑ In the following code, as in many previous I/O examples, the
entire 8 bits of Port B are accessed
27
I/O BIT MANIPULATION
PROGRAMMING
❑ In this section we further examine the PIC18
I/O instructions.
28
I/O ports and bit-addressability
❑ Sometimes we need to access only 1 or 2 bits of the
port instead of the entire 8 bits.
30
Table 4-9: Single-Bit Addressability
of Ports for PIC18F458/4580
31
BSF (bit set fileReg)
❑ To set HIGH a single bit of a given fileReg, we use the syntax
❑ Mostly they are used for I/O ports in embedded systems. For
example:
"BSF PORTB, 5" sets HIGH bit 5 of Port B.
32
Example 4-2
❑An LED is connected to each pin of Port D. Write a program to turn on each
LED from pin D0 to pin D7. Call a delay module before turning on the next
LED.
Solution:
33
BCF (bit clear fileReg)
❑ To CLEAR a single bit of a given fileReg, we use the
syntax
"BCF fileReg, bit_number".
❑ I/O ports, we must activate the appropriate bit in the TRISx
register if we want the pin to reflect the changes.
34
Example 4-3
❑Write the following programs:
(a)Create a square wave of 50% duty cycle on bit 0 of Port C.
(b) Create a square wave of 66% duty cycle on bit 3 of Port C.
𝑻𝑯
𝑫𝒖𝒕𝒚 𝒄𝒚𝒍𝒆 = × 𝟏𝟎𝟎
𝑻 𝒕𝒐𝒕𝒂𝒍
35
COUNT …….
36
BTG (bit toggle fileReg)
❑To toggle a single bit of a given fileReg, we use the
syntax
37
Checking an input pin
❑ To make decisions based on the status of a given bit in the file
register, we use the instructions
❑ This instruction tests the bit and skips the next instruction if it
is HIGH. Example 4-4 shows how it is used (see on next slide).
❑ This instruction tests the bit and skips the instruction right
below it if the bit is LOW. Example 4-5 shows how it is used.
39
Example 4-4
❑Write a program to perform the following:
(a) Keep monitoring the RB2 bit until it becomes HIGH.
(b) When RB2 becomes HIGH, write value 45H to Port C, and also send a
HIGH-to LOW pulse to RD3
TRIS D
❑ Instruction "BTFSS PORTE, 2" stays in the loop as long as RB2 is LOW.
❑When RB2 becomes HIGH, it skips the branch instruction to get out of the
loop, and writes the value 45H to Port C. It also sends a HIGH-to-LOW pulse
to RD3. 40
Example 4-5
❑Assume that bit RB3 is an input and represents the condition of a door alarm.
If it goes LOW, it means that the door is open. Monitor the bit continuously.
Whenever it goes LOW, send a HIGH-to-LOW pulse to port RC5 to turn on a
buzzer.
Solution:
42
43
Example 4-7:
❑ A switch is connected to pin RB2. Write a program to check the status of SW
and perform the following:
❑ (a) If SW = 0, send letter 'N' to PORTD.
❑ (b) If SW = 1, send letter 'Y' to PORTD.
Use the BTFSC instruction to check the SW status. This is another version of
Example 4-6 using the BTFSC instruction instead of BTFSS
44
45
Reading a single bit
❑ We can also use the bit test instructions to read the status of a single bit and
send it to another bit or save it. This is shown in Examples 4-8 and 4-9
❑ Example 4-8: A switch is connected to pin RBO and an LED to pin RB7.
Write a program to get the status of SW and send it to the LED.
46