/*
Project: ShiftRegister_7Seg_Demo
Description: In a real circuit add transistors on each digit!
(and invert "DIGIT_PINS")
The gray wires are digits, colored are the segments.
Coded for common anode display.
Creation date: 2/29/24
Author: AnonEngineering
License: https://en.wikipedia.org/wiki/Beerware
Discord | Arduino | hardware-help
Arduino 74hc595 7 segment display
fendidrp — 02/26/2024 10:50 AM
We are Working now with the STM32 Nucleo L152RE
in Combination on the Multi-Function-Shield.
www.makershop.de/download/arduino-multi-function-shield.pdf
We have to controll the 7 Segment Display with
the "74hc595" Shift Register.
On a Nucleo 64 Board
#define BUTTON_1_PIN A1 Clear
#define BUTTON_2_PIN A2 Decrement
#define BUTTON_3_PIN A3 Increment
#define LATCH_PIN 4
#define CLK_PIN 7
#define DATA_PIN 8
*/
#include <mechButton.h>
// pin definitions
const int BTN_UP = A3;
const int BTN_DN = A2;
const int BTN_CLR = A1;
const int CLOCK_PIN = 7;
const int LATCH_PIN = 4;
const int DATA_PIN = 8;
// bits to set for digit driver
const uint8_t DIGIT_PINS[] = {0x80, 0x40, 0x20, 0x10};
// lookup table, which segments are on for each number,
// hex values for each digit 0 - 9 & blank
const int SEG_BYTES[] = {
0xFC, /* 0 */ 0x60, /* 1 */ 0xDA, /* 2 */ 0xF2, /* 3 */ 0x66, /* 4 */
0xB6, /* 5 */ 0xBE, /* 6 */ 0xE0, /* 7 */ 0xFE, /* 8 */ 0xF6, /* 9 */
0x00 /* blank */
};
int countVal = 0;
mechButton UpBtn(BTN_UP);
mechButton DnBtn(BTN_DN);
mechButton ClrBtn(BTN_CLR);
// for hardware test
void testDisplay() {
writeShiftDigit(0, 0);
writeShiftDigit(1, 1);
writeShiftDigit(2, 2);
writeShiftDigit(3, 3);
}
void updateDisplay() {
int displayValue = countVal;
int displayValK = 0;
int displayValH = 0;
int displayValD = 0;
int displayValU = 0;
// leading zero blanking
if (countVal < 1000) {
displayValK = 10; // blank
} else {
displayValK = (displayValue % 10000) / 1000; // thousands
}
if (countVal < 100) {
displayValH = 10; // blank
} else {
displayValH = (displayValue % 1000) / 100; // hundreds
}
if (countVal < 10) {
displayValD = 10; // blank
} else {
displayValD = (displayValue % 100) / 10; // tens
}
displayValU = displayValue % 10; // units
// write values to display
writeShiftDigit(0, displayValK);
writeShiftDigit(1, displayValH);
writeShiftDigit(2, displayValD);
writeShiftDigit(3, displayValU);
}
void writeShiftDigit(int digit, int number) {
digitalWrite(LATCH_PIN, LOW);
// shift out highbyte
shiftOut(DATA_PIN, CLOCK_PIN, LSBFIRST, DIGIT_PINS[digit]);
// shift out lowbyte
shiftOut(DATA_PIN, CLOCK_PIN, LSBFIRST, ~SEG_BYTES[number]);
digitalWrite(LATCH_PIN, HIGH);
}
// button handlers
void clrCallback(void) {
if (ClrBtn.trueFalse()) {
countVal = 0;
}
}
void dnCallback(void) {
if (DnBtn.trueFalse()) {
countVal = countVal - 10;
}
}
void upCallback(void) {
if (UpBtn.trueFalse()) {
countVal = countVal + 10;
}
}
void setup() {
//Serial.begin(115200); // for test
pinMode(DATA_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
UpBtn.setCallback(upCallback);
DnBtn.setCallback(dnCallback);
ClrBtn.setCallback(clrCallback);
}
void loop() {
// let idlers run
idle();
// update display often
updateDisplay();
//testDisplay();
}
Loading
st-nucleo-c031c6
st-nucleo-c031c6