如何使用matplotlib绘制从ADC获取的数据?什么都没有策划

2024-04-16 23:36:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经从我的ADC(MCP3208)获得了读数,完整的代码写在下面。你知道吗

import matplotlib.pyplot as plt

import time

t_start = time.time()
t_end = t_start + 1
t = []

# Import SPI library and MCP3008 library.
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008

# Hardware SPI configuration:
SPI_PORT   = 0
SPI_DEVICE = 0
mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))

print('Reading MCP3008 values, press Ctrl-C to quit...')

# Print nice channel column headers.
print('| {0:>4} | {1:>4} | {2:>4} | {3:>4} | {4:>4} | {5:>4} | {6:>4} | {7:>4} |'.format(*range(8)))
print('-' * 57)

# Main program loop.
while time.time() < t_end:

    # Read all the ADC channel values in a list.
    values = [0]*8
    for i in range(8):

        # The read_adc function will get the value of the specified channel (0-7).
        values[i] = mcp.read_adc(i)*330/1024

    # Print the ADC values.
    print('| {0:>4} | {1:>4} | {2:>4} | {3:>4} | {4:>4} | {5:>4} | {6:>4} | {7:>4} |'.format(*values))

    # Pause for 0.02 seconds
    time.sleep(0.02)
    t_operating = time.time() - t_start
    t.append(t_operating)

# Plot value from channel 0
plt.plot(t[1], values[1])
plt.show()

我可以读取我的ADC值,甚至将它们记录到一个.bat文件中,并使用GNUplot来绘制数据,但是当我使用这个代码时,什么都没有绘制?你知道吗

编辑:我已经把我的t.append移到了我的循环中,但是我仍然有同样的问题。未显示绘图。你知道吗


Tags: the代码importspiadafruittimeaschannel