explanation of step array


Closed Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2006
    Posts
    2

    Default explanation of step array

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Interesting Code.

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



    Code:
    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.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  3. #3
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343


    Did you find this post helpful? Yes | No

    Default

    Just a small correction to the first 4 items
    Code:
    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

  4. #4
    Join Date
    Jul 2006
    Posts
    2


    Did you find this post helpful? Yes | No

    Default

    Guys.

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

    I appreciate your reply.
    Ross

Similar Threads

  1. Bits, Bytes Words and Arrays
    By Melanie in forum FAQ - Frequently Asked Questions
    Replies: 24
    Last Post: - 14th June 2016, 07:55
  2. Simple Array Demo
    By Archangel in forum Code Examples
    Replies: 5
    Last Post: - 15th February 2010, 04:46
  3. WRITECODE stores wrong 14-bit word values in FlashMEM
    By BobPigford in forum mel PIC BASIC Pro
    Replies: 18
    Last Post: - 26th June 2009, 04:35
  4. Old and beyond help ?
    By DavidFMarks in forum mel PIC BASIC Pro
    Replies: 46
    Last Post: - 11th December 2008, 15:23
  5. RS232 receive an array (packet)
    By ELCouz in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 12th February 2008, 05:02

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts