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.

pic18f

Status
Not open for further replies.

pixman

Member
Hi to ALL

can someone please help me with an example of a look table for pic 18f
i used the table from 16f and it repeats the letters twice before moving to the next line.
has this got something to do with the even numbers.

i want to send text to pc via rs232 port.
i have it working except fot the double letters
i want send:' running'
it reads:'rruunnnning'

i am using assemble only.

thanks
Pixman
 
My guess is that your table reading is good, but that you have a problem with your UART transmit code.

Maybe you are transmitting when TXIF is low, instead of waiting for TXIF high.

Only a guess. Hard to know what is wrong without seeing your code.
 
uart working fine.
it is the table that is the problem.
when i run it in mplab sim it actually goes to the line twice and then to the next line and does that twice and so on. i will post my code a bit latter.
 
HERE IS THE CODE

Loop
call NextChar
goto Loop

NextChar ;display selected string
; movlw 0x07 ;set PCLATH for strings in last page of memory
; movwf PCLATH
clrf count
next movf count, w ;put counter value in W
call Strings ;get a character from the text table
clrf PCLATH ;reset PCLATH
xorlw 0x00 ;is it a zero?
btfsc STATUS, Z
return ;exit from routine
call XMIT_RS232
incf count, f
goto next

Strings addwf PCL , f

retlw 'R'
retlw 'U'
retlw 'N'
retlw 'N'
retlw 'I'
retlw 'N'
retlw 'G'
retlw 0x0A
retlw 0x0D

retlw 'F'
retlw 'O'
retlw 'R'
retlw 'W'
retlw 'A'
retlw 'R'
retlw 'D'
retlw '!'
retlw 0x0A
retlw 0x0D
retlw 0x00

XMIT_RS232
btfss PIR1, TXIF ;xmit buffer empty?
GOTO XMIT_RS232 ;no, wait
MOVWF TXREG ;now send
RETURN
 
Your problem is due to the way the program counter works on the 18 series chips. Each byte is 1 location but each instruction takes two locations. Change your code to,
Code:
		clrf	count
next		[COLOR="red"]rlncf[/COLOR]	count,w		;put counter value [COLOR="red"]* 2[/COLOR] in W
		call	Strings		;get a character from the text table
		clrf	PCLATH		;reset PCLATH
		xorlw	0x00		;is it a zero?
		btfsc	STATUS, Z
		return			;exit from routine
		call	XMIT_RS232
		incf	count, f
		goto	next

Edit, @Ian, clearing PCLATH is required if the table is in page zero. Otherwise you would load it with the high byte of the table address.

Mike.
 
Last edited:
Thanks mike
all sorted out.

I am using a pic18f4520 and in the data sheet chapter 6.1 it has something about table reads and table writes. Can someone explain how this works and give me a small example.
The final project is to build a gsm modem.i am trying to get a handle on the pic program before trying to connect to the gsm modem.

The next question is how would you read text coming in and comparring it to text in the pic to do something.
For example i can send 4 diffrent text strings to the pic
'start motor1'
'stop motor1'
start motor2'
'stop motor2'
if the pic receives start or stop motor1 it will switch portb,0
if the pic receives start or stop motor2 it will switch portb,1

thanks
david
 
Hi

To keep your main code simple and just Call the table without extra code there, use this little routine in front of all your tables.

Code:
lookup4         movwf   TABWORK		; sets system for 18F lookups - 2 byte calls
		bcf	STATUS,C
		rlcf	TABWORK,F
		movlw	HIGH(table4)
		btfsc	STATUS,C
		incf	WREG,W
		movwf	PCLATH
		movlw	LOW(table4)
		addwf	TABWORK,W
		btfsc	STATUS,C
		incf	PCLATH,F
		movwf	PC

table4  retlw 0x70	; 00
		retlw	0x70	; 01              
		retlw	0x70	; 02
		retlw	0x70	; 03
                etc ....
 
Look for "TBLRD" and "TBLWT"" in the instruction set of any 18F micro and forget all the above.

Way much simpler.
 
I was bored this morning so thought I'd have a go at some asm,

Here's a routine which will check a buffer against a list of strings and return zero for no match or the message number that matches.
Code:
		cblock	0x80	;start of general purpose registers
buffer:20
count					
		endc

Search	movlw	upper mess
	movwf	TBLPTRU
	movlw	high mess
	movwf	TBLPTRH
	movlw	low mess
	movwf	TBLPTRL
	clrf	count
loop	tblrd*
	movf	TABLAT,W	;test for end of table
	bz	done
	movlw	low buffer
	movwf	FSR0L
	movlw	high buffer
	movwf	FSR0H
Compare	tblrd*+
	tstfsz	TABLAT
	goto	Contin
;found match
	incf	count,w
done	return
Contin	movf	POSTINC0,W
	xorwf	TABLAT,W
	bz	Compare
;no match
skip	tblrd*+
	tstfsz	TABLAT	;test for end of table
	goto	skip
	incf	count,F
	goto	loop
	


mess	db	"on motor1",0
	db	"off motor1",0
	db	"on motor2",0
	db	"off motor2",0
	db	0
Note, if doesn't check if the strings are the same length so if buffer contains "off motor123" then it will match entry 2.

Have fun.

Mike.
 
Thanks Mike. will try it later today.

ATFERRARI, I had a look at 'TBLRD' and 'TBLWT' BUT NOT SURE HOW TO IMPLEMENT IT.LOOKED AT INSTUCTION SET AND STILL NO LUCK. COULD YOU POST A SIMPLE EXAMPLE OF HOW IT IS USED.
 
Not an example but proof that you could use it...

Hola Pix,

What follows are excerpts of code I used to get text on the screen of my scope.
They are not related to each other, just examples of the different functions.

Basically, in its simples form, it works like this:

At programming time, you write the data you want to read at running time, for example, as this:

Code:
TEXT_LINE_4             DW "to get on-screen text.   ",0
TEXT_LINE_5             DW "The idea sprang while    ",0
TEXT_LINE_6             DW "working with the scope.  ",0
TEXT_LINE_7             DW "Simple hardware:         ",0
TEXT_LINE_8             DW "                         ",0
TEXT_LINE_9             DW "Micro PIC 18F4431 - Xtal ",0

Then, somewhere in your fantastic code you will come to the point where you need that data for something. You have to read it from the table (here, "TEXT_LINE_X").

For that you load the address of that table, probably this way (there are some other ways much more complex but efficient as well):

Code:
PROCESS_NEXT_CHAR
  MOVFF [COLOR="#ff0000"]TBLPTRU[/COLOR],[COLOR="#ff0000"]UPPER[/COLOR] TEXT_LINE_X       ;we need TBLPTR pointing 
  MOVFF T[COLOR="#ff0000"]BLPTRH[/COLOR],[COLOR="#ff0000"]HIGH[/COLOR] TEXT_LINE_X       ;to first byte of data
  MOVFF [COLOR="#ff0000"]TBLPTRL[/COLOR],[COLOR="#ff0000"]LOW[/COLOR] TEXT_LINE_X       ;in the table

Look how the expressions above in red are used.

Now, that you have the pointer aiming to the right place it will come the moment that you could use the data in the table as this:

Code:
  [COLOR="#ff0000"]TBLRD*+                         ;TABLAT =X:Y values pair (& incr TBLPTR)[/COLOR]
  SWAPF TABLAT,W                  ;W =Y:X pair
  ANDLW MASK_HI_NIBBLE            ;W =0:X value
  ADDWF CURR_X,W                  ;add index to show dot in proper column
  MOVWF PORT_X                    ;output value ---> R2R

Or as this:

Code:
[COLOR="#ff0000"]  TBLRD*+                         ;get next char & incr TBLPTR - byte in TABLAT[/COLOR]
  TSTFSZ TABLAT                   ;TABLAT =0 (ASCII NULL) - means we reached...
  BRA PROCESS_NEXT_CHAR           ;TABLAT >0 - process next char  
  DECF CNTR_MSG_LINES,F           ;...end of line - go for next text line

I could show maybe two more examples of where I used "TBLRD" but the point is that TABLRD is just for that: reading the table. Whatever follows would be related to what you want to do with the byte in TABLAT.

While typing this reply I run across the reply from Mike who IS telling you about this function.

When you come to something like this which is "new" for you, always test it in the most single program, just by simulating it. Then go to hardware and then increase complexity.

In the past, and sometimes now, I used the press of one (or two) button to increase (decrease) something, be it data or addresses to see if what I did was right.

Hope you are not tired of reading this much of words.:(
 
Last edited:
Thanks for reply

to learn something new i find out how it works, get it working in simulator, then with pic.

Then the task of putting it all together and get one final program.

Pixman
 
Has anyone else worked with the table read and write function in the 18f pics.
This could be useful in saving Data or text when using rs232.
 
It's not a good idea to write to flash when using peripherals as the write can take 2mS in which time you will lose data. Put the received data into a ram buffer and then use a routine like the one I posted above to see what it says.

Mike.
 
How would you up received date into a ram buffer, or create a ram buffer.
this is all new to me.
for example if i receive text via rs232 "motor1 on" how and were do you put it to use it in the compare
code Mike posted.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top