Continue to Site

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.

  • 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.

C Bit Access

Status
Not open for further replies.

Suraj143

Active Member
I have a variable called temp1.I need to check its bits individually.

The below code works for SFR.
Code:
if(PORTB.B0==1){.....}

But it won't work for GP registers.It gives an error "Operator '.' is not applicable to these operands 'temp1'"

Code:
if(temp1.B0==1){.....}
 
Last edited:
The generic way to check a bit is with a bitwise and.

Code:
if(temp1&0x01){.....}
or in binary
if(temp1&0b00000001){.....}

Some embedded c compilers will let you do a struct with bit fields. The definitions in the C18 processor header files are an example of that.

Code:
extern          near struct {
  unsigned C:1;
  unsigned DC:1;
  unsigned Z:1;
  unsigned OV:1;
  unsigned N:1;
} STATUSbits;

But I if you stick with the method above it will work on all c compilers.
 
Last edited:
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top