|
|
View previous topic :: View next topic |
Author |
Message |
The Puma
Joined: 23 Apr 2004 Posts: 227 Location: The Netherlands
|
How can i rotating the values of an an array |
Posted: Sat May 20, 2006 9:22 am |
|
|
This is the array
How can i move the array values one position down
Here is how the array is build
There are 10 dot matrix displays
Each dot matrix has 5 bytes
as example: dot matrix 10 =
message[0] --> 1. column of the dot matrix (left side of display)
message[1] --> 2. column
message[2] --> 3. column
message[3] --> 4. column
message[4] --> 5. column (right side of display)
Code: |
Address Symbol Name Value
0005 message
0005 [0] 0x7F
0006 [1] 0x49
0007 [2] 0x49
0008 [3] 0x49
0009 [4] 0x36
000A [5] 0x00
000B [6] 0x00
000C [7] 0x00
000D [8] 0x00
000E [9] 0x00
000F [10] 0x00
0010 [11] 0x00
0011 [12] 0x00
0012 [13] 0x00
0013 [14] 0x00
0014 [15] 0x00
0015 [16] 0x00
0016 [17] 0x00
0017 [18] 0x00
0018 [19] 0x00
0019 [20] 0x00
001A [21] 0x00
001B [22] 0x00
001C [23] 0x00
001D [24] 0x00
001E [25] 0x00
001F [26] 0x00
0020 [27] 0x00
0021 [28] 0x00
0022 [29] 0x00
0023 [30] 0x00
0024 [31] 0x00
0025 [32] 0x00
0026 [33] 0x00
0027 [34] 0x00
0028 [35] 0x00
0029 [36] 0x00
002A [37] 0x00
002B [38] 0x00
002C [39] 0x00
002D [40] 0x00
002E [41] 0x00
002F [42] 0x00
0030 [43] 0x00
0031 [44] 0x00
0032 [45] 0x00
0033 [46] 0x00
0034 [47] 0x00
0035 [48] 0x00
0036 [49] 0x00
|
Last edited by The Puma on Sun May 21, 2006 3:56 am; edited 5 times in total |
|
|
luckyluke
Joined: 18 Apr 2006 Posts: 45
|
|
Posted: Sat May 20, 2006 11:04 am |
|
|
Code: |
for (i=0;i<50;i++){
temp[i]=message[i];
}
for (i=0;i<50;i++){
message[i+1]=temp[i];
if(i==49)
message[49]=temp[0];
} |
|
|
|
tsupuntu
Joined: 05 May 2006 Posts: 9
|
|
Posted: Sat May 20, 2006 12:18 pm |
|
|
for (i=0;i<50;i++){
if(i==0) temp=message[i];
if(i<49) message[i]=message[i+1];
if(i==49) message[i]=temp;
} ///// this method save ram because not array temp |
|
|
Ttelmah Guest
|
|
Posted: Sat May 20, 2006 3:39 pm |
|
|
Realistically though, don't...
Look at how circular buffers work. It is far more efficient to move your access 'point', than to move the whole array.
Best Wishes |
|
|
The Puma
Joined: 23 Apr 2004 Posts: 227 Location: The Netherlands
|
|
Posted: Sun May 21, 2006 3:47 am |
|
|
The array must move this way.
message[4] moves to message[3] / dot matrix 10
message[3] moves to message[2]
message[2] moves to message[1]
message[1] moves to message[0]
message[0] moves to message[9]
message[9] moves to message[8] / dot matrix 9
message[8] moves to message[7]
message[7] moves to message[6]
message[6] moves to message[5]
message[5] moves to message[14]
......
and so until dot matrix 1 is reached |
|
|
Ttelmah Guest
|
|
Posted: Sun May 21, 2006 4:15 am |
|
|
Why????
The point is, that if you implement a circular buffer, the data will address as if it moves, but no physical movement need take place. This is how serial I/O buffers are implemented, and is hundreds of times more efficient in terms of processor work. It is like the difference between using a wheel, and trying to move stuff on bit of board. Once the wheel is invented, nobody would bother to use the board if they don't have to...
#define read_byte(x,y) message[(x+y) % sizeof(message))]
Then just have a variable 'offset'. Start with this as '0', and
'read_byte(0,offset)', will return message[0]. Increment it to '1', and 'read_byte(0,offset), will return message[1], and so on. The message never has to move, but your access to it shifts, as if it has moved.
Best Wishes |
|
|
The Puma
Joined: 23 Apr 2004 Posts: 227 Location: The Netherlands
|
|
Posted: Sun May 21, 2006 4:25 am |
|
|
I did't understand it
but this is the display code.
It shows first the left most column (=C1) of each dot matrix display
and then the next column, until of five colums are proceded
Code: |
void max7221_display_message() {
int column,matrix,index;
for(column=DIGIT_0;column<=DIGIT_4;column++) {
index=column-1;
output_low(MAX7221_CS);
for(matrix=DOT_MATRIX_DISPLAY_LEFT;matrix<=DOT_MATRIX_DISPLAY_RIGHT;matrix++) {
spi_write(column);
spi_write(message[index]);
index+=5;
}
output_high(MAX7221_CS);
}
}
|
How can i move now the letter "B" in this example one column to right?
For the movement the array buffer is filled so:
message[0]=0x7f = column C1
message[1]=0x49 = column C2
message[2]=0x49 = column C3
message[3]=0x49 = column C4
message[4]=0x36 = column C5
after the letter "B" is moved one column position to rigth the array buffer must be so to display it correct on the dot matrix displays
message[9]=0x7f = column C5 of next dot matrix display
message[0]=0x49
message[1]=0x49
message[2]=0x49
message[3]=0x36 |
|
|
Ttelmah Guest
|
|
Posted: Sun May 21, 2006 5:27 am |
|
|
Use the 'read_byte' define I posted, where you currently read from the array. Have a variable containing the 'offset' you want in the array. Then if you want to move forward by one column, increment this. If you want to move forward by five columns, add 5 to it.
Imagine the array is like this (a smaller version to save my typing...):
M[0] = 0
M[1] = 1
M[2] = 2
M[3] = 3
M[4] = 4
M[5] = 5
M[6] = 6
M[7] = 7
M[8] = 8
M[9] = 9
and the array is just ten elements long as shown (hence 'sizeof(M]', will return ten).
Now if you have a counter, and access the array, from element 0 to 9, you retrieve 0,1,2.......9
If though you now use the code I show (but modified since I couldn't be bothered to type 'message' ten times!), as:
#define read_byte(x,y) M[(x+y) % sizeof(M))]
and retrieve the values with:
read_byte(counter,offset),
Then if offset=0, the behaviour will be exactly 'normal'. However now add 1 to 'offset'. Read_byte(0,offset), now retrieves element 1 (since 1+0=1). However the key is that when you use read_byte(9,offset), you get element 0!. What happens, is that 9+1=10. The array is ten elements long, and the '%' operator, gives the remainder when you divide by this. 10/10, has a remainder of 0, so you access element 0.
So calling read_byte(counter,offset), with offset = 1, gives :
1,2,3,4,5,6,7,8,9,0
Just as if the array had moved by one address.
If you change offset to 3 (say), you get:
3,4,5,6,7,8,9,0,1,2
Just as if the array had moved by 3.
Effectively, 'offset' gives where in the array you will start reading. So if you move offset forward by the amount you want to move through the array, you will access the array, as if it's start 'point', is offset by this amount. The data never has to move, you just change where you start looking at i!.
Best Wishes |
|
|
tsupuntu
Joined: 05 May 2006 Posts: 9
|
|
Posted: Mon May 22, 2006 3:45 pm |
|
|
tsupuntu wrote: | for (i=0;i<50;i++){
if(i==0) temp=message[i];
if(i<49) message[i]=message[i+1];
if(i==49) message[i]=temp;
} ///// this method save ram because not array temp |
reverse ways
for(i=49;i>=0;i--){
if(i==49)temp=message[i];
if(i<49 && i>0)mess[i]=message[i-1];
if(i==0)message[i]=temp;
} |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|