You are on page 1of 6

11/13/2015 AVR C Programming Basics | AVR Tutorial

m ic ah  c ar ric k Django, Python, GTK+, PHP, and a pinch of salt.

4 Tweet 4 Like 53 Aug 14, 2012


Hello, my name is
Micah and I write
AVR Tutorial software. I love
Python, GTK+,
A Hobbyist's Guide to Hacking 8­Bit AVR Microcontrollers
Linux, and robots.
< Getting Started: Blinking an LED Table of Contents

Email   Facebook   Twitter

Github   RSS Feed

AVR C Programming Basics LinkedIn   Stack Overflow

In this chapter you will explore some of the key concepts of C programming for AVR
microcontrollers. Need some work done? Hire
me!
3.1   AVR Registers

3.2   Bits and Bytes Categories
3.3   Bitwise Operations Android Programming
3.4   Clearing and Setting Bits Django

3.5   The Bit Value Macro _BV() Game Programming

GNOME

3.1  AVR Registers GTK+ Programming

Linux
A register is a special storage space in which the state of the bits in the register have some
special meaning to the AVR microcontroller. Most of the registers are 8­bits wide (there are a few Python Programming
exceptions). Robotics & Electronics

Each register has a name and individual bits may also have unique names. All of the register and Web Development
bit names are described in the ATmega48/88/168/328 datasheet. The AVR Libc library will define
identifiers for the register names and bit names so that they can be easily accessed in C.

Manipulating the bits in the various registers of the AVR is the basis for AVR programming. Tutorials
Everything from configuring the AVR device's built­in peripherals to using the AVR's pins for digital
Autotools Tutorial for Python and
I/O is done by manipulating bits in these registers. GTK+

AVR Tutorial
3.2  Bits and Bytes GTK+ 2 / Glade Tutorial

A bit represents one of two possible states: 1 or 0 (aka: on/off, set/clear, high/low). Several bits
together represent numerical values in binary, where each bit is one binary digit. An AVR
microcontroller groups 8 bits together to form one byte wtih the Least Significant Bit (LSB) on the Recent Posts
right. Each bit is numbered, starting from 0, at the LSB.
No Package Error with Jhbuild in
Consider the decimal number 15 for example. 15 represented in 8­bit binary is 00001111. The 4 least Ubuntu 12.10 64
significant bits 0, 1, 2, and 3, are set. Show git Branch in Shell Prompt

JSON Validation and Formatting in
Gedit

Raspberry Pi WiFi
Figure 3.1 ­ The decimal number 15 represented in 8 bits Django 1.5, Python 3.3, and Virtual
Environments
If you do not already know how to convert numbers between decimal, binary, and hexadecimal, you can Django Contact Form with
find numerous lessons online or use my Printable Decimal­Binary­Hex Conversion Chart reCAPTCHA

Notes on Serving Django Apps with
Another example, the decimal number 40 is represented in binary as 00101000. Bits 3 and 5 are set. uWSGI

Getting an IP Address in Django
behind an Nginx Proxy

SSL behind an Nginx Reverse
Proxy in Django 1.4
http://www.micahcarrick.com/tutorials/avr­microcontroller­tutorial/avr­c­programming.html 1/6
11/13/2015 AVR C Programming Basics | AVR Tutorial
Proxy in Django 1.4

Using libgdx without Eclipse

Figure 3.2 ­ The decimal number 40 represented in 8 bits

Numerical values in a C program for an AVR microcontroller may be defined using a decimal,
hexadecimal, or binary notation depending on the context and the programmer's preference. A
hexadecmial number is defined using the 0x prefix and a binary number is defined using the 0b
prefix.

The following C code shows 3 ways in which a variable might be initialized to the decimal value of
15.

uint8_t a = 15;         /* decimal */
uint8_t b = 0x0F;       /* hexidecimal */
uint8_t c = 0b00001111; /* binary */

The uint8_t data type is one of the fixed width integer types from the C99 standard. It defines an 8­bit
unsigned integer. The C99 style data types will be used throughout this tutorial series.

3.3  Bitwise Operations
Since individual bits often have a significant meaning when programming AVR microcontrollers,
bitwise operations are very important.

A bitwise AND operation results in bits being set only if the same bits are set in both of the
operands. In other words: bit n will be set in the result if bit n is set in the first operand and the
second operand.

Figure 3.3 ­ Bitwise AND operation

A single ampersand character (&) is the bitwise AND operator in C.

uint8_t a = 0xAA; /* 10101010 */
uint8_t b = 0x0F; /* 00001111 */
uint8_t = a & b;  /* 00001010 */

A bitwise OR operation results in bits being set if the same bits are set in either of the operands.
In other words: bit n will be set in the result if bit n is set in the first operand or the second operand.

