Skip to content

Instantly share code, notes, and snippets.

@joefutrelle
Last active June 11, 2021 16:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joefutrelle/662c9245231158f886af to your computer and use it in GitHub Desktop.
Save joefutrelle/662c9245231158f886af to your computer and use it in GitHub Desktop.
attiny85 driving an Adafruit HT16K33 4-digit alphanumeric LED display
/* Name: main.c
* Author: Joe Futrelle
* License: CC0 - Free as in freedom
*/
/* Adafruit 4-digit alphanumeric backpack, I2C
* Via USI_TWI_master from application note AVR310
* http://www.atmel.com/Images/Atmel-2561-Using-the-USI-Module-as-a-I2C-Master_AP-Note_AVR310.pdf
*/
/* Datasheet for HT16K33
* http://www.adafruit.com/datasheets/ht16K33v110.pdf
*/
/* Adafruit's Arduino library:
* https://github.com/adafruit/Adafruit-LED-Backpack-Library/blob/master/Adafruit_LEDBackpack.cpp
*/
#include <avr/io.h>
#include <stdlib.h>
#include <util/delay.h>
#include "USI_TWI_Master.h"
#define HT16K33_ADDR 0x70
#define HT16K33_CMD_OSC_ON 0x21
#define HT16K33_CMD_BLINK 0x80
#define HT16K33_BLINK_DISPLAYON 0x01
#define HT16K33_BLINK_OFF 0
#define HT16K33_BLINK_2HZ 1
#define HT16K33_BLINK_1HZ 2
#define HT16K33_BLINK_HALFHZ 3
#define HT16K33_CMD_BRIGHTNESS 0xE0
uint16_t a4_font[] = {
0b0000000000111111, // 0
0b0000000000000110, // 1
0b0000000011011011, // 2
0b0000000011001111, // 3
0b0000000011100110, // 4
0b0000000011101101, // 5
0b0000000011111101, // 6
0b0000000000000111, // 7
0b0000000011111111, // 8
0b0000000011101111, // 9
0b0000000011110111, // A
0b0001001010001111, // B
0b0000000000111001, // C
0b0001001000001111, // D
0b0000000011111001, // E
0b0000000011110001, // F
0b0000000010111101, // G
0b0000000011110110, // H
0b0001001000000000, // I
0b0000000000011110, // J
0b0010010001110000, // K
0b0000000000111000, // L
0b0000010100110110, // M
0b0010000100110110, // N
0b0000000000111111, // O
0b0000000011110011, // P
0b0010000000111111, // Q
0b0010000011110011, // R
0b0000000011101101, // S
0b0001001000000001, // T
0b0000000000111110, // U
0b0000110000110000, // V
0b0010100000110110, // W
0b0010110100000000, // X
0b0001010100000000, // Y
0b0000110000001001, // Z
};
#define WRITE_MODE (HT16K33_ADDR << 1)
#define READ_MODE ((HT16K33_ADDR << 1) | 1)
#define TX(b,n) USI_TWI_Start_Transceiver_With_Data(b,n)
void a4_cmd(uint8_t cmd) {
uint8_t buf[] = {
WRITE_MODE,
cmd
};
TX(buf,2);
}
void a4_display(uint16_t *raw) {
uint8_t buf[] = {
WRITE_MODE,
0x00,
raw[0] & 0xFF,
raw[0] >> 8,
raw[1] & 0xFF,
raw[1] >> 8,
raw[2] & 0xFF,
raw[2] >> 8,
raw[3] & 0xFF,
raw[3] >> 8
};
TX(buf,10);
}
void a4_clear() {
uint16_t raw[] = { 0x0000, 0x0000, 0x0000, 0x0000 };
a4_display(raw);
}
void a4_osc_on() {
uint8_t buf[] = {
WRITE_MODE,
0x21
};
TX(buf,2);
}
void a4_begin() {
a4_cmd(HT16K33_CMD_OSC_ON);
a4_cmd(HT16K33_CMD_BRIGHTNESS | 15); // set brightness to 15
a4_cmd(HT16K33_CMD_BLINK | HT16K33_BLINK_DISPLAYON); // no blink
a4_clear();
}
void a4_hex(uint16_t val) {
uint16_t raw[4];
int i;
for(i = 0; i < 4; i++) {
uint8_t b = (val >> (12 - (4 * i))) & 0xF;
raw[i] = a4_font[b];
}
a4_display(raw);
}
// does not accept numbers yet!
void a4_text(char *text) {
uint16_t raw[4];
int i;
for(i = 0; i < 4; i++) {
uint8_t j = (*(text + i)) - 87;
raw[i] = a4_font[j];
}
a4_display(raw);
}
char* pattern[] = {
"bush",
NULL,
NULL,
NULL,
"dole",
NULL,
NULL,
"bush",
NULL,
"bush",
"bush",
NULL,
"dole",
NULL,
NULL,
NULL
};
int main(void)
{
USI_TWI_Master_Initialise();
a4_begin();
a4_text("tiny");
_delay_ms(200);
_delay_ms(200);
_delay_ms(200);
_delay_ms(200);
a4_clear();
_delay_ms(200);
_delay_ms(200);
_delay_ms(200);
_delay_ms(200);
_delay_ms(200);
for(;;) {
int i = 0;
for(i = 0; i < 16; i++) {
if(pattern[i]) {
a4_text(pattern[i]);
_delay_ms(100);
a4_clear();
_delay_ms(150);
} else {
a4_clear();
_delay_ms(250);
}
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment