Skip to content

Instantly share code, notes, and snippets.

@YuruPuro
Last active May 20, 2023 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YuruPuro/8374f4035241c59ba1c845df98cf4a27 to your computer and use it in GitHub Desktop.
Save YuruPuro/8374f4035241c59ba1c845df98cf4a27 to your computer and use it in GitHub Desktop.
Sample to operate 4-digit 7-segment LED with HT16K33
// Sample to operate 4-digit 7-segment LED with HT16K33
#include <Wire.h>
#define HT16K33_ADDR 0x70
#define SEG_0 0b00111111
#define SEG_1 0b00000110
#define SEG_2 0b01011011
#define SEG_3 0b01001111
#define SEG_4 0b01100110
#define SEG_5 0b01101101
#define SEG_6 0b01111101
#define SEG_7 0b00000111
#define SEG_8 0b01111111
#define SEG_9 0b01101111
#define SEG_A 0b01110111
#define SEG_B 0b01111100
#define SEG_C 0b00111001
#define SEG_D 0b01011110
#define SEG_E 0b01111001
#define SEG_F 0b01110001
#define SEG_L 0b00111000
#define SEG_H 0b01110110
#define SEG_DOT 0b10000000
#define SEG_COL 0b00000010
#define SEG_MINUS 0b01000000
#define SEG_CLR 0b00000000
#define SEG_h 0b01110100
#define DIGITS 5
static const uint8_t DIGITS_ADDR[ ] = {
0,2,6,8,4
};
static const uint8_t DIGITS_PATTERN[ ] = {
SEG_0,SEG_1,SEG_2,SEG_3,SEG_4,SEG_5,SEG_6,SEG_7,
SEG_8,SEG_9,SEG_A,SEG_B,SEG_C,SEG_D,SEG_E,SEG_F
};
// ======================
// HT16K33 Control
void HT16K33_Init( ) {
HT16K33_SendComman(0x21); //Turn on oscillator
HT16K33_Clear( ) ;
HT16K33_SendComman(0x81); //Display ON / Blink off
}
void HT16K33_Clear() {
for (uint8_t i = 0; i < DIGITS; i++) {
HT16K33_SendDisp(i,SEG_CLR) ;
}
}
void HT16K33_Blink(uint8_t val) {
if ( val > 3 ) val = 3;
switch(val) {
case 1: val = 0x02 ; break ;
case 2: val = 0x04 ; break ;
case 3: val = 0x06 ; break ;
default: val = 0x00 ; break ;
}
HT16K33_SendComman(0x81 | val);
}
void HT16K33_Brightness(uint8_t val) {
if ( val > 15 ) val = 15;
HT16K33_SendComman(0xE0 | val);
}
// Send 1Byte
void HT16K33_SendComman(uint8_t data) {
Wire.beginTransmission(HT16K33_ADDR);
Wire.write(data);
Wire.endTransmission();
}
// Send Byte Data
void HT16K33_SendDisp(uint8_t no, uint8_t pattern) {
Wire.beginTransmission(HT16K33_ADDR);
Wire.write(DIGITS_ADDR[no]);
Wire.write(pattern);
Wire.endTransmission();
}
// ======================
void setup() {
// Wire.begin(21,25); // I2C SDA-21,SCL-25 :: When to use M5AtomLITE
Wire.begin(); // When to use ArduinoUNO
HT16K33_Init( ) ;
delay(1000);
}
// ======================
void loop() {
HT16K33_Clear( ) ;
HT16K33_Brightness(2) ;
HT16K33_Blink(0) ;
HT16K33_SendDisp(0,SEG_1) ;
HT16K33_SendDisp(1,SEG_2) ;
HT16K33_SendDisp(2,SEG_3) ;
HT16K33_SendDisp(3,SEG_4) ;
for (int j=0;j<20;j++) {
HT16K33_SendDisp(4,(j%2==0?SEG_COL:SEG_CLR)) ;
delay(250) ;
}
// ----------------
HT16K33_Clear( ) ;
for (int i=0;i<=28;i++) {
HT16K33_SendDisp(0,DIGITS_PATTERN[i%16]) ;
HT16K33_SendDisp(1,DIGITS_PATTERN[(i+1)%16]) ;
HT16K33_SendDisp(2,DIGITS_PATTERN[(i+2)%16]) ;
HT16K33_SendDisp(3,DIGITS_PATTERN[(i+3)%16]) ;
for (int j=0;j<2;j++) {
HT16K33_SendDisp(4,(j%2==0?SEG_COL:SEG_CLR)) ;
delay(250) ;
}
}
delay(5000) ;
// ---------------
HT16K33_Clear( ) ;
HT16K33_Blink(0) ;
HT16K33_SendDisp(0,SEG_0) ;
HT16K33_SendDisp(1,SEG_8) ;
HT16K33_SendDisp(2,SEG_1) ;
HT16K33_SendDisp(3,SEG_h) ;
delay(3000) ;
HT16K33_SendDisp(2,SEG_3) ;
HT16K33_Blink(1) ;
delay(3000) ;
HT16K33_SendDisp(2,SEG_5) ;
HT16K33_Blink(2) ;
delay(3000) ;
HT16K33_SendDisp(2,SEG_7) ;
HT16K33_Blink(3) ;
delay(3000) ;
HT16K33_Blink(0) ;
// ---------------
HT16K33_Clear( ) ;
HT16K33_SendDisp(0,SEG_0) ;
HT16K33_SendDisp(1,SEG_E) ;
HT16K33_SendDisp(2,SEG_F) ;
HT16K33_SendDisp(3,SEG_h) ;
for (int i=15 ;i>=0;i--) {
HT16K33_SendDisp(2,DIGITS_PATTERN[i]) ;
HT16K33_Brightness(i) ;
delay(1000) ;
}
for (int i=1 ;i<=15;i++) {
HT16K33_SendDisp(2,DIGITS_PATTERN[i]) ;
HT16K33_Brightness(i) ;
delay(1000) ;
}
}
// Sample to operate 16x8 LEX MATRIX
#include <Wire.h>
#define I2C_ADDR 0x70
static uint8_t pattern[] = { 0x3C,0x42,0x81,0xA5,0x81,0x99,0x42,0x3C} ;
static uint8_t pattern1[] = { 0x3C,0x00,0x42,0x00,0x81,0x00,0xA5,0x00,0x81,0x00,0x99,0x00,0x42,0x00,0x3C,0x00} ;
static uint8_t pattern2[][8] = {
{0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00} ,
{0x40,0x80,0x00,0x00,0x00,0x00,0x00,0x00} ,
{0x20,0x40,0x80,0x00,0x00,0x00,0x00,0x00} ,
{0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x00} ,
{0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00} ,
{0x04,0x08,0x10,0x20,0x40,0x80,0x00,0x00} ,
{0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x00} ,
{0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80} ,
{0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x40} ,
{0x00,0x00,0x01,0x02,0x04,0x08,0x10,0x20} ,
{0x00,0x00,0x00,0x01,0x02,0x04,0x08,0x10} ,
{0x00,0x00,0x00,0x00,0x01,0x02,0x04,0x08} ,
{0x00,0x00,0x00,0x00,0x00,0x01,0x02,0x04} ,
{0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x02} ,
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01}
} ;
// ======================
// I2C Control
// Send 1Byte Command
void I2CSendCommand(uint8_t command) {
Wire.beginTransmission(I2C_ADDR);
Wire.write(command);
Wire.endTransmission();
}
// Send 2Byte Command
void I2CSendCommand(uint8_t command, uint8_t data1) {
Wire.beginTransmission(I2C_ADDR);
Wire.write(command);
Wire.write(data1);
Wire.endTransmission();
}
// Send Multi Byte Command
void I2CSendCommand(uint8_t command, uint8_t *data,int N) {
Wire.beginTransmission(I2C_ADDR);
Wire.write(command);
for (int i=0;i<N;i++) {
Wire.write(data[i]);
}
Wire.endTransmission();
}
void setup() {
Wire.begin( );
I2CSendCommand(0x21) ; // normal operation mode
I2CSendCommand(0xE7) ; // output pulse width 8/16 duty
I2CSendCommand(0xA0) ; // INT/ROW output pin is set to ROW driver output.
I2CSendCommand(0x81) ; // Blinking OFF / Display on
}
void loop() {
// DEMO1
for (int i=0;i<15;i++) {
for (int j=0;j<8;j++) {
I2CSendCommand(j*2,pattern2[i][j]) ;
}
delay(200) ;
}
for (int i=14;i>=0;i--) {
for (int j=0;j<8;j++) {
I2CSendCommand(j*2,pattern2[i][j]) ;
}
delay(200) ;
}
// DEMO2
for (int j=7;j>0;j--) {
for (int i=0;i<8;i++) {
I2CSendCommand(i*2,pattern[i]>>j) ;
}
delay(200) ;
}
I2CSendCommand(0,pattern1,16) ;
delay(1000) ;
// BLINK
I2CSendCommand(0x83) ; // Blinking 2Hz / Display on
delay(5000) ;
I2CSendCommand(0x81) ; // Blinking OFF / Display on
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment