From 2bd3fe4e9be8a113a77073099c0c3d9eab55030f Mon Sep 17 00:00:00 2001 From: Orell-Pieter Schwarzbach <2122623@stud.hs-mannheim.de> Date: Wed, 3 May 2023 17:05:33 +0200 Subject: [PATCH] Upload files to '' MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Erste Lösung Pi mit ADC. 32*X an Bits als .bin und .txt --- erzeuge_bin_file_statt_print.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 erzeuge_bin_file_statt_print.py diff --git a/erzeuge_bin_file_statt_print.py b/erzeuge_bin_file_statt_print.py new file mode 100644 index 0000000..018e91d --- /dev/null +++ b/erzeuge_bin_file_statt_print.py @@ -0,0 +1,30 @@ +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_data1.bin" # Choose filename for binary file +txt_filename = "binary_data1.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 & 0b00000001 # Extract 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.00001) # Wait for a short time before generating next sample + +print("Binary file {} and text file {} generated successfully!".format(binary_filename, txt_filename))