32 lines
1.4 KiB
Python
32 lines
1.4 KiB
Python
import time
|
|
import random
|
|
import Adafruit_ADS1x15
|
|
import RPi.GPIO as GPIO
|
|
import struct
|
|
|
|
adc = Adafruit_ADS1x15.ADS1115()
|
|
GAIN = 1
|
|
|
|
adc.start_adc_difference(0, gain=GAIN, data_rate=860)
|
|
|
|
num_samples = 1000 # Choose number of 32-bit binary numbers to generate
|
|
binary_filename = "binary_data4.bin" # Choose filename for binary file
|
|
txt_filename = "binary_data4.txt" # Choose filename for text file
|
|
|
|
with open(binary_filename, "wb") as bin_file, open(txt_filename, "w") as txt_file:
|
|
for i in range(num_samples):
|
|
bin_string = "" # Initialize binary string
|
|
for j in range(32): # Loop 32 times to generate 32-bit binary number
|
|
value = adc.get_last_result() # Read last conversion result
|
|
lsb = value & 0b01 # Extract LSB
|
|
#print (lsb)
|
|
bin_string += str(lsb) # Add LSB to binary string
|
|
bin_data = struct.pack("I", int(bin_string, 2)) # Convert binary string to 32-bit unsigned integer and pack into binary data
|
|
bin_file.write(bin_data) # Write binary data to file
|
|
txt_file.write(bin_string) # Write binary string to text file
|
|
if i < num_samples-1: # If not at the last sample, add a space to the end of the binary string in the text file
|
|
txt_file.write("")
|
|
time.sleep(0.00000001) # Wait for a short time before generating next sample
|
|
|
|
print("Binary file {} and text file {} generated successfully!".format(binary_filename, txt_filename))
|