0% found this document useful (0 votes)
53 views11 pages

8.circuito Con Un Led (Parte 4)

The document discusses using registers to control multiple LEDs with a single line of code. It explains that each register controls 8 pins and has 8 bits that correspond to the pin modes (output or input) and states (high or low). Setting the bits in the DDR and PORT registers allows controlling 8 LEDs at once, making programming animations and circuits with many LEDs much easier than using individual pinMode and digitalWrite statements for each pin. However, the bit positions in the registers do not exactly match the physical pin numbers, so it is best to use pins 0-7 when first learning to use registers.

Uploaded by

carlo belli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views11 pages

8.circuito Con Un Led (Parte 4)

The document discusses using registers to control multiple LEDs with a single line of code. It explains that each register controls 8 pins and has 8 bits that correspond to the pin modes (output or input) and states (high or low). Setting the bits in the DDR and PORT registers allows controlling 8 LEDs at once, making programming animations and circuits with many LEDs much easier than using individual pinMode and digitalWrite statements for each pin. However, the bit positions in the registers do not exactly match the physical pin numbers, so it is best to use pins 0-7 when first learning to use registers.

Uploaded by

carlo belli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Registers

Program an LED Light Show


Using pinMode() and digitalWrite() to
control LEDs requires one line of code
per pin. This means a LOT of code for
complicated animations and circuits
with lots of LEDs.
You can use registers to control
8 LEDs with a single line of
code! This makes programming
LED patterns much easier.
Each register has 8 bits:

Each bit can be 0 or 1.

7 6 5 4 3 2 1 0

Bit 7 Bit 0

Count the bits from right to left, starting at 0.


The bits in the register correspond to physical pins on the Arduino:

Bit 7
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0

(We will talk about pins 8-13 a little later.)


The DDRD register replaces the pinMode() command.
Setting a bit to 1 sets the pin as an output.
Setting a bit to 0 sets the pin as an input.

For example:

DDRD = 0b11111111;

This line of code sets pins 0-7 as


outputs.

Note: the “0b” tells the Arduino


that this number is in binary.
The PORTD register replaces the digitalWrite() command.
Setting a bit to 1 sets the pin HIGH.
Setting a bit to 0 sets the pin LOW.

For example:

PORTD = 0b10000000;

This line of code sets pin 7 high and


all the other pins low.
You can now shrink eight lines of code into a single line!
pinMode(0, OUTPUT);
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT); DDRD = 0b11111111;
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);

digitalWrite(0, HIGH);
digitalWrite(1, LOW);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW); PORTD = 0b10000000;
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
But what about Arduino pins 8-13?

?
They are controlled by the DDRB and PORTB registers.

DDRB: set pin mode (input or output)


PORTB: set pin high or low if it’s an output
But be careful – the bit numbers in the registers do not match up to
the Arduino pin numbers! This can be confusing, so it’s easier to
work with pins 0-7 when you are first learning to use registers.

Bit 5 For example, these two lines of code would


Bit 4 set all the pins as outputs, then set pin 13
Bit 3 (bit 5) high:
Bit 2
Bit 1 DDRB = 0b00111111;
PORTB = 0b00100000;
Bit 0

You might also like