0% found this document useful (0 votes)
24 views7 pages

Program The Arduino Uno in Assembly Language - Micropi

The document provides a guide on programming an Arduino Uno using assembly language with Microchip Studio. It details the installation process, project creation, and setup of AVRDUDE for programming the microcontroller over USB. An example program is included that reverses a string inputted via the serial monitor, demonstrating the use of assembly language for Arduino development.

Uploaded by

Srdjan Matic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views7 pages

Program The Arduino Uno in Assembly Language - Micropi

The document provides a guide on programming an Arduino Uno using assembly language with Microchip Studio. It details the installation process, project creation, and setup of AVRDUDE for programming the microcontroller over USB. An example program is included that reverses a string inputted via the serial monitor, demonstrating the use of assembly language for Arduino development.

Uploaded by

Srdjan Matic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

micropi

Programming ideas.

ARDUINO

Program the Arduino Uno in Assembly


Language

Date: March 23, 2021 Author: micropi  0 Comments


Recently I have been experimenting with programming an Arduino Uno using
assembly language. Here I am going to explain how to install and set up Microchip
Studio for this, show how to use the Arduino’s serial interface and write an
example program to demonstrate.
Installing Microchip Studio

Microchip Studio is the IDE that we’re going to use to program the Arduino – it
contains the assembler that is needed to translate the assembly language to
machine language. Microchip is the company that acquired Atmel, which makes
the AVR microcontrollers that are used by Arduinos. You’ll find that many online
tutorials talk about Atmel Studio, which became part of Microchip Studio when
Atmel was acquired and is no longer available.

You can download Microchip Studio from the Microchip website here. The
installer asks which board families you want to install – I only chose AVR, since
this is what Arduinos use, and deselected the other two. However, you can install
the others if you want. I also chose not to install the extended framework, but you
can if you want to.

Creating the first project

Once Microchip Studio has installed you can open it and go File > New > Project to
create your first project. In the dialog that opens, select ‘Assembler’ on the left and
choose the ‘AVR Assembler Project’ option that comes up. Give it a name and
location at the bottom and press OK to create the project.

Next you’ll be asked which device you want the project to run on. The Arduino
Uno contains an ATmega328P, so select this option – you can search in the top
right. Make sure not to select ATmega328 or ATmega328PB, unless you think that
your board uses one of these instead.

Setting up AVRDUDE

Microchip Studio can only actually program microcontrollers directly if you buy a
special programmer. However, AVRDUDE is a command line utility that can be
used to program them over USB, like you usually would with an Arduino. The
Arduino IDE actually uses AVRDUDE to program Arduinos under the hood.

If you have the Arduino IDE installed you don’t need to install AVRDUDE, you can
use the copy that came with the IDE.

We’re going to set up AVRDUDE as an external tool in Microchip Studio. This can
be done by going to Tools > External Tools. Give the entry a title, I called it ‘Flash’.
The command should be the path to the AVRDUDE executable, for me it was
C:\Program Files
(x86)\Arduino\hardware\tools\avr\bin\avrdude.exe

And the arguments should look like the following:

-v -patmega328p -carduino -PCOM4 -b115200 -D -


Uflash:w:"$(ProjectDir)Debug\$(TargetName).hex":i -
C"C:\Program Files
(x86)\Arduino\hardware\tools\avr\etc\avrdude.conf"

-v: verbose output


-p: specifies which chip we want to write programs to
-c: specifies the programming method to be used, arduino in this case
-P: the port the Arduino is connected to. It can be found on Windows by going
to Device Manager, selecting ‘Ports (COM & LPT)’ and locating the port
corresponding to the Arduino
-b: the baud rate to use when programming the chip
-D: disables erasing the flash memory before writing to it
-U: this specifies what AVRDUDE should do. ‘flash’ tells it to use the flash
memory of the chip, ‘w’ tells it to write to it, the directory is the machine code
file to write and ‘i’ specifies that the file is in Intel hex format. ‘$(ProjectDir)’
and ‘$(TargetName)’ are automatically replaced by Microchip Studio to get the
path of the assembled file.
-C: specifies the path of the AVRDUDE configuration file – it will vary based on
where you have the Arduino IDE installed.

It’s also worth ticking the ‘Use Output Window’ option so that you can easily see
the output when you program the Arduino.

Example program – string reverser

Now we can try and create a program to test our setup.

Writing to the serial monitor

The following two subroutines can be used to send data to the serial monitor on
the connected PC.
serial_init configures the USART connection to use a 9600 baud rate and the
expected formatting and parity settings, and serial_transmit waits until the
last character has been transmitted before transmitting the character stored in
register 19 over the serial connection. These subroutines came from the useful
GitHub repository by Alan Pearson here.

Reading from the serial monitor

We can also read data from the serial monitor using this subroutine:

It waits for the ‘receive complete’ bit in the USART status register (bit 7 of 0xc0) to
be set, and then stores the byte received in the I/O data register (0xc6) in register
19.

Using the stack to reverse strings

Now that we can receive and transmit data over the serial connection we can
write a program that receives data and sends it back in reverse.
It works by pushing each character the user enters to the stack, then popping them
off one by one and printing them. Items pushed to a stack are popped off in the
opposite order because it’s a last-in-first-out data structure.

It starts by calling serial_init to initialise the serial connection.

At the start label it pushes 0x0a to the stack ( 0a is the ASCII code of the
newline character in hexadecimal) and prints the byte 0x3e (the character code
of ‘>’) – this is the prompt for the user to enter a string.

In the loop it calls serial_receive to load the next character into register 19,
then immediately calls serial_transmit to print the character the user just
entered. It then compares the character with the newline character 0x0a . If they
are equal, it jumps to the input_finished label. Otherwise, it pushes the
character onto the stack and loops again.

At the input_finished label, it pops a character from the stack and prints it. If
the character was a newline (the newline we pushed at the start) the whole string
has been printed, so it jumps back to the start label. Otherwise it jumps back to
input_finished and continues popping and printing characters until it
encounters a newline.

You can see the program on GitHub here.

Writing the program to the Arduino

Now that the program is finished we need to assemble it into machine code. This
can be done by hitting F7 or going Build > Build Solution. You’ll hopefully see the
output of the assembler with “Build succeeded”. However, if there are errors a list
of errors and their line numbers will appear.
Once the program is assembled it’s ready to be flashed to the Arduino. Go to Tools
and click on what you named AVRDUDE earlier (I called it ‘Flash’). Hopefully you’ll
see the content of flash memory and the fuses being verified in the output window,
indicating that it was successful.

If you see a message like ser_open(): can't open device "\\.\COM5": The
system cannot find the file specified ensure that the COM port you
specified earlier when setting up AVRDUDE as an external tool is still correct and
that the Arduino is actually connected to the PC.

Now that the program has been written to the board, you can try it out. Open a
serial monitor (I use the one that is included with the Arduino IDE) and ensure
that the baud rate is set to 9600.

When you send text to the Arduino you should find that it is sent back in reverse.
Ta da!

Note that if you try to flash the Arduino again now, you’ll get a message like
ser_open(): can't open device "\.\COM4": Access is denied. This is
because only one application can access the COM port at a time – you have to close
the serial monitor before AVRDUDE can use it.

I hope you found this useful or interesting! When you’re using assembly it’s
incredibly useful to have the official instruction set manual open, as it describes all
of the available instructions.
ARDUINO ASSEMBLY LANGUAGE

Published by micropi

View all posts by micropi


© 2024 MICROPI

BLOG AT WORDPRESS.COM.

You might also like