Can i access single bits?(C)

Discussion in 'Embedded Systems and Microcontrollers' started by markosloizou, Jan 6, 2012.

  1. markosloizou

    Thread Starter New Member

    Jan 6, 2012
    3
    0
    I'm new to microcontrollers and i was wondering if i could access single bits instead of the whole register.Im programming microcontrollers using C. For example to blink a led, i have to set the bit to output and then to high by using something like TRISA = 0; PORTA = 0010;(pic16f84a) to set the pin high. Is there a way to access that bit only without having to write the binary or hex number for the whole register?
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,388
    1,605
    Yes you can. Go look for the pic16f84a.h file (it's in the program files area for the C18 compiler). It gives you definitions for all the symbols for the registers.

    The individual bits of PORTA can be accessed like so:

    PORTAbits.RA0 = 1;
    PORTAbits.RA1 = 1;
    ...
    PORTAbits.RA7 = 1;

    The definitions attempt to follow the device names given in the data sheet.
     
  3. DerStrom8

    Well-Known Member

    Feb 20, 2011
    2,428
    1,328
    I think that's only for C18, Ernie. I think it's different for other compilers.

    By the way, the OP didn't mention what compiler he's using. markosloizou, are you using Hi-Tech C or another compiler?
     
  4. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    Many C compilers for micro-controllers have extensions to the C language that support bit operations but as it is not part of the C standard each compiler vendor does it their own way. Check your compiler documentation.

    Failing that you can use the following set of macros to perform bit operations. Just add the macros to the start of your source code or to you header file

    Code ( (Unknown Language)):
    1.  
    2. #define set_bit(ADDRESS,BIT) (ADDRESS |= (1<<BIT))
    3. #define clear_bit(ADDRESS,BIT) (ADDRESS &= ~(1<<BIT))
    4. #define toggle_bit(ADDRESS,BIT) (ADDRESS ^= (1<<BIT))
    5. #define test_bit(ADDRESS,BIT) (ADDRESS & (1<<BIT))
    6.  
    7. // The example below demonstrates typical use
    8.  
    9. if(test_bit(portb, 3)) //if bit 3 of port b is true
    10. {
    11.     set_bit(porta, 4); //set bit 4 of port a to 1
    12. }
    13. else
    14. {
    15.     clear_bit(porta, 4); //set bit 4 of port a to 0
    16. }
    17.  
     
  5. markosloizou

    Thread Starter New Member

    Jan 6, 2012
    3
    0
    I'm currently using mikroC pro for PIC compiler(demo version)
     
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,388
    1,605
    OK, Mikro C does things differently. Their C silently included a C file into your project that defines the special needs of a particular PIC. Look thru the Microelectronica area for the defs folder and find your P16F84A.c file. That shows you all the special definitions for bit access and such.

    The individual bits of PORTA can be accessed like so:

    PORTA.B0 = 1;
    PORTA.B6 = 1;
    ...
    PORTA.B7 = 1;

    Note B0 to B7 is used for every register unlike the above C18 example.
     
    markosloizou likes this.
  7. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    MikroC also accepts F instead of B;

    PORTA.F0
    PORTA.F1 (etc)

    I see B used in some of the MikroC earlier examples but F seems to be more popular now.
     
  8. markosloizou

    Thread Starter New Member

    Jan 6, 2012
    3
    0
    I don't really care which is more popular :p

    but since both work i'm happy. Thanks for the reply btw
     
  9. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,012
    Why F????

    B= Bit I would think. But F?
     
  10. PaulEE

    Member

    Dec 23, 2011
    423
    32
    Also, I work with MikroC daily. If you have any other questions, do start a thread and PM me about it. MikroC is a great little compiler; very user friendly.
     
    markosloizou likes this.
  11. DerStrom8

    Well-Known Member

    Feb 20, 2011
    2,428
    1,328
    Generally it is preferred that you do not ask technical questions through PMs. A new thread would be best.
     
  12. cebrax

    Member

    Jan 30, 2010
    26
    0
    I think he didn't mean that.
    He wanted the question starter person to PM him that (s)he started a new question.
     
Loading...