0% found this document useful (0 votes)
118 views

AVR Microcontroller Training Bit Manipulation: Mensun Lakhemaru

This document discusses bit manipulation techniques for AVR microcontrollers. It introduces common bitwise operators like OR, AND, NOT, XOR, left and right shift. It shows examples of how to set, clear, and flip individual bits using these operators. Macros are defined for SETBIT, CLEARBIT, FLIPBIT, and CHECKBIT to make bit manipulation cleaner. The document is intended to teach bit manipulation, which is important for programming microcontrollers at the bit level.

Uploaded by

Amir Shahzad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views

AVR Microcontroller Training Bit Manipulation: Mensun Lakhemaru

This document discusses bit manipulation techniques for AVR microcontrollers. It introduces common bitwise operators like OR, AND, NOT, XOR, left and right shift. It shows examples of how to set, clear, and flip individual bits using these operators. Macros are defined for SETBIT, CLEARBIT, FLIPBIT, and CHECKBIT to make bit manipulation cleaner. The document is intended to teach bit manipulation, which is important for programming microcontrollers at the bit level.

Uploaded by

Amir Shahzad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

AVR Microcontroller Training

Bit Manipulation
Mensun Lakhemaru

Bit Manipulation


Working in bit level, mcu is 8 bit

C language bit operators


|
bit OR
& bit AND
~ bit NOT
^ bit EXLUSIVE OR (XOR)
<< bit LEFT SHIFT
>> bit RIGHT SHIFT

Mensun Lakhemaru

8/7/2012

Bit Manipulation


byteX = byteX | 0x01; // setting bit 0

byteX = byteX & ~0x01; clearing bit 0

byteX = byteX ^ 0x01 // flipping the bit

Mensun Lakhemaru

8/7/2012

Bit Manipulation


if(byteX & 0x80) condn will be zero if the bit is clear, and
the
condn will be non-zero if the bit is set.
{
}

if(~(byteX) & 0x80)


{
}
bit_is_set(byteX, bitPos)
bit_is_clear(byteX, bitPos)




Mensun Lakhemaru

8/7/2012

Bit Mannipulation


(0x01 << 2) (for Bit Masking)

_BV(bitPosition)

Mensun Lakhemaru

8/7/2012

MACRO definitions


#define SETBIT (x,y) (x |= _BV(y))

#define CLEARBIT (x,y) (x &= ~_BV(y))

#define FLIPBIT (x,y) (x ^= _BV(y))

#define CHECKBIT (x,y) (x & _BV(y))

Mensun Lakhemaru

8/7/2012

You might also like