Figure 3.4 ­ Bitwise OR operation

A single pipe character (|) is the bitwise OR operator in C.

uint8_t a = 0xAA; /* 10101010 */
uint8_t b = 0x0F; /* 00001111 */
uint8_t = a | b;  /* 10101111 */

http://www.micahcarrick.com/tutorials/avr­microcontroller­tutorial/avr­c­programming.html 2/6
11/13/2015 AVR C Programming Basics | AVR Tutorial
A bitwise XOR operation ("exclusive or") results in bits being set if, and only if, the same bit is set
in one of the operands but not the other. In other words: bit n will be set in the result if bit n is
exclusively set in only one of the operands.

Figure 3.5 ­ Bitwise XOR operation

The caret character (^) is the bitwise XOR operator in C.

uint8_t a = 0xAA; /* 10101010 */
uint8_t b = 0x0F; /* 00001111 */
uint8_t = a ^ b;  /* 10100101 */

A NOT operation, also known as a one's complement, is a unary operation. That means the
operation is performed on a single value instead of two. The NOT operation will simply negate
each bit. Every 1 becomes 0 and every 0 becomes 1.

Figure 3.6 ­ Bitwise NOT operation

A tilde character (~) is the NOT operator in C.

uint8_t a = 0xAA; /* 10101010 */
uint8_t b = ~a;   /* 01010101 */

A shift operation shifts all of the bits to the left or the right. In a left shift, bits get "shifted out" on
the left and 0 bits get "shifted in" on the right. The opposite goes for a right shift.

Figure 3.7 ­ Bitwise shift operation

Two less­than symbols (<<) is the left shift operator and two greater­than symbols (>>) is the right
shift operator in C. The right side of the operator is the number of bits to shift.

uint8_t a = 0x99; /* 10011001 */
uint8_t b = a<<1; /* 00110010 */
uint8_t c = a>>3; /* 00010011 */

3.4  Clearing and Setting Bits
http://www.micahcarrick.com/tutorials/avr­microcontroller­tutorial/avr­c­programming.html 3/6
11/13/2015 AVR C Programming Basics | AVR Tutorial
3.4  Clearing and Setting Bits
Setting and clearing a single bit, without changing any other bits, is a common task in AVR
microcontroller programming. You will use these techniques over and over again.

When manipulating a single bit, it is often necessary to have a byte value in which only the bit of
interest is set. This byte can then be used with bitwise operations to manipulate that one bit. Let's
call this a bit value mask. For example, the bit value mask for bit 2 would be 00000100 and the bit
value mask for bit 6 would be 01000000.

Since the number 1 is represented in binary with only bit 0 set, you can get the bit value mask for a
given bit by left shifting 1 by the bit number of interest. For example, to get the bit value mask for
bit 2, left shift 1 by 2.

Figure 3.7 ­ Bit value mask

To set a bit in C, OR the value with the bit value mask.

uint8_t a = 0x08; /* 00001000 */
                  /* set bit 2 */
a |= (1<<2);      /* 00001100 */

Use multiple OR operators to set multiple bits.

uint8_t a = 0x08;   /* 00001000 */
                    /* set bits 1 and 2 */
a |= (1<<2)|(1<<1); /* 00001110 */

To clear a bit in C, NOT the bit value mask so that the bit of interest is the only bit cleared, and
then AND that with the value.

uint8_t a = 0x0F; /* 00001111 */
                  /* clear bit 2 */
a &= ~(1<<2);     /* 00001011 */

Use multiple OR operators to clear multiple bits.

uint8_t a = 0x0F;      /* 00001111 */
                       /* clear bit 1 and 2 */
a &= ~((1<<2)|(1<<1)); /* 00001001 */

To toggle a bit in C, XOR the value with the bit value mask.

uint8_t a = 0x0F; /* 00001111 */
                  /* toggle bit 2 */
a ^= (1<<2);      /* 00001011 */
a ^= (1<<2);      /* 00001111 */

3.5  The Bit Value Macro _BV()
AVR Libc defines a the _BV() macro which stands for "bit value". It is a convenience macro to get
the bit value mask for a given bit number. The idea is to make the code a little more readable over
using a bitwise left shift. Using _BV(n) is functionally equivelent to using (1<<n).

/* set bit 0 using _BV() */
a |= _BV(0);

/* set bit 0 using shift */
a |= (1<<0);

Which method you choose is entirely up to you. You will see both in wide use and should be
comfortable seeing either. Since the _BV(n) macro is unique to gcc, it is not as portable to other
http://www.micahcarrick.com/tutorials/avr­microcontroller­tutorial/avr­c­programming.html 4/6
11/13/2015 AVR C Programming Basics | AVR Tutorial
compilers as the (1<<n) method. However, that is not usually a concern for a hobbyist and the
_BV(n) is often more comfortable for a beginner.

