2022年12月5日 星期一

[Arduino] LED Bar display

There is an example in Arduino called LED Bar Graph, which uses a LED bar to simulate an audio signal display. When the volume is low, only a few LEDs will be lit, while when the volume is high, almost all of the LEDs will be lit. Today, I made some modifications to this example.

Here's a learning tip. It's mainly to help understand the representation of digital systems, and through this way, it's easy to understand the relationship between binary, hexadecimal, and hardware. In the early days of doing this experiment, the entire breadboard was filled with TTL ICs. Because it was just beginning to be understood, some classmates spent a lot of time debugging the circuit, just to understand and verify the digital knowledge in the textbook. As a more advanced exercise, this practice can also be used to verify basic operations such as left shift, right shift, OR, and AND operations.

The required materials are:

  • Arduino UNO x 1
  • Red LED x 8
  • 330 ohm resistor x 8
  • Some wires and jumper wires

Reference circuit:

Arduino



Code:


byte value;

void setup() {

DDRD = B11111111;

value = B01010000; //  給於 0x50 值


PORTD = value;


}

void loop() {

}

What will be displayed? If we add an OR operation.


byte value;

void setup() {

DDRD = B11111111;

value = B01010000; // 0x50

PORTD = value | 0x3;


}

void loop() {

}

What if we change it to an AND operation?


byte value;

void setup() {

DDRD = B11111111;

value = B01010000; // 0x50

PORTD = value & 0x10; // AND 運算


}

void loop() {

}

Sometimes, when dealing with more complex scenarios, using the programmer mode of the Windows calculator can help with verification.

If we want to implement left shift, right shift, and scrolling marquee effects, how should we do it?


void setup() {
DDRD = B11111111;
PORTD = B10000000; //0x80
}

void loop() {

for(byte x = 0; x <7 portd="" x="">>= 1;  //右移1個位元
 delay(500); 
}

for(byte x = 0; x <7 1="" code="" delay="" portd="" x="">

"This is a method that directly uses the PORT, but you can also use the 74HC595 shift register. Here we just use it to make bit manipulation easier to understand.

Please note: Because the D0 pin has a wire connected to it, there may be issues when uploading the program. Just remove the wire from D0 before uploading, and then reconnect it after uploading is complete.