/*
Raspberry Pi driving the HT16K33
port from here
https://github.com/lpaseen/ht16k33
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <wiringPi.h>
#include <wiringPiI2C.h>
#define I2CADR 0x70 // I2Cアドレス
// HT16K33コマンド
#define
HT16K33_DDAP
0x00 // Display data address pointer: 0000xxxx
#define
HT16K33_SS
0x20 // System setup register
#define HT16K33_SS_STANDBY 0x00 //
System setup - oscillator in standby mode
#define HT16K33_SS_NORMAL 0x01 //
System setup - oscillator in normal mode
#define
HT16K33_KDAP
0x40 // Key Address Data Pointer
#define
HT16K33_IFAP
0x50 // Read status of INT flag
#define
HT16K33_DSP
0x80 // Display setup
#define
HT16K33_DSP_OFF 0x00
// Display setup - display off
#define
HT16K33_DSP_ON
0x01 // Display setup - display on
#define HT16K33_DSP_NOBLINK 0x00 // Display
setup - no blink
#define HT16K33_DSP_BLINK2HZ 0x02 // Display setup -
2hz blink
#define HT16K33_DSP_BLINK1HZ 0x04 // Display setup -
1hz blink
#define HT16K33_DSP_BLINK05HZ 0x06 // Display setup -
0.5hz blink
#define
HT16K33_RIS
0xA0 // ROW/INT Set
#define
HT16K33_RIS_OUT 0x00
// Set INT as row driver output
#define HT16K33_RIS_INTL
0x01 // Set INT as int active low
#define HT16K33_RIS_INTH
0x03 // Set INT as int active high
#define
HT16K33_DIM
0xE0 // Dimming set
#define
HT16K33_DIM_1
0x00 // Dimming set - 1/16
#define
HT16K33_DIM_2
0x01 // Dimming set - 2/16
#define
HT16K33_DIM_3
0x02 // Dimming set - 3/16
#define
HT16K33_DIM_4
0x03 // Dimming set - 4/16
#define
HT16K33_DIM_5
0x04 // Dimming set - 5/16
#define
HT16K33_DIM_6
0x05 // Dimming set - 6/16
#define
HT16K33_DIM_7
0x06 // Dimming set - 7/16
#define
HT16K33_DIM_8
0x07 // Dimming set - 8/16
#define
HT16K33_DIM_9
0x08 // Dimming set - 9/16
#define
HT16K33_DIM_10
0x09 // Dimming set - 10/16
#define
HT16K33_DIM_11
0x0A // Dimming set - 11/16
#define
HT16K33_DIM_12
0x0B // Dimming set - 12/16
#define
HT16K33_DIM_13
0x0C // Dimming set - 13/16
#define
HT16K33_DIM_14
0x0D // Dimming set - 14/16
#define
HT16K33_DIM_15
0X0E // Dimming set - 15/16
#define
HT16K33_DIM_16
0x0F // Dimming set - 16/16
#define NLEDS
8 // Number of Digit
// グローバル変数
//unsigned short buf[8]; //
8x16ドットパターン
// HT16K33 コマンド送信
void ht_command(int i2cd, uint8_t reg, uint8_t cmd) {
int ret;
ret = wiringPiI2CWrite(i2cd, reg | cmd);
// printf("reg=%02x cmd=%02x ret=%d\n",reg,cmd,ret);
}
#define SA 0x01
#define SB 0x02
#define SC 0x04
#define SD 0x08
#define SE 0x10
#define SF 0x20
#define SG 0x40
#define SDP 0x80
void ht_8segment(int i2cd, int digit, int display) {
uint8_t pattern[] = {
SA+SB+SC+SD+SE+SF,
// 0
SB+SC,
// 1
SA+SB+SD+SE+SG,
// 2
SA+SB+SC+SD+SG,
// 3
SB+SC+SF+SG,
// 4
SA+SC+SD+SF+SG,
// 5
SA+SC+SD+SE+SF+SG,
// 6
SA+SB+SC,
// 7
SA+SB+SC+SD+SE+SF+SG,
// 8
SA+SB+SC+SD+SF+SG,
// 9
SG,
// -
SB+SC+SE+SF+SG,
// H
SA+SD+SE+SF+SG,
// E
SD+SE+SF,
// L
SA+SB+SE+SF+SG,
// P
0
// Space
};
int addr;
if (display > 15) return;
uint8_t data = pattern[display];
if (digit < 4) {
addr = digit * 2;
} else {
addr = (digit-4) * 2 + 1;
}
// printf("display=%d addr=%d
data=%02x\n",display,addr,data);
int reg = HT16K33_DDAP | addr;
int ret = wiringPiI2CWriteReg8(i2cd, reg, data);
}
void displayNumber(int i2cd, int digit, uint32_t number) {
int i;
uint8_t data;
uint32_t num = number;
for(i=0;i<digit;i++) {
data = num % 10;
// printf("data[%d]=%d ",i,data);
ht_8segment(i2cd, i
,data); // displays the 1 digit
number
num = num / 10;
}
// printf("\n");
}
int main(int argc, char **argv){
int i2cd;
i2cd = wiringPiI2CSetup(I2CADR);
if (i2cd < 0 ) {
printf("wiringPiI2CSetup error
%d\n",i2cd);
return;
}
// Wakeup
ht_command(i2cd, HT16K33_SS, HT16K33_SS_NORMAL);
// Display on and no blinking
ht_command(i2cd, HT16K33_DSP, HT16K33_DSP_ON |
HT16K33_DSP_NOBLINK);
// INT pin works as row output
ht_command(i2cd, HT16K33_RIS, HT16K33_RIS_OUT);
// Brightness set to max
ht_command(i2cd, HT16K33_DIM, HT16K33_DIM_16);
/*
Display Memory is 16 byte(Register Address is
0x00-0x0F)
ROW0-7 ROW8-15
COM0 00H=Digit1
01H=Digit5
COM1 02H=Digit2
03H=Digit6
COM2 04H=Digit3
05H=Digit7
COM3 06H=Digit4
07H=Digit8
COM4 08H=Digit9
09H=Digit13
COM5 0AH=Digit10
0BH=Digit14
COM6 0CH=Digit11
0DH=Digit15
COM7 0EH=Digit12
0FH=Digit16
*/
int addr;
int reg;
int ret;
// Clear all memory
for(addr=0;addr<16;addr++) {
reg = HT16K33_DDAP | addr;
ret = wiringPiI2CWriteReg8(i2cd, reg,
0);
}
int digit;
int display;
for(display=0;display<16;display++) {
for(digit=0;digit<NLEDS;digit++) {
ht_8segment(i2cd, digit,
display);
}
delay(500);
}
uint32_t number = 1;
for(digit=0;digit<NLEDS;digit++) {
displayNumber(i2cd, NLEDS, number);
number = number * 10 + (digit+2);
//printf("number=%d\n",number);
delay(500);
}
// Change Brightness
int bright;
for(bright=0x0f;bright>0;bright--) {
ht_command(i2cd, HT16K33_DIM, bright);
delay(500);
}
}
|