Over 10 years we help companies reach their financial and branding goals. Engitech is a values-driven technology agency dedicated.



411 University St, Seattle, USA


+1 -800-456-478-23

Daily Coding Embedded Basics Miscellaneous

Introduction on embedded c

Embedded C is an extremely popular programming language when we talk about electronic devices. If you wanna go for robotics it would be a good start for you. In our day-to-day life, we use mobile, laptops, and fridges every electronic device we use is made up of using embedded C. Now without wasting any time let\’s dive a bit deep into it by looking into a very basic example

Blinking LED using Embedded C.

Blinking Led is a hello world for embedded C which means this is the first basic code that takes us into the world of embedded C.

#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
int main(void)
DDRB|= (0B00100000);
while (1)

Let’s understand our code from top to bottom.

#define F_CPU 16000000UL

Let’s break it into 3 parts

  • #define F_CPU

Here we are defining the clock speed of the processor.

  • 16000000

Here we are setting the clock speed at 16Mhz as our atmega328p’s default value is 1Mhz hence 16Mhz will make it 16 times slower.

  • UL

Here we are doing nothing but declaring the data type of the clock speed. We used unsigned long because clock speed cant is negative.

#include <avr/io.h>

Whenever we want to use libraries of some function from some other source or even we wanna use our own code we use #include.  #include <avr/io.h>  includes a header file that contains code for using pins, ports, etc. for the Avr microcontroller.

#include <util/delay.h>

This library is used to put delays in our code.

int main(void)

The main function is when the AVR starts executing code.


While loops execute the code inside it until the condition inside the parentheses remains true. We all know in C 1 refers to true and 0 refers to false. Here the code inside the while loop will run again and again because 1 can never be false.


If we don\’t write return(0) at the end of the code we will get an error for sure. This happens because our operating system needs confirmation that the code we ran is running properly. This is more of a line of confirmation.

You must be wondering what these words DDRB or PORTB are? Well, these are called hardware registers which are extremely important to understand so let\’s get to know what they are and what they do?

Hardware Registers


In our Atmega series of Avr, we have mainly 3 hardware registers.

  1. DDRx
  2. PORTx
  3. PINx

Here x is referred to the bank. We have three of’em B, C, D.Every bank contains 8 pins. Depending on which bank’s pin you are using we chose the bank.

    • DDRx[Data-Direction registers]

    DDRx configures the pins as output or input. As we are using an 8-bit microcontroller. The default value of the DDR is 0 which means whenever we give power it is not giving any output. If we want to set it for the output we need to set the bit we want to use for sending the output as 1.

    Output = 1
    Input    = 0
    DDRB = 0b00000000       
    DDRB = 0b00100000

    Here 0b is telling us that we are writing the number in binary. We all know 0 refers to the ‘off’ and 1 refers to the ‘on’. Hence the above example is telling us at first all pins were off later on we set pin 5 as 1 for giving output.

    If you are familiar with Arduino Ide then you can relate DDR with pinMode.     

  • PORTx[Port x data registers]

    After setting the DDRx bits to the 1(output), The port registers the voltage as HIGH or LOW. When we say the voltage is Low it means the voltage is 0V and if we say voltage is HIGH we consider the voltage as 5V. 

    HIGH = 1
    LOW    = 0
    PORTB = 0b00000000 
    PORTB = 0b00100000

    If we take the example of our code we have seen above we are setting pb5 as output. In the picture you can see above PB5 is connected with pin 13 of Arduino. Now you can see we are turning we Led on which is connected with pin 13 which is connected with PB5 on Atmega 328p.

    If you are familiar with Arduino Ide then you can relate PORTx with digitalWrite.

  • PINx[port c input data pin address]

    The pin register addresses are used when we want to read the digital voltage values for each pin we set as input in DDRX.

    You can relate this pin with digitalRead from Arduino IDE.

I believe now it\’s clear what we did in the 

above example. In the next blog, we will see how we can write a function and use it for calling. So that we can reduce our writing.



Leave a comment

Stay Updated With Us

Error: Contact form not found.