Here I will highlight some features of C language commonly used in 8 bit embedded platforms like 8051, AVR and PICs. While programming microcontrollers in C most of the time we have to deal with registers. Most common tasks are setting and clearing bits in a register and check whether a bit is 0 or 1 in a given register. So here I will give detail on those topics, it will help you if you are new to embedded programming in C and if you get confused when you see some codes.
A Register
A register is simply a collection of some bits (mostly 8 bits in case of 8bit MCUs). Either each different bit in a register has some purpose or the register as a whole holds a value. Registers serves as connection between a CPU and a Peripheral device (like ADC or TIMER). By modifying the register the CPU is actually instructing the PERIPHERAL to do something or it is configuring it in some way. And by reading a register, the CPU can know the state of peripheral or read associated data.
![]() |
Fig.: CPU writing to Peripheral Register |
![]() |
Fig.: CPU Reading from Peripheral Register |
Binary Numbers in C
When you write a=110; in C it means you are setting the value of variable"a" to "one hundred and ten" (in decimal). Many time in embedded programming we are not interested in the value of a variable but the state of each bits in the variable. Like when you want to set the bits of a register (MYREG) to a bit pattern like 10010111 (binary). Then you cannot write MYREG=10010111. Because compiler will interpret 10010111 as decimal. To specify a binary number in C program you have to prefix it with 0b (zero followed by b). So if you write
MYREG=0b10010111;
it assigns the bit pattern 10010111 to the bits of Register MYREG.
HEX Numbers in C
In same way if you prefix a number by 0x (a zero followed by x) then compiler interpret it like a HEX number. So
MYREG=0x10; (10 in HEX is 16 in decimal)
MYREG=0xFF;(Set all bits to 11111111 or decimal 255)
Setting a BIT in Register
Here our aim is to set (set to logical 1) any given bit (say bit 5) of a given register (say MYREG). The syntax is
MYREG=MYREG | 0b00100000;
The above code will SET bit 5 to 1 leaving all other bits unchanged. What the above code does is that it ORs each Bit of MYREG with each bit of 0b00100000 and store the value back in MYREG. If you know how logical OR works then you will get it.
In short you can write the same code as
MYREG|=0b00100000;
Now lets come to practical usage. In practice each bit has got a name according to its work/function. Say our BIT (the 5th bit) has got name ENABLE, and what it does is clear by its name,when we set it to 1 it enables the peripheral and when cleared (0) it disables it. So the right way to set it is.
MYREG|=(1<<ENABLE);
The << is called left shift operator. It shifts the bits of LHS variable left by the amount on its RHS variable. If you write
b=1<<3;
then, 1 whose binary value is 00000001 is shifted 3 places to left which results in 00001000
So if ENABLE is defined as 5 (as enable is 5th bit) then
MYREG|=(1<<ENABLE);
will result in
MYREG|=(1<<5);
which again result in
MYREG|=(0b00100000);
Now a beginner would ask "What's the Advantage ?". And once you know it you would realize that advantage is immense!
- Readability of code: MYREG|=(1<<ENABLE); gives a clue that we are enabling the peripheral while MYREG|=0b00100000; does not give any clue what it is doing, we have to go to data sheet and find out which bit actually ENABLEs the peripheral. While ENABLE=5 is already defined in header files by the developer of compiler by carefully studying the datasheets of device.
- Easier Portability: Suppose you use this code many times in your program (and your program is reasonably large and uses other register also) and you now want the same code to run on some other MCU model. The new MCU is of similar family but has slightly different bit scheme, say ENABLE is bit 2 instead of bit 5. Then you have to find all occurrence of MYREG|=(0b00100000); and change that to MYREG|=(0b00000100); But if you have used the other method then you simply need to inform the compiler (by its setting options) that you are going to use the other MCU and compiler will automatically get the definitions for the new device. And in this definition ENABLE=2 will already be defined by the compiler developer. So it will be lot easier.
Clearing a BIT in Register
For clearing a bit logical AND(symbol &) operator is used in place of logical OR (symbol |). The syntax is as follows
MYREG&=~(1<<ENABLE);
![]() |
Fig.: How to clear (0) a bit in C language. |
This will clear (i.e. set to value 0) a given bit (identified by name ENABLE) in a register called MYREG. This operation will not affect any other bits of register except ENABLE.
Let us see how it works with the help of following diagram.
Fig.: "Clearing a BIT" how it works? |
So now you know how you can selectively clear any bit in any given register. If you want to clear more than one bit at a time you can write like this
//This will clear bits ENABLE,FAST_MODE and BUSY, leaving all other bits untouched MYREG&=(~((1<<ENABLE)|(1<<FAST_MODE)|(1<<BUSY)));
Similarly the syntax for setting(set to 1) multiple bits at a time is as follows
//This will set bits ENABLE,FAST_MODE and BUSY, leaving all other bits untouched MYREG|=((1<<ENABLE)|(1<<FAST_MODE)|(1<<BUSY));
Testing The Status of a Bit.
Till now we were modifying the registers either setting or clearing bits. Now we will learn how can be know that a specific bit is 0 or 1. To Know if a bit is 0 or 1 we AND it with a AND MASK. Suppose if we want to check bit 5 of a register MYREG then the AND MASK would be 0b00100000. If we AND this value with the current value of MYREG then result will be non-zero only if the 5th bit in MYREG is '1' else the result will be '0'.
The syntax would be like this.
if(MYREG & (1<<ENABLE))
{
//ENABLE is '1' in MYREG
...
}
else
{
//ENABLE is '0' in MYREG
}
So now you know the basic operation on bits, they are widely used in firmware programming and will help you understand other codes on my web site. And Please don't forget to post your comment regarding any doubts, or reporting errors in the above article, or simply to tell how you liked the stuff.
By
Avinash Gupta
me@avinashgupta.com





