问题描述
我有一个完整的wxpython代码,可以从雨量传感器中读取数据,并通过mcp3008将其从模拟转换为数字.问题是我使用的当前树莓派,已经有一个20x4的LCD显示屏,该显示屏使用引脚24或GPIO 8,这是我的雨量传感器程序所需要的.我已经从 https://learn阅读了. adafruit.com/raspberry-pi-analog-to-digital-converters/mcp3008#software-spi 关于如何将我的spi连接更改为软件spi.这是我的mcp3008到树莓派的引脚分配,不带LCD显示:MCP3008 VDD-> 5V
I have a completed wxpython code that can read data from rain sensor and convert it from analogue to digital with mcp3008. The problem is the current raspberry pi that I use, already has a 20x4 lcd display that uses pin 24 or GPIO 8 which I need for my rain sensor program. I have read from https://learn.adafruit.com/raspberry-pi-analog-to-digital-converters/mcp3008#software-spi on how to change my spi connection to software spi. Here is my mcp3008 pinout to raspberry pi without lcd display:MCP3008 VDD -> 5V
MCP3008 VREF-> 5V
MCP3008 VREF -> 5V
MCP3008 AGND-> GND
MCP3008 AGND -> GND
MCP3008 CLK->引脚23
MCP3008 CLK -> pin 23
MCP3008 DOUT->引脚21
MCP3008 DOUT -> pin 21
MCP3008 DIN->针19
MCP3008 DIN -> pin 19
MCP3008 CS->引脚24
MCP3008 CS -> pin 24
MCP3008 DGND-> GND
MCP3008 DGND -> GND
这是我的wxpython雨量传感器代码:
Here is my wxpython code for rain sensor:
import datetime
import spidev
from time import sleep
import os
spi = spidev.SpiDev()
spi.open(0,0)
#global adcOut
adcOut = 0
percent = 0
volts = 0
rain_condition = ""
global current_time
current_time = datetime.datetime.strftime(datetime.datetime.now(), '%d-%m-%y %H:%M:%S')
try:
import wx
except ImportError:
raise ImportError, "The wxPython module is required to run this program."
class RainSensorApp_wx(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, size = (700, 300))
self.SetBackgroundColour(wx.WHITE)
self.parent = parent
self.initialize()
def initialize(self):
sizer = wx.GridBagSizer()
font = wx.Font(20, wx.DECORATIVE, wx.ITALIC, wx.NORMAL)
self.SetFont(font)
self.label1 = wx.StaticText(self, -1, label = u'Rain Sensor Level: {0:4d} Percentage: {1:3}% Voltage: {2}V'.format(adcOut, percent, volts))
self.label1.SetBackgroundColour(wx.WHITE)
self.label1.SetForegroundColour(wx.BLACK)
sizer.Add(self.label1, (1,0), (1,2), wx.EXPAND)
self.label2 = wx.StaticText(self, -1, label = u'Rain Condition: {}'.format(rain_condition))
self.label2.SetBackgroundColour(wx.WHITE)
self.label2.SetForegroundColour(wx.BLACK)
sizer.Add(self.label2, (2,0), (1,3), wx.EXPAND)
self.label3 = wx.StaticText(self, -1, label = u'Time Updated: {}'.format(current_time))
self.label3.SetBackgroundColour(wx.WHITE)
self.label3.SetForegroundColour(wx.BLACK)
sizer.Add(self.label3, (3,0), (1,4), wx.EXPAND)
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.on_timer, self.timer)
self.timer.Start(1000)
self.SetSizer(sizer)
self.Show(True)
def on_timer(self,event):
channel = 0
if ((channel>7) or (channel<0)):
return -1
#r = spi.xfer2([1, (8+channel) << 4, 0])
#REPLACEMENT
r = [0]*8
for i in range(8):
# The read_adc function will get the value of the specified channel (0-7).
r[i] = mcp.read_adc(i)
#END REPLACEMENT
adcOut = ((r[1]&3) << 8) + r[2]
#global adcOut
percent = int(round(adcOut/10.24))
volts = ((adcOut/float (1023)) * 5)
volts = round(volts, 2)
global current_time
current_time = datetime.datetime.strftime(datetime.datetime.now(), '%d-%m-%y %H:%M:%S')
if adcOut >= 0 and adcOut <= 300:
self.label1.SetLabel("ADC Output: {0:4d} Percentage: {1:3}% Voltage: {2}V".format(adcOut, percent, volts))
self.label2.SetLabel("Rain Condition: Heavy Rain")
self.label3.SetLabel("Time Updated: {}".format(current_time))
elif adcOut >= 0 and adcOut <= 500:
self.label1.SetLabel("ADC Output: {0:4d} Percentage: {1:3}% Voltage: {2}V".format(adcOut, percent, volts))
self.label2.SetLabel("Rain Condition: Moderate Rain")
self.label3.SetLabel("Time Updated: {}".format(current_time))
elif adcOut >= 0 and adcOut <= 700:
self.label1.SetLabel("ADC Output: {0:4d} Percentage: {1:3}% Voltage: {2}V".format(adcOut, percent, volts))
self.label2.SetLabel("Rain Condition: Light Rain")
self.label3.SetLabel("Time Updated: {}".format(current_time))
else:
self.label1.SetLabel("ADC Output: {0:4d} Percentage: {1:3}% Voltage: {2}V".format(adcOut, percent, volts))
self.label2.SetLabel("Rain Condition: No Rain")
self.label3.SetLabel("Time Updated: {}".format(current_time))
if __name__ == "__main__":
Rs = wx.App()
RainSensorApp_wx(None, -1, 'Rain Sensor Monitor')
Rs.MainLoop()
这是我按照网上安装库的步骤操作后,从AdaFruit_MCP3008中获得的示例代码:
Here is the example code from the AdaFruit_MCP3008 after I followed the steps on the web given to install the library:
# Simple example of reading the MCP3008 analog input channels and printing
# them all out.
# Author: Tony DiCola
# License: Public Domain
import time
# Import SPI library (for hardware SPI) and MCP3008 library.
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008
# Software SPI configuration:
CLK = 18
MISO = 23
MOSI = 24
CS = 25
mcp = Adafruit_MCP3008.MCP3008(clk=CLK, cs=CS, miso=MISO, mosi=MOSI)
# 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 True:
# 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)
# 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 half a second.
time.sleep(0.5)
有人可以帮助我如何将程序更改为使用软件spi读取数据,因为我需要将软件spi配置更改为:
Can somebody help me on how to change my program to read data with the software spi, as I need to change the Software spi configuration to:
CLK = 23
MISO = 21
MISO = 21
MOSI = 19
MOSI = 19
CS = 29
推荐答案
我不知道Adafruit_MCP3008返回了什么,并且无法访问硬件来对其进行测试,我怀疑您现有的线路:
Without knowing what Adafruit_MCP3008 is returning and without access to the hardware to be able to test it, I suspect that your existing line:
r = spi.xfer2([1, (8+channel) << 4, 0])
应替换为:
r = [0]*8
for i in range(8):
# The read_adc function will get the value of the specified channel (0-7).
r[i] = mcp.read_adc(i)
唯一可以确定的方法是打印出当前设置中的r
,然后对新设置进行相同操作,以查看是否存在差异以及它们之间的差异.
The only way to know for sure would be to print out r
as it is in your current set up and then do the same for the new one, to see if there are any differences and what they are.
这篇关于如何更改将mcp3008读取并显示降雨传感器数据的wxpython程序更改为软件spi?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!