How to use bound on a byte

Discuss PIC and electronic related things

Moderators: David Barker, Jerry Messina

Post Reply
be80be
Registered User
Registered User
Posts: 90
Joined: Mon Feb 23, 2009 2:15 am
Location: tn

How to use bound on a byte

Post by be80be » Sat Jan 12, 2013 7:01 am

How do you use bound on a byte
Like

Code: Select all

dim data as byte
dim X as byte
    for X = 0 to bound (data)
         portb.0 = data(bit)
   next


Jerry Messina
Swordfish Developer
Posts: 1471
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Post by Jerry Messina » Sat Jan 12, 2013 2:22 pm

bound isn't for use with bytes, it's for use with arrays. It's a function that returns the max upper index of an array.

Say you have:

dim array(4) as word

bound(array) will return '3' since that's the max index of a four element array (0-3)

Check out the users manual pdf page 10 for a good decription and some examples.

be80be
Registered User
Registered User
Posts: 90
Joined: Mon Feb 23, 2009 2:15 am
Location: tn

Post by be80be » Sat Jan 12, 2013 2:29 pm

I no what bound is for thanks thats not what i'm asking a byte is 8 bits.

So how do you use it to get data.0 ,data.1, data.2 the 8 bit's of data

I did it before but I can't remember how I did it. I want to return the bit's

dim data(8) as bit
Last edited by be80be on Sat Jan 12, 2013 2:41 pm, edited 1 time in total.

Jerry Messina
Swordfish Developer
Posts: 1471
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Post by Jerry Messina » Sat Jan 12, 2013 2:38 pm

Original question:
How do you use bound on a byte

Answer:
Same as before

Are you asking how to get each of the bits in a byte using a for...next loop?

be80be
Registered User
Registered User
Posts: 90
Joined: Mon Feb 23, 2009 2:15 am
Location: tn

Post by be80be » Sat Jan 12, 2013 2:44 pm

A byte is a bit array right or wrong?

Jerry Messina
Swordfish Developer
Posts: 1471
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Post by Jerry Messina » Sat Jan 12, 2013 2:51 pm

I see what you're saying. Sorry, I didn't understand what you were looking for.

Check out the sample file in the install dir - samples\blink\blink-2.bas

Code: Select all

{
 An incredibly inefficient way to blink an led. However
 the program does show some interesting capabilities. For
 example, passing a PORT 'bit' by reference and also
 accessing a PORT as a bit array - every byte, word etc
 in a program can be treated as a bit array by default
}

// if device and clock are omitted, then the compiler defaults to 
// 18F452 @ 20MHz - they are just used here for clarity...
device = 18F452
clock = 20

// blink an LED, PORT bit is passed by
// reference to the subroutine...
sub Blink(byref pBit as bit, pDelay as byte = 50)
   high (pBit)
   delayms (pDelay)
   low (pBit)
   delayms (pDelay)
end sub

// loop counters...
dim Index as byte

// main program...
while true
   for Index = 0 to bound(PORTD.Bits)
      Blink(PORTD.Bits(Index))
   next
wend 
Is that what you were after?

be80be
Registered User
Registered User
Posts: 90
Joined: Mon Feb 23, 2009 2:15 am
Location: tn

Post by be80be » Sat Jan 12, 2013 2:55 pm

Yes that's it Thanks

Post Reply