49 lines
1.6 KiB
C++
49 lines
1.6 KiB
C++
|
#include <wiringPi.h>
|
||
|
#include <wiringPiI2C.h>
|
||
|
|
||
|
const int adcAddr = 0x48; // ADC address
|
||
|
const int serialBaudRate = 115200; // Serial monitor baud rate
|
||
|
const float toggleDelay = 0.2; // Delay between toggling bits, in microseconds
|
||
|
|
||
|
int main(void) {
|
||
|
int fd; // File descriptor for I2C device
|
||
|
uint32_t buffer[8] = {0}; // Initialize buffer array with zeroes
|
||
|
uint8_t bitCount = 0; // Number of generated bits
|
||
|
|
||
|
// Initialize WiringPi and open I2C device
|
||
|
wiringPiSetup();
|
||
|
fd = wiringPiI2CSetup(adcAddr);
|
||
|
|
||
|
// Print buffer values in binary format
|
||
|
for (int i = 0; i < 8; i++) {
|
||
|
uint32_t randomValue = 0; // Initialize random value
|
||
|
|
||
|
while (bitCount < 32) {
|
||
|
uint16_t adcValue = wiringPiI2CReadReg16(fd, 0x00); // Read ADC value
|
||
|
uint32_t newBit = adcValue & 0x01; // Extract LSB from ADC value
|
||
|
randomValue = (randomValue << 1) | newBit; // Add new bit to LSB
|
||
|
bitCount++; // Increment number of generated bits
|
||
|
delayMicroseconds(toggleDelay); // Delay
|
||
|
}
|
||
|
|
||
|
buffer[i] = randomValue; // Save random value in buffer array
|
||
|
bitCount = 0; // Reset number of generated bits
|
||
|
}
|
||
|
|
||
|
// Print buffer values in binary format
|
||
|
for (int i = 0; i < 8; i++) {
|
||
|
Serial.print(formatBinary(buffer[i])); // Print generated 32-bit values in binary format
|
||
|
}
|
||
|
Serial.println(); // New line to separate from other outputs
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
String formatBinary(uint32_t number) {
|
||
|
String binaryString = String(number, BIN); // Convert to binary string
|
||
|
while (binaryString.length() < 32) {
|
||
|
binaryString = "0" + binaryString; // Pad with leading zeroes
|
||
|
}
|
||
|
return binaryString;
|
||
|
}
|