People ask, if the Arduino is a computer then can it do mathematics? Yes, it performs binary mathematics using 8 bit numbers. In this project we will have one count.
Imagine you have an abacus with 8 beads on each row. Each bead can either be in the 0 position or in the 1 position. Each row can hold a number from 0 to 255. You figure out ways of counting, adding and subtracting with it. But your fingers can only move so fast.
Let's electrify our abacus. Miniature beads of stone switch quickly from 0 to 1 and back again. The smallest beads move the fastest and use the least electricity. Instead of moving the stone we charge it with electrons for a one, or discharge to get a zero.
Each binary digit is a bit, eight together are a byte. Char, uint_8 are the same eight on and off bits. Add bytes together for words, integers, floats, etc.
Can we make music?Yes. Our abacus can hold numbers from 0 to 255. We can store a melody with 256 levels and play it out a speaker. It's what was done with the video game Space Invaders and the video games of the 1980s and 1990s.
Can we do graphics, pictures, movies?Yes. We can make and store low resolution images. Like cartoons you are limited in the colours you can use, how many levels of brightness and how many pixel elements.
This image is under 10kBytes of data, ten thousand abacus rods. Each pixel is stored as an 8bit number. It fits into the Uno flash memory and look okay on a small display.
Mathematics?Counting to 255 is good but can we go farther? Microcontrollers from the Atmel AVR family such as the Atmega328p in the Arduino Uno can extend our abacus up to 16 beads by splitting the numbers into HIGH and LOW bytes.
Calculations can handle numbers up to 65,535. And we can store AVR sketches into the flash memory with program and data that can be 65,000 instructions long. The Arduino Uno has a flash capacity of 32k or half that amount.
We are going to focus on one specific abacus rod inside of the Atmega328p. It is named PORTB and we find it at hexadecimal address 0x25. This address is written 37 in decimal or 0b00100101 in binary.
MicroChip Studio IDEIn the project Upload AVR Program we downloaded and installed MicroChip Studio IDE. It came with the avrasm2 toolchain to generate programs from assembly code.
8 Bit AVR Compiler Programs
Go back to the MicroChip website and download the XC8 compiler program to install in your Windows PC. MicroChip Studio needs to add a compiler program that will convert C/C++ programs into hex that can be uploaded into an Arduino board.
We are going to load a simple program into MicroChip Studio and watch it simulate an Arduino counting in binary. Open a new C/C++ project math. Select XC8 C Application Project and name it math.
The IDE program has scanned this computer and found more than one compiler that it can work with.
Select device Atmega328p.
We are going to run our program on the simulator Tool. Select from this menu and click control-S on the keyboard to save.
We arrive at a screen showing a default main.c program. It will compile and build a hex program because it is grammatically correct. Use the Build menu or press F7 on your keyboard.
We have just generated a binary hex executable file that we can upload and run on an Arduino board. The program will not do anything. The hex file is in the project folder we created.
Select all of the text in main.c and paste the following program into the screen. This is a program that takes a number called PORTB and increments it. Then it repeats the operation.
// PORTB is an 8bit number at address 0x25
#define F_CPU 16000000
#include <avr/io.h>
#include <util/delay.h>
int main(void){ DDRB = 0xFF; // turn PORTB on
while(1){PORTB++; // increment PORTB
_delay_ms(100); }}
Go to the left hand side of the main.c program and click next to the line while(1){ } look for Toggle Breakpoint. It is in the Debug menu or you can press F9. A red circle will appear in front of the line to show a breakpoint has been added.
In the menus find Debug->Windows->IO and then select PORTB. Try to arrange your windows to look like this. The IO display can be floating. All the PORTB registers are zero, the program has not started.
A yellow arrow says the program halted at the start of the main function int main(void). This is an actual location in the SRAM memory and has its own address.
Click the Debug menu and find Start Debugging and Break or press Alt-F5 on your keyboard. MicroChip Studio IDE will begin running the program and stop at the breakpoint. Look at the IDE and you will see another yellow arrow.
To continue we need to find the Debug commands that will let us step, hop or jump through the program. Look for the Debug Toolbar with a green play button, red stop, hover over each icon and you will see names for their commands.
Continue is one command for the program to continue running. F5 on your keyboard is continue. Step Into F11, Step Over F10 or Step Out will step the program forward. Continue is the green triangle in the toolbar. Reset takes the program back to the beginning.
Our program has run up to the breakpoint and stopped. 0xFF has been loaded into address 0x24 which is called DDRB for Data Direction Register Port B. All 8 outputs of PORTB are now setup to output data. Red shows data change from the last display update.
Explore the Debug->Windows menu for other options that show what is going on. PORTB is register 0x25 and a data registers window will also show you its value. data IRAM and MAPPED_IO windows display the same information.
Continue with the F5 or clicking the green play button triangle on the menu. It will run until it arrives at the next breakpoint.
Step again. The simulator may take a while to complete the _delay_ms function. Address 0x23 PINB is the input to the same PORTB circuit, so it detects when the output goes to high level.
Step again.
One last step
Continue, on your own, and you will see the program step all the way to 255 and then repeat at zero. Reset will take you back to the beginning.
This is a good exercise to practice your use of breakpoints and debugging commands in the IDE interface.
Upload to an Arduino Uno boardAnother project shows you how to upload a program from the MicroChip Studio IDE directly into the flash of the Arduino.
Configure an External Tool in the Tools menu. Enter Command: avrdude.exe and use these arguments:
-p atmega328p -c arduino -P com9 -U flash:w:$(BinDir)\$(TargetName).hex:i
The hex file in your MicroChip Studio project will be uploaded to an Arduino uno on com port 9. Change to your com port number.
Look at the Built-In LED connected to PB5 which is D13 on the Arduino board. It blinks about once per second. PORTB is a register inside of the Atmega328p chip on the Arduino board. PORTB also connects to pins D8 to D13 on the board.
Comments