Bits to Word?


Closed Thread
Results 1 to 7 of 7

Thread: Bits to Word?

  1. #1
    Qacer's Avatar
    Qacer Guest

    Question Bits to Word?

    Hello again all,

    Thanks very much to those who have helped me in the past! I appreciate it a lot.

    I wanted to double check here. I am reading a couple of bytes from an external device. Each bit in the byte represents a configuration code. I wanted to get bit 5 to bit 0 of one byte, also get all 7 bits of another byte, and then combine them all into a 14 bit word.

    I had the following in mind:

    Code:
    databyte1 VAR byte     'This variable contains bit 5 to bit 0
    databyte2 VAR byte     'This variable contains all 7 bits of the config
    wordtoyou VAR word
    
    [..assuming that databyte1 and databyte2 already contain the read values..]
    
    databyte1 = databyte1 & %00111111
    wordtoyou.byte1 = databyte1
    wordtoyou.byte0 = databyte2
    
    write 0, wordtoyou.byte0
    wrte 1, wordtoyou.byte1
    Does this look okay?

    Thanks!

  2. #2
    Join Date
    Oct 2004
    Location
    Hangover, Germany
    Posts
    289


    Did you find this post helpful? Yes | No

    Default

    In the past I used .lowbyte and .highbyte instead of .byte0 and .byte1.
    What does the compiler do ?
    PBP 2.50C, MCS+ 3.0.0.5, MPLAB 8, MPASM 5.14, ASIX Presto, PoScope, mE mikroBasic V7.2, PICKIT2

  3. #3
    Qacer's Avatar
    Qacer Guest


    Did you find this post helpful? Yes | No

    Question

    It actually compiled without any problems. But now I think I have another question. If I had a byte that represents different config bits, how can I set each bit individually?

    If I had variable set as testbit VAR databyte1.0, will bit0 of databyte1 change if I set testbit to another value?

    Errr.... how would I also set multiple bits in a byte? Say, I want to set bit5 to bit0 to a certain value.

    Thanks!
    Last edited by Qacer; - 10th March 2006 at 21:17.

  4. #4
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    I'M BACK!!!

    Sorry to be impolite and a pest but why don't simply test it and send results to a LCD or via Serial communication to your PC to see what's happen with?
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  5. #5
    Qacer's Avatar
    Qacer Guest


    Did you find this post helpful? Yes | No

    Question

    Hi mister_e,

    :-) Sorry, I was feeling a bit lazy. Plus, I don't have the most efficient computer setup in the world. Anyway, this actually the question that I can't figure out: how would I set multiple bits in a byte simultaneously? Say, I want to set bit5 to bit0 to a certain value.

    I was hoping there was something like this:

    databyte1.[5 to 0] = %101011

  6. #6
    Join Date
    Aug 2004
    Posts
    64


    Did you find this post helpful? Yes | No

    Default handling bits

    Hi:
    You can create aliases for each configuration bit. Ex:
    name1 var byte1.0
    name2 var byte1.2 etc.
    then, you can clear or set each one by

    name1 = 0, or name1 = 1

    Or you can test them:
    if name1 = 1 then do_something
    I hope this helps.
    Greetings...
    Ruben de la Pena

  7. #7


    Did you find this post helpful? Yes | No

    Default individual bit setting/clearing

    To set bit 4 on and leave the rest unchanged:

    mybyte=mybyte | %00010000 'OR function with selected bit masked ON

    To turn bit 4 off and leave the rest unchanged:

    mybyte=mybyte & %11101111 'AND function with selected bit masked OFF

    OR you can set multiple bits on or off by either ADDING or SUBTRACTING the bit weight to/from either 0 or 255 respectively.

    ------------------Picster-----------------

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. Minimizing code space
    By Tobias in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 30th May 2009, 07:25
  3. DS2760 Thermocouple Kit from Parallax in PicBasicPro
    By seanharmon in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 8th July 2008, 23:19
  4. calculation problem
    By nicolelawsc in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 31st March 2006, 15:23
  5. error on compiling
    By parker in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 16th June 2005, 14:31

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