Tin tức - Blog

Code mẫu sản phẩm Module hiển thị led 7 thanh 0.56inch sử dụng IC 74HC595

30/06/2026

Code mẫu sản phẩm 

Module hiển thị led 7 thanh 0.56inch sử dụng IC 74HC595

 

#include <Arduino.h>

const uint8_t PIN_LATCH = 10;  // ST_CP

const uint8_t PIN_CLOCK = 13;  // SH_CP

const uint8_t PIN_DATA = 11;   // DS

const bool COMMON_ANODE = true;

const uint8_t SEG_FONT_CC[12] = {

  0x3F,  // 0

  0x06,  // 1

  0x5B,  // 2

  0x4F,  // 3

  0x66,  // 4

  0x6D,  // 5

  0x7D,  // 6

  0x07,  // 7

  0x7F,  // 8

  0x6F,  // 9

  0x40,  // '-'

  0x00   // blank

};

void write4(uint8_t b3, uint8_t b2, uint8_t b1, uint8_t b0) {

  digitalWrite(PIN_LATCH, LOW);

  shiftOut(PIN_DATA, PIN_CLOCK, MSBFIRST, b3);

  shiftOut(PIN_DATA, PIN_CLOCK, MSBFIRST, b2);

  shiftOut(PIN_DATA, PIN_CLOCK, MSBFIRST, b1);

  shiftOut(PIN_DATA, PIN_CLOCK, MSBFIRST, b0);

  digitalWrite(PIN_LATCH, HIGH);

}

uint8_t segOut(uint8_t segCC) {

  return COMMON_ANODE ? (uint8_t)~segCC : segCC;

}

void displayNumber(int value) {

  if (value < 0) value = 0;

  if (value > 9999) value = value % 10000;

 

  int d3 = (value / 1000) % 10;

  int d2 = (value / 100) % 10;

  int d1 = (value / 10) % 10;

  int d0 = (value / 1) % 10;

  uint8_t b0 = segOut(SEG_FONT_CC[d0]);

  uint8_t b1 = segOut(SEG_FONT_CC[d1]);

  uint8_t b2 = segOut(SEG_FONT_CC[d2]);

  uint8_t b3 = segOut(SEG_FONT_CC[d3]);

  write4(b0, b1, b2, b3);

}

void setup() {

  pinMode(PIN_LATCH, OUTPUT);

  pinMode(PIN_CLOCK, OUTPUT);

  pinMode(PIN_DATA, OUTPUT);

  displayNumber(0);

}

void loop() {

  static uint32_t last = 0;

  static int v = 0;

  if (millis() - last >= 50) {

    last = millis();

    displayNumber(v);

    v++;

    if (v > 9999) v = 0;

  }

}

Bình luận (0)

Viết bình luận :

Hotline
0945485187