View
144
Download
9
Category
Tags:
Preview:
Citation preview
PIC Microcontroller and E b dd d S tEmbedded Systems
Muhammad Ali Mazidi, Rolin McKinlay and Danny Causey
Eng. Husam AlzaqE g m qThe Islamic Uni. Of Gaza
The PIC uCs 4-1
Chapter 4:Chapter 4:PIC I/O Port /O ort Programming
I/O Port Programming in P
PIC Microcontroller d E b dd d S
PIC18I/O Bit Manipulation
and Embedded SystemsMuhammad Ali Mazidi, Rolin McKinlay and D C s F b
pProgramming
Danny Causey, February 2007.
The PIC uCs 4-2
ObjectiveObjective
ll h f h P 1List all the ports of the PIC18Describe the dual role of PIC18 pinspCode Assembly to use ports for input or outputoutputCode PIC instructions for I/O handling Code I/O bit manipulation Programs for Code I/O bit-manipulation Programs for PICE l i h bi dd bili f PIC Explain the bit addressability of PIC ports
The PIC uCs 4-3
I/O Port Programming in PIC18I/O Port Programming in PIC18
P 1 h PIC18 has many ports Depending on the family memberDepending on the number of pins on the chipEach port can be configured as input or output.
• Bidirectional portEach port has some other functions
• Such as timer , ADC, interrupts and serial communication
Some ports have 8 bits while others have notSome ports have 8 bits, while others have not
The PIC uCs 4-4
Figure 4-1. PICF458 Pin Diagram
The PIC uCs
Pins Add 18-pin 28-pin 40-pin 64-pin 80-pinp p p p pChip PIC18F1220 PIC18F2220 PIC18F458 PIC18F6525 PIC18F8525PORT A F80H X X X X XPORT B F81H X X X X XPORT C F82H X X X XPORT D F83H X X XPORT D F83H X X XPORT E F84H X X XPORT F F85H X XPORT F F85H X XPORT G F86H X XPORT H X XPORT J X XPORT K XPORT L X
The PIC uCs 4-6
I/O SFR I/O SFR
E h h h f Each port has three registers for its operation:
TRIS register (Data Direction register)• If the corresponding bit is 0 Output
If h di bi i 1 I• If the corresponding bit is 1 InputPORT register (reads the levels on the pins of the device)the device)LAT register (output latch)
Th D t L t h (LAT) ist is s f l The Data Latch (LAT) register is useful for read-modify-write operations on the
l th t th I/O pins d i inThe PIC uCs
value that the I/O pins are driving.4-7
I/O SFR Pins AddressPORT A F80HI/O SFR PORT A F80HPORT B F81HPORT C F82H
P 1 F4 h PORT C F82HPORT D F83HPORT E F84H
PIC18F458 has 5 ports
LATA F89HLATB F8AHLATC F8BH
Upon reset, all ports are
LATC F8BHLATD F8CHLATE F8DH
pconfigured as inputTRISx register LATE F8DH
TRISA F92HTRISB F93H
TRISx register has 0FFH
TRISC F94HTRISD F95H
The PIC uCs
TRISE F96H4-8
Figure 4-2. CMOS States for P and N Transistors
The PIC uCs
Figure 4-3. Outputting (Writing) 0 to a Pin in the PIC18
The PIC uCs
Figure 4-4. Outputting (Writing) 1 to a Pin in the PIC18
The PIC uCs
Figure 4-5. Inputting (Reading) 0 from a Pin in the PIC18
The PIC uCs
Figure 4-6. Inputting (Reading) 1 from a Pin in the PIC18
The PIC uCs
Port APort A
P b d b d l PORTA is a 7-bit wide, bidirectional port. Sometimes A6 is not available. why?
The corresponding Data Direction register is TRISA. Setting a TRISA bit (= 1) will make the corresponding PORTA pin an input corresponding PORTA pin an input Clearing a TRISA bit (= 0) will make the corresponding PORTA pin an output corresponding PORTA pin an output On a Power-on Reset, these pins are configured as inputs and read as ‘0’
The PIC uCs
configured as inputs and read as 0 .4-14
Example 1Example 1
MOVLW B’00000000’MOVLW B’00000000’MOVWF TRISA
B CK MOVLW 0 55
BACK MOVLW 0x55MOVWF PORTA
BACK MOVLW 0x55MOVWF PORTACALL DELAY
CALL DELAYMOVLW 0xAA
CALL DELAYMOVLW 0xAAMOVWF PORTA
MOVWF PORTACALL DELAY
MOVWF PORTACALL DELAYGOTO BACK
GOTO BACK
GOTO BACK
The PIC uCs 4-15
Example 2Example 2
MYREG EQU 0 20MYREG EQU 0x20MOVLW B’11111111’MOVWF TRISAMOVWF TRISAMOVF PORTA, w
F EG MOVWF MYREG
The PIC uCs 4-16
PORT B, PORT C, PORT D and PORT E
P B PORTB is 8 pinsPORTC is 8 pinspPORTD is 8 pinsPORTE is 3 pinsPORTE is 3 pins
The PIC uCs 4-17
Read followed by write operationRead followed by write operation
B f l F BBe carefulDon’t have a two I/O
h
CLRF TRISBSETF TRISC
operations one right after the other.
D t D d
L4 MOVF PORTC,WMOVWF PORTBData Dependency
A NOP is needed to k th t d t i
MOVWF PORTBBRA L4
make that data is written into WREG before it read for before it read for outputting to PortB.
The PIC uCs 4-18
Figure 4-7. Pipeline for Read Followed by Write I/O
The PIC uCs
Two SolutionsTwo Solutions
Solution1 Solution2Solution1 Solution2
CLRF TRISBSETF TRISC
CLRF TRISBSETF TRISCSETF TRISC
L4 MOVF PORTC,WSETF TRISCL4 MOVFF PORTC, PORTB
NOPMOVWF PORTB
BRA L4
BRA L4 MOVFF is 4-byte instruction
The PIC uCsThe PIC uCs
instruction.4-20
Example 4-1 list P=PIC18F458Example 4-1
W it t t
#include P18F458.INCR1 equ 0x07Write a test program
for the PIC18 chipt t l ll th
R1 equ 0x07R2 equ 0x08ORG 0to toggle all the
bits of PORTB,PORTC d PORTD
ORG 0CLRF TRISB
PORTC and PORTDevery 0.25 of a
d (
CLRF TRISCCLRF TRISDsecond. (suppose
that there is a 4MH )
CLRF TRISDMOVLW 0x55MOVWF PORTBMHz) MOVWF PORTBMOVWF PORTC
The PIC uCsMOVWF PORTD
4-21
SolutionQDELAY
MOVLW D'200'Solution MOVLW D 200MOVWF R1
D1 MOVLW D'250'L3 COMF PORTB,F
COMF PORTC F
D1 MOVLW D'250'MOVWF R2
COMF PORTC,FCOMF PORTD,F
D2 NOPNOP
CALL QDELAYBRA L3
NOPDECF R2, FBNZ D2BNZ D2DECF R1, FBNZ D1RETURN
The PIC uCs
RETURNEND 4-22
I/O Bit Manipulation ProgrammingI/O Bit Manipulation ProgrammingI/O ports and bit-addressabilityp yMonitoring a single bitR di i l biReading a single bit
Section 4-2The PIC uCs 3-23
I/O ports and bit-addressability
PORT A PORT B PORT C PORT D PORT E PORT BitRA0 RB0 RC0 RD0 RE0 D0RA0 RB0 RC0 RD0 RE0 D0RA1 RB1 RC1 RD1 RE1 D1RA2 RB2 RC2 RD2 RE2 D2RA2 RB2 RC2 RD2 RE2 D2RA3 RB3 RC3 RD3 D3RA4 RB4 RC4 RD4 D4RA5 RB5 RC5 RD5 D5
RB6 RC6 RD6 D6RB7 RC7 RD7 D7
The PIC uCs 4-24
Bit Oriented Instruction for PIC18
Instruction Function
BSF fileReg bit Bit Set File RegisterBSF fileReg, bit Bit Set File RegisterBCF fileReg, bit Bit Clear File Registerg, gBTG fileReg, bit Bit Toggl File Register
i il i ki ifBTFSC fileReg, bit
Bit Test File Register, skip if clear
BTFSS fileReg, bitBit Test File Register, skip if set
The PIC uCs
BTFSS fileReg, bit set4-25
Example 4-2 CLRFTRISDExample 4-2
ED d
BSF PORTD,0CALLDELAY
A LED is connected to each pin of port D
BSF PORTD,1CALLDELAYD. Write a program
to turn on each LED f D0
CALLDELAYBSF PORTD,2
LED from pin D0 to D4.
CALLDELAYBSF PORTD,3CALLDELAYBSF PORTD 4BSF PORTD,4CALLDELAY
The PIC uCs 4-26
Example 4-3 Solution 1Example 4-3
h f ll BCF TRISC 0 Write the following programs
BCF TRISC,0 HERE
BSF PORTC 0A. Create a square wave of 50% duty
BSF PORTC,0CALL DELAYy
cycle on bit 0 of C BCF PORTC,0CALL DELAYCALL DELAYBRA HERE
How many byte are d
The PIC uCs
used? 4-27
Example 4-3 Solution 2
h f ll Write the following programs BCF TRISC,0
BACK A. Create a square wave of 50% duty
BACK BTF PORTC,0y
cycle on bit 0 of C CALL DELAYBRA BACKBRA BACK
H m b t How many byte are used?
The PIC uCs 4-28
Example 4-4 BSF TRISB,2Example 4-4
F ,CLRF TRISCBCF PORTD 3Write a program to
perform the following:BCF PORTD,3MOVLW 0x45
a) Keep monitoring the RB2 bit until it
AGAINBTFSS PORTB,2
becomes HIGH (1)b) When RB2 becomes
,BRA AGAINMOVWF PORTCb) When RB2 becomes
HIGH, write value 45H to portC and
MOVWF PORTCBSF PORTD,345H to portC and
send a HIGH to LOW plus to RD3
CALL DELAYBCF PORTD,3
The PIC uCs
plus to D3 ,
4-29
Example 4-5Example 4-5
B B d Bit RB3 is an input and represents the
d f d condition of a door alarm.Whenever it goes LOW, send a HIGH-to-LOW pulse to RC5 to turn on a buzzer
The PIC uCs 4-30
SolutionSolution
B F BBSF TRISB,3BCF TRISC,5
HEREBTFSC PORTB 3BTFSC PORTB,3BRA HEREB F PORTC 5BSF PORTC,5BCF PORTC,5CALL DELAYBRA HERE
The PIC uCs
BRA HERE4-31
Reading a single bitExamole4-8
h BSF TRISB,0
A switch is connected to pin RB0 d LED
BCF TRISB,7 AGAIN
RB0 and a LED to pin RB7. Write a
d
G NBTFSS PORTB,0 GOTO OVERprogram to read
the status of SW d d i h
GOTO OVERBSF PORTB,7
and send it to the LED
GOTO AGAINOVERBCF PORTB,7 GOTO AGAIN
The PIC uCs
GOTO AGAIN4-32
Reading input pins VS. LATx port
h b l d ’ There are two possibilites to read port’s value
Through reading the status of the input pinThrough reading the internal latch of the LAT register.
• Some instructions do thatThe action is • The action is
1. The instruction read the latch instead of the pin2. Execute the instruction. Execute the nstruct on3. Write back the result to the Latch4. The data on the pins are changed only if the TRISx
bi l d
The PIC uCs
bits are cleared.
٣٣- ٤
FunctionInstruction Function InstructionAdd WREG from ffileReg,dADDWFBit S t fil Rfil R bitBSF Bit Set fileRegfileReg,bitBSFBit Clear fileRegfileReg,bitBCFComplement ffileReg,dCOMFIncrement FfileReg,dINCF gSubtruct WREG from ffileReg,dSUBWFExclusive-OR WREG with ffileReg,dXORWF
The PIC uCs
Exclusive OR WREG with ffileReg,dXORWF
٣٤- ٤
Figure 4-8. LATx Register Role in Reading a Port or Latch
The PIC uCs
Chapter 4: SummaryChapter 4: SummaryWe focused on the We focused on the I/O Ports of the PIC. These ports used for These ports used for input or output. ProgrammingProgrammingWe discussed Bit m nip l ti n
Next:Arithmetic, logic manipulation
instructions
, gInstruction and programs
The PIC uCs 4-36
Recommended