Title: Assembler programming Configuring IO, Lookup tables and Interrupts
1Assembler programming(Configuring I/O, Lookup
tables and Interrupts)Â Â
EE1A2Â
- S. I. Woolley
- Electronic, Electrical and Computer Engineering
2Configuring I/O TRIS
- TRIS is the (obsolete) instruction for
configuring inputs and outputs. - The PIC16F84 has 13 lines that can be used as
inputs or outputs. - The lines are grouped into 8 PORTB lines and 5
PORTA lines. - On the chip they are labelled RB7..RB0 and
RA4..RA0. - The programmer needs to tell the processor what
inputs or outputs are needed. - The simple way to do this uses the TRIS
instruction - (we know TRIS is not recommended for upward
compatibility and we will look at the preferred
register bank switching method later).
3Using TRIS
- METHOD
- Put a pattern of bits in the working register.
Os for outputs and 1s for inputs. Now use the
TRIS instruction with PORTA or PORTB (whichever
you want to configure).
4Using TRIS - examples
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
- Setting PORTB lines as all outputs
- MOVLW 0x00
- TRIS PORTB
- Setting PORTA lines as all outputs
- MOVLW 0x00
- TRIS PORTA
5Using TRIS - examples
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
- Setting PORTB lines as all inputs
- MOVLW 0xFF
- TRIS PORTB
- Setting PORTA lines as all inputs
- MOVLW 0xFF
- TRIS PORTA
- Note - it doesnt matter that we have configured
3 lines that dont exist. But if you wanted you
could use the value 1F (ie. 00011111)
6Using TRIS - examples
0
0
0
0
0
0
0
1
0
0
0
1
0
0
0
1
- Setting PORTB line RB0 as an input and all others
as as outputs - MOVLW 0x01
- TRIS PORTB
- Setting PORTA lines RA0 and RA2 as inputs and all
others as outputs - MOVLW 0x05
- TRIS PORTA
7TRIS - what else?
- In our examples here we havent cleared the
values on the ports. - (e.g., CLRF PORTB)
- Its usually good practice to clear them, i.e.,
to make sure you know they start at zero or some
other specified value. - We advise against using RA4.
- This is a special line. For example, it can be
used for an external timer. The data sheets go
into the technical details but for our
introductory course we simply avoid using it.
8Using Inputs and Outputs?
- Examples of inputs and outputs?
- Buttons (or switches) are INPUTS to the processor
- Lights or LEDs are OUTPUTS
- LCD displays can be connected as inputs and
outputs. - The processor can receive signals (INPUTS), e.g.,
ready signals or information about what is
currently on display. - The processor can send signals (OUPTUTS) to clear
the display or to display a new character.
www.talkingelectronics.com
9Using Inputs and Outputs?
- If we configure RB0 as an input and connect a
button to it then pressing the button will change
the value of bit 0 in the PORTB register. - If we configure RA2 as an input and connect a
button to it then pressing the button will change
the value of bit 2 in the PORTA register. - This is REGISTER-MAPPED I/O
- If the button is active high, pressing it will
make the bit in the register a 1. - If it is active low, pressing it will make the
bit a 0.
Pictures are random examples of PIC led light
projects from the Internet. The top picture
shows a breadboard circuit, the middle is on
veroboard and the bottom is on PCB (printed
circuit board).
10Lookup/Data Tables
- Lookup tables or data tables can be very useful
for data conversion and reference. - Difficult computations can be avoided by having a
ready-made table of conversion data. - To implement data tables, the PIC programmer has
to enter data values as dummy instructions in
program memory. This is done using a subroutine
call which modifies the program counter with an
appropriate offset specifying the location of a
RETLW command. In this way the required data
value is returned by assigning a literal to w on
return. - Â
-
117 Segment Display
- Example We can use a lookup output values for a
7 segment display. - The 8 bits of a byte can be used to represent the
on or off status of the individual segments (and
decimal point) of a seven-segment display. - Segments are typically labelled as a..g as shown
with a decimal point as h. - Our program segment example uses a data table to
lookup the bit configuration needed to illuminate
the right segments to display values between 0-9.
12- -------GENERAL EQUATES--------
- W EQU 0
- F EQU 1
- ------- I/O EQUATES --------
- PORTB EQU 0X06
- -------REGISTER EQUATES ------
- PC EQU 0X02
- --------MAIN PROGRAMME -------
- ORG 0X00
- START CLRF PORTB
- MOVLW 0X00
- TRIS PORTB
- CHAR MOVLW 0X03 A TEST VALUE
- CALL SEG_TABLE PUT OFFSET IN W
- MOVWF PORTB OUTPUT RESULT
- LOOP GOTO LOOP FINISH WITH LOOP
-
- SEG_TABLE ADDWF PC,F ADD OFFSET TO PC
- RETLW 0X3F 0 SEVEN SEGMENT
http//www.talkingelectronics.com/Projects/PIC_LAB
-1/PicLab1_experiments-P1b.html
13Interrupts
- Interrupts are used to change the normal flow of
a program so that it can perform another
(specified) function. - Interrupts allow external events to change
(interrupt) the normal flow of the software,
executing code specifically designed for a
response to the change. - Processors without in-built interrupt support
require programs which regularly inspect selected
input lines. This is called polling. Polling
is very expensive in terms of processing. - Interrupts enable processors to automatically
respond to specified events and concentrate
processing power on executing a main program.
14Interrupts
- When an interrupt occurs the instruction
currently being executed is completed. The
program counter then jumps to address 0x04 in
program memory and executes the instruction
stored there. - Interrupts can be enabled or disabled (masked)
individually or globally (all disabled regardless
of source.) This is done via the interrupt
control register (INTCON.) - Using interrupts
- ORG 0X00
- GOTO START
-
- ORG 0X04
- GOTO INT_SERVICE
- Â
- INT_SERVICE INT. SERVICE ROUTINE HERE
-
- RETFIE RETURN FROM INTERRUPT
-
- START MAIN PROGRAM GOES HERE
-
- END
15Interrupts
- The PIC16F84 supports 4 in-built interrupt
sources - RB0 interrupt Edge-triggered interrupt (via
RB0/INT pin) - Port B change (bits 7-4) Port B logic level
change on bits 7,6,5,4 - TMR0 overflow Timer/counter overflow interrupt
- EEPROM write complete
Â
Â
Â
Â
Â
- The RB0/INT is an edge-triggered interrupt. It
can be enabled and disabled using the INTE flag
in the INTCON register. - PORTB pins (RB7 to RB4 inclusive) can be used as
external interrupts. When configured as inputs,
these pins can trigger an interrupt on change. - Only 5 PORTB pins support interrupts. There are
no interrupts on PORTA.
16Interrupts
- Interrupts are controlled by the INTCON register
Â
Â
Â
Â
Â
Individual Interrupt Enable bits
Global Interrupt Enable (GIE)
Individual Interrupt Flags
17Interrupt Example
- How would you configure (enable) an interrupt on
line RB7? - Does RB7 need to be an input or an output?
- Write a program template and include an interrupt
service routine that clears the correct flag. - Make appropriate equate statements.
- What would happen if we didnt clear the flag?
Â
Â
Â
___ EQU ___ ___ EQU ___ ___ EQU ___ ___ EQU _
__ ORG 0x00 GOTO START ORG 0x04 GOTO
INT_SER INT_SER RETFIE START .
-configure line RB7 /
-enable interrupt /
Â
Â
18Interrupt Example
- How would you configure (enable) an interrupt on
line RB7? - Does RB7 need to be an input or an output?
- Write a program template and include an interrupt
service routine that clears the correct flag. - Make appropriate equate statements.
- What would happen if we didnt clear the flag?
Â
Â
Â
GIE EQU 0x07 RBIE EQU 0x03 RBIF EQU 0x00 INTCO
N EQU 0x0B ORG 0x00 GOTO START ORG
0x04 GOTO INT_SER INT_SER BCF INTCON,
RBIF RETFIE START . MOVLW 0x08 -configure
line RB7 TRIS PORTB / BSF INTCON, GIE
-enable interrupt BSF INTCON, RBIE/
/
Â
Â
19Interrupts Saving Context
- We usually need to save context (save important
current values) inside an interrupt service
routine. - These usually include the working register and
the STATUS register. - The values are backed up and restored inside the
interrupt service routine. - The values are backed up at the start and
restored at the end.
20Interrupts Example from notes
W EQU 0 F EQU 1 RBIF EQU 0 RBIE EQU 3 GIE EQU 7
----- I/O EQUATES --------------------------------
---------------- PORTA EQU 0X05 PORTB EQU 0X06
----- REGISTER EQUATES ------------------------
----------------------- INTCON EQU 0X0B MCOUNT E
QU 0X0C NCOUNT EQU 0X0D LED_VAL EQU 0X0E TEM
P_W EQU 0X0F ---------------------------------
-------------------------------------- ORG 0X00
GOTO START ORG 0X04 GOTO INT_SER
---------------- INTERRUPT SERVICE SUBROUTINE
------------------------ INT_SER MOVWF TEMP_W BAC
KUP CONTENTS OF W MOVLW 0X00 MOVWF LED_VAL R
ESET LED_VALUE MOVWF PORTB RESET
LEDS BCF INTCON,RBIF CLEAR INTERRUPT FLAG IN
INTCON MOVF TEMP_W,W RESTORE W RETFIE RETURN
FROM INTERRUPT -----------------------
SUBROUTINE 'DELAY' ----------------------------
21Interrupts
- To use interrupts we need to set (enable) GIE.
- GIE is the Global Interrupt Enable and it lives
at bit number 7 in the interrupt control
register. - But enabling GIE doesnt enable any of the four
types of interrupts. - We still have to set each type of interrupt we
want to use, by setting the corresponding enable
bit. - GIE is useful if we want to turn OFF interrupts.
If we have all four types of interrupts enabled,
then clearing GIE will stop all the different
types of interrupts from having an effect.
Â
Â
Â
Â
Â
22Interrupts
- EXAMPLE To use the INTE interrupt.
- INTE is the edge-triggered interrupt available on
RB0. - We need to connect our interrupt source (a button
perhaps) to RB0. - We need to configure RB0 as an INPUT.
- We need to write an interrupt service routine.
This is a special subroutine that will run
whenever the interrupt happens. - We need to set GIE and INTE.
- Now our interrupt service routine will run when
the button is pressed, because this will set the
interrupt flag. - We must make sure our interrupt service routine
clears the interrupt flag (INTF). - There needs to be a PORTB read or write prior to
the clearing of RBIF.
Â
Â
Â
GIE EQU 7 INTE EQU 4 INTF EQU 1 INTCON EQU 0x
0B ORG 0x00 GOTO START ORG 0x04 GOTO
INT_SER INT_SER BCF INTCON,INTF RETFIE S
TART . MOVLW 0x01 -RB0 is an
I/P TRIS PORTB / BSF INTCON, GIE
-Enable interrupt BSF INTCON, INTE/
Â
Â
Thanks to Alex Smith for this useful note.
23A Practical Lab Checklist
- If something doesnt work kit may be one of the
following more common problems - If the programmer loses connection it fails to
work on reattempts even if the connector is
pushed in. The PIC Programmer software needs to
be restarted. - Not selecting the PIC16F84 in MPLAB and/or the
PIC Programmer. - Not selecting the correct fuses (XT selected all
others NOT selected - WATCHDOG timer must be
cleared) - Do not use pin RA4. It will not work.
- The project board buttons are active LOW - i.e. a
0 when pressed and a 1 otherwise. - Physically connecting the wrong lines, e.g.,
physically connecting a button to RA0 but the
software senses line RA1. - Putting the PIC in the wrong way around (it will
not work again - a replacement will be needed). - Putting an END directive before the end of the
program. (The assembler will not look at code
below it). - Using GOTO instead of CALL
- Typing o instead of 0 or I instead of 1 (i.e.
letters instead of numbers) - Not configuring the right port or configuring an
output as an input or vice versa.
24Test.asm
- WRITTEN BY SIW
- DATE 01/01/2004
- FILE SAVED AS TEST.ASM
- DEVICE 16F84
- OSCILLATOR XT (4MHZ)
- WATCHDOG DISABLED
- FUNCTION OUTPUTS THE VALUE 0XF1 TO
8 LEDS CONNECTED TO PORTB - ----------------------- EQUATES
------------------------------------ - PORTB EQU 0X06 ASSIGN THE PORTB
REGISTER TO THE LABEL 'PORTB' - ----------------------- MAIN PROGRAM
------------------------------------ - START ORG 0X00 'ORG' SPECIFIES THE
MEMORY LOCATION OF THE PROGRAM - MOVLW 0X00 MOVE THE VALUE 00, I.E.,
ALL 0'S TO W - TRIS PORTB CONFIGURE PORTB WITH THE
VALUE IN W (THE - WORKING REGISTER)
1INPUT AND 0OUTPUT. - SO 00 (ALL 0'S) MAKES
ALL PORTB LINES OUTPUTS. - CLRF PORTB CLEAR THE PORTB REGISTER
- MOVLW 0XF1 MOVE THE HEX VALUE F1 TO
THE WORKING REGISTER - MOVWF PORTB MOVE THE VALUE OF W TO
THE OUTPUT (PORTB) - LOOP GOTO LOOP
25LAB1 Create a button input and increment output
if pressed
- WRITTEN BY SIW
- DATE 01/01/2004
- FILE SAVED AS TEST.ASM
- DEVICE 16F84
- OSCILLATOR XT (4MHZ)
- WATCHDOG DISABLED
- FUNCTION OUTPUTS THE VALUE 0XF1 TO
8 LEDS CONNECTED TO PORTB - ----------------------- EQUATES
------------------------------------ - PORTB EQU 0X06 ASSIGN THE PORTB
REGISTER TO THE LABEL 'PORTB' - ----------------------- MAIN PROGRAM
------------------------------------ - START ORG 0X00 'ORG' SPECIFIES THE
MEMORY LOCATION OF THE PROGRAM - MOVLW 0X00 MOVE THE VALUE 00, I.E.,
ALL 0'S TO W - TRIS PORTB CONFIGURE PORTB WITH THE
VALUE IN W (THE - WORKING REGISTER)
1INPUT AND 0OUTPUT. - SO 00 (ALL 0'S) MAKES
ALL PORTB LINES OUTPUTS. - CLRF PORTB CLEAR THE PORTB REGISTER
- MOVLW 0XF1 MOVE THE HEX VALUE F1 TO
THE WORKING REGISTER - MOVWF PORTB MOVE THE VALUE OF W TO
THE OUTPUT (PORTB) - LOOP GOTO LOOP
26Code Solution
- WRITTEN BYÂ Â Â Â Â Â Â Â Â Â Â SIW DATEÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â
  11/02/2009 FILE SAVED AS        TEST.ASM
DEVICEÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â PIC16F84
OSCILLATORÂ Â Â Â Â Â Â Â Â Â Â XT (4MHZ)
WATCHDOGÂ Â Â Â Â Â Â Â Â Â Â Â Â DISABLED
FUNCTIONÂ Â Â Â Â Â Â Â Â Â Â Â Â OUTPUTS THE VALUE 0XF1 TO
LEDS CONNECTED TO PORTBÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
AND INCREMENTS TO 0XF2 ON BUTTON (RA0) PRESS
-----------------------Â Â EQUATESÂ Â Â
------------------------------------FÂ Â EQUÂ Â
0X01PORTAÂ EQUÂ Â 0X05Â PORTBÂ Â EQUÂ Â Â Â
0X06Â Â Â ----------------------- MAIN PROGRAM
------------------------------------Â Â Â Â Â Â Â
ORGÂ Â Â Â 0X00Â Â Â 'ORG' SPECIFIES THE MEMORY
LOCATION OF THE PROGRAMÂ Â Â Â Â Â Â MOVLWÂ Â 0X00Â Â Â
- CONFIGURE PORTB AS ALL OUTPUTSÂ Â Â Â Â Â Â TRISÂ Â Â
PORTBÂ Â /Â Â Â Â Â Â Â CLRFÂ Â Â PORTBÂ Â
- CONFIGURE PORTA AS ALL INPUTSÂ Â
 /       MOVLW  0XF1   - OUTPUT VALUE
0XF1 TO PORTB LEDSÂ Â Â Â Â Â Â MOVWFÂ Â PORTBÂ Â / - BUTTONÂ CHECK FOR BUTTON PRESS
(ACTIVE LOW) CONTINUE
CHECKING IF NOT PRESSEDÂ Â
INCREMENT PORTB LEDS TO F1 IF PRESSEDLOOPÂ Â Â
GOTOÂ Â Â LOOPÂ Â Â Â Â Â Â END
27Exercises
- Write the instructions needed to configure line
RB0 as an input and also as an interrupt. Write
supporting equate statements to make your code
more readable. - Write the an interrupt service routine for the
above interrupt that will back-up and restore the
working register and also clear the interrupt
flag. - Study the look-up table example in the tutorial
example sheet.