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.

How to create a loop-up table using a PIC16F684 and assebmly language

Status
Not open for further replies.

Steve311

Member
Hi All, I'm stuck with figuring out how to create a look-up table using a pic16f684 in assembly using 3 outputs from an accelerometer.

I basically am using 3 outputs from an accelerometer (X, Y and Z) to create two new values (A and B) depending on each unique value of the accelerometer outputs. From datasheets and breaboard data, accelerometers are naturally non-linear to some extent, which in my application will need a look-up table.

My thoughts are... to create variable A from X, Y and Z...I will need one table for variable A, and a different table for variable B. They will both reference the accelerometers outputs X, Y and Z which all will be read at the same time.

Any thoughts or advice on creating a loop-up table would be greatly appreciated!

Thanks in Advance
Steve
 
accelerometers are sinusoidal outputs... you'll need only one table... cosine, and 90 positions ( 1 per degree)..

But the worst is the filter... you'll need a really good filter...

The 90 position lookup should be placed in one page... (you'll need 180 if you need int's)
 
Hi ian, thanks for your response. I am actually using a kionix kxtj2 accelerometer which has a built in digital engine. This engine converts the analog output to "counts" which converts to a binary output.
It also has built in selectable low pass filters.

Do you have any examples of look up tables? or know of any good references for this?

Thanks again
 
pic chip lookup tables are relatively small as they must fit in one bank... Look at Nigel's tutorials if you are looking at ASM or mine for C examples..

Both links are in my signature... How big are the tables?
 
I'm not quite sure how big the table(s) will be. I'm still in process of taking data with the accelerometer.

Could you point me to which of yours/nigel's tutorial has look up table examples? I took a look and nothing jumped out at me.

Thanks Again
Steve
 
ASM: tutorial 2.3

Code:
;Tutorial 2.3 - Nigel Goodwin 2002
	LIST	p=16F628		;tell assembler what chip we are using
	include "P16F628.inc"		;include the defaults for the chip
	__config 0x3D18			;sets the configuration settings (oscillator type etc.)

	cblock 	0x20 			;start of general purpose registers
		count			;used in table read routine
		count1 			;used in delay routine
		counta 			;used in delay routine 
		countb 			;used in delay routine
	endc

LEDPORT	Equ	PORTB			;set constant LEDPORT = 'PORTB'
LEDTRIS	Equ	TRISB			;set constant for TRIS register
SWPORT	Equ	PORTA
SWTRIS	Equ	TRISA
SW1	Equ	7			;set constants for the switches
SW2	Equ	6
SW3	Equ	5
SW4	Equ	4
LED1	Equ	3			;and for the LED's
LED2	Equ	2
LED3	Equ	1
LED4	Equ	0
	
	org	0x0000			;org sets the origin, 0x0000 for the 16F628,
					;this is where the program starts running	
	movlw	0x07
	movwf	CMCON			;turn comparators off (make it like a 16F84)

   	bsf 	STATUS,		RP0	;select bank 1
   	movlw 	b'00000000'		;set PortB all outputs
   	movwf 	LEDTRIS
	movlw 	b'11110000'		;set PortA 4 inputs, 4 outputs
   	movwf 	SWTRIS
	bcf	STATUS,		RP0	;select bank 0
	clrf	LEDPORT			;set all outputs low
	clrf	SWPORT
	bsf	SWPORT,	LED1		;set initial pattern


Start	clrf	count			;set counter register to zero
Read	movf	count, w		;put counter value in W
	btfsc	SWPORT,	LED1		;check which LED is lit
	call	Table1			;and read the associated table
	btfsc	SWPORT,	LED2
	call	Table2
	btfsc	SWPORT,	LED3
	call	Table3
	btfsc	SWPORT,	LED4
	call	Table4
	movwf	LEDPORT
	call	Delay
	incf	count,	w
	xorlw	d'14'			;check for last (14th) entry
	btfsc	STATUS,	Z
	goto	Start			;if start from beginning
	incf	count,	f		;else do next
	goto	Read

Table1	ADDWF   PCL, f			;data table for bit pattern
	retlw	b'10000000'
        retlw   b'01000000'
        retlw   b'00100000'
        retlw   b'00010000'
        retlw   b'00001000'
        retlw   b'00000100'
        retlw   b'00000010'
        retlw   b'00000001'
        retlw   b'00000010'
        retlw   b'00000100'
        retlw   b'00001000'
        retlw   b'00010000'
        retlw   b'00100000'
        retlw   b'01000000'

Table2	ADDWF   PCL, f			;data table for bit pattern
	retlw	b'11000000'
        retlw   b'01100000'
        retlw   b'00110000'
        retlw   b'00011000'
        retlw   b'00001100'
        retlw   b'00000110'
        retlw   b'00000011'
        retlw   b'00000011'
        retlw   b'00000110'
        retlw   b'00001100'
        retlw   b'00011000'
        retlw   b'00110000'
        retlw   b'01100000'
        retlw   b'11000000'

Table3	ADDWF   PCL, f			;data table for bit pattern
	retlw	b'01111111'
        retlw   b'10111111'
        retlw   b'11011111'
        retlw   b'11101111'
        retlw   b'11110111'
        retlw   b'11111011'
        retlw   b'11111101'
        retlw   b'11111110'
        retlw   b'11111101'
        retlw   b'11111011'
        retlw   b'11110111'
        retlw   b'11101111'
        retlw   b'11011111'
        retlw   b'10111111'

Table4	ADDWF   PCL, f			;data table for bit pattern
	retlw	b'00111111'
        retlw   b'10011111'
        retlw   b'11001111'
        retlw   b'11100111'
        retlw   b'11110011'
        retlw   b'11111001'
        retlw   b'11111100'
        retlw   b'11111100'
        retlw   b'11111001'
        retlw   b'11110011'
        retlw   b'11100111'
        retlw   b'11001111'
        retlw   b'10011111'
        retlw   b'00111111'

ChkKeys	btfss	SWPORT,	SW1
	call	Switch1
	btfss	SWPORT,	SW2
	call	Switch2
	btfss	SWPORT,	SW3
	call	Switch3
	btfss	SWPORT,	SW4
	call	Switch4
	retlw	0x00

Switch1	clrf	SWPORT			;turn all LED's off
	bsf	SWPORT,	LED1		;turn LED1 on
	retlw	0x00

Switch2	clrf	SWPORT			;turn all LED's off
	bsf	SWPORT,	LED2		;turn LED2 on
	retlw	0x00

Switch3	clrf	SWPORT			;turn all LED's off
	bsf	SWPORT,	LED3		;turn LED3 on
	retlw	0x00

Switch4	clrf	SWPORT			;turn all LED's off
	bsf	SWPORT,	LED4		;turn LED4 on
	retlw	0x00


Delay	movlw	d'250'			;delay 250 ms (4 MHz clock)
	movwf	count1
d1	call 	ChkKeys 		;check the keys
	movlw	0xC7			;delay 1mS
	movwf	counta
	movlw	0x01
	movwf	countb
Delay_0
	decfsz	counta, f
	goto	$+2
	decfsz	countb, f
	goto	Delay_0

	decfsz	count1	,f
	goto	d1
	retlw	0x00

	end

and the same in C:

Code:
#include <pic.h>				// pic specific identifiers
#define _XTAL_FREQ  4000000		// Xtal speed
__CONFIG(0x3D18);				// Config bits

#define LEDPORT PORTB
#define LEDTRIS	TRISB
#define SWTRIS	TRISA
#define SWPORT	PORTA
#define	SW1		RA7				// Pre- defined in pic.h
#define SW2		RA6
#define SW3		RA5
#define SW4		RA4
#define LED1	RA3				// Led's 
#define LED2	RA2
#define LED3	RA1
#define LED4	RA0

void Dly(void);					// C requires function
void chkkey(void);				// prototypes

unsigned char TABLE1[14] ={		// 14 components pattern 1
		0b10000000,
		0b01000000,
		0b00100000,
		0b00010000,
		0b00001000,
		0b00000100,
		0b00000010,
		0b00000001,
		0b00000010,
		0b00000100,
		0b00001000,
		0b00010000,
		0b00100000,
		0b01000000};

unsigned char TABLE2[14] ={		// 14 components pattern 2
		0b11000000,
		0b01100000,
		0b00110000,
		0b00011000,
		0b00001100,
		0b00000110,
		0b00000011,
		0b00000011,
		0b00000110,
		0b00001100,
		0b00011000,
		0b00110000,
		0b01100000,
		0b11000000};

unsigned char TABLE3[14] ={		// 14 components pattern 3
		0b01111111,
		0b10111111,
		0b11011111,
		0b11101111,
		0b11110111,
		0b11111011,
		0b11111101,
		0b11111110,
		0b11111101,
		0b11111011,
		0b11110111,
		0b11101111,
		0b11011111,
		0b10111111};

unsigned char TABLE4[14] ={		// 14 components pattern 4
		0b00111111,
		0b10011111,
		0b11001111,
		0b11100111,
		0b11110011,
		0b11111001,
		0b11111100,
		0b11111100,
		0b11111001,
		0b11110011,
		0b11100111,
		0b11001111,
		0b10011111,
		0b00111111};

void main(void)					// program entry
	{
	char index = 0;				// new loop variable
	CMCON = 0x7;				// Comparitors off
	SWTRIS = 0b11110000;		// Switch port as inputs LED's as outpts
	LEDTRIS = 0b00000000;		// Led port as outputs
	SWPORT = 0x0;
	LEDPORT = 0x0;
	LED1 = 0x1;					// Set initial pattern
	while(1)					// Loop forever
		{
		for(index=0;index<14;index++)		// loop through tables
			{
			if(LED1)
				LEDPORT = TABLE1[index];	// Check which Led 
			if(LED2)
				LEDPORT = TABLE2[index];	// is lit and
			if(LED3)
				LEDPORT = TABLE3[index];	// get pattern from
			if(LED4)
				LEDPORT = TABLE4[index];	// table
			Dly();
			}
		}
	}

void Dly()					// required to simulate Nigels tutorial
	{
	int index = 250;		// 250 ms
	while(index--)
		{
		__delay_ms(1);		// Every 1 ms
		chkkey();			// scan keys
		}		
	}

void chkkey()
	{
		if(!SW1)				// Switch 1 on
			{
			SWPORT = 0x0;		// Clear port
			LED1 = 0x1;			// led 1 on
			}				
		if(!SW2)				// Switch 2 on
			{
			SWPORT = 0x0;		// Clear port
			LED2 = 0x1;			// led 2 on
			}					
		if(!SW3)				// Switch 3 on
			{
			SWPORT = 0x0;		// Clear port
			LED3 = 0x1;			// led 3 on
			}
		if(!SW4)				// Switch 4 on
			{
			SWPORT = 0x0;		// Clear port
			LED4 = 0x1;			// led 4 on		
			}
	}			// End!!
 
Status
Not open for further replies.

Latest threads

Back
Top