Re: PISO 74HC165 till arduino ger opålitlig data
Postat: 16 april 2016, 13:49:24
Jag tittade lite i den gamla koden men gav snart upp...
Svenskt forum för elektroniksnack.
https://elektronikforumet.com/forum/
Kod: Markera allt
// Set up Pins
const int plPin = 31; // This pin will control the parallel load pin(s)
const int cpPin = 33; // This pin will control the clock pin(s)
const int q7Pin = 37; // This pin will recieve the serial data
// Variables
unsigned int chips = 2; // Enter how many chips you use totally
unsigned int bits_per_chip = 8; // Enter the amount of bits to read per chip
unsigned int total_bits_to_read = chips * bits_per_chip; // Calculates the total amount of bits to read
unsigned long pinValues;
unsigned long oldPinValues;
// Setup once
void setup()
{
Serial.begin(9600); // Set up serial monitoring for output of the serial data collected
pinMode(plPin, OUTPUT);
pinMode(cpPin, OUTPUT);
pinMode(q7Pin, INPUT_PULLUP); // Set this pin to INPUT and activate the internal pull-up resistor so it's not floating
digitalWrite(plPin, HIGH); // This is active LOW so I set this inactive for now
digitalWrite(cpPin, LOW); // This is active HIGH so I set this inactive for now
}
void loop()
{
load_parallel_data_into_register();
pinValues = shift_out_data();
if(pinValues != oldPinValues) // Only print to serial monitor if there's been a change
{
output_data_to_serial_monitor(pinValues);
oldPinValues = pinValues;
}
}
void load_parallel_data_into_register()
{
digitalWrite(plPin, LOW); // Pull PL low (active) to load parallel data into the register
digitalWrite(plPin, HIGH); // Pull PL high (inactive) so we can shift out the register
}
unsigned long shift_out_data()
{
long bitVal;
unsigned int bytesVal = 0;
// Send clock-signals to CP to shift out the register
for(int i = 0; i < total_bits_to_read; i++) {
bitVal = digitalRead(q7Pin);
bytesVal |= (bitVal << ((total_bits_to_read-1) - i));
digitalWrite(cpPin, HIGH); // Activate clock-signal to shift to next bit
digitalWrite(cpPin, LOW); // Deactivate clock-signal
}
return(bytesVal);
}
void output_data_to_serial_monitor(unsigned long data)
{
Serial.print("Data = ");
Serial.println(data, BIN);
}