This circuit consists of an Arduino UNO microcontroller connected to a 4-digit 7-segment display. The Arduino UNO controls the display to show numbers sequentially on each digit. The display is driven by the digital pins of the Arduino, with each segment and digit being controlled individually.
// Khai báo các chân segment
#define SEG_A 2
#define SEG_B 3
#define SEG_C 4
#define SEG_D 5
#define SEG_E 6
#define SEG_F 7
#define SEG_G 8
#define SEG_DP 9
// Khai báo các chân digit (katot chung)
#define DIGIT_1 10
#define DIGIT_2 11
#define DIGIT_3 12
#define DIGIT_4 13
// Mảng lưu cấu hình cho các số từ 0 đến 9 (gồm a, b, c, d, e, f, g)
const byte digits[] = {
0b00111111, // 0
0b00000110, // 1
0b01011011, // 2
0b01001111, // 3
0b01100110, // 4
0b01101101, // 5
0b01111101, // 6
0b00000111, // 7
0b01111111, // 8
0b01101111 // 9
};
// Chân của các segment
const byte segmentPins[] = {SEG_A, SEG_B, SEG_C, SEG_D, SEG_E, SEG_F, SEG_G};
// Chân của các digit
const byte digitPins[] = {DIGIT_1, DIGIT_2, DIGIT_3, DIGIT_4};
// Hàm thiết lập ban đầu
void setup() {
// Cài đặt các chân segment là OUTPUT
for (byte i = 0; i < 7; i++) {
pinMode(segmentPins[i], OUTPUT);
}
// Cài đặt các chân digit là OUTPUT
for (byte i = 0; i < 4; i++) {
pinMode(digitPins[i], OUTPUT);
}
}
// Hàm hiển thị một số trên một digit
void displayDigit(int num, int digit) {
// Tắt tất cả các digit
for (byte i = 0; i < 4; i++) {
digitalWrite(digitPins[i], HIGH);
}
// Bật digit cần hiển thị
digitalWrite(digitPins[digit], LOW);
// Hiển thị số bằng cách điều khiển các segment
byte segments = digits[num];
for (byte i = 0; i < 7; i++) {
digitalWrite(segmentPins[i], (segments >> i) & 0x01);
}
}
// Hàm lặp
void loop() {
// Hiển thị các số lần lượt trên 4 chữ số
displayDigit(1, 0); // Hiển thị số 1 trên digit 1
delay(5);
displayDigit(2, 1); // Hiển thị số 2 trên digit 2
delay(5);
displayDigit(3, 2); // Hiển thị số 3 trên digit 3
delay(5);
displayDigit(4, 3); // Hiển thị số 4 trên digit 4
delay(5);
}
This code initializes the pins connected to the segments and digits of the 7-segment display as outputs. It then sequentially displays the numbers 1, 2, 3, and 4 on the four digits of the display. The displayDigit
function is responsible for turning on the appropriate segments to display a given number on a specified digit.