Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
17 views

Microcontroller Lecture # 6_edited

Whole little micro abotu

Uploaded by

nqm9frn4vq
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Microcontroller Lecture # 6_edited

Whole little micro abotu

Uploaded by

nqm9frn4vq
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

BIOEN 442: Microprocessor

Chapter 4

PIC I/O PORT PROGRAMMING

Week 6
Biomedical Engineering Department
College of Engineering
Imam Abdulrahman Bin Faisal University, Saudi Arabia 1

Eng. Kamran Hameed


Today's topic

0- Course 1- Pic 2- PIC Architecture PIC


Overview /Introduction Microcontrollers:history Programming using
to Computing and features Assembly Language

5-Arithmetic, Logic
3-Branch, Call and Time 4-PIC I/O Port
Instructions, and
Delay Loop Programming
Programs

17- Motor Control:


12-LCD and Keypad 13- ADC, DAC and
Relay, PWM, DC and
Interfacing sensor interfacing
Stepper Motors

2
Today’s lecture
➢List all the ports of the PIC18

➢Describe the dual role of PIC18 pins and Ports A, B, C, and D

➢Code Assembly language to use the ports for input or output

➢I/O bit-manipulation programs for the PIC

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.

❑ Notice that 0 stands for out and 1 for in.

❑ This is easy to remember because O and 0 look a like


the same way that I looks like 1.

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:

❑Another, more efficient, version of the program is as follows:

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

❑ Examine the following rewrite of an earlier code fragment in


which data was read from PORTC and sent to PORTB:

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 B occupies a total of 8 pins (RBO RB7).

❑ Port C occupies a total of 8 pins (RCO-RC7).

❑ Port D occupies a total of 8 pins (RDO-RD7).

❑ To use the pins of Port A, B, C and D as both input and output


ports, each bit must be connected externally to the pin by enabling
the bits of the TRISA,B,C and D registers.
16
❑ For example, the following code will continuously send out to
Port A, B, C, D the alternating values of 55H and AAH:

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.

❑ In the following up coming codes, Port A, B, C and D is


configured first as an input port by writing all 1s to register TRISA
B, C and D, and then data is received from Port A, B, C and D
and saved in some RAM location of the fileReg:

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.

❑ We pay special attention to I/O bit


manipulation because it is a powerful and widely
used feature of the PIC family.

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.

❑ A powerful feature of PIC I/O ports is their capability


to access individual bits of the port without altering the
rest of the bits in that port.

❑ For all PIC ports, we can access either all 8 bits or


any single bit without altering the rest.

❑ Table 4-8 lists the single-bit instructions for the PIC18.


(see on next slide)
29
Table 4-8: Single-Bit (Bit-Oriented)
Instructions for PIC18

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

"BSF fileReg, bit_num"


❑ Where fileReg can be any location in the file register and
bit_num is the desired bit number from 0 to 7.

❑ The bit-oriented instructions can be used for manipulation of


bits DO-D7 of any file register,

❑ 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.

❑ For example, the following code toggles pin RB2 continuously:

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

"BTG fileReg, bit number".

37
Checking an input pin
❑ To make decisions based on the status of a given bit in the file
register, we use the instructions

BTFSC (bit test fileReg skip if clear)


BTFSS (bit test fileReg skip if set).
❑ These single-bit instructions are widely used for I/O operations.

❑ They allow you to monitor a single pin and make a decision


depending on whether it is 0 or 1.

❑ Again it must be noted that the instructions BTFSC and BTFSS


can be used for any bits of the file register, including the I/O ports
A, B, C, D, and so on. 38
BTFSS (bit test fileReg, skip if set)
❑ To monitor the status of a single bit for HIGH, we use the
BTFSS instruction.

❑ 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).

BTFSC (bit test fileReg, skip if clear)


❑ To monitor the status of a single bit for LOW, we use the
BTFSC instruction.

❑ 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:

3/17/2024 Eng.Kamran Hameed 41


Monitoring a single bit
❑ The bit test instructions to monitor the status of a single bit and make a
decision to perform an action.
❑ Example 4-6: 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 the letter 'N' to PORTD.
(b) If SW =1. send the letter 'Y' to PORTD.

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

You might also like