Skip to main content

Arduino-Register Programming

Simple LED Blink Program
Here I'm explained my first register level coding, It is awesome and it's make me much more understand about how the microcontroller is working, I'm still learning, so here I'm sharing what I understood. Are you ready for this then let's go

Arduino language Blink program
This is simple Blink program without using any functions!. Take a look at the code the same code is converting to register level

unsigned long counter = 0;// initializing  counter variable and set to 0
void setup() {
  pinMode(LED_BUILTIN, OUTPUT);// set 13th pin LED as output
  Serial.begin(9600);// initializing Serial communications
}

void loop() {


  counter++;//increment the counter
  Serial.println(counter);// print the counter value on Serial monitor
  if (counter > 250) {
    digitalWrite(LED_BUILTIN, HIGH);// if counter value greater than 250 then LED turn ON
  }
  if (counter > 500) {
    digitalWrite(LED_BUILTIN, LOW);// if counter value greater than 500 then LED turn OFF
    counter = 0;// resetting the counter to 0
  }


}

Before going to Register level programming we have to understand some basics.
Port registers allow for lower-level and faster manipulation of the i/o pins of the microcontroller on an Arduino board. The chips used on the Arduino board is ATMEAG328 (Arduino Nano/UNO) have three ports

What is Ports?
For example ATMEGA328 (in Arduino Nano) have 32 pins and it has 3 Ports totally PORT B, PORT C and PORT D,
  1. B (digital pin 8 to 13)
  2. C (analog input pins)
  3. D (digital pins 0 to 7)
PORT D
PORT D01234567
ARDUINO PIN01234567
ATMEGA PIN3031321291011

PORT B
PORT B01234567
ARDUINO PIN891011121320/XTAL121/XTAL2
ATMEGA PIN12131415161778

PORT C
PORT C01234567
ARDUINO PINA0/14A1/15A2/16A3/17A4/18A5/1922/RST
ATMEGA PIN23242526272829

Each port is controlled by three registers, which are also defined variables in the Arduino language.
I can show simply in the table how the Arduino coding is different from Register coding.

ARDUINO CODINGREGISTER CODING
pinMode()DDR
digitalWrite()PORT
digitalRead()PIN

I will explain that what is DDR, PORT & PIN.
DDR - data direction register – DDRx
The port input pins I/O location is read only, It means default all the ports are inputs, so if we want to  manipulate a pin to output we have to use Data Direction Register.

Comments

Popular posts from this blog

ATMEGA328P-TQFP32-PINOUT

Here'a a pinout diagram I created for the ATMEGA328P TQFP32 . It is shared under CC-BY-SA license. You're free to modify it and share. Just give a backlink to my project website if possible (I have not posted the pinout diagram on my website yet. I'm also attaching a PDF,SVG version so that you can edit with any vector editing software. If you want me to create pinout diagrams for other boards, I'd be happy spend some free time on it. Let me know. Hope you'll find it useful :)  Github: https://github.com/Krishnawa/ATMEGA328P-TQFP32-PINOUT

Bootloader - Arduino

What is a bootloader, and what is bootloading? The bootloader is the little program that runs when you turn the Arduino on, or press the reset button. Its main function is to wait for the Arduino software on your computer to send it a new program for the Arduino, which it then writes to the memory on the Arduino. This is important, because normally you need a special device to program the Arduino. The bootloader is what enables you to program the Arduino using just the USB cable. When we refer to "bootloading" the Arduino, we are talking about using a special device (called an In-System Programmer or ISP) to replace the bootloader software. Normally the default bootloader of arduino is Optiboot Here I am showing that an alternative and some more advanced options in Arduino bootloader option called MiniCore Why MiniCore? An Arduino core for the ATmega328, ATmega168, ATmega88, ATmega48 and ATmega8, all running a custom version of Optiboot for increased functio