THoughts on how create a look-up table to determine a byte value

Status
Not open for further replies.

Steve311

Member
Hi All;

What would be the best approach to create a look-up table to determine the value of an 8bit byte in a register, ranging from 0 to 255?

Any thoughts?

Thanks!
 
Why would you want to? just read the register, it's going to return the value as 0 to 255 any way.

Oh we have b'11111111 it is 255

It would be easy just to tell you want dec values which would return 0 to 255 any way.
 
I think he's wanting a table that will convert an 8 bit value into digits to be shown on a display, which would take up 1 byte for each digit.
 
Hi folks, first post here.

I use lookup tables for AD conversions when I can't compute results mathmatically. I use it for non-linear functions like temperature sensors. For PICs that have program access to flash memory, there's a syntax that allows you to simply list your table in code. A "table read" instruction is used to to return values from it. There's also a way to access a table in flash memory by manipulating the program counter. If nothing else, you can do something similar using volatile memory. Just write the table to memory on initialization then set up a pointer that references address locations to register values.

Here's the assembly code that does a table read for a battery capacity gauge. This is for a PIC24F using ASM30.

Code:
    mov            #tblpage(Batt_Cap_Data), W0        ;Load W0 with program memory page of data array
    mov            W0, TBLPAG                         ;Copy W0 to table page register (high word of program memory address)
    mov            #tbloffset(Batt_Cap_Data), W0      ;Load W0 with low word program memory address of data array first entry
    mov            ADC1BUF0, W1                       ;Copy ADC result to W1
    mov            W1, BATTCAP_ADC                    ;Copy W1 to BattCap_ADC
    sl             W1, W1                             ;Shift W1 left (multiply by 2), put in W1
    add            W0, W1, W0                         ;Compute the offset here (must be a multiple of 2)
    tblrdl         [W0], W1                           ;Copy value in program memory address TBLPAG:W0 to W1
    mov            W1, BATTCAP                        ;Copy W1 to battery capacity

Here's how I did a temp sensor table for a PIC18F with MPASM. This one writes a table in flash then manipulates the program counter to read from it. Sorry for the lack of comments there. It's an old snippet I hung onto that I never commented.

Code:
Temp_Table
	movff	PCL, PCL_TMP
	movlw	0x16
	addwf	PCL_TMP, f
	clrf	WREG
	addwfc	PCLATH, f
	movf	INDEX1, w
	addwf	PCL_TMP, f
	movf	INDEX2, w
	addwfc	PCLATH, f
	movf	PCL_TMP, w
	movwf	PCL

	dt		d'255', d'255', d'255', d'255', d'255', d'255', d'255', d'255' ;007
	dt		d'255', d'255', d'255', d'255', d'255', d'255', d'255', d'255' ;015
	dt		d'255', d'255', d'255', d'255', d'255', d'255', d'255', d'255' ;023
	dt		d'255', d'255', d'255', d'255', d'255', d'255', d'255', d'255' ;031
	dt		d'255', d'255', d'255', d'255', d'255', d'255', d'255', d'255' ;039
	dt 		d'255', d'255', d'255', d'255', d'255', d'255', d'255', d'255' ;047
	dt		d'255', d'255', d'255', d'255', d'255', d'255', d'255', d'255' ;055
        dt              d'255', d'255', d'255', d'255', d'255', d'255', d'255', d'255' ;063
	dt		d'255', d'255', d'255', d'255', d'255', d'254', d'252', d'250' ;071
	dt		d'248', d'247', d'246', d'245', d'244', d'242', d'241', d'240' ;079
	dt		d'239', d'238', d'237', d'236', d'234', d'233', d'232', d'230' ;087
	dt		d'229', d'228', d'227', d'226', d'225', d'224', d'223', d'222' ;095
	dt		d'220', d'219', d'218', d'217', d'216', d'215', d'214', d'213' ;103
	dt		d'212', d'211', d'210', d'209', d'208', d'207', d'206', d'205' ;111
	dt		d'204', d'203', d'202', d'201', d'200', d'199', d'198', d'198' ;119
	dt		d'197', d'196', d'195', d'194', d'193', d'192', d'191', d'190' ;127
	dt		d'189', d'188', d'187', d'186', d'186', d'185', d'184', d'183' ;135
	dt		d'182', d'181', d'180', d'180', d'179', d'178', d'177', d'176' ;143
 	dt		d'175', d'174', d'173', d'172', d'171', d'170', d'169', d'168' ;151
 	dt		d'168', d'167', d'166', d'166', d'165', d'164', d'163', d'162' ;159
 	dt		d'161', d'160', d'159', d'158', d'157', d'156', d'155', d'154' ;167
 	dt		d'153', d'152', d'151', d'150', d'149', d'148', d'147', d'146' ;175
 	dt		d'145', d'144', d'143', d'142', d'141', d'140', d'139', d'138' ;183
 	dt		d'137', d'136', d'135', d'134', d'133', d'132', d'131', d'130' ;191
 	dt		d'129', d'128', d'127', d'126', d'125', d'124', d'123', d'122' ;199
 	dt		d'120', d'118', d'117', d'116', d'115', d'114', d'113', d'112' ;207
	dt		d'111', d'110', d'108', d'106', d'105', d'104', d'103', d'102' ;215
	dt		d'101', d'100', d'099', d'098', d'096', d'094', d'092', d'090' ;223
	dt		d'088', d'086', d'084', d'082', d'080', d'078', d'076', d'074' ;231
	dt		d'072', d'070', d'068', d'066', d'064', d'062', d'060', d'058' ;239
	dt		d'054', d'050', d'046', d'042', d'040', d'035', d'030', d'025' ;247
	dt		d'020', d'010', d'000', d'000', d'000', d'000', d'000', d'000' ;255
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…