Bitwise operations and masks: basics, help?


Closed Thread
Results 1 to 12 of 12

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default

    Why not a WORD array instead? Anyways, yes it's clear enough. I'll do something a little bit later.
    Steve

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

  2. #2
    xnihilo's Avatar
    xnihilo Guest


    Did you find this post helpful? Yes | No

    Smile

    Quote Originally Posted by mister_e View Post
    Why not a WORD array instead? Anyways, yes it's clear enough. I'll do something a little bit later.
    Yes, I may use a WORD array instead... Anyway, it wont be much different I guess. The principle of bit manipulations will remain the same I beleive...

    Anyway, thank you very much for your help.

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


    Did you find this post helpful? Yes | No

    Default

    Code:
            '
            '   Tested on 16F877
            '   ----------------
    @       __CONFIG _XT_OSC & _LVP_OFF & _WDT_OFF
    
            TRISA=0
            TRISB=0
            TRISC=0
            TRISD=0
            
            
            Aval VAR byte [10]
            Cval VAR BYTE [10]
            CounterA VAR BYTE
            CounterB VAR BYTE
            
            PORTA=0
            PORTB=0
            PORTC=0
            PORTD=0
            ADCON1=7
            
            AVAL[0]=%00000000
            AVAL[1]=%00000010
            AVAL[2]=%00000100
            AVAL[3]=%00001010
            AVAL[4]=%00010000
            AVAL[5]=%00100010
            AVAL[6]=%01000100
            AVAL[7]=%10000010
            AVAL[8]=%10101001
            AVAL[9]=%11111111
    
            CVAL[0]=%00000000
            Cval[1]=%00000001
            CVAL[2]=%00000010
            CVAL[3]=%00000100
            CVAL[4]=%00001000
            CVAL[5]=%00010000
            CVAL[6]=%00101000
            CVAL[7]=%01000100
            CVAL[8]=%10000010
            CVAL[9]=%11100001
    
    Start:
            for CounterB = 0 to 255
                PORTA=CounterB
                PORTB=COUNTERB
                PORTC=CounterB
                PORTD=COUNTERB
                fOR CounterA = 0 TO 9
                    '
                    '   this should be the section you need
                    PORTA = (PORTA & %11111001) | (AVAL[COUNTERA] & %00000110)
                    PORTC = (PORTC & %11100000) | (CvAL[CounterA] & %00011111)
                    '
                    '
                    PAUSE 250
                    NEXT        
                NEXT
            STOP
    PS: As you only use 7 bit... you could also us ONLY 1 byte array instead of 2.
    Steve

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

  4. #4


    Did you find this post helpful? Yes | No

    Default

    What I would do is:
    Code:
    PORTC = PORTC & %11100000   / Clear all the bits in PORTC that are segments
    PORTC = AVAL[x] | PORTC        / sets the bits in PORTC that are '1' in AVAL[x]
    
    PORTA = PORTA & %11111001  /  Clear all the bits in PORTA that are segments
    PORTA = CVAL[x] | PORTA       / sets the bits in PORTA that are '1' in CVAL[x]
    if you just want to use one matrix for segment data you can do this:
    Code:
    ORMASK = SEGVAL[x] & %00011111 / makes sure only 5 lsb of SEGVAL[x] '1' bits get set below
    PORTC = PORTC & %11100000    / Clear all the bits in PORTC that are segments 
    PORTC = ORMASK | PORTC         / sets the bits in PORTC that are '1' in AVAL[x]
    
    ORMASK = SEGVAL[x] >> 4         / shift data 4 to right to position correct SEGVAL[x] bits for PORTA
    ORMASK = ORMASK & %0000110 / makes sure only 2 bits of SEGVAL[x] '1' bits get set below
    PORTA = PORTA | %11111001    / Clear all the bits in PORTA that are segments
    PORTA = ORMASK | PORTA        / sets the bits in PORTC that are '1' in AVAL[x]
    Last edited by falingtrea; - 18th April 2008 at 20:52. Reason: fixed logic
    Tim Barr

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


    Did you find this post helpful? Yes | No

    Default

    Yup, mine is just in case something is badly define in the array, unless

    PORTA = (PORTA & %11111001) | AVAL[COUNTERA]
    PORTC = (PORTC & %11100000) | CvAL[CounterA]

    Would work anyways.
    Steve

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

  6. #6
    xnihilo's Avatar
    xnihilo Guest


    Did you find this post helpful? Yes | No

    Smile

    Hello,

    Thank you for this information.
    Bitwise operations are less simple than I thought but are necessary

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


    Did you find this post helpful? Yes | No

    Default

    Nah not that complicated.. but once you get them they're pretty handy.
    http://en.wikipedia.org/wiki/Bitwise_operation
    Steve

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

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