Skip to content

Instantly share code, notes, and snippets.

@pondahai
Last active July 31, 2022 15:12
Show Gist options
  • Save pondahai/27f93818fc1969b4f16759cc2f3af1a5 to your computer and use it in GitHub Desktop.
Save pondahai/27f93818fc1969b4f16759cc2f3af1a5 to your computer and use it in GitHub Desktop.
int latchPin = 6; // Latch pin (STCP腳位)
int clockPin = 5; // Clock pin (SHCP腳位)
int dataPin = 7; // Data pin (DS腳位)
int blankPin = 4;
void setup()
{
digitalWrite(latchPin, LOW); // 送資料前要先把 latchPin 設成低電位
shiftOut(dataPin, clockPin, MSBFIRST, 0); //
shiftOut(dataPin, clockPin, MSBFIRST, 0); //
shiftOut(dataPin, clockPin, MSBFIRST, 0); //
digitalWrite(latchPin, HIGH); // 送完資料後要把 latchPin 設成高電位
Serial.begin(115200);
// Set all the pins of 74HC595 as OUTPUT
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(blankPin, OUTPUT);
}
void iv18_digit(int digit, int num)
{
unsigned char byte1,byte2,byte3;
byte3=0xff;
int digit_convert_array[9]={12,19,13,18,14,17,16,15,11};
// int digit_convert_array[9]={15,14,13,16,12,17,11,18,19};
// int digit_convert_array[9]={18,11,17,12,16,13,14,15,19};
byte1=0x10>>(20-digit_convert_array[digit]);
byte2=0x100>>(16-digit_convert_array[digit]);
byte2|=0x07;
// #define dp 1<<7
// #define a 1
// #define b 1<<2
// #define c 1<<5
// #define d 1<<6
// #define e 1<<4
// #define f 1<<1
// #define g 1<<3
#define dp 1<<0
#define a 1<<7
#define b 1<<5
#define c 1<<2
#define d 1<<1
#define e 1<<3
#define f 1<<6
#define g 1<<4
int segment_convert_array[11]={a|b|c|d|e|f, b|c, a|b|g|e|d,
a|b|g|c|d, f|g|b|c, a|f|g|c|d,
a|f|g|c|d|e, a|b|c, a|b|c|d|e|f|g, a|b|c|d|f|g,0};
byte3 = segment_convert_array[num];
//byte3 = 0x80;
digitalWrite(latchPin, LOW); // 送資料前要先把 latchPin 設成低電位
shiftOut(dataPin, clockPin, MSBFIRST, byte1); //
shiftOut(dataPin, clockPin, MSBFIRST, byte2); //
shiftOut(dataPin, clockPin, MSBFIRST, byte3); //
digitalWrite(latchPin, HIGH); // 送完資料後要把 latchPin 設成高電位
// Serial.print(digit,HEX);Serial.print(" ");
// Serial.print(digit_convert_array[digit],DEC);Serial.print(" ");
// Serial.print(byte1,HEX);Serial.print(" ");
// Serial.print(byte2,HEX);Serial.print(" ");
// Serial.println(byte3,HEX);
}
void print_iv18(float num)
{
char str[20];
dtostrf(num, 1, 7, str);
//sprintf(str,"%f",num);
Serial.println(str);
}
int fetch_digit(long n,int i){
int r;
switch(i){
case 0:
r=(n/1L)%10;
break;
case 1:
r=(n/10L)%10;
break;
case 2:
r=(n/100L)%10;
break;
case 3:
r=(n/1000L)%10;
break;
case 4:
r=(n/10000L)%10;
break;
case 5:
r=(n/100000L)%10;
break;
case 6:
r=(n/1000000L)%10;
break;
case 7:
r=(n/10000000L)%10;
break;
default:
break;
}
return r;
}
unsigned char i=0x0;
unsigned long tick = millis();
unsigned long n=12345678;
int digit_selector=7;
int high_zero_blank=1;
String serialData;
void loop()
{
if (Serial.available() > 0) {
// read the incoming byte:
serialData = Serial.readStringUntil('\n');
n=serialData.toInt();
}
int dgt;
if(serialData.length()>0){
char ch = serialData.charAt(digit_selector);
if(isDigit(ch)){
dgt=String(ch).toInt();
iv18_digit(7-digit_selector,dgt);
}
tick = millis()+2000;
}else{
dgt=fetch_digit(n,digit_selector);
if(high_zero_blank && dgt==0){
iv18_digit(digit_selector,10); //blank
}else{
high_zero_blank=0;
iv18_digit(digit_selector,dgt);
}
}
// Serial.print(i);
// Serial.print(" ");
digit_selector--;
if(digit_selector<0){
// Serial.println(" ");
high_zero_blank=1;
digit_selector=7;
}
if(millis()-tick < 1300){
n=random(0,99999999);
// n=12345678;
Serial.println(n);
Serial.print(fetch_digit(n,7));
Serial.print(" ");
Serial.print(fetch_digit(n,6));
Serial.print(" ");
Serial.print(fetch_digit(n,5));
Serial.print(" ");
Serial.print(fetch_digit(n,4));
Serial.print(" ");
Serial.print(fetch_digit(n,3));
Serial.print(" ");
Serial.print(fetch_digit(n,2));
Serial.print(" ");
Serial.print(fetch_digit(n,1));
Serial.print(" ");
Serial.print(fetch_digit(n,0));
Serial.println(" ");
}
if(millis()-tick > 12000){
tick = millis();
}
delay(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment