Hi there!
I'm relatively new to this. I'm programming an ATTiny2313 from my Arduino UNO. The ATTiny2313 controls a 16 channel LED driver that is connected to two separate seven segment displays (no multiplexing).
My current code confirms that I can individually set each segment with a 16-bit data stream latched to the LED driver:
#include <avr/io.h>
// Pins that the LED driver is connected to
#define DATA_PIN 9
#define CLOCK_PIN 8
#define LATCH_PIN 7
#define BLANK_PIN 6
// 16-bit data string to turn on all LEDs
#define ALL_LEDS_ON 0xFFFF
void setup() {
// Set the pins as outputs
pinMode(DATA_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
pinMode(BLANK_PIN, OUTPUT);
digitalWrite(BLANK_PIN, HIGH);
}
void loop() {
// Set all LEDs on
setAllLedsOn();
digitalWrite(BLANK_PIN, LOW);
}
// Function to set all LEDs on
void setAllLedsOn() {
// Send the data for the segment
shiftOut(ALL_LEDS_ON);
// Latch the data
digitalWrite(LATCH_PIN, HIGH);
digitalWrite(LATCH_PIN, LOW);
}
// Function to shift out a 16-bit data
void shiftOut(uint16_t data) {
// Shift out the data one bit at a time
for (int i = 0; i < 16; i++) {
// Set the data pin to the current bit of the data
if (data & 1) {
digitalWrite(DATA_PIN, HIGH);
} else {
digitalWrite(DATA_PIN, LOW);
}
// Clock the data
digitalWrite(CLOCK_PIN, HIGH);
digitalWrite(CLOCK_PIN, LOW);
// Shift the data one bit to the right
data >>= 1;
}
}
However, I would like to make a countdown timer that starts at 99 and counts down. So I can't simply input 0xFFFF to switch all segments on. I gather I have to somehow separate the two digits of my decimal number, convert them into binary (or HEX, which would then be converted to binary again during shiftOut, which seems like an unnecessary additional step), and then combine the two 8-bit bytes into a 16-bit data stream again.
How do I best go about this?
I know that I can separate the numbers with "% 10" and "/ 10 % 10", but where do I go from there? How do I convert them into binary and save them somewhere in a byte to be shifted out?
I guess I could simply use the shiftOut function to first shift out the first byte, then the second byte, and only then use the latch, so that the LED driver receives a single 16-bit data stream, but the whole "splitting-apart, converting-into-binary and then stichting-together-again" process eludes me...
I'd be very glad if you could help me with this! Thanks in advance.