PDA

View Full Version : explanation of step array



Rabm
- 29th September 2006, 01:18
Hi. First post. I'm a relative newbie to PICbasic. I've been intermittantly tinkering with pics since 97, (but only ever got to the stage of flashing LEDs on and off etc with assembly. Now recently switched to PIC basic pro, and enjoying the satisfaction of getting real projects to work. Now Ive started playing with steppers and had a bit of success with making the thing rotate via a 16F84- although a bit jittery- and looking to improve things.

Ive tried to do my homework and figure this out for myself, but no luck.
I'm looking at controlling a stepper from a 16F84, and reading snippets from the net to understand various ways of doing it. ( directly or using a stepper driver like the 5804 chip).
I took the code below from tigoe.net, but don't understand a few things.

1. How exactly does the step array fuction work in the program below?. Does the program automatically step through each step array from [0] to [3]? I don't see any loop function.

2. Whats the [steps //4] bit do. I though this was for division, but why use it here?

Apologies if this is too simple, and doesn't belong on this forum.

Thanks

Regards
Ross


start:
High PORTB.0

' set variables:
x VAR BYTE
steps VAR WORD
stepArray VAR BYTE(4)
clear

TRISD = %11110000
PORTD = 255
input portb.4
Pause 1000

stepArray[0] = %00001010
stepArray[1] = %00000110
stepArray[2] =%00000101
stepArray[3] = %00001001


main:
if portb.4 = 1 then
steps = steps + 1
else
steps = steps - 1
endif

portD = stepArray[steps //4]
pause 2

GoTo main

sayzer
- 29th September 2006, 03:42
Interesting Code.

Here what it does. Say we get from 0 to 20 for example.





Steps Steps //4
0 0 => stepArray[0] = %00001010
1 0 => stepArray[0] = %00001010
2 0 => stepArray[0] = %00001010
3 0 => stepArray[0] = %00001010
4 0 => stepArray[0] = %00001010
5 1 => stepArray[1] = %00000110
6 2 => stepArray[2] = %00000101
7 3 => stepArray[3] = %00001001
8 0 => stepArray[0] = %00001010
9 1 => stepArray[1] = %00000110
10 2 => stepArray[2] = %00000101
11 3 => stepArray[3] = %00001001
12 0 => stepArray[0] = %00001010
13 1 => stepArray[1] = %00000110
14 2 => stepArray[2] = %00000101
15 3 => stepArray[3] = %00001001
16 0 => stepArray[0] = %00001010
17 1 => stepArray[1] = %00000110
18 2 => stepArray[2] = %00000101
19 3 => stepArray[3] = %00001001
20 0 => stepArray[0] = %00001010
.
..
...

So that array index will take its corresponding port arrangement and drive PORTD with that arrangement.


Steps//4 is the remainder after divided by 4.

SteveB
- 29th September 2006, 04:09
Just a small correction to the first 4 items ;)


Steps Steps //4 => Steps MOD 4

0 0 => stepArray[0] = %00001010
1 1 => stepArray[1] = %00000110
2 2 => stepArray[2] = %00000101
3 3 => stepArray[3] = %00001001
4 0 => stepArray[0] = %00001010
5 1 => stepArray[1] = %00000110
6 2 => stepArray[2] = %00000101
7 3 => stepArray[3] = %00001001
8 0 => stepArray[0] = %00001010
9 1 => stepArray[1] = %00000110
10 2 => stepArray[2] = %00000101
11 3 => stepArray[3] = %00001001
12 0 => stepArray[0] = %00001010
13 1 => stepArray[1] = %00000110
14 2 => stepArray[2] = %00000101
15 3 => stepArray[3] = %00001001
16 0 => stepArray[0] = %00001010
17 1 => stepArray[1] = %00000110
18 2 => stepArray[2] = %00000101
19 3 => stepArray[3] = %00001001
20 0 => stepArray[0] = %00001010
.
..
...


Steve

Rabm
- 29th September 2006, 18:00
Guys.

Thanks for the explanation. Its finally clicked, and makes sense.
That //4 stuff is rather nifty

I appreciate your reply.
Ross