Looping through MyVar.n - will this work?


Closed Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2012
    Posts
    2

    Default Looping through MyVar.n - will this work?

    Hello all.

    I dug around a bit and couldn't find a solid answer. I'm hoping this is a quick answer from someone.

    Can I loop through the bit position in a variable by using another variable to represent the bit position?

    For example,

    I know I can do this:


    MyVar.0 = 1
    MyVar.1 = 1
    MyVar.2 = 0 ... etc



    I want do this:


    n=0

    MyVar.n = 1

    ... later in the loop, n is 5, so

    MyVar.n = 0

    etc.


    I've compiled it and PBP doesn't seem to like it.

    Much appreciated.

  2. #2
    Join Date
    Jan 2012
    Location
    Grid EN19MV
    Posts
    159


    Did you find this post helpful? Yes | No

    Default Re: Looping through MyVar.n - will this work?

    Quote Originally Posted by kjavrd View Post
    Hello all.

    I dug around a bit and couldn't find a solid answer. I'm hoping this is a quick answer from someone.

    Can I loop through the bit position in a variable by using another variable to represent the bit position?

    For example,

    I know I can do this:


    MyVar.0 = 1
    MyVar.1 = 1
    MyVar.2 = 0 ... etc



    I want do this:


    n=0

    MyVar.n = 1

    ... later in the loop, n is 5, so

    MyVar.n = 0

    etc.


    I've compiled it and PBP doesn't seem to like it.

    Much appreciated.
    I had much the same question the other day - only with an I/O port. I think this will do it:

    Code:
    MyVar.0[n] = 0
    As always, I stand to be corrected

  3. #3
    Join Date
    Nov 2012
    Posts
    2


    Did you find this post helpful? Yes | No

    Default Re: Looping through MyVar.n - will this work?

    I found the answer, as usual thanks to Melanie.

    http://www.picbasic.co.uk/forum/show...=2017#post2017

    she says.....


    1. How can I reference a BIT in a BYTE?

    We know that MyByte.0 references Bit 0 of MyByte, all the way up to MyByte.7 referencing Bit 7 of MyByte… but there’s another way of accessing the bits in a Byte (MyVar is a BYTE variable)…

    MyByte.0(MyVar)

    When MyVar is in the range 0-7, it will access Bits 0-7 of MyByte.

    Example to display all eight BITS of a BYTE sequentially, Bit at a time…

    For MyVar=0 to 7
    MyBit=MyByte.0(MyVar)
    LCDOut $FE,1,"Bit ",#MyVar,"=",#MyBit
    Pause 2000
    Next MyVar



    Thanks. :-)

Similar Threads

  1. Cant get it to work
    By bearpawz in forum mel PIC BASIC Pro
    Replies: 23
    Last Post: - 9th April 2010, 09:21
  2. SerIn Looping problem ???
    By gtvmarty in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 24th September 2009, 00:31
  3. Looping Variable Assignments
    By jcb344 in forum General
    Replies: 3
    Last Post: - 5th July 2008, 09:50
  4. looping / counter
    By tech in forum General
    Replies: 3
    Last Post: - 18th May 2008, 23:31
  5. looping
    By lerameur in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 11th February 2007, 14:41

Members who have read this thread : 1

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