LED Interfacing With Arduino UNO With Port Registers
by Electronics_times in Circuits > Arduino
885 Views, 1 Favorites, 0 Comments
LED Interfacing With Arduino UNO With Port Registers
Hello Everyone,
In this Instructable, I will be showing you how to use the Port registers of Arduino UNO using DDR and PORT commands.
DDR stands for "Data Direction Register (DDRx)"
Port Registers:
Port registers allow for lower-level and faster manipulation of the i/o pins of the microcontroller on an Arduino board. The chips used on the Arduino board (the ATmega8 and ATmega168) have three ports:
- B (digital pin 8 to 13)
- C (analog input pins)
- D (digital pins 0 to 7)
Each port is controlled by three registers, which are also defined variables in the Arduino language. The DDR register determines whether the pin is an INPUT or OUTPUT. The PORT register controls whether the pin is HIGH or LOW, and the PIN register reads the state of INPUT pins set to input with pinMode(). The maps of the ATmega8 and ATmega168 chips show the ports. The newer Atmega328p chip follows the pinout of the Atmega168 exactly.
DDR and PORT registers may be both written to, and read. PIN registers correspond to the state of inputs and may only be read.
PORTD maps to Arduino digital pins 0 to 7 :
DDRD - The Port D Data Direction Register - read/write
PORTD - The Port D Data Register - read/write
PIND - The Port D Input Pins Register - read-only
same for PORTB and PORTC.
Before that let's check the pin diagram of Arduino UNO which is containing an IC named ATMEGA328p, which manufacture by Atmel Company now acquired by Microchip.
​Pin Diagram:
Connection:
All the LED Connected in Source Mode and in Common Cathode Mode with the Protection Resister of 330ohm.
Source Code:
Open Arduino IDE:
void setup() {
DDRD = DDRD | B11111100; // this is safer as it sets pins 2 to 7 as outputs
// without changing the value of pins 0 & 1, which are RX & TX
// Or we can use // DDRD = B11111100; // sets Arduino pins 1 to 7 as outputs, pin 0 as input
}
void loop() {
PORTD = B01111100; // sets digital pins 6,5,4,3,2 HIGH
delay(500);
PORTD = B00000000; // sets digital pins 6,5,4,3,2 LOW
delay(500);
PORTD = B01111100; // sets digital pins 6,5,4,3,2 HIGH
delay(500);
PORTD = B00000000; // sets digital pins 6,5,4,3,2 LOW
delay(100);
/// forward and backward patterns ////
PORTD = B01000000; // sets digital pins 6 HIGH delay(100); PORTD = B01100000; delay(100); PORTD = B01110000; delay(100); PORTD = B01111000; delay(100); PORTD = B01111100; delay(100); PORTD = B01111000; delay(100); PORTD = B01110000; delay(100); PORTD = B01100000; delay(100); PORTD = B01000000; delay(100); PORTD = B00000000; delay(100);
}