#include <stdio.h>
#include <stdio.h>
#include <math.h>
#include<string.h>
#include <LiquidCrystal_I2C.h> // Library for LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);
int count;
int ss;
const int inputPin = 5;
const int debounceDelay = 10;
//74HC165
const int dataPin = 2; /* Q7 */
const int clockPin = 3; /* CP */
const int latchPin = 4; /* PL */
const int numBits = 16; /* Set to 8 * number of shift registers */
//74HC595
#define DATA_PIN 8 // Pin connected to DS of 74HC595
#define LATCH_PIN 9 // Pin connected to STCP of 74HC595
#define CLOCK_PIN 10 // Pin connected to SHCP of 74HC595
// How many of the shift registers
#define NUM_SHIFT_REGS 2
const uint8_t numOfRegisterPins = NUM_SHIFT_REGS * 8;
bool registers[numOfRegisterPins];
float out ,out1;
void setup() {
//74hc165
Serial.begin(115200);
pinMode(dataPin, INPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
//74hc595
pinMode(DATA_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
clearRegisters();
writeRegisters();
lcd.init(); // initialize the lcd
lcd.backlight();
pinMode(inputPin, INPUT);
}
int flag = 1;
void loop() {
lcd.clear(); // clear display
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print("Arduino"); // print message at (0, 0)
delay(100); // display the above for two seconds
// Step 1: Sample
delay(100);
while(flag){
int bits[numBits];
digitalWrite(latchPin, LOW);
digitalWrite(latchPin, HIGH);
// Step 2: Shift
Serial.print("Bits: ");
for (int i = 0; i < numBits; i++) {
int bit = digitalRead(dataPin);
bits[i] = bit;
if (bit == HIGH) {
Serial.print("1");
} else {
Serial.print("0");
}
digitalWrite(clockPin, HIGH); // Shift out the next bit
digitalWrite(clockPin, LOW);
}
Serial.println();
delay(100);
Serial.println("//////");
Serial.println(bits[0]);
Serial.println(bits[1]);
Serial.println(bits[2]);
Serial.println(bits[3]);
Serial.println(bits[4]);
Serial.println(bits[5]);
Serial.println(bits[6]);
Serial.println(bits[7]);
int decimalValue = 0;
for (int i = 0; i < numBits; i++) {
decimalValue += bits[i] * pow(2, numBits - i - 1);
}
Serial.print("\nDecimal value: ");
Serial.println(decimalValue);
out = decimalValue * (5.0/252.0);
out1 = decimalValue * (5.0/4092.0);
Serial.println(out);
if(debounce(inputPin))
{
count++;
}
//显示1-5是八位的1-5V数字量,显示6-10是十二位1-5V的数字量,
switch (count)
{
case 1:
setRegisterPin(0, HIGH);
setRegisterPin(1, HIGH);
setRegisterPin(2, LOW);
setRegisterPin(3, LOW);
setRegisterPin(4, HIGH);
setRegisterPin(5, HIGH);
setRegisterPin(6, LOW);
setRegisterPin(7, LOW);
setRegisterPin(8, LOW);
setRegisterPin(9, LOW);
setRegisterPin(10, LOW);
setRegisterPin(11, LOW);
setRegisterPin(12, LOW);
setRegisterPin(13, LOW);
setRegisterPin(14, LOW);
setRegisterPin(15, LOW);
writeRegisters();
break;
case 2:
setRegisterPin(0, LOW);
setRegisterPin(1, HIGH);
setRegisterPin(2, HIGH);
setRegisterPin(3, LOW);
setRegisterPin(4, LOW);
setRegisterPin(5, HIGH);
setRegisterPin(6, HIGH);
setRegisterPin(7, LOW);
setRegisterPin(8, LOW);
setRegisterPin(9, LOW);
setRegisterPin(10, LOW);
setRegisterPin(11, LOW);
setRegisterPin(12, LOW);
setRegisterPin(13, LOW);
setRegisterPin(14, LOW);
setRegisterPin(15, LOW);
writeRegisters();
break;
case 3:
setRegisterPin(0, HIGH);
setRegisterPin(1, LOW);
setRegisterPin(2, LOW);
setRegisterPin(3, HIGH);
setRegisterPin(4, HIGH);
setRegisterPin(5, LOW);
setRegisterPin(6, LOW);
setRegisterPin(7, HIGH);
setRegisterPin(8, LOW);
setRegisterPin(9, LOW);
setRegisterPin(10, LOW);
setRegisterPin(11, LOW);
setRegisterPin(12, LOW);
setRegisterPin(13, LOW);
setRegisterPin(14, LOW);
setRegisterPin(15, LOW);
writeRegisters();
break;
case 4:
setRegisterPin(0, LOW);
setRegisterPin(1, LOW);
setRegisterPin(2, HIGH);
setRegisterPin(3, HIGH);
setRegisterPin(4, LOW);
setRegisterPin(5, LOW);
setRegisterPin(6, HIGH);
setRegisterPin(7, HIGH);
setRegisterPin(8, LOW);
setRegisterPin(9, LOW);
setRegisterPin(10, LOW);
setRegisterPin(11, LOW);
setRegisterPin(12, LOW);
setRegisterPin(13, LOW);
setRegisterPin(14, LOW);
setRegisterPin(15, LOW);
writeRegisters();
break;
case 5:
setRegisterPin(0, HIGH);
setRegisterPin(1, HIGH);
setRegisterPin(2, HIGH);
setRegisterPin(3, HIGH);
setRegisterPin(4, HIGH);
setRegisterPin(5, HIGH);
setRegisterPin(6, HIGH);
setRegisterPin(7, HIGH);
setRegisterPin(8, LOW);
setRegisterPin(9, LOW);
setRegisterPin(10, LOW);
setRegisterPin(11, LOW);
setRegisterPin(12, LOW);
setRegisterPin(13, LOW);
setRegisterPin(14, LOW);
setRegisterPin(15, LOW);
writeRegisters();
break;
case 6:
setRegisterPin(0, HIGH);
setRegisterPin(1, HIGH);
setRegisterPin(2, LOW);
setRegisterPin(3, LOW);
setRegisterPin(4, HIGH);
setRegisterPin(5, HIGH);
setRegisterPin(6, LOW);
setRegisterPin(7, LOW);
setRegisterPin(8, HIGH);
setRegisterPin(9, HIGH);
setRegisterPin(10, LOW);
setRegisterPin(11, LOW);
setRegisterPin(12, LOW);
setRegisterPin(13, LOW);
setRegisterPin(14, LOW);
setRegisterPin(15, LOW);
writeRegisters();
break;
case 7:
setRegisterPin(0, LOW);
setRegisterPin(1, HIGH);
setRegisterPin(2, HIGH);
setRegisterPin(3, LOW);
setRegisterPin(4, LOW);
setRegisterPin(5, HIGH);
setRegisterPin(6, HIGH);
setRegisterPin(7, LOW);
setRegisterPin(8, LOW);
setRegisterPin(9, HIGH);
setRegisterPin(10, HIGH);
setRegisterPin(11, LOW);
setRegisterPin(12, LOW);
setRegisterPin(13, LOW);
setRegisterPin(14, LOW);
setRegisterPin(15, LOW);
writeRegisters();
break;
case 8:
setRegisterPin(0, HIGH);
setRegisterPin(1, LOW);
setRegisterPin(2, LOW);
setRegisterPin(3, HIGH);
setRegisterPin(4, HIGH);
setRegisterPin(5, LOW);
setRegisterPin(6, LOW);
setRegisterPin(7, HIGH);
setRegisterPin(8, HIGH);
setRegisterPin(9, LOW);
setRegisterPin(10, LOW);
setRegisterPin(11, HIGH);
setRegisterPin(12, LOW);
setRegisterPin(13, LOW);
setRegisterPin(14, LOW);
setRegisterPin(15, LOW);
writeRegisters();
break;
case 9:
setRegisterPin(0, LOW); //低位
setRegisterPin(1, LOW);
setRegisterPin(2, HIGH);
setRegisterPin(3, HIGH);
setRegisterPin(4, LOW);
setRegisterPin(5, LOW);
setRegisterPin(6, HIGH);
setRegisterPin(7, HIGH);
setRegisterPin(8, LOW);
setRegisterPin(9, LOW);
setRegisterPin(10, HIGH);
setRegisterPin(11, HIGH);
setRegisterPin(12, LOW);
setRegisterPin(13, LOW);
setRegisterPin(14, LOW);
setRegisterPin(15, LOW); //高位
writeRegisters();
break;
case 10:
setRegisterPin(0, HIGH);
setRegisterPin(1, HIGH);
setRegisterPin(2, HIGH);
setRegisterPin(3, HIGH);
setRegisterPin(4, HIGH);
setRegisterPin(5, HIGH);
setRegisterPin(6, HIGH);
setRegisterPin(7, HIGH);
setRegisterPin(8, HIGH);
setRegisterPin(9, HIGH);
setRegisterPin(10, HIGH);
setRegisterPin(11, HIGH);
setRegisterPin(12, LOW);
setRegisterPin(13, LOW);
setRegisterPin(14, LOW);
setRegisterPin(15, LOW);
writeRegisters();
break;
}
if (count == 1 || count == 6) ss = 1;
if (count == 2 || count == 7) ss = 2;
if (count == 3 || count == 8) ss = 3;
if (count == 4 || count == 9) ss = 4;
if (count == 5 || count == 10) ss = 5;
if (count>10){
count =0;
}
delay(100);
lcd.clear(); // clear display
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print(decimalValue); // print message at (0, 0)
lcd.setCursor(0, 1);
lcd.print(out);
lcd.setCursor(6, 0); // move cursor to (0, 0)
lcd.print(decimalValue); // print message at (0, 0)
lcd.setCursor(6, 1);
lcd.print(out1);
lcd.setCursor(13, 0); // move cursor to (0, 0)
lcd.print(count);
lcd.setCursor(13, 1); // move cursor to (0, 0)
lcd.print(ss);
lcd.setCursor(15, 1); // move cursor to (0, 0)
lcd.print("V");
delay(100);
}
}
/*
int bits[numBits];
// Step 1: Load
digitalWrite(latchPin, LOW);
digitalWrite(latchPin, HIGH);
// Step 2: Shift
Serial.print("Bits: ");
for (int i = 0; i < numBits; i++) {
int bit = digitalRead(dataPin);
bits[i] = bit;
if (bit == HIGH) {
Serial.print("1");
} else {
Serial.print("0");
}
digitalWrite(clockPin, HIGH); // Shift out the next bit
digitalWrite(clockPin, LOW);
}
// Convert binary to decimal
int decimalValue = 0;
for (int i = 0; i < numBits; i++) {
decimalValue += bits[i] * pow(2, i);
}
Serial.print("\nDecimal value: ");
Serial.println(decimalValue);
delay(1000);
*/
boolean debounce(int pin)
{
boolean state;
boolean previousState;
previousState = digitalRead(pin);
for(int counter=0;counter<debounceDelay; counter++)
{
delay(1);
state = digitalRead(pin);
if(state != previousState)
{
counter = 0;
previousState = state;
}
}
return state;
}
void clearRegisters() {
// Reset all register pins
for (int i = numOfRegisterPins - 1; i >= 0; i--) {
registers[i] = LOW;
}
}
void setRegisterPin(int index, int value) {
// Set an individual pin HIGH or LOW
registers[index] = value;
}
void writeRegisters() {
// Set and display registers
digitalWrite(LATCH_PIN, LOW);
for (int i = numOfRegisterPins - 1; i >= 0; i--) {
digitalWrite(CLOCK_PIN, LOW);
digitalWrite(DATA_PIN, registers[i]);
digitalWrite(CLOCK_PIN, HIGH);
}
digitalWrite(LATCH_PIN, HIGH);
}