Collapse OS Documentation Browser


../ 6502/ 6809/ 8086/ avr/ z80/ acia.txt at28.txt avr.txt intro.txt tty.txt

Making an ATmega328P blink

Collapse OS has an AVR assembler and an AVR programmer. If you
have a SPI relay (see doc/hw/spi.txt), then you almost have all
it takes to make an ATmega328P blink.

First, read doc/avr.txt. You'll see that it tells you how to
build an AVR programmer that works with your SPI relay. You
might already have such device. For example, I use the same
device as the one I connect to my Sparkfun AVR Pocket
Programmer, but I've added an on/off switch to it. I then use
a 6-pin ribbon cable to connect it to my SPI relay.

If you have a SD card connected to the same SPI relay, you'll
face a timing challenge: SD specs specifies that the minimum
SPI clock is 100kHz, but depending on your setup, you might end
up with an effective SCK below that. My own clock setup looks
like this:

I have a RC2014 Dual clock which allows me to have easy access
to many clock speeds, but the slowest option is 300kHz, not
slow enough. My SPI relay has a pin for input clock override,
and I built a pluggable 4040 with a switch that selects a
divisor. I plug that module in my SPI relay, then I plug that
into my RC2014 Dual clock. When doing SD card stuff, I select
the "no division" position, and when I communicate with the
AVR chip, I move the switch to increase the divisor.

Once you've done this, you can test that you can communicate
with your AVR chip by doing "160 163 LOADR" (turn off your
programmer or else it might mess up the SPI bus and prevent you
from using your SD card) and then running:

    1 asp$ aspfl@ .x 0 (spie)

(Replace "1" by your SPI device ID) If everything works fine,
you'll get the value of the low fuse of the chip.

Building the blink binary

A blink program for the ATmega328P in Collapse OS would look
like this:

    50 LOAD ( avra ) 65 66 LOADR ( atmega328p ) H@ ORG !
    R16 TCCR0B IN, R16 $05 ORI, TCCR0B R16 OUT,
    R1 CLR,
    L1 LBL! ( loop )
        R16 TIFR0 IN,
        R16 0 ( TOV0 ) SBRS,
            L1 ( loop ) ' RJMP LBL, ( no overflow )
        R16 $01 LDI, TIFR0 R16 OUT,
        R1 INC,
        PORTB 5 CBI,
        R1 7 SBRS,
            PORTB 5 SBI,
        L1 ( loop ) ' RJMP LBL,

See doc/asm.txt for details. For now, you'll paste this into
an arbitrary unused block. Let's use 999.

    $ cd arch/z80/rc2014
    $ xsel > blk/999
    $ rm blkfs
    $ make
    $ dd if=blkfs of=/dev/<your-sdcard> bs=1024

Now, with your updated SD card in your RC2014, let's assemble
this binary:

    999 LOAD
    H@ CREATE end ,
    CREATE wordcnt end ORG - 2 / ,
    : write 1 asp$ asperase wordcnt 0 DO
    ORG I 2 * + @ I aspfb! LOOP
    0 aspfp! 0 (spie) ;

The first line assembles a 16 words binary beginning at ORG,
then the rest of the lines are about writing these 16 words to
the AVR chip (see doc/avr.txt for details). After you've run
this, if everything went well, that chip if it has a LED
attached to PB5, will make that LED blink slowly.

Collapse OS and its documentation are created by Virgil Dupras and licensed under the GNU GPL v3.

This documentation browser by James Stanley. Please report bugs on github or to

This page generated at 2022-08-18 21:05:03 from documentation in CollapseOS snapshot 20220509.