.. note:: Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts. **Why Join?** - **Expert Support**: Solve post-sale issues and technical challenges with help from our community and team. - **Learn & Share**: Exchange tips and tutorials to enhance your skills. - **Exclusive Previews**: Get early access to new product announcements and sneak peeks. - **Special Discounts**: Enjoy exclusive discounts on our newest products. - **Festive Promotions and Giveaways**: Take part in giveaways and holiday promotions. 👉 Ready to explore and create with us? Click [|link_sf_facebook|] and join today! .. _1.1.5_c: 1.1.5 4-Digit 7-Segment Display ==================================== Introduction ----------------- Next, follow me to try to control the 4-digit 7-segment display. Required Components ------------------------------ In this project, we need the following components. .. image:: ../img/list_4_digit.png It's definitely convenient to buy a whole kit, here's the link: .. list-table:: :widths: 20 20 20 :header-rows: 1 * - Name - ITEMS IN THIS KIT - LINK * - Raphael Kit - 337 - |link_Raphael_kit| You can also buy them separately from the links below. .. list-table:: :widths: 30 20 :header-rows: 1 * - COMPONENT INTRODUCTION - PURCHASE LINK * - :ref:`cpn_gpio_board` - |link_gpio_board_buy| * - :ref:`cpn_breadboard` - |link_breadboard_buy| * - :ref:`cpn_wires` - |link_wires_buy| * - :ref:`cpn_resistor` - |link_resistor_buy| * - :ref:`cpn_4_digit` - \- * - :ref:`cpn_74hc595` - |link_74hc595_buy| .. note:: In this projiect, for the 4-Digit 7-Segment Display we should use BS model,if you use AS model it may not light up. Schematic Diagram -------------------------- ============ ======== ======== === T-Board Name physical wiringPi BCM GPIO17 Pin 11 0 17 GPIO27 Pin 13 2 27 GPIO22 Pin 15 3 22 SPIMOSI Pin 19 12 10 GPIO18 Pin 12 1 18 GPIO23 Pin 16 4 23 GPIO24 Pin 18 5 24 ============ ======== ======== === .. image:: ../img/schmatic_4_digit.png Experimental Procedures ----------------------------------- **Step 1**: Build the circuit. .. image:: ../img/image80.png **Step 2**: Go to the folder of the code. .. raw:: html .. code-block:: cd ~/raphael-kit/c/1.1.5/ **Step 3**: Compile the code. .. raw:: html .. code-block:: gcc 1.1.5_4-Digit.c -lwiringPi **Step 4**: Run the executable file. .. raw:: html .. code-block:: sudo ./a.out After the code runs, the program takes a count, increasing by 1 per second, and the 4-digit 7-segment display displays the count. .. note:: If it does not work after running, or there is an error prompt: \"wiringPi.h: No such file or directory\", please refer to :ref:`install_wiringpi`. **Code** .. code-block:: c #include #include #include #include #include #define SDI 5 #define RCLK 4 #define SRCLK 1 const int placePin[] = {12, 3, 2, 0}; unsigned char number[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90}; int counter = 0; void pickDigit(int digit) { for (int i = 0; i < 4; i++) { digitalWrite(placePin[i], 0); } digitalWrite(placePin[digit], 1); } void hc595_shift(int8_t data) { int i; for (i = 0; i < 8; i++) { digitalWrite(SDI, 0x80 & (data << i)); digitalWrite(SRCLK, 1); delayMicroseconds(1); digitalWrite(SRCLK, 0); } digitalWrite(RCLK, 1); delayMicroseconds(1); digitalWrite(RCLK, 0); } void clearDisplay() { int i; for (i = 0; i < 8; i++) { digitalWrite(SDI, 1); digitalWrite(SRCLK, 1); delayMicroseconds(1); digitalWrite(SRCLK, 0); } digitalWrite(RCLK, 1); delayMicroseconds(1); digitalWrite(RCLK, 0); } void loop() { while(1){ clearDisplay(); pickDigit(0); hc595_shift(number[counter % 10]); clearDisplay(); pickDigit(1); hc595_shift(number[counter % 100 / 10]); clearDisplay(); pickDigit(2); hc595_shift(number[counter % 1000 / 100]); clearDisplay(); pickDigit(3); hc595_shift(number[counter % 10000 / 1000]); } } void timer(int timer1) { if (timer1 == SIGALRM) { counter++; alarm(1); printf("%d\n", counter); } } void main(void) { if (wiringPiSetup() == -1) { printf("setup wiringPi failed !"); return; } pinMode(SDI, OUTPUT); pinMode(RCLK, OUTPUT); pinMode(SRCLK, OUTPUT); for (int i = 0; i < 4; i++) { pinMode(placePin[i], OUTPUT); digitalWrite(placePin[i], HIGH); } signal(SIGALRM, timer); alarm(1); loop(); } **Code Explanation** .. code-block:: c const int placePin[] = {12, 3, 2, 0}; These four pins control the common anode pins of the four-digit 7-segment display. .. code-block:: c unsigned char number[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90}; A segment code array from 0 to 9 in Hexadecimal (Common anode). .. code-block:: c void pickDigit(int digit) { for (int i = 0; i < 4; i++) { digitalWrite(placePin[i], 0); } digitalWrite(placePin[digit], 1); } Select the place of the value. there is only one place that should be enable each time. The enabled place will be written high. .. code-block:: c void loop() { while(1){ clearDisplay(); pickDigit(0); hc595_shift(number[counter % 10]); clearDisplay(); pickDigit(1); hc595_shift(number[counter % 100 / 10]); clearDisplay(); pickDigit(2); hc595_shift(number[counter % 1000 / 100]); clearDisplay(); pickDigit(3); hc595_shift(number[counter % 10000 / 1000]); } } The functionis used to set the number displayed on the 4-digit 7-segment display. * ``clearDisplay()``:write in 11111111 to turn off these eight LEDs on 7-segment display so as to clear the displayed content. * ``pickDigit(0)``:pick the fourth 7-segment display. * ``hc595_shift(number[counter%10])``:the number in the single digit of counter will display on the forth segment. .. code-block:: c signal(SIGALRM, timer); This is a system-provided function, the prototype of code is: .. code-block:: c sig_t signal(int signum,sig_t handler); After executing the ``signal()`` , once the process receives the corresponding signum (in this case SIGALRM), it immediately pauses the existing task and processes the set function (in this case ``timer(sig)`` ). .. code-block:: c alarm(1); This is also a system-provided function. The code prototype is: .. code-block:: c unsigned int alarm (unsigned int seconds); It generates a SIGALRM signal after a certain number of seconds. .. code-block:: c void timer(int timer1) { if (timer1 == SIGALRM) { counter++; alarm(1); printf("%d\n", counter); } } We use the functions above to implement the timer function. After the ``alarm()`` generates the SIGALRM signal, the timer function is called. Add 1 to counter, and the function, ``alarm(1)`` will be repeatedly called after 1 second. Phenomenon Picture ----------------------- .. image:: ../img/image81.jpeg