Thanks for guiding me
Hi Avinash!
Thank you very much for making all these tutorials. its very great of you.. cheers!
Jayashan from Sri Lanka
Great work done, I am basically a Embedded Hardware designer,so this tutorial was a good guide for me in understanding some basics of embedded C…
HI, myself rupa, i am working on a DSP controller
am i unable to convert a floating point to fixed point
Please send me C codes for floating point which would help my project.
Thank You very much. This is a very good start for me.
Very instructive. Thank You.
Great Work I have been searching for this type of tutor,
Its really helpful ..
Thanks a lot !!!!!!
Keep it up!!
sir pls could you provide me with the code of avr microcontrollers used for rotating the rod (via dc motors) in one direction for 30 seconds and then again rotating it in opposite direction for 30 seconds
thank you
@Priya,
Why don’t you make it your self? What is the problem?
i would love to do that but am a beginner having no idea about programming in past i had referred some books and asked many faculties about my problems regarding the same but everything seems fruitless now..
@Priya,
So if you are in this field then I better suggest you learn programming as it will come to your use in the future. So what is your experience level in C? How much do you know?
sir i have read your tutorials on avr they are really helpful though it took some time to understand each and everything but finally i was able to understand most of it and it would have been a more enriching experience if the pdf versions for all the tutorials are available anyways i enjoyed the parts i had read thank you for replying and all the wonderful tutorials
Thank you !
I tried to make PDFs in the beginning but the problem was that web article were constantly updated with new information and corrections made. And PDFs soon became obsolete. Thats why I dropped the idea.
thank you sir once again for replying
i am familiar with the basic concepts of c(C++)
but its my bad luck i couldn’t find any such tutorials of c like these avr tutorials of yours
@Priya,
To complete what you are trying to make you need the following info:-
Basic details of AVR Studio 6 it is explained here
http://extremeelectronics.co.in/lfrm8/Help/AS6.htm
Then you need the basic info about i/o ports
http://extremeelectronics.co.in/avr-tutorials/part-v-digital-io-in-avrs/
Finally you need info about DC motor control for that you can refer to
http://extremeelectronics.co.in/avr-tutorials/dc-motor-control/
After reading all those I think you can easily implement the solution
How much experience you have working with AVRs? Do you have some setup to do the experiment?
thank you sir
these links were helpful…
i have all the basic knowledge of working with AVRs given in your tutorials..and had bought usb avr programmer, avr development board and atmega 16 microcontroller
i and one of my friend( pursuing mechanical engineering) are trying to develop a book scanner for which i needed
the programming code for rotating the rod (as mentioned earlier) the whole setup of the book scanner is as follows-
we have 2 rods at right angle to each other(say one along the x axis and another one along the y axis) the rod along the y axis rotates 60 degree clockwise(cw) and anticlockwise(acw) direction with time delay of 10 seconds as the rod rotates cw and acw the glass panel(which is moulded in the shape of an open book) attached to the rod along the x axis moves up and down….now moving the rod in 60 degree cw for 10 seconds then moving it acw is the 1st mechanism we need to build…nextly we need some automatic setup to turn the pages of the books which are to be scanned (since as the glass panel moves up there is a provision to turn the pages manually or through some automatic setup so that the further pages can be scanned)this automatic setup for turning the pages is the 2nd mechanism we need to build..third we need an speed regulator to make the rod move slow and fast as and when needed..
i request you if you could help us out in this..thank you once again and sorry for replying so late..
@Priya,
Okay, I will have a look at your description when I have some free time.
This is probably the best bit shifting tutorial I have read. I have a much clearer idea now. I wish I had read this a month ago then I could have stopped pulling my hair out.
@Steve
Thanks!
thank you sir
Nice info.!! thanks a lot sir…
I want to know about the embedded C program codings. please send me sir
As noticed tutorial quite complete the knowledge of any basic learner of Embeded System…
hii,
i am sadasiba, i am highly interested in embedded c but i am not so strong in coding
now i am learning embedded c, i have a question that no one is getting able to answer it or some of did not want to answer it, whom i asked yet
and i could not leave that question out of my mind and the question is”why there is no match between hex code generated after compiling and the previous original c code,if i am taking the ASCII equivalent of both charter used in c code and hex code generated” .is there any fact regarding compiler designing?.please kindly give a reply i am waiting for your reply.
loved da way u thaught
nice explanation
Thanks alot sir ,that was really helpful
Hey Avinash,
Your tutorial turned out to be silver lining for me when I was in a confused state. I was browsing a lot to clear my doubts. I don’t know how I landed at your site but it cleared tons of my doubts and made me regain my lost interest in embedded systems. Thanks a ton for bringing up such wonderful tutorials.
Nice explanation.
Please send some notes on embedded C.
Thanks.
Sir i want to study more about Embedded c please guide me this stuff was very sourcefull and i am very thankfull to you
Thank u very much !!! nice explanation !!