This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Bits to Hex?

Hi all,

I need a little assistance wrapping my head around this issue. It's not strictly an arm/cortex problem, so everyone feel free to chime in! Anyways, to the issue...

I am receiving serial data at one of the pins on my microcontroller. So for example, at PA0 (port A, pin 0), I might get a feed of 010010000. What is the best way to accept this data/modify it so that I can transmit it through UART.

My current method was to create an 8 array element, and then saving each value. But I don't think that would work because an 8 array element does not necessarily mean 8 bits, which is what my UART is setup to transmit... I'm not sure what the best way would be to handle this problem. Any help would be appreciated, thanks!

Parents
  • You can use a "bit array":

    #define BITMASK(b)      (1 << ((b) % CHAR_BIT))
    #define BITSLOT(b)      ((b) / CHAR_BIT)
    #define BITSET(a, b)    ((a)[BITSLOT(b)] |= BITMASK(b))
    #define BITCLEAR(a, b)  ((a)[BITSLOT(b)] &= ~BITMASK(b))
    #define BITTEST(a, b)   ((a)[BITSLOT(b)] & BITMASK(b))
    #define BITNSLOTS(nb)   ((nb + CHAR_BIT - 1) / CHAR_BIT)
    

    and

    // bit array to indicate, per block, whether it is defective of not (either established by factory data, runtime
    // verification or NAND flash operation failing
    int8u        g_nand_flash_defective_physical_blocks[BITNSLOTS(NANDFLASH_BLOCKNUM)] ;
    .
    .
    .
    BITSET(g_nand_flash_defective_physical_blocks, l_physical_block ) ;
    .
    .
    .
    if ( (BITTEST(g_nand_flash_defective_physical_blocks, l_physical_block) == 0) &&
                     (BITTEST(g_nand_flash_allocated_physical_blocks, l_physical_block) == 0) )
    .
    .
    .
    

Reply
  • You can use a "bit array":

    #define BITMASK(b)      (1 << ((b) % CHAR_BIT))
    #define BITSLOT(b)      ((b) / CHAR_BIT)
    #define BITSET(a, b)    ((a)[BITSLOT(b)] |= BITMASK(b))
    #define BITCLEAR(a, b)  ((a)[BITSLOT(b)] &= ~BITMASK(b))
    #define BITTEST(a, b)   ((a)[BITSLOT(b)] & BITMASK(b))
    #define BITNSLOTS(nb)   ((nb + CHAR_BIT - 1) / CHAR_BIT)
    

    and

    // bit array to indicate, per block, whether it is defective of not (either established by factory data, runtime
    // verification or NAND flash operation failing
    int8u        g_nand_flash_defective_physical_blocks[BITNSLOTS(NANDFLASH_BLOCKNUM)] ;
    .
    .
    .
    BITSET(g_nand_flash_defective_physical_blocks, l_physical_block ) ;
    .
    .
    .
    if ( (BITTEST(g_nand_flash_defective_physical_blocks, l_physical_block) == 0) &&
                     (BITTEST(g_nand_flash_allocated_physical_blocks, l_physical_block) == 0) )
    .
    .
    .
    

Children