PDA

View Full Version : Is there a better way to do this?... strings of letters



Heckler
- 23rd March 2011, 01:45
Hey group,

I am finishing the code for my scrolling 8x8 led matrix for the Boy Scout electronics merit badge...

I have it working pretty well... BUT I wonder if there is a better way to handle the strings of alphabet characters in the various phrases that I want the LED matrix to display.

Below is a snippet of the code where I have just one of the messages I want to display. I just cant help but think there might be a better way to store several separate words and cycle through them one at a time.
I know I can just do multiple iterations of what is below in the code snippet and do a for/next .

Is there a better way to store and reference the strings of characters??


len= 4 'message length=characters -1
for ltr = 0 to len
lookup ltr, ["H","o","n","o","r"],char

complete code...

@ device pic16F690, intrc_osc_noclkout, BOD_OFF, PWRT_OFF, wdt_off, mclr_off, protect_off

' -----[ I/O Definitions ]-------------------------------------------------
' DEFINE DEBUG_REGG PORTA 'set debug port to porta
' DEFINE DEBUG_BIT 0 'use pin a0 of porta for debug
' DEFINE DEBUG_BAUD 2400 'set baud rate to 2400
' DEFINE DEBUG_MODE 0 'communicate in true mode
' DEFINE DEBUGIN_BIT 1 'use pin a0 of porta for debug

' -----[ Constants ]-------------------------------------------------------

' -----[ Variables ]-------------------------------------------------------
x88 var byte[8] 'array to hold the 8 column bytes
col var byte 'byte to hold fetched coulmn
row var byte 'byte to hold current row being scanned via walking "1"
r var byte 'row counter
i var byte 'col counter
char var byte 'current character to fetch
view var byte 'delay to allow eye to see
ltr var byte 'number of letters in current message
pos var byte 'scrolling column counter
len var byte
now var byte 'holds current position for storing data to matrix
pix var byte
done var byte
' -----[ Initialization ]--------------------------------------------------
Reset:
TrisA = %00101000 'Port A3,A5 inupts rest outputs (A3 cannot output)
TrisB = %00000000 'Port B all outputs
TrisC = %00000000 'Port C all outputs
ansel = 0 'and turn off analog
AnselH = 0 'turn of rest of analogs
CM1CON0 = 0 'turn off comparators
CM2CON0 = 0 'turn off comparators
SSPCON.bit5 = 0 ' disable serial port, pins are I/O
OPTION_REG = %10000000 '1 turn off weak pull ups
INTCON = %00000000


' -----[ Program Code ]----------------------------------------------------
main:
Arraywrite x88,[REP $FF\8] 'fill array with $FF, all LED's off

now=0 'starting position for writing data to matrix

Letter:
len= 4 'message length=characters -1
for ltr = 0 to len
lookup ltr, ["H","o","n","o","r"],char
pos = 0 'reset pos counter for new letter

gosub getchar 'get first value representing number of columns
pix=col 'set pix to be number of columns in current character

for pos = 1 to pix 'pos starts at 1 to skip first character data
gosub getchar 'get actual character data
x88[now]=col 'store character data in current position of matrix
gosub display 'display each new character column data
next pos
x88[now]=$FF 'follow each character with 2 blank spaces
gosub display
x88[now]=$FF
gosub display
next ltr

Finish: 'follow each phrase with a complete blank screen
for done = 1 to 8
x88[now]=$FF
gosub display
next done

goto main
end


'================================================= ==========
Display: 'display the current 8x8 matrix 10 times

now=now+1 'increment position counter for scrolling
if now=8 then now=0

for view = 1 to 7 'scroll speed, number of time to scan each screen
For r = 0 to 7 'count for row scanning
lookup r, [1,2,4,16,16,32,64,128], row 'creates walking "1" for row scanning
if r<4 then
PortA=row 'PortA 0-3 is connected to row 1-4
Else
PortB=row 'PortB 4-7 is connected to row 5-8
endif

i=r + now 'combine row count with position counter(now)
if i>7 then i=i-8 'to create scrolling effect
col=x88[i]
PortC=col 'set the column LED's with current col

pause 1 'on time for each col 1ms
PortA=0 :PortB=0 'all rows off
PortC=%11111111 'all cols off
NEXt r
next view

return
'================================================= ==========

GetChar:
Select Case char

Case"!" :LOOKUP pos, [1,$05], col
Case"A" :LOOKUP pos, [5,$C1,$B7,$77,$B7,$C1], col
Case"B" :LOOKUP pos, [5,$7D,$01,$6D,$6D,$93], col
Case"C" :LOOKUP pos, [5,$83,$7D,$7D,$7D,$BB], col
Case"D" :LOOKUP pos, [5,$7D,$01,$7D,$7D,$83], col
Case"E" :LOOKUP pos, [5,$01,$6D,$6D,$6D,$7D], col
Case"F" :LOOKUP pos, [5,$01,$6F,$6F,$6F,$7F], col
Case"G" :LOOKUP pos, [5,$83,$7D,$7D,$6D,$61], col
Case"H" :LOOKUP pos, [5,$01,$EF,$EF,$EF,$01], col
Case"I" :LOOKUP pos, [3,$7D,$01,$7D], col
Case"J" :LOOKUP pos, [5,$FB,$FD,$FD,$FD,$03], col
Case"K" :LOOKUP pos, [5,$01,$EF,$D7,$BB,$7D], col
Case"L" :LOOKUP pos, [5,$01,$FD,$FD,$FD,$FD], col
Case"M" :LOOKUP pos, [5,$01,$BF,$CF,$BF,$01], col
Case"N" :LOOKUP pos, [5,$01,$DF,$EF,$F7,$01], col
Case"O" :LOOKUP pos, [5,$83,$7D,$7D,$7D,$83], col
Case"P" :LOOKUP pos, [5,$01,$6F,$6F,$6F,$9F], col
Case"Q" :LOOKUP pos, [5,$83,$7D,$75,$7B,$85], col
Case"R" :LOOKUP pos, [5,$01,$6F,$67,$6B,$9D], col
Case"S" :LOOKUP pos, [5,$9B,$6D,$6D,$6D,$B3], col
Case"T" :LOOKUP pos, [5,$7F,$7F,$01,$7F,$7F], col
Case"U" :LOOKUP pos, [5,$03,$FD,$FD,$FD,$03], col
Case"V" :LOOKUP pos, [5,$1F,$E7,$F9,$E7,$1F], col
Case"W" :LOOKUP pos, [5,$03,$FD,$E3,$FD,$03], col
Case"X" :LOOKUP pos, [5,$39,$D7,$EF,$D7,$39], col
Case"Y" :LOOKUP pos, [5,$3F,$DF,$E1,$DF,$3F], col
Case"Z" :LOOKUP pos, [5,$79,$75,$6D,$5D,$3D], col

Case"a" :LOOKUP pos, [5,$E3,$DD,$DD,$EB,$C1], col
Case"b" :LOOKUP pos, [5,$01,$ED,$ED,$ED,$F3], col
Case"c" :LOOKUP pos, [5,$E3,$DD,$DD,$DD,$EB], col
Case"d" :LOOKUP pos, [5,$E3,$DD,$DD,$DD,$01], col
Case"e" :LOOKUP pos, [5,$E3,$D5,$D5,$D5,$E5], col
Case"f" :LOOKUP pos, [5,$EF,$81,$6F,$6F,$BF], col
Case"g" :LOOKUP pos, [5,$E7,$DA,$DA,$EA,$C0], col
Case"h" :LOOKUP pos, [5,$01,$DF,$DF,$DF,$E1], col
Case"i" :LOOKUP pos, [1,$41], col
Case"j" :LOOKUP pos, [3,$F9,$FE,$41], col
Case"k" :LOOKUP pos, [4,$01,$F7,$EB,$DD], col
Case"l" :LOOKUP pos, [3,$03,$FD,$FD], col
Case"m" :LOOKUP pos, [5,$C1,$DF,$E1,$DF,$E1], col
Case"n" :LOOKUP pos, [5,$C1,$EF,$DF,$DF,$E1], col
Case"o" :LOOKUP pos, [5,$E3,$DD,$DD,$DD,$E3], col
Case"p" :LOOKUP pos, [5,$C0,$EB,$DB,$DB,$E7], col
Case"q" :LOOKUP pos, [5,$E7,$DB,$DB,$C0,$FE], col
Case"r" :LOOKUP pos, [5,$C1,$EF,$DF,$DF,$EF], col
Case"s" :LOOKUP pos, [5,$EF,$D5,$D5,$D5,$FB], col
Case"t" :LOOKUP pos, [4,$DF,$03,$DD,$FB], col
Case"u" :LOOKUP pos, [5,$C3,$FD,$FD,$FB,$C1], col
Case"v" :LOOKUP pos, [5,$C7,$FB,$FD,$FB,$C7], col
Case"w" :LOOKUP pos, [5,$C3,$FD,$C3,$FD,$C3], col
Case"x" :LOOKUP pos, [5,$DD,$EB,$F7,$EB,$DD], col
Case"y" :LOOKUP pos, [4,$C7,$FA,$FA,$C1], col
Case"z" :LOOKUP pos, [5,$DD,$D9,$D5,$CD,$DD], col

end select

Return

HenrikOlsson
- 23rd March 2011, 06:31
Hi,
You could use ArrayWrite and a pointer into that array, something like

Msg VAR BYTE[32] '32 byte array for displayed message, zero-indexed
MsgPointer VAR BYTE

ArrayWrite Message, ["Honor",13] 'You can use 13 (or something else) as the end of message character to avoid having to count characters.
GOSUB DisplayMsg
ArrayWrite Message, ["This is the second message",13]
GOSUB DisplayMsg

END

DisplayMsg:
MsgPointer = 0 : Char = 0

While Char <> 13
Char = Msg[MsgPointer]
'Do your display magic here or GOSUB it.
MsgPointer = MsgPointer + 1
Wend

RETURN

This caught my eye:
lookup r, [1,2,4,16,16,32,64,128], row 'creates walking "1" for row scanning

Well, that's one idea...

Heckler
- 25th March 2011, 13:45
Thanks Henrik,

I have used your suggestion... Program is working good.

Good catch on the duplicated "16". (it is correct in my case, though)

I am always amazed at how one can look at someone elses code and follow it through with logic. The person writing the code uaually has "eat,slept and breathed" their code as they write and debug it. But the person trying to help is looking at the code for the first time, cold.

The reason I skip 8 and have 16 twice in the lookup is because PortA.3 is an input only pin. So I am using bit A.4 then B.4 next. So I just write the walking bit to the A port for the first 4 postions, then the B port for the next four.


lookup r, [1,2,4,16,16,32,64,128], row 'creates walking "1" for row scanning
if r<4 then
PortA=row 'PortA 0,1,2,4 is connected to row 1-4
Else
PortB=row 'PortB 4,5,6,7 is connected to row 5-8
endif

Thanks for your help