Low Level Arduino Programming — LED Blink (Part 1)

Required Tools

sudo apt install gcc-avr avrdude

Registers

Modifying Registers

PORTB |= (1 << PORTB5);
PORTB |= (1 << PORTB5) | (1 << PORTB2);
PORTB &= ~(1 << PORTB5);
PORTB &= ~((1 << PORTB5) | (1 << PORTB2));
PORTB |= _BV(PORTB5); // _BV(x) is the same as (1 << x)

Controlling I/O Pins

Pin Layout

The pins on the Arduino Uno.

The I/O Registers

DDRB

We will need to set DDB5 to 1 to configure the LED as an output.

PORTB

PINB

Bringing It All Together

#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
DDRB |= _BV(DDB5); // Set LED as an output
while (1)
{
PORTB |= _BV(PORTB5);
_delay_ms(500);
PORTB &= ~_BV(PORTB5);
_delay_ms(500);
}
return 0; // We will never get here!
}
while (1)
{
PINB |= _BV(PINB5);
_delay_ms(500);
}

Building and Programming

avr-gcc -Os -DF_CPU=16000000 -mmcu=atmega328p -o blink.elf blink.c
avr-objcopy -O ihex blink.elf blink.hex
avrdude -p atmega328p -c arduino -P /dev/ttyACM0 -b 115200 -U flash:w:blink.hex

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Test-With-Expect: A BDD-style Go naming pattern

Data Center : An Overview

Supercharge your Mac - Replace the spotlight

A primer on the JAMstack

AWS RDS: Amazon Relational Database Service

Restarting the grind.

How Ionic Application Development Help SMEs to Grow in 2019

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sam Munday-Hall

Sam Munday-Hall

More from Medium

How integers are stored in memory using two’s complement

View vs Copy in NumPy

Variadic Functions

In depth of static libraries in C