PDA

View Full Version : Variable in array



Gusse
- 18th February 2009, 15:16
I'm trying to increase LCD refresh rate by doing SPI commands without using SHIFTOUT. There seems to be quite big improvement between code #1 and code #2.

Code #1: Lcd_CLK high time is 3.6us
Code #2: Lcd_CLK high time is 0.8us

This speed increasement is big and you can really notice it visually :)

Code #1

SHiftOUT Lcd_SDA , Lcd_CLK , MSBFIRST, [FC(0),FC(1),FC(2),FC(3),FC(4),FC(5)]
Code #2

Lcd_SDA = FC.0(7) : Lcd_CLK = 1 : Lcd_CLK = 0
Lcd_SDA = FC.0(6) : Lcd_CLK = 1 : Lcd_CLK = 0
Lcd_SDA = FC.0(5) : Lcd_CLK = 1 : Lcd_CLK = 0
Lcd_SDA = FC.0(4) : Lcd_CLK = 1 : Lcd_CLK = 0
Lcd_SDA = FC.0(3) : Lcd_CLK = 1 : Lcd_CLK = 0
Lcd_SDA = FC.0(2) : Lcd_CLK = 1 : Lcd_CLK = 0
Lcd_SDA = FC.0(1) : Lcd_CLK = 1 : Lcd_CLK = 0
Lcd_SDA = FC.0(0) : Lcd_CLK = 1 : Lcd_CLK = 0

Lcd_SDA = FC.0(15) : Lcd_CLK = 1 : Lcd_CLK = 0
Lcd_SDA = FC.0(14) : Lcd_CLK = 1 : Lcd_CLK = 0
....
....
....

Lcd_SDA = FC.0(47) : Lcd_CLK = 1 : Lcd_CLK = 0
Lcd_SDA = FC.0(46) : Lcd_CLK = 1 : Lcd_CLK = 0
Lcd_SDA = FC.0(45) : Lcd_CLK = 1 : Lcd_CLK = 0
Lcd_SDA = FC.0(44) : Lcd_CLK = 1 : Lcd_CLK = 0
Lcd_SDA = FC.0(43) : Lcd_CLK = 1 : Lcd_CLK = 0
Lcd_SDA = FC.0(42) : Lcd_CLK = 1 : Lcd_CLK = 0
Lcd_SDA = FC.0(41) : Lcd_CLK = 1 : Lcd_CLK = 0
Lcd_SDA = FC.0(40) : Lcd_CLK = 1 : Lcd_CLK = 0

Because Code #2 is quite long (48 lines) and consumes memory, I tried to make it with loops but there I faced big problems.

I tried many different ways but never managed to get it work as Code #2.


for i = 0 to 5
k=7+8*i : Lcd_SDA = FC.0[k] : Lcd_CLK = 1 : Lcd_CLK = 0
k=6+8*i : Lcd_SDA = FC.0[k] : Lcd_CLK = 1 : Lcd_CLK = 0
k=5+8*i : Lcd_SDA = FC.0[k] : Lcd_CLK = 1 : Lcd_CLK = 0
k=4+8*i : Lcd_SDA = FC.0[k] : Lcd_CLK = 1 : Lcd_CLK = 0
k=3+8*i : Lcd_SDA = FC.0[k] : Lcd_CLK = 1 : Lcd_CLK = 0
k=2+8*i : Lcd_SDA = FC.0[k] : Lcd_CLK = 1 : Lcd_CLK = 0
k=1+8*i : Lcd_SDA = FC.0[k] : Lcd_CLK = 1 : Lcd_CLK = 0
k=0+8*i : Lcd_SDA = FC.0[k] : Lcd_CLK = 1 : Lcd_CLK = 0
next i

for i = 0 to 5
Lcd_SDA = FC.0[7+8*i] : Lcd_CLK = 1 : Lcd_CLK = 0
Lcd_SDA = FC.0[6+8*i] : Lcd_CLK = 1 : Lcd_CLK = 0
Lcd_SDA = FC.0[5+8*i] : Lcd_CLK = 1 : Lcd_CLK = 0
Lcd_SDA = FC.0[4+8*i] : Lcd_CLK = 1 : Lcd_CLK = 0
Lcd_SDA = FC.0[3+8*i] : Lcd_CLK = 1 : Lcd_CLK = 0
Lcd_SDA = FC.0[2+8*i] : Lcd_CLK = 1 : Lcd_CLK = 0
Lcd_SDA = FC.0[1+8*i] : Lcd_CLK = 1 : Lcd_CLK = 0
Lcd_SDA = FC.0[0+8*i] : Lcd_CLK = 1 : Lcd_CLK = 0
next i

for k = 0 to 5
for i = (8+(8*k)) to (1+(8*k)) step -1
j = i-1
Lcd_SDA = FC.0(j) : Lcd_CLK = 1 : Lcd_CLK = 0
next i
next k
(Variables i, j and k are bytes)

I have read Melanies guide about Bits, Byte Words and Arrays, which tells how to you can reference to Arrays, etc...
http://www.picbasic.co.uk/forum/showpost.php?p=2017&postcount=1
but it didn't help me too much. I have understood that variable in array is OK, am I wrong?

-Gusse-

aratti
- 18th February 2009, 16:41
Gusse, this one should be better:


for i = 0 to 40 step 8
Lcd_SDA = FC.0[7+i] : Lcd_CLK = 1 : Lcd_CLK = 0
Lcd_SDA = FC.0[6+i] : Lcd_CLK = 1 : Lcd_CLK = 0
Lcd_SDA = FC.0[5+i] : Lcd_CLK = 1 : Lcd_CLK = 0
Lcd_SDA = FC.0[4+i] : Lcd_CLK = 1 : Lcd_CLK = 0
Lcd_SDA = FC.0[3+i] : Lcd_CLK = 1 : Lcd_CLK = 0
Lcd_SDA = FC.0[2+i] : Lcd_CLK = 1 : Lcd_CLK = 0
Lcd_SDA = FC.0[1+i] : Lcd_CLK = 1 : Lcd_CLK = 0
Lcd_SDA = FC.0[0+i] : Lcd_CLK = 1 : Lcd_CLK = 0
next i

Al.

Gusse
- 18th February 2009, 18:07
Hi Al,

Your code worked a bit better but no as Code #2.

Even code below won't run as Code #2. I wounder why? Only difference is that there is a variable in array. And why i = 0-7 is OK, but next are not??

i=7 : Lcd_SDA = FC.0(i) : Lcd_CLK = 1 : Lcd_CLK = 0
i=6 : Lcd_SDA = FC.0(i) : Lcd_CLK = 1 : Lcd_CLK = 0
i=5 : Lcd_SDA = FC.0(i) : Lcd_CLK = 1 : Lcd_CLK = 0
i=4 : Lcd_SDA = FC.0(i) : Lcd_CLK = 1 : Lcd_CLK = 0
i=3 : Lcd_SDA = FC.0(i) : Lcd_CLK = 1 : Lcd_CLK = 0
i=2 : Lcd_SDA = FC.0(i) : Lcd_CLK = 1 : Lcd_CLK = 0
i=1 : Lcd_SDA = FC.0(i) : Lcd_CLK = 1 : Lcd_CLK = 0
i=0 : Lcd_SDA = FC.0(i) : Lcd_CLK = 1 : Lcd_CLK = 0

i=15 : Lcd_SDA = FC.0(i) : Lcd_CLK = 1 : Lcd_CLK = 0 '<- Stop working
...
...
i=40 : Lcd_SDA = FC.0(i) : Lcd_CLK = 1 : Lcd_CLK = 0

It seems that 1st character (1st 8 bits) are OK, but next charaxters 1-5 are not working (shows only blanck screen at those places).

-Gusse-

aratti
- 18th February 2009, 18:41
Even code below won't run as Code #2. I wounder why?

you have an extra operation to do in this code (i=7:..), while in code 2 you don't do.

Al.

Gusse
- 18th February 2009, 19:27
To my understanding these two codes should be as equal as possible. One is using a constant, the other variable (i).

Lcd_SDA = FC.0(7) : Lcd_CLK = 1 : Lcd_CLK = 0

i=7 : Lcd_SDA = FC.0(i) : Lcd_CLK = 1 : Lcd_CLK = 0
This extra operation i=7 shouldn't be a problem if arrays support variable(s).

-Gusse-

Gusse
- 22nd February 2009, 19:46
Now when I re-opened this problematic project, it was very clear to me what was wrong. It was too obvious and I can only say "Me Stupid!".
If I have defined a new variable for loop, something else than i, j or k (which were used in other part of the code) then I would never faced the problem.

All codes posted under this topic are working well.

Thanks Al for your help, I appreciate it a lot!

-Gusse-