Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

18F Lookup Table Cose Snippet [ASM]

Status
Not open for further replies.

pittuck

New Member
Ok well i posted b4 but it got lost in the forum restore.

Anyhow, i have been working on some code for table reading and thing this should work! There is not much documented use of the table read on the 18F's from what i have seen, although i have not spent a lot of time looking, nothing was overt. And the datasheet explains it Ok, but not too good on application (ie the db, or data directives in mpasm to store the table data, it only documents the table write procedure to input data, which means writing data at run time...)

In the 16F series the RETLW cmd and some playing with the PC would work well, indeed it does on the 18F but due to the nature of the 18F's architecture you will only be able to address 128 bytes of data (in the 18F the program counter increments 2 per instruction, unlike the 16F where the PC only increments once per instruction. This is so you can address single bytes in the 18F, but means when reading values of 0 and 1 will return data from the same place on the table, like 2 and 3, 4 and 5 etc.)

So using the TBLPRT (table pointer) and the TABLAT register to get data out of the porgram memory.

As the table pointer is the same format of the program counter you can address 2 bytes per program word, so a 256 byte table takes up only 128 code words.

The code, basically the code gets the address of the start of the table using a label and the upper, high, low directives in MPASM. So this gets the start address then adds the offset. Basically the current code will only work with 256bytes, but i am sure people will figure out how to use larger tables....

There is only 0x00's in the table ATM, but i have a full one with 256 entries and am able to get data out correctly.

The data is input using the db directive.

db 0x01, 0x02

If this is at the start of the table, position 0 in the table will be 0x01, and position 1 will be 0x02. [As i side note this will actually be stored as 0x0201 in the program memory as the lower byte is read first]

Code:
SensorTable

	MOVWF table_temp, 0					; Save W
	MOVLW low Sensor_Data 				; Get low byte of first address
	ADDWF table_temp, 0, 0				; Add W to address
	MOVWF TBLPTRL, 0					; Put new address in low byte of tblptr
	MOVLW high Sensor_Data				; Get High Byte of first address
	BTFSC STATUS, C						; Check for bit carry
		ADDLW 0x01						; Add 1 if there is
	MOVWF TBLPTRH, 0					; Put new address in high byte of tblptr
	MOVLW upper Sensor_Data				; Get Upper Byte of first address
	BTFSC STATUS, C						; Check for bit carry
		ADDLW 0x01						; Add 1 if there is
	MOVWF TBLPTRU, 0					; Put new address in upper byte of tblprt
	TBLRD*
	MOVF TABLAT, 0 ,0								; Get data

	RETURN								; Done

Sensor_Data db 0x00, 0x00
			db 0x00, 0x00
			db 0x00, 0x00
			db 0x00, 0x00


Regards,

- Martyn
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top