LOW-LEVEL ARDUINO PROGRAMMING(ASSEMBLER): BLINKING LED

INTRODUCTION
If we use Arduino IDE with Atmel microcontrollers, processes occur quickly, but it can not be said the same for input/output pins (GPIO). To speed up those processes, we can use lower level commands.
NOTE: things that we will do in the beginning are not pure C language, and we will also do programming from Arduino IDE. The aim here is to gradually explain the background of “graphic” programming that Arduino offers.
Atmega328 is the most used Atmel’s microcontroller with Arduino platform (Croduino, Dasduino, Arduino Uno, etc.). This microcontroller has three series of pins or so called ports:
Port B are digital pins 8 – 13
Port C are analog input pins
Port D are digital pins 0 – 7.
Each one of these series has three 8-bit control registers:
DDRÂ (Data Direction) is a register which determines whether to write or read from pin, 0 = input 1 = output.
PINÂ register is used for reading digital value of pin
PORT register has two functions: if DDR is set as output: 0 sets pin in low state and 1 in high state. If DDR is set as input: 1 turns on internal pull-up resistor.
Names of these registers can also be found as DDRB, DDRC, DDRC etc. whereas B, C and D are signs for ports.
REGISTERS FOR PIN CONTROL
Each pin is one bit on control registers. This is a list to the map above:
B0 = digital pin 8
B1 = digital pin 9
B2 = digital pin 10
B3 = digital pin 11
B4 = digital pin 12
B5 = digital pin 13
B6 = reserved for crystal, don’t use
B7 = reserved for crystal, don’t use
C0 = analog pin 0
C1 = analog pin 1
C2 = analog pin 2
C3 = analog pin 3
C4 = analog pin 4
C5 = analog pin 5
C6 = analog pin 6, only on Dasduino Core and Arduino nano
C7 = analog pin 7, only on Dasduino Core and Arduino nano
D0 = digital pin 0, used for serial communication, use is not recommended
D1 = digital pin 1, used for serial communication, use is not recommended
D2 = digital pin 2
D3 = digital pin 3
D4 = digital pin 4
D5 = digital pin 5
D6 = digital pin 6
D7 = digital pin 7
LED BLINKING
For this code, you need to have Dasduino Core or other board with Atmega328 micro controller.
Copy and upload this code in Arduino IDE. We will turn on intern LED diode on 13 pin of Dasduino, pin B5.
void
setup
()
{
 Â
// setting DDRB as output
 Â
DDRB = B00100000;
}
void
loop
()
{
 Â
PORTB = B00100000;
// Turn on LED
 Â
delay
(1000);
 Â
PORTB = B00000000;
// Turn off LED
 Â
delay
(1000);
}
Before the explanation, let’s keep this  Atmega328 datasheet near by (table register of port B).
As we said in the introduction, DDR (in this case, DDRB) sets pin as input or output. From the table 13.4.3. above, we can see that B5 pin is on the position 5 of this registry, and 1 (high) indicates that the pin is set as output. Therefore, we are changing the state of DDRB registry into B00100000. “B” in front of ones and zeros means that we are using a binary number. We could have written the same as number 32 in decimal notation or 0x20 in hexadecimal notation.
The next step is to turn on or turn off pin B5 (digital pin 13). As we said before, the writing pins use the registry port (in this case, PORTB), and “1” indicates HIGH and “0” LOW. Let’s look at the table 13.4.2. Pin B5 is again at the position 5, and so will the state of the pin B00100000 set pin in a high state, and B00000000 pin in a low state.
NOTE: changing the status of other bits in the registers won’t affect the status of this pin, B5.