LPC Demo Board Lessons
3.2.12
Lesson 12: Look-up Table (ROM Array)
Lesson 8 introduced function calls. Lesson 12 shows how function calls and calculated
modification of the Program Counter may be used to implement a Look-up Table (see
Example 3-11).
It is sometimes useful to implement a table to convert from one value to another.
Expressed in a high-level language it might look like this:
y = function(x) ;
That is for every value of x , it returns the corresponding y value.
Look-up tables are a fast way to convert an input to meaningful data because the
transfer function is pre-calculated and “looked up” rather than calculated on the fly.
PICmicro MCUs implement these by directly modifying the Program Counter. For
example, a function that converts hexadecimal numbers to the ASCII equivalent. We
can strip out the individual nibble and call the Look-up Table. The index advances the
program counter to the appropriate RETLW instruction to load Wreg with the constant
and returns to the calling program.
EXAMPLE 3-11:
LOOK-UP TABLE
;Enter with index in Wreg
Look-upTable
ADDWF
RETLW
RETLW
...
RETLW
PCL,f
'0'
'1'
'F'
;jump to
;index 0
;index 1
;index 15
Calling the Look-up Table works most of the time. However, if the table falls across a
256 byte page boundary, or if somehow the Look-up Table is called with an out of
bounds index value, it will jump to a location out of the table.
Good programming practices dictate a few additional instructions. First, since the table
is only sixteen entries, make sure a number no larger than 16 is passed in. The simplest
way to do this is to logically AND the contents of Wreg before modifying PCL: ANDLW
0x0F . More complex error recovery schemes may be appropriate, depending on the
application.
In addition, there are some nuances to be aware of should the table cross a 256 word
boundary. The Program Counter is 13 bits wide, but only the lower 8 bits are
represented in PCL (see Figure 3-12). The remaining 5 bits are stored in PCLATH.
However, an overflow of the lower 8 bits is not automatically carried over into PCLATH.
Instead, be sure to check for and handle that case in our code. See the PCL and
PCLATH Section in the PIC16F685/687/689/690 Data Sheet (DS41262) for more
details of how PCLATH is used.
FIGURE 3-12:
PC LOADING AS DESTINATION OF INSTRUCTION
PCH
PCL
Instruction with
12
8
7
0
PCL as
PC
Destination
5
PCLATH<4:0>
8
ALU Result
PCLATH
? 2005 Microchip Technology Inc.
DS51556A-page 33
相关PDF资料
DM164120-3 BOARD DEMO PICKIT2 28-PIN
DM164120-5 BOARD DEMO PICKIT 2 64/80-PIN
DM164123 KIT MANAGEMENT SYSTEM PICDEM
DM180021 KIT STARTER MPLAB FOR PIC18F MCU
DM183022 BOARD DEMO PIC18FXX22 64/80TQFP
DM183032 BOARD EXPLORER PICDEM PIC18
DM240001 BOARD DEMO PIC24/DSPIC33/PIC32
DM240002 BOARD DEV EXPLORER 16 44-PIN
相关代理商/技术参数
DM164120-1 制造商:Microchip Technology Inc 功能描述:DEVELOPMENT TOOLS - PICKIT 2 LOW PIN COU
DM164120-2 功能描述:开发板和工具包 - PIC / DSPIC PICkit 2 44-Pin Count Demo Brd RoHS:否 制造商:Microchip Technology 产品:Starter Kits 工具用于评估:chipKIT 核心:Uno32 接口类型: 工作电源电压:
DM164120-3 功能描述:开发板和工具包 - PIC / DSPIC PICkit 2 Count Demo Board RoHS:否 制造商:Microchip Technology 产品:Starter Kits 工具用于评估:chipKIT 核心:Uno32 接口类型: 工作电源电压:
DM164120-4 功能描述:开发板和工具包 - PIC / DSPIC PICkit 2 1 Dem Board RoHS:否 制造商:Microchip Technology 产品:Starter Kits 工具用于评估:chipKIT 核心:Uno32 接口类型: 工作电源电压:
DM164120-4 制造商:Microchip Technology Inc 功能描述:PICkit 2 18-Pin Demo Board
DM164120-4/16F1827/PG164130 制造商:Microchip Technology Inc 功能描述:PICKIT PIC16F648A DEMO BOARD 制造商:Microchip Technology Inc 功能描述:KIT STARTER 18PIN F1 ENHANCED CORE 制造商:Microchip Technology Inc 功能描述:PICKIT, PIC16F648A, DEMO BOARD 制造商:Microchip Technology Inc 功能描述:PICKIT, PIC16F648A, 18 PIN, DEMO BOARD; Silicon Manufacturer:Microchip; Core Architecture:PIC; Core Sub-Architecture:PIC16; Silicon Core Number:PIC16F; Silicon Family Name:PIC16F18xx; Kit Contents:Demo Board, 2x Bare PCB Boards
DM164120-5 功能描述:开发板和工具包 - PIC / DSPIC PICkit 2 64 demoBoard RoHS:否 制造商:Microchip Technology 产品:Starter Kits 工具用于评估:chipKIT 核心:Uno32 接口类型: 工作电源电压:
DM164123 功能描述:开发板和工具包 - PIC / DSPIC PICDEM Sys Mgmt Board RoHS:否 制造商:Microchip Technology 产品:Starter Kits 工具用于评估:chipKIT 核心:Uno32 接口类型: 工作电源电压: