1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Accessing bits of variable, XC8

Discussion in 'Microcontrollers' started by camacho, Jun 26, 2017.

  1. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,145
    Likes:
    907
    Location:
    Rochdale UK
    All that volatile does, is to let the optimiser skip over the declaration.. This way the compiler lets it stay where you put it... If, for example, you are testing but not actually using the variable, the optimiser will just leave it out!! Very annoying whilst trying to debug!!

    If you are using the free version, then forget the volatile bit as there is no / little optimisation done..
     
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,145
    Likes:
    907
    Location:
    Rochdale UK
    Also... I never use true and false... Just use 0 and 1..
     
  3. TucsonDon

    TucsonDon Member

    Joined:
    Sep 4, 2016
    Messages:
    45
    Likes:
    0
    I tried your suggestion to assign it to an address and the compiler did that but in the main code it still doesn't call the function when the bit is changed. I looked @ the program memory for the "if" statement and it shows MOVF tstatus, W, ACCESS. If I understand this it is looking @ the whole byte and not the bit. I have tried to mask i.e. if (tstatus.a &0x01 ==true)call function ,but it still will not execute that line.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. TucsonDon

    TucsonDon Member

    Joined:
    Sep 4, 2016
    Messages:
    45
    Likes:
    0

    1
     
    Last edited: Jul 25, 2017
  6. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,145
    Likes:
    907
    Location:
    Rochdale UK
    Can I see your code? I can run it through my sim...
     
  7. TucsonDon

    TucsonDon Member

    Joined:
    Sep 4, 2016
    Messages:
    45
    Likes:
    0
    I figured it out. When I declared the bits I was doing

    bool a:0;
    bool b:0;
    ect.....

    so apparently the 0 is not initializing the bit as I thought, as I have change them all to 1's and now it works.
     
    Last edited: Jul 25, 2017
  8. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,145
    Likes:
    907
    Location:
    Rochdale UK
    The whole point of creating a union is to tell the compiler what is where, you can have groups of bits 1 ~ 7
    Code (c):

    union{
       struct{
       unsigned flags:3;
       unsigned over:1;
       unsigned startup:1;
       unsigned mode:2;
       };
    } mystuf;
     
    So any thing that has 7 states can fit into the flags.... mystuf.flags = 6;
     
    • Thanks Thanks x 1
  9. TucsonDon

    TucsonDon Member

    Joined:
    Sep 4, 2016
    Messages:
    45
    Likes:
    0
    Ian Rogers

    Thank you for your assistance and patience with this I have learned a lot.
     

Share This Page