The Flashing LED

A simple Flashing LED written in assembler for the AVR chipset. I wrote this for the ATMega128…

.include "m128def.inc"  ;ATMEGA128 definitions file

.def temp = R16  

.def d0  = R17
.def d1  = R18
.def d2  = R19

;  The following number is based on the crystal attached to
;  your project. You can modify the 16 to whatever your
;  crystal value (as an integer). Later lessons will explain
;  this delay in more detail.  

.equ OneSecond = 16 * 1000000 / 5

.org $0000 jmp  RESET    ; $0000 HW Reset or Watchdog Handler

RESET:
   ldi  temp,high(RAMEND)
   out  SPH,temp
   ldi  temp,low(RAMEND)
   out  SPL,temp

;  Using PortB, Pin7 as output
;               76543210
   ldi  temp, 0b10000000
   out  DDRB, temp

MAIN:
   sbi  PortB, 7
   rcall delay200

   cbi  PortB, 7
   rcall delay200

   rjmp MAIN

DELAY200:

   ldi  d2, byte3 (OneSecond / 5)
   ldi  d1, high (OneSecond / 5)
   ldi  d0, low  (OneSecond / 5)

   subi d0, 1
   sbci d1, 0
   sbci d2, 0
   brcc pc-3

   ret

One Reply to “The Flashing LED”

  1. Not bad, a bit more commenting might help first timers, but your codes a lot smaller than i’ve seen for a blink on AVR.

Leave a Reply

Your email address will not be published. Required fields are marked *