Did you enjoy AVR C Programming Basics? If you would like to help support my work, A
donation of a buck or two would be very much appreciated.

21 Comments Micah Carrick 
1  Login

  Recommend  13 ⤤ Share Sort by Newest

Join the discussion…

yaswanth raj  •  a month ago
thank you. great and easy tutorial
△  ▽ • Reply • Share › 

Jakalashe  •  a year ago
Good tutorial indeed,,for the very basics
1 △   ▽ • Reply • Share › 

Guy  •  a year ago
Thank you. Very good article.
1 △   ▽ • Reply • Share › 

Bojan  •  a year ago
Thank you so much, keep up the good work!
△  ▽ • Reply • Share › 

Rakesh Rakeshmenon  •  2 years ago
a &= ~(1<<2); instead of clearing bits like this, why can't we simply do: a I= (0<<2);??, I have seen
many people using like this ..
△  ▽ • Reply • Share › 

Guest > Rakesh Rakeshmenon  •  a year ago
you used OR that's why (1 OR 0 = 1) so it wont clear the bit. Try it on your calculator.
△  ▽ • Reply • Share › 

Badri M > Guest  •  10 months ago
a&=(0<<2); is it correct
△  ▽ • Reply • Share › 

davor > Badri M  •  10 months ago
No. Try it! You end up doing a bitwise AND with a zero which turns off all
bits.
△  ▽ • Reply • Share › 

Guest > Badri M  •  10 months ago
No. a &= (0<<2); only defines bit2 as a 0. The other bits could stay set and
you would be setting all bits high except bit2.
△  ▽ • Reply • Share › 

PRAGEETH KARUNADHEERA  •  2 years ago
Bitwise AND and bitwise OR operations are incorrectly shown. Please correct.
1 △   ▽ • Reply • Share › 

navin datt  •  2 years ago
U wrote:­ [When manipulating a single bit: "Let's call this a 'bit value mask'. ­e.g.­ mask for bit 2
would be 00000100 ,...and bit value mask for bit 6 would be 01000000."]

http://www.micahcarrick.com/tutorials/avr­microcontroller­tutorial/avr­c­programming.html 5/6
11/13/2015 AVR C Programming Basics | AVR Tutorial

Wow!!!!.. That's above STATEMENT alone is sufficient & v. great..!!!!,.. ­­ I hv been reading many
tutorials for weeks, yet not ONE of those idiots ever told me, what is the bloody VALUE of that
'mask' ???? ­ by which to "OR", or to "AND",...only the single bit of my concern.

Instead,.. they wasted my time, writing big theories about why u should use a mask etc.,­ yet they
never cared to write even "half a line" to tell me, ­what is the 'mask' equal to???? ­ Is it equal to the
current value of the bit????, or the target value of the bit..????, ­­they only confused me
like...HELL,.!!!..but only..till now...!.. Thanx a lot....just for telling me the MAIN basic point of what is
the "mask value", and with two v. important examples...!...many...many...Thanx...again..!
3 △   ▽ • Reply • Share › 

Donguy  •  2 years ago
This is first intro with AVR's and its programming's...I feel like enjoying it for quite a while from now
on...!!!
1 △   ▽ • Reply • Share › 

Ram Krishna  •  2 years ago
i learned some what too good,,,, thanks a lot ,,,, keep it up . its will great use to beginners .,.... thank
you for posted it .
△  ▽ • Reply • Share › 

Jonatas  •  2 years ago
Your tutorial is quite good! Thanks for posting it.
△  ▽ • Reply • Share › 

studyembedded  •  3 years ago
Thanks buddy very well explained...!! Please check www.zembedded.com for more AVR
tutorials...thanks!
1 △   ▽ • Reply • Share › 

yvonlefait  •  3 years ago
Merci pour ces explications!!!
△  ▽ • Reply • Share › 

Hassan  •  3 years ago
Great ............I benefited more and Thank you so much......as I am a new Avr newbie
△  ▽ • Reply • Share › 

haider ali  •  3 years ago
wow really cool ...!!
△  ▽ • Reply • Share › 

disqus_TnxSUCvefI  •  3 years ago
useful information for beginers in avr programing ,thank u
1 △   ▽ • Reply • Share › 

Wilson Martinez  •  3 years ago
Thank you Micah, great tutorial.
1 △   ▽ • Reply • Share › 

Mohsen Seif  •  3 years ago
very thanks
4 △   ▽ • Reply • Share › 

✉ Subscribe d Add Disqus to your site ὑ Privacy

Copyright © 2004 ­ 2015 Micah Carrick.

http://www.micahcarrick.com/tutorials/avr­microcontroller­tutorial/avr­c­programming.html 6/6

You might also like