Line 3: Line 3:
 
It is often required to set, clear, toggle and check bit status of a Register without '''affecting any other bits.''' Also it is important to do in a way that is compiler independent and code can be ported without modifications.  
 
It is often required to set, clear, toggle and check bit status of a Register without '''affecting any other bits.''' Also it is important to do in a way that is compiler independent and code can be ported without modifications.  
  
=Setting a bit=
+
 
Consider the Data Direction Register (DDRD) shown below.  As you know we need to set or clear bits in order to make the  corresponding pin output or input respectively.  
+
Consider the Data Direction Register (DDRD) shown below.  As you know we need to '''set''' or '''clear''' bits in order to make the  corresponding pin '''output''' or '''input''' respectively.
  
 
{| class="table table-striped table-bordered primary"
 
{| class="table table-striped table-bordered primary"
Line 12: Line 12:
 
| DDD7|| DDD6 || DDD5|| DDD4 || DDD3|| DDD2|| DDD1 || DDD0
 
| DDD7|| DDD6 || DDD5|| DDD4 || DDD3|| DDD2|| DDD1 || DDD0
 
|}
 
|}
 +
Let us also say that there is a switch connected to  PORTD2 and LED connected to PORTD4 as shown.
 +
[[File:0 AVR C basics.png|none]]
 +
 +
Now what we need to accomplish is this:
 +
{| class="table table-striped table-bordered primary"
 +
|-
 +
|7||6||5||4||3||2||1||0
 +
|-
 +
| DDD7|| DDD6 || DDD5|| DDD4 || DDD3|| DDD2|| DDD1 || DDD0
 +
|-
 +
| 0|| 0|| 0||style ="background-color:orange"|1|| 0||style ="background-color:blue"| 0|| 0|| 0
 +
|}
 +
 +
=Setting a bit=
  
 
=Clearing a bit=
 
=Clearing a bit=
 
=Checking a bit=
 
=Checking a bit=
 
=Toggling a bit=
 
=Toggling a bit=

Revision as of 10:09, 23 March 2016

Let us look at the basics of 'C' for programming AVR Micrcontrollers in this tutorial. Simple stuff like setting and clearing bits is important to any project you do.
It is often required to set, clear, toggle and check bit status of a Register without affecting any other bits. Also it is important to do in a way that is compiler independent and code can be ported without modifications.


Consider the Data Direction Register (DDRD) shown below. As you know we need to set or clear bits in order to make the corresponding pin output or input respectively.

7 6 5 4 3 2 1 0
DDD7 DDD6 DDD5 DDD4 DDD3 DDD2 DDD1 DDD0

Let us also say that there is a switch connected to PORTD2 and LED connected to PORTD4 as shown.

0 AVR C basics.png

Now what we need to accomplish is this:

7 6 5 4 3 2 1 0
DDD7 DDD6 DDD5 DDD4 DDD3 DDD2 DDD1 DDD0
0 0 0 1 0 0 0 0

Setting a bit

Clearing a bit

Checking a bit

Toggling a bit