Exploring Arduino with Register Development

01

Abstract
The commonly used chip for the Arduino development board is the AVR microcontroller series produced by Atmel. The most common one is the ATmega328P, which is widely used in the Arduino Uno development board. Developing AVR microcontrollers using the Arduino platform is very convenient.
The Arduino IDE provides a very simple and user-friendly development environment, making it easy to write and upload code. It provides a simplified set of function libraries and APIs, allowing developers to easily interact with the hardware of the ATmega328P without needing to delve into the underlying register operations. However, Arduino can also be developed using registers.

02

Turning on the Light with Arduino
Using the Arduino IDE to develop the AVR ATmega328P microcontroller is very convenient. After downloading the Arduino IDE from the official website, you can play with the built-in examples.
Exploring Arduino with Register Development
// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}
Exploring Arduino with Register Development
This looks very simple, which is completely different from learning STM32 microcontrollers. Many people might think that Arduino is too basic. In fact, this microcontroller also has more advanced ways to play with.

03

AVR Microcontroller IO Operation Steps
Before playing with the AVR ATmega328P, it is essential to understand the AVR microcontroller. The IO ports of AVR are standard bidirectional ports, and all ports are in a high-impedance state upon reset. Each port of AVR corresponds to three registers: DDRx, PORTx, and PINx.
Before using the AVR microcontroller, it is crucial to initialize the corresponding port according to the pin function; otherwise, the port may not work correctly. When the pins of the microcontroller are used as general digital I/O ports, each pin has three register bits: DDRxn, PORTxn, and PINxn.
DDRxn
PORTxn
PINxn
I/O
Pull-up Resistor
Description
0
0
Input
None
High-Impedance State
0
1
Input
Yes
With Pull-up Resistor
1
0
Output
None
Output Low Level
1
1
Output
None
Output High Level

Before operating the IO ports in the AVR microcontroller, the corresponding initialization settings need to be made, as follows:

  • 1 Set the corresponding port as input or output through the direction register DDRx.

  • 2 If set to output, send the required output data to the data register PORTx. If set to input, read the external input value from the input register PINx, while the PORTx can be set to determine whether the corresponding pin needs a pull-up resistor.

04

Using Atmel Studio to Turn on the Light

Atmel Studio is an integrated development environment (IDE) launched by Atmel (now Microchip), specifically for embedded system development. It provides rich tools and features to support programming, debugging, and deploying Atmel microcontrollers.
Atmel Studio supports various compilers, including GCC and IAR compilers, which can be used to generate optimized code. It supports multiple programming languages, including C, C++, and assembly language. Developers can choose the most suitable programming language according to their needs to write their applications.
First, download the installation package for Atmel Studio from the official website. Since Atmel has been acquired by Microchip, Atmel Studio has also been renamed Microchip Studio, which integrates the functions of Atmel Studio and expands support for more Microchip microcontroller series, including PIC and dsPIC series.
Both Atmel Studio and Microchip Studio are IDEs for developing microcontroller applications. Atmel Studio mainly focuses on Atmel microcontrollers, while Microchip Studio has expanded support for more Microchip microcontroller series.
Exploring Arduino with Register Development
After downloading, the installation is straightforward.
The interface after installation is as follows, which looks quite familiar. Atmel Studio has many similarities in interface and functionality with Visual Studio because Atmel Studio is developed based on the Visual Studio Shell.The main difference between them lies in the targeted platform and hardware.
Atmel Studio focuses on the development of Atmel microcontrollers, while Visual Studio is a generic development environment that can be used for developing various applications.
Therefore, if you are already familiar with Visual Studio, switching to Atmel Studio will be relatively easy because they share many features and workflows.
Exploring Arduino with Register Development

4.1 Creating a New Project

Exploring Arduino with Register Development
Exploring Arduino with Register Development
Exploring Arduino with Register Development
/*
 * GccApplication1.c
 *
 * Created: 2023/5/18/Wednesday 14:48:19
 * Author : LiuYao
 * Board  : Arduino Nano
 */ 
