PDA

View Full Version : Need some examples using Lookup( I'm playing with led matrix sample code)



tacbanon
- 5th February 2012, 10:52
Hi everyone, I know this is a beginner question but I need help how on how to understand this code(not mine) below and perhaps later on modify it for my application.


Lookup yazi_reg,[30,"HELLO WORLD LED MATRIX DISPLAY "],harf_reg

"LOOKUP statement can be used to retrieve values from a table of 8-bit constants." I got this from the manual but unfortunately I don't really get it yet.
Hope anyone can tell me how the above line should work..and can I make an array variable to place "HELLO WORLD LED MATRIX DISPLAY "? Assuming string of characters was entered from PC - mcu.

The whole code:


porta=0 : portb=0 : portc=0 : portd=0
trisa=0 : trisb=0 : trisc=0 : trisd=0
porte=0 : trise=0
harf_reg var byte
adres_sec var byte
yinele var byte
sutun_reg var byte[300]
veri var byte
yazi_reg var byte
i VAR BYTE
x var byte
z var byte
y var byte
t var byte
q var byte
sure var byte
yedek var byte
'Define OSC 48
CLEAR : portd=255:sure=200 :GOTO oku:
oku:
call yazi : yazi_reg=yazi_reg+1 : z=harf_reg
for i=1 to z
call yazi : yazi_reg=yazi_reg+1 : call data_sec
for y=0 to yinele
call datalar
sutun_reg[t]=veri : t=t+1
adres_sec=adres_sec+1
next y
next i
t=0
goto gonder
gonder:
FOR Q=0 TO 5
for i=0 to 15 ' 15
portb=sutun_reg[t] : t=t+1
porta=i
portc=111110:pauseus sure:portc=111111
next i
for i=0 to 15
portb=sutun_reg[t] : t=t+1
porta=i
portc=111101:pauseus sure:portc=111111
next i
for i=0 to 15
portb=sutun_reg[t] : t=t+1
porta=i
portc=111011:pauseus sure:portc=111111
next i
for i=0 to 15
portb=sutun_reg[t] : t=t+1
porta=i
portc=110111:pauseus sure:portc=111111
next i
for i=0 to 15
portb=sutun_reg[t] : t=t+1
porta=i
portc=101111:pauseus sure:portc=111111
next i
for i=0 to 15
portb=sutun_reg[t] : t=t+1
porta=i
portc=011111:pauseus sure:portc=111111
next i
for i=0 to 15
portb=sutun_reg[t] : t=t+1
porta=i
portc=111111:pauseus sure:portc=111111
next i
for i=0 to 15
portb=sutun_reg[t] : t=t+1
porta=i
portc=111111:pauseus sure:portc=111111
next i
t=0
NEXT Q
goto kaydir
kaydir:
yedek=sutun_reg[0]
for i=0 to 239
sutun_reg[i]=sutun_reg[i+1]
next i
sutun_reg[240]=yedek
goto gonder
yazi:
lookup yazi_reg,[30,"HELLO WORLD LED MATRIX DISPLAY "],harf_reg
return


data_sec:
select case harf_reg
case "A"
adres_sec=0 : yinele=5 : return
case "B"
adres_sec=6 : yinele=5 : return
case "C"
adres_sec=12 : yinele=5 : return
case "D"
adres_sec=18 : yinele=5 : return
case "E"
adres_sec=24 : yinele=5 : return
case "F"
adres_sec=30 : yinele=5 : return
case "G"
adres_sec=36 : yinele=5 : return
case "H"
adres_sec=42 : yinele=5 : return
case "I"
adres_sec=48 : yinele=3 : return
case "i"
adres_sec=52 : yinele=3 : return
case "J"
adres_sec=56 : yinele=5 : return
case "K"
adres_sec=62 : yinele=5 : return
case "L"
adres_sec=68 : yinele=5 : return
case "M"
adres_sec=74 : yinele=5 : return
case "N"
adres_sec=80 : yinele=5 : return
case "O"
adres_sec=86 : yinele=5 : return
case "P"
adres_sec=92 : yinele=5 : return
case "Q"
adres_sec=98 : yinele=5 : return
case "R"
adres_sec=104 : yinele=5 : return
case "S"
adres_sec=110 : yinele=5 : return
case "T"
adres_sec=116 : yinele=5 : return
case "U"
adres_sec=122 : yinele=5 : return
case "ü"
adres_sec=128 : yinele=5 : return
case "V"
adres_sec=134 : yinele=5 : return
case "W"
adres_sec=140 : yinele=5 : return
case "X"
adres_sec=146 : yinele=5 : return
case "Y"
adres_sec=152 : yinele=5 : return
case "Z"
adres_sec=158 : yinele=5 : return
case " "
adres_sec=164 : yinele=5 : return
case "0"
adres_sec=170 : yinele=5 : return
case "1"
adres_sec=176 : yinele=3 : return
case "2"
adres_sec=180 : yinele=5 : return
case "3"
adres_sec=186 : yinele=5 : return
case "4"
adres_sec=192 : yinele=5 : return
case "5"
adres_sec=198 : yinele=5 : return
case "6"
adres_sec=204 : yinele=5 : return
case "7"
adres_sec=210 : yinele=5 : return
case "8"
adres_sec=216 : yinele=5 : return
case "9"
adres_sec=222 : yinele=5 : return
end select


datalar:


lookup adres_sec,[$7C,$0A,$09,$0A,$7C,0_ ;A
,$7F,$49,$49,$49,$36,0_ ;B
,$3E,$41,$41,$41,$22,0_ ;C
,$7F,$41,$41,$41,$3E,0_ ;D
,$7F,$49,$49,$49,$41,0_ ;E
,$7F,$09,$09,$09,$01,0_ ;F
,$3E,$41,$49,$49,$3A,0_ ;G
,$7F,$08,$08,$08,$7F,0_ ;H
,$41,$7F,$41,0_ ;I
,$44,$7D,$44,0_ ;Ý
,$20,$40,$41,$3F,$01,0_ ;J
,$7F,$08,$14,$22,$41,0_ ;K
,$7F,$40,$40,$40,$40,0_ ;L
,$7F,$02,$04,$02,$7F,0_ ;M
,$7F,$04,$08,$10,$7F,0_ ;N
,$3E,$41,$41,$41,$3E,0_ ;O
,$7F,$09,$09,$09,$06,0_ ;P
,$3E,$41,$51,$61,$3E,0_ ;Q
,$7F,$09,$19,$29,$46,0_ ;R
,$26,$49,$49,$49,$32,0_ ;S
,$01,$01,$7F,$01,$01,0_ ;T
,$3F,$40,$40,$40,$3F,0_ ;U
,$1E,$41,$01,$41,$1E,0_ ;Ü
,$1F,$20,$40,$20,$1F,0_ ;V
,$3F,$40,$30,$40,$3F,0_ ;W
,$63,$14,$08,$14,$63,0_ ;X
,$07,$08,$70,$08,$07,0_ ;Y
,$61,$51,$49,$45,$43,0_ ;Z
,$00,$00,$00,$00,$00,0_ ;Space
,$3E,$51,$49,$45,$3E,0_ ;0
,$42,$7F,$40,0_ ;1
,$62,$51,$49,$45,$42,0_ ;2
,$22,$49,$49,$49,$36,0_ ;3
,$18,$14,$12,$7F,$10,0_ ;4
,$2F,$49,$49,$49,$31,0_ ;5
,$3E,$49,$49,$49,$32,0_ ;6
,$61,$11,$09,$05,$03,0_ ;7
,$36,$49,$49,$49,$36,0_ ;8
,$26,$49,$49,$49,$3E,0],veri ;9

return


Thanks in advance,
tacbanon

HenrikOlsson
- 5th February 2012, 11:22
Hi,
In your example, if yazi_reg is 0 harf_reg will be set to 30 when executing the LOOKUP statement. If yazi_reg is 1 harf_reg will be set to 72 (which is the ASCII code for 'H') and so on.
In other words, harf_reg will be set to the value within the brackets "pointed at" by yazi_reg

LOOKUP works with constants stored an compile time. You can not execute LOOKUP on an array of bytes, and there's really no need to. You can just do something like myByte = myArray[n] where n is the location in the array you want to "get".

/Henrik.

tacbanon
- 5th February 2012, 23:54
Good Day Henrik, Thanks for helping me out. I searched a little on SERIN2 to capture string of characters from PC to MCU.
My study code:


DEFINE OSC 4

str1 var byte[10]
pause 200

main:
Serin2 PortC.7,84,[ str str1\30] ' From PC
Serout2 PORTC.6, 84,[str str1\30,13] ' Check if everything is okay
goto main
End




You can not execute LOOKUP on an array of bytes, and there's really no need to. You can just do something like myByte = myArray[n] where n is the location in the array you want to "get".

Correct if I'm wrong, you mean to say I would not need to use LOOKUP if I use my variable (str1)? I'm sorry if I'm still confuse.

Regards,
tacbanon

HenrikOlsson
- 6th February 2012, 06:44
That's correct. You can not use LOOKUP with arrays since LOOKUP accesses static data stored in flash and not in RAM where your array is.


i VAR Byte
myByte VAR Byte
For i = 0 to 15
Lookup i, ["This is 16 bytes"], myByte ' When you do this the string is stored in flash.
Serout myByte
NEXT

In the above code the string This is 16 bytes is static, stored in program memory and can not be changed at runtime. If accessing arrays (ie data in RAM) you can get the same result by doing something like:


i Var Byte
myArray VAR BYTE[16]
ArrayWrite myArray, ["This is 16 bytes"] ' When you do this the string is stored in RAM
For i = 0 to 15
Serout myArray[i]
NEXT


/Henrik.

tacbanon
- 6th February 2012, 12:32
Now it's clearer :) for me.
Back in the led matrix code...


Lookup yazi_reg,[30,"HELLO WORLD LED MATRIX DISPLAY "],harf_reg


Will this code be valid?


i VAR Byte
ArrayWrite str1,["HELLO WORLD LED MATRIX DISPLAY "]
For i = 0 To 29
harf_reg = str1[i]
GoSub data_sec

Next


I assumed harf_reg will now hold the index value of each character and locate the corresponding ascii character...

Thanks again,
tacbanon

tacbanon
- 7th February 2012, 17:58
I was able to run the original code in proteus...


porta=0 : portb=0 : portc=0 : portd=0
trisa=0 : trisb=0 : trisc=0 : trisd=0
porte=0 : trise=0
harf_reg var byte
adres_sec var byte
yinele var byte
sutun_reg var byte[300]
veri var byte
yazi_reg var byte
i VAR BYTE
x var byte
z var byte
y var byte
t var byte
q var byte
sure var byte
yedek var byte


Define OSC 4
CLEAR : portd=255:sure=200 :GOTO oku:
oku:
call yazi : yazi_reg=yazi_reg+1 : z=harf_reg
for i=1 to z
call yazi : yazi_reg=yazi_reg+1 : call data_sec
for y=0 to yinele
call datalar
sutun_reg[t]=veri : t=t+1
adres_sec=adres_sec+1
next y
next i
t=0
goto gonder
gonder:
FOR Q=0 TO 5
for i=0 to 15 ' 15
portb=sutun_reg[t] : t=t+1
porta=i
portc=111110:pauseus sure:portc=111111
next i
for i=0 to 15
portb=sutun_reg[t] : t=t+1
porta=i
portc=111101:pauseus sure:portc=111111
next i
for i=0 to 15
portb=sutun_reg[t] : t=t+1
porta=i
portc=111011:pauseus sure:portc=111111
next i

t=0
NEXT Q
goto kaydir
kaydir:
yedek=sutun_reg[0]
for i=0 to 239
sutun_reg[i]=sutun_reg[i+1]
next i
sutun_reg[240]=yedek
goto gonder
yazi:
lookup yazi_reg,[30,"HELLO WORLD LED MATRIX DISPLAY "],harf_reg
' For i = 0 To 29
' harf_reg = str1[i]
' GoSub data_sec
' Next
return


data_sec:
select case harf_reg
case "A"
adres_sec=0 : yinele=5 : return
case "B"
adres_sec=6 : yinele=5 : return
case "C"
adres_sec=12 : yinele=5 : return
case "D"
adres_sec=18 : yinele=5 : return
case "E"
adres_sec=24 : yinele=5 : return
case "F"
adres_sec=30 : yinele=5 : return
case "G"
adres_sec=36 : yinele=5 : return
case "H"
adres_sec=42 : yinele=5 : return
case "I"
adres_sec=48 : yinele=3 : return
case "i"
adres_sec=52 : yinele=3 : return
case "J"
adres_sec=56 : yinele=5 : return
case "K"
adres_sec=62 : yinele=5 : return
case "L"
adres_sec=68 : yinele=5 : return
case "M"
adres_sec=74 : yinele=5 : return
case "N"
adres_sec=80 : yinele=5 : return
case "O"
adres_sec=86 : yinele=5 : return
case "P"
adres_sec=92 : yinele=5 : return
case "Q"
adres_sec=98 : yinele=5 : return
case "R"
adres_sec=104 : yinele=5 : return
case "S"
adres_sec=110 : yinele=5 : return
case "T"
adres_sec=116 : yinele=5 : return
case "U"
adres_sec=122 : yinele=5 : return
case "ü"
adres_sec=128 : yinele=5 : return
case "V"
adres_sec=134 : yinele=5 : return
case "W"
adres_sec=140 : yinele=5 : return
case "X"
adres_sec=146 : yinele=5 : return
case "Y"
adres_sec=152 : yinele=5 : return
case "Z"
adres_sec=158 : yinele=5 : return
case " "
adres_sec=164 : yinele=5 : return
case "0"
adres_sec=170 : yinele=5 : return
case "1"
adres_sec=176 : yinele=3 : return
case "2"
adres_sec=180 : yinele=5 : return
case "3"
adres_sec=186 : yinele=5 : return
case "4"
adres_sec=192 : yinele=5 : return
case "5"
adres_sec=198 : yinele=5 : return
case "6"
adres_sec=204 : yinele=5 : return
case "7"
adres_sec=210 : yinele=5 : return
case "8"
adres_sec=216 : yinele=5 : return
case "9"
adres_sec=222 : yinele=5 : return
end select


datalar:


lookup adres_sec,[$7C,$0A,$09,$0A,$7C,0_ ;A
,$7F,$49,$49,$49,$36,0_ ;B
,$3E,$41,$41,$41,$22,0_ ;C
,$7F,$41,$41,$41,$3E,0_ ;D
,$7F,$49,$49,$49,$41,0_ ;E
,$7F,$09,$09,$09,$01,0_ ;F
,$3E,$41,$49,$49,$3A,0_ ;G
,$7F,$08,$08,$08,$7F,0_ ;H
,$41,$7F,$41,0_ ;I
,$44,$7D,$44,0_ ;Ý
,$20,$40,$41,$3F,$01,0_ ;J
,$7F,$08,$14,$22,$41,0_ ;K
,$7F,$40,$40,$40,$40,0_ ;L
,$7F,$02,$04,$02,$7F,0_ ;M
,$7F,$04,$08,$10,$7F,0_ ;N
,$3E,$41,$41,$41,$3E,0_ ;O
,$7F,$09,$09,$09,$06,0_ ;P
,$3E,$41,$51,$61,$3E,0_ ;Q
,$7F,$09,$19,$29,$46,0_ ;R
,$26,$49,$49,$49,$32,0_ ;S
,$01,$01,$7F,$01,$01,0_ ;T
,$3F,$40,$40,$40,$3F,0_ ;U
,$1E,$41,$01,$41,$1E,0_ ;Ü
,$1F,$20,$40,$20,$1F,0_ ;V
,$3F,$40,$30,$40,$3F,0_ ;W
,$63,$14,$08,$14,$63,0_ ;X
,$07,$08,$70,$08,$07,0_ ;Y
,$61,$51,$49,$45,$43,0_ ;Z
,$00,$00,$00,$00,$00,0_ ;Space
,$3E,$51,$49,$45,$3E,0_ ;0
,$42,$7F,$40,0_ ;1
,$62,$51,$49,$45,$42,0_ ;2
,$22,$49,$49,$49,$36,0_ ;3
,$18,$14,$12,$7F,$10,0_ ;4
,$2F,$49,$49,$49,$31,0_ ;5
,$3E,$49,$49,$49,$32,0_ ;6
,$61,$11,$09,$05,$03,0_ ;7
,$36,$49,$49,$49,$36,0_ ;8
,$26,$49,$49,$49,$3E,0],veri ;9

return


And did a modification on the code, to receive strings from PC but did not work, probable I do not understood the following code.


oku:
call yazi : yazi_reg=yazi_reg+1 : z=harf_reg
for i=1 to z
call yazi : yazi_reg=yazi_reg+1 : call data_sec
for y=0 to yinele
call datalar
sutun_reg[t]=veri : t=t+1
adres_sec=adres_sec+1
next y
next i
t=0
goto gonder
gonder:
FOR Q=0 TO 5
for i=0 to 15 ' 15
portb=sutun_reg[t] : t=t+1
porta=i
portc=111110:pauseus sure:portc=111111
next i
for i=0 to 15
portb=sutun_reg[t] : t=t+1
porta=i
portc=111101:pauseus sure:portc=111111
next i
for i=0 to 15
portb=sutun_reg[t] : t=t+1
porta=i
portc=111011:pauseus sure:portc=111111
next i

t=0
NEXT Q
goto kaydir
kaydir:
yedek=sutun_reg[0]
for i=0 to 239
sutun_reg[i]=sutun_reg[i+1]
next i
sutun_reg[240]=yedek
goto gonder


I hope someone can extend their help on this....

tacbanon