Patrick Fitzgerald
Published © GPL3+

Marquee Light Demo on Arduino Uno PORTD

All 8 pins of PORTD are available on the IO pins of the Uno board. Let's do some demo sketches that use this byte.

AdvancedProtip3 hours62

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Marquee LED bar arduino
×1

Software apps and online services

Arduino IDE
Arduino IDE
atmel studio

Story

Read more

Code

demoD.ino

Arduino
Arduino sketch written in C
demos binary patterns on port D
assumes LED bar to view
#define reps 3
#include <avr/io.h>
#include <util/delay.h>
int main() {
  DDRD = 0xFF; // PORTD is all output
  while (1) {
    // decimal version of PORTD = 1 then PORTD = 0
    PORTD = 1;         _delay_ms(100);      // PORTD.0 on
    PORTD = 0;         _delay_ms(100);      // PORTD.0 off
    // decimal version of PORTD = 255 then PORTD = 0
    PORTD = 255;       _delay_ms(100);
    PORTD = 0;         _delay_ms(100);
    // binary version of PORTD = 1 then PORTD = 0
    PORTD = 0b00000001;    _delay_ms(100);  // led on
    PORTD = 0b00000000;    _delay_ms(100);  // led off
    // binary version of PORTD = 255 then PORTD = 0
    PORTD = 0b11111111;    _delay_ms(100);
    PORTD = 0b00000000;    _delay_ms(100);
    // hexadecimal version of PORTD = 1 then PORTD = 0
    PORTD = 0x01;      _delay_ms(100);      // led on
    PORTD = 0x00;      _delay_ms(100);      // led off
    // hexadecimal version of PORTD = 255 then PORTD = 0
    PORTD = 0xFF;      _delay_ms(100);      // led on
    PORTD = 0x00;      _delay_ms(100);      // led off
    // shifts single bit from PORTD.0 to PORTD.7
    for (int j = 0; j < reps; j++) {
      for (int i = 0; i < 8 ; i++) {
        PORTD = (1 << i);
        _delay_ms(100);
      }
      for (int i = 0; i < 8 ; i++) {
        PORTD = (128 >> i);
        _delay_ms(100);
      }
    }
    // masking of bits with |= and &= operators
    for (int j = 0; j < reps; j++) {
      PORTD |= 0b11111111;  _delay_ms(100);   // mask PORTD = all 1s
      PORTD &= 0b11111111;  _delay_ms(100);   // mask PORTD = all 0s
    }
    // alternate pattern
    for (int j = 0; j < reps; j++) {
      PORTD = 0x55;  _delay_ms(100);          // PORTD = 0b01010101
      PORTD = 0xAA;   _delay_ms(100);         // PORTD = 0b10101010
    }
    // mathematical inverse of PORTD is put into PORTD
    for (int j = 0; j < reps; j++) {
      PORTD = !PORTD; _delay_ms(100);
    }
  }
}

countD.asm

Arduino
AVR assembly code for Arduino Uno ATmega328p
enables PORTD then binary count
name needs to be .asm extension after download
.include "m328pdef.inc"
    .def	mask 	= r16	; mask register
    .def	ledR 	= r17	; led register
    .def	oLoopR 	= r18	; outer loop register
    .def	iLoopRl = r24	; inner loop register low
    .def	iLoopRh = r25	; inner loop register high
    .equ	oVal 	= 80	; outer loop value
    .equ	iVal 	= 800	; inner loop value
    .cseg
    .org	0x00
    clr	    ledR		; clear led register to zero
    ldi	    mask,0xFF		; load the mask register 11111111
    out	    DDRD,mask		; set PORTD to all 8 bits OUT
start:
    out	    PORTD,ledR		; write led register to PORTD
    inc     ledR		; increment ledR from 0 to 255
    ldi     oLoopR,oVal		; initialize outer loop count
oLoop:
     ldi    iLoopRl,LOW(iVal)	; intialize inner loop count in inner
     ldi    iLoopRh,HIGH(iVal)	; loop high and low registers
iLoop:
    sbiw    iLoopRl,1	        ; decrement inner loop register
    brne    iLoop		; branch to iLoop if iLoop register != 0
    dec	    oLoopR		; decrement outer loop register
    brne    oLoop		; branch to oLoop if outer loop register != 0
    rjmp    start		; jump back to start

Credits

Patrick Fitzgerald

Patrick Fitzgerald

112 projects • 37 followers

Comments