#ifndef F_CPU
#define F_CPU 10000000UL
#endif

#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
 DDRB =(1<<DDB5);
 /* Replace with your application code */
 while (1)
 {
  PORTB |=(1<<PORTB5);
  _delay_ms(1000);
  PORTB &= !(1<<PORTB5);
  _delay_ms(1000);  
 }
 return 0;
}

4.2 Tool Configuration

Atmel Studio does not natively support Arduino development and requires configuration. The board used in this article is Arduino UNO, and the microcontroller model is ATmega328P. Note to select the correct model when creating a new project.
Exploring Arduino with Register Development
Exploring Arduino with Register Development
After opening, the key points are to fill in the following three options and check the Use Output window option.
  • Title: Arduino UNO (can be filled in arbitrarily)
  • Command: D:\Software\Arduino\hardware\tools\avr\bin\avrdude.exe, this path is the path to avrdude.exe in Arduino IDE, fill in according to your own path.
  • Arguments: Here, pay attention to the COM port number; mine is COM7, fill in the correct COM number according to your board recognition.
-C "D:\Software\Arduino\hardware\tools\avr\etc\avrdude.conf" -v -p atmega328p -c arduino -P COM7 -b 115200 -D -U flash:w:"$(ProjectDir)Debug\$(TargetName).hex":i
The explanation of the Arguments parameters is as follows:
Usage: avrdude.exe [options]
Options:
  -p <partno>                Required. Specify AVR device.
  -b <baudrate>              Override RS-232 baud rate.
  -B <bitclock>              Specify JTAG/STK500v2 bit clock period (us).
  -C <config-file>           Specify location of configuration file.
  -c <programmer>            Specify programmer type.
  -D                         Disable auto erase for flash memory
  -i <delay>                 ISP Clock Delay [in microseconds]
  -P <port>                  Specify connection port.
  -F                         Override invalid signature check.
  -e                         Perform a chip erase.
  -O                         Perform RC oscillator calibration (see AVR053). 
  -U <memtype>:r|w|v:<filename>[:format]
                             Memory operation specification.
                             Multiple -U options are allowed, each request
                             is performed in the order specified.
  -n                         Do not write anything to the device.
  -V                         Do not verify.
  -u                         Disable safemode, default when running from a script.
  -s                         Silent safemode operation, will not ask you if
                             fuses should be changed back.
  -t                         Enter terminal mode.
  -E <exitspec>[,<exitspec>] List programmer exit specifications.
  -x <extended_param>        Pass <extended_param> to programmer.
  -y                         Count # erase cycles in EEPROM.
  -Y <number>                Initialize erase cycle # in EEPROM.
  -v                         Verbose output. -v -v for more.
  -q                         Quell progress output. -q -q for less.
  -l logfile                 Use logfile rather than stderr for diagnostics.
  -?                         Display this usage.

4.3 Compiling and Burning Programs

First, compile the project.
Exploring Arduino with Register Development
Connect the Arduino UNO to the computer, and go to Tools to click the configuration just set Arduino UNO.
Exploring Arduino with Register Development
At this point, you can burn the program to the Arduino, and the output window will display the following content.
Exploring Arduino with Register Development
If an error occurs, consider whether the path mentioned above is correct and check the COM port number.
Note: This method cannot be used to download programs to the Arduino Nano board because the Arduino Nano board mostly uses the Old Bootloader, and using this method will result in download failure.

Experimental Phenomenon

Exploring Arduino with Register Development
Next time someone asks you what microcontroller you are playing with, you should say: I don’t play with Arduino, I play with AVR, just like your ARM.

-END-

Previous Recommendations: Click the image to jump to read
Exploring Arduino with Register Development

Beginner Selection: Arduino or Raspberry Pi?

Exploring Arduino with Register Development

Comparison of STM32 and Arduino, who is more powerful?

Exploring Arduino with Register Development

Various Arduino peripherals usage, vividly illustrated, quite appealing!

Leave a Comment

×