Raspberry Pi仿真扩展板

长期以来,Raspberry Pi微型计算机已进入极客,系统管理员,程序员和电子工程师的生活。相对便宜,相对同类产品而言功能强大,具有内置的I / O端口,它可以应付各种任务并满足用户需求。买了Raspberry Pi之后,我想要一些东西来打开,测量和管理外部设备。当前正在出售大量扩展卡,例如此处,您可以使用带有电线的面包板进行快速原型制作,但是我更愿意自己制作设备来完成特定任务。第一次,我没有为所有输出使用两排梳,而是将自己限制在几个I / O端口,SPI,I2C和UART总线上。我将Raspberry Pi与目标导线相连,以制作“ mom-mom”原型。

图片

在这方面,开发了一系列的三个原型开发板,其中一个是我将在本文中讨论的最简单的一个。

所以我需要什么:

  • 使用带有输入和输出的GPIO;
  • 负载管理;
  • 在1-Wire总线上连接温度传感器;
  • 电压测量
  • 实时时钟;
  • 通过RS485总线监视和控制外部设备;
  • 扩展卡的独立电源;

根据上述清单,已开发出一种电路
图片
高分辨率存储

电路该电路使用具有两个输出绕组的变压器电源。第一绕组在具有5V输出的线性稳压器上运行,以为RS485驱动器芯片和实时时钟供电。第二个绕组用于为电磁继电器供电,并与达林顿晶体管组装。

DS18B20温度传感器由Raspberry Pi板上的+ 3.3V供电,该板上具有用于连接外部DS18B20传感器的连接器,ADC所用的电压相同,并且与实时时钟相匹配。该电路使用四个按钮S1-S4来控制以低逻辑电平运行的任何动作。为了控制负载,使用了带有内置保护二极管的晶体管组件DD1 ULN2003。继电器K1和K2连接到晶体管组件的针脚16、15,用于指示的LED连接到针脚14-12,针脚11、10设计为根据方案通过一个公共集电极连接到外部设备,或者具有+ 12V绕组电压的其他继电器。

为了测量电压,使用了带SPI接口,输入低通滤波器的10位ADC DD3 MCP3008的两个通道。模拟节点最初是出于学术目的或快速调试而制作的。如果对模拟信号的定性测量产生疑问,则必须将数字地与模拟地去耦,使用外部参考电压源。或者,可以通过以下方式完成模拟部分:



仅需要在SPI总线上使用TTL→LVTTL电平转换器。

实时时钟DD4在DS1307芯片上制成,并且使用32.768 kHz的石英谐振器Q1进行计时。一块焊接在板上的3伏锂电池用于为时钟的RAM芯片供电。该微电路通过I2C总线(在MOS晶体管VT2,VT3上使用的5V-3.3V电平转换器)通过I2C总线连接到Raspberry Pi(CAD使用的是直接连接到线路,无显式连接)。

DD2 ST485芯片用作UART→RS485驱动器。使用晶体管VT1的电路解决方案允许放弃单独的输出以控制收发器。仅当UART正在发送数据时,它才切换到发送模式,其余时间ST485处于接收模式。从晶体管VT1的集电极中去除收发器控制命令。

除了转换接口之外,该方案还执行使UART Raspberry Pi接口的LVTTL电平与ST485驱动器的TTL电平匹配的功能。从晶体管VT1的集电极获取的信号将DI输入端的电平拉至5V,电阻器R16和齐纳二极管VD8将输出R0的电平限制为3.3V。不要注意电阻R11,它在调试过程中仍留在电路中。RS485已通过ModBus RTU协议在115200波特下进行了测试。

因此,进一步开发了电路板:



印刷电路板的3D模型



成品设备的照片以及与扩展板一起使用的照片:







与外部设备的连接方案的变体:


为了检查扩展板的节点,我使用了python中的几个脚本(我不知道这种语言,是通过反复试验编写的,并遵循专家的代码)。
要测量来自连接到ADC通道的电位计的电压,我编写了一个简单的脚本:

MCP3008 python源
#!/usr/bin/python

# Example program to read data from MCP3008 10 bit ADC

import spidev
import time
import os
 
# Open SPI bus
spi = spidev.SpiDev()
spi.open(0,0)
 
# Function to read SPI data from MCP3008 chip
# Channel must be an integer 0-7
def ReadChannel(channel):
  adc = spi.xfer2([1,(8+channel)<<4,0])
  data = ((adc[1]&3) << 8) + adc[2]
  return data
 
# Function to convert data to voltage level,
# rounded to specified number of decimal places.
def ConvertVolts(data,places):
  volts = (data * 3.3) / float(1023)
  volts = round(volts,places)
  return volts
 
# Define sensor channels
first_channel = 0
second_channel  = 1
 
# Define delay between readings
delay = 5

print "------------------------------------------------------"
 
while True:
 
  # Read the first channel data
  first_level = ReadChannel(first_channel)
  first_channel_volts = ConvertVolts(first_level,2)
 
  # Read the second channel data
  second_level = ReadChannel(second_channel)
  second_channel_volts = ConvertVolts(second_level,2)
  
 
  # Print out results
  print "------------------------------------------------------"
  print("First ADC channel: {} ({}V)".format(first_level,first_channel_volts))
  print("Second ADC channel : {} ({}V)".format(second_level,second_channel_volts))
 
  # Wait before repeating loop
  time.sleep(delay)



以下脚本用于DS18B20温度计:

DS18B20 python源

# Example program to read data from DS18B20
# This code taken from
# https://kropochev.com/?go=all/raspberry-pi-and-onewire-sensor/

import os
import glob
import time

os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '10*')[0]
device_file = device_folder + '/w1_slave'

def read_temp_raw():
    f = open(device_file, 'r')
    lines = f.readlines()
    f.close()
    return lines

def read_temp():
    lines = read_temp_raw()
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = read_temp_raw()
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_c = float(temp_string) / 1000.0
        temp_f = temp_c * 9.0 / 5.0 + 32.0
        return temp_c, temp_f

while True:
    print(read_temp())
    time.sleep(1)


我还记录了Raspbian中的设置:

配置设备
— 1 Wire settings:
— Raspbian wheezy

Enter command in console:
pi@raspberrypi~$ sudo modprobe w1-gpio
pi@raspberrypi~$ sudo modprobe w1_therm

Then check sensor in system:
pi@raspberrypi ~ $ sudo ls /sys/bus/w1/devices/w1_bus_master1/

You can see the tabel below entered comman and
there you should find HEX like 28-000002da8328;
This is DS18B20 address;

Next read the data from DS18B20 sensor using command:
pi@raspberrypi ~ $ cat /sys/bus/w1/devices/w1_bus_master1/28-000002da8328/w1_sla

And you are going see temperature in console like:
6f 01 4b 46 7f ff 01 10 67: crc=67 YES
6f 01 4b 46 7f ff 01 10 67 t=22937

t=22937 — you should divide this number on 1000 and you will have temperature in Celsius;

— Raspbian Jezzy

Enter command:
pi@raspberrypi~$ sudo nano /boot/config.txt

On next step you should write in config file
dtoverlay=w1-gpio,gpiopin=4
dtoverlay=w1-gpio-pullup

Then you should reboot your raspberry Pi;

— I2C Real Time Clock settings:
— Update your system if it needed and install i2c-tools:
pi@raspberrypi~$ sudo apt-get update
pi@raspberrypi~$ sudo apt-get -y upgrade
pi@raspberrypi~$ sudo apt-get i2c-tools:

Enter command:
pi@raspberrypi~$ sudo nano /etc/modules
Add these lines:
i2c-bcm2708
i2c-dev
rtc_ds1307

Comment one line in file:
pi@raspberrypi~$ sudo nano /etc/modprobe.d/raspi-blacklist.conf
Add # Symbol in beginning of line

blacklist i2c-bcm2708
________________________________________________________
Reboot system;
Enter command:
pi@raspberrypi~$ sudo lsmod

You will see lines like:
rtc_ds1307 7715 0
i2c_dev 5277 0
i2c_bcm2708 4719 0
________________________________________________________

Get DS1307 address:
pi@raspberrypi~$ sudo i2cdetect -y 1

You will see table in console:

0 1 2 3 4 5 6 7 8 9 a b c d e f
00: — -- — -- — -- — -- — -- — -- — 10: — -- — -- — -- — -- — -- — -- — -- — --
20: — -- — -- — -- — -- — -- — -- — -- — --
30: — -- — -- — -- — -- — -- — UU — -- — --
40: — -- — -- — -- — -- — -- — -- — -- — --
50: — -- — -- — -- — -- — -- — -- — -- — --
60: — -- — -- — -- — -- 68 — -- — -- — -- — 70: — -- — -- — -- — --

In address 0x3b some device without driver and 0x68 perhaps DS1307 clock address.

Enter command:
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device

Read clock:
pi@raspberrypi~$ sudo hwclock -r

Set time:
pi@raspberrypi~$ sudo hwclock -w
Set system time from RTC:
pi@raspberrypi~$ sudo hwclock -s

Automatic RTC start;
Add lines in /etc/rc.local file

echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
sudo hwclock -s
Before last line in file looks like:

exit 0
— Uart settings:
— Back up files:
cp /boot/cmdline.txt /boot/cmdline.bak
cp /etc/inittab /etc/inittab.bak

Delete «console=ttyAMA0,115200» «kgdboc=ttyAMA0,115200» lines from configuration file:
pi@raspberrypi~$ nano /boot/cmdline.txt

Comment last line looks like «T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100: in /etc/inittab file: using # symbol:
pi@raspberrypi~$ nano /etc/inittab


急需电路板,因此我自己使用LUT技术和几个小时的空闲时间来制作电路板。为了方便工作并节省时间,电路板的一侧制成了MGShV 0.5的跳线。DipTrace电路和PCB设计,测试源代码,组件列表以及在
存储库中配置命令的教程

PS:在下面的视频中,使用了第一个仿真板,在20分钟内将其组装在带有按钮和LED的面包板上,并基于以下版本出现:

Source: https://habr.com/ru/post/zh-CN401089/


All Articles