byte digits[] = {0x7e, 0x30, 0x6d, 0x79, 0x33, 0x5b, 0x5f, 0x70, 0x7f, 0x7b, 0x77, 0x1f, 0x4e, 0x3d, 0x4f, 0x47};
byte cathodePins[] = {0x70, 0xb0, 0xd0, 0xe0};
//74hc165 connections
int SH_LD = 2;
int CLK = 3;
int CE = 4;
int QH = 5;
//74hc595 connections
int dataPin = 6;
int clockPin = 7;
int latchPin = 8;
int switchPin1 = 9;
int switchPin2 = 10;
void setup() {
// put your setup code here, to run once:
pinMode(SH_LD, OUTPUT);
pinMode(CLK, OUTPUT);
pinMode(CE, OUTPUT);
pinMode(QH, INPUT );
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(switchPin1, INPUT);
pinMode(switchPin2, INPUT);
}
void loop() {
// 2's complement mode
if(digitalRead(switchPin2) == HIGH && digitalRead(switchPin1) == HIGH){
int val = readInput();
if(val <= 127){
val = val;
printDigits(val, 10);
}
if(val > 127){
val = (val - 128) - 128;
printDigits(val, 10);
}
}
//decimal mode
else if(digitalRead(switchPin2) == LOW && digitalRead(switchPin1) == LOW){
printDigits(readInput(), 10);
}
// hexadecimal mode
else if(digitalRead(switchPin2) == LOW && digitalRead(switchPin1) == HIGH){
printDigits(readInput(), 16);
}
// octal mode
else if(digitalRead(switchPin2) == HIGH && digitalRead(switchPin1) == LOW){
printDigits(readInput(), 8);
}
}
void printDigits(int num, int base){
if(abs(num)>=0 && abs(num) <= 9){
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, digits[abs(num) % base]);
shiftOut(dataPin, clockPin, MSBFIRST, cathodePins[0]);
digitalWrite(latchPin, HIGH);
delay(33);
if(num < 0){
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, 0x01);
shiftOut(dataPin, clockPin, MSBFIRST, cathodePins[1]);
digitalWrite(latchPin, HIGH);
delay(33);
}
if(base == 8 && num == 8){
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, digits[(abs(num)/base) % base]);
shiftOut(dataPin, clockPin, MSBFIRST, cathodePins[1]);
digitalWrite(latchPin, HIGH);
delay(33);
}
else{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, 0x00);
shiftOut(dataPin, clockPin, MSBFIRST, cathodePins[1]);
digitalWrite(latchPin, HIGH);
delay(33);
}
}
if(abs(num) > 9 && abs(num) <= 99){
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, digits[abs(num) % base]);
shiftOut(dataPin, clockPin, MSBFIRST, cathodePins[0]);
digitalWrite(latchPin, HIGH);
delay(33);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, digits[(abs(num)/base) % base]);
shiftOut(dataPin, clockPin, MSBFIRST, cathodePins[1]);
digitalWrite(latchPin, HIGH);
delay(33);
if(num < 0){
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, 0x01);
shiftOut(dataPin, clockPin, MSBFIRST, cathodePins[2]);
digitalWrite(latchPin, HIGH);
delay(33);
}
else{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, 0x00);
shiftOut(dataPin, clockPin, MSBFIRST, cathodePins[2]);
digitalWrite(latchPin, HIGH);
delay(33);
}
}
if(abs(num) > 99 && abs(num) < 256){
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, digits[abs(num) % base]);
shiftOut(dataPin, clockPin, MSBFIRST, cathodePins[0]);
digitalWrite(latchPin, HIGH);
delay(33);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, digits[(abs(num)/base) % base]);
shiftOut(dataPin, clockPin, MSBFIRST, cathodePins[1]);
digitalWrite(latchPin, HIGH);
delay(33);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, digits[(abs(num)/(base*base)) % base]);
shiftOut(dataPin, clockPin, MSBFIRST, cathodePins[2]);
digitalWrite(latchPin, HIGH);
delay(33);
if(num < 0){
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, 0x01);
shiftOut(dataPin, clockPin, MSBFIRST, cathodePins[3]);
digitalWrite(latchPin, HIGH);
delay(33);
}
else{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, 0x00);
shiftOut(dataPin, clockPin, MSBFIRST, cathodePins[3]);
digitalWrite(latchPin, HIGH);
delay(33);
}
}
}
/*
void printDigits(int num, int base){
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, digits[abs(num) % base]);
shiftOut(dataPin, clockPin, MSBFIRST, cathodePins[0]);
digitalWrite(latchPin, HIGH);
delay(33);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, digits[abs(num/base) % base]);
shiftOut(dataPin, clockPin, MSBFIRST, cathodePins[1]);
digitalWrite(latchPin, HIGH);
delay(33);
if(base == 16){
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST,0x00);
shiftOut(dataPin, clockPin, MSBFIRST, cathodePins[2]);
digitalWrite(latchPin, HIGH);
delay(33);
}
else{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, digits[abs(num/(base * base)) % base]);
shiftOut(dataPin, clockPin, MSBFIRST, cathodePins[2]);
digitalWrite(latchPin, HIGH);
delay(33);
}
digitalWrite(latchPin, LOW);
if(num < 0){
shiftOut(dataPin, clockPin, MSBFIRST, 0x01);
shiftOut(dataPin, clockPin, MSBFIRST, cathodePins[3]);
}
else{
shiftOut(dataPin, clockPin, MSBFIRST, 0x00);
shiftOut(dataPin, clockPin, MSBFIRST, cathodePins[3]);
}
digitalWrite(latchPin, HIGH);
delay(33);
}*/
int readInput(){
digitalWrite(SH_LD, LOW);
delayMicroseconds(5);
digitalWrite(SH_LD, HIGH);
delayMicroseconds(5);
digitalWrite(CLK, HIGH);
digitalWrite(CE, LOW);
int data = shiftIn(QH, CLK, MSBFIRST);
digitalWrite(CE, HIGH);
return data;
delay(200);
}