基于AD7606八通道高速数据采集模块

01前言


AD7606八通道AD采集模块 是一款16比特AD转换器。它最大可以提供200kps的采样速率。通过 带有128KB缓存的AD7606模拟采集板 可以在占用C51CPU执行时间30%的情况下,将采集到的8路16bit数据存储在24LC1024内。然后在通过 Base64 ASCII编码 方式,通过串口将采集数据传送到计算机。

为了便于使用,本文将该模块应用方面说明进行汇总,便于之后能够用于实时数据采集中。

前期工作包括有:

02使用条件


1.硬件条件

A. 采集模块以及接口板

▲ 采集板的两个模块

▲ 采集板的两个模块

B. STC 8G 单片机高速下载板

使用 STC自动高速下载线 可以最大限度保证数据传输的可靠性。如果使用 制作新版STC单片机WiFi下载器 则可能由于UDP传输过程中的延迟和丢包,会引起数据丢失字节的现象。

2.软件条件

需要使用STM32BOOTLOADER作为中间数据的接受缓存。通过UDP和Windows的剪切板与PYTHON应用程序进行交换数据。

▲ 单片机ISP调试程序

▲ 单片机ISP调试程序

03 PYTHON软件包


1.软件包接口介绍

AD7606.py集成了采集数据的两个主要接口:

  1. ad7606buffer(num, ch, period)
  2. ad7606sample(num, ch, period)

这两个接口的基本功能是相同的。只是AD7606Buffer采用了板上的23LC1024作为缓冲,可以采用高达20kHz的采样率。而AD7606Sample则只能最高在1kHz左右的采样率。

函数返回值都是一样的。是包括有 n u m × c h num \times ch 个有符号的整形数。

除了前面两个主要接口之外,还有两个STM32的函数用于设定采样的时间单位以及输入信号量程:

  • 设定输入信号的范围命令是:

stm32cmd(‘ad5v’)
stm32cmd(‘ad10v’)

  • 设定采样时间单位命令:

stm32cmd(‘setus 100’)

其中 100的单位是微秒。这个实践单位与前面采集数据中的period相乘,等于采样时间间隔。

2.软件包源文件

下面是ad7606软件源文件。该源文件也可以在 python\head\下进行安装。

#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# AD7606.PY                    -- by Dr. ZhuoQing 2020-06-04
#
# Note:
#============================================================
from head import *
from tsmodule.tsstm32       import *
#------------------------------------------------------------
stm32cmd('ad5v')
stm32cmd('setus 100')
#------------------------------------------------------------
def Base64Ascii2Bit(ascii):
    ascii = int(ascii)
    if ascii >= ord('A') and ascii <= ord('Z'): return ascii - ord('A')
    if ascii >= ord('a') and ascii <= ord('z'): return ascii - ord('a') + 26
    if ascii >= ord('0') and ascii <= ord('9'): return ascii - ord('0') + 52
    if ascii == ord('*'): return 62
    if ascii == ord('/'): return 63
    return 0
def Base64Ascii2Byte(fourb):
    data = bytearray()
    bits0 = Base64Ascii2Bit(fourb[0])
    bits1 = Base64Ascii2Bit(fourb[1])
    bits2 = Base64Ascii2Bit(fourb[2])
    bits3 = Base64Ascii2Bit(fourb[3])
    data.append(bits0 * 4 + int(bits1 / 16))
    data.append((bits1 & 0xf) * 16 + int(bits2 / 4))
    data.append((bits2 & 0x3) * 64 + bits3)
    if fourb[2] == ord('=') and fourb[3] == ord('='):
        data = data[0:1]
        return data
    if fourb[3] == ord('='):
        return data[0:2]
    return data
def Base64Ascii2Data(ascii):
    data = bytearray()
    length = len(ascii)
    for i in range(int(length / 4)):
        bytedata = Base64Ascii2Byte(ascii[i*4:i*4+4])
        if len(bytedata) > 0:
            data.extend(bytedata)
    valdim = [x*256+y for x,y in zip(data[0::2], data[1::2])]
    valdim = [(d & 0x7fff) - (d & 0x8000) for d in valdim]
    return valdim
#------------------------------------------------------------
def ad7606buffer(num, ch, period):
    val = stm32val()[10]
    stm32cmd('adbuf %d %d %d'%(num, ch, period))
    while True:
        time.sleep(.2)
        valnew = stm32val()[10]
        if valnew != val: break
    stm32cmd('CLEAR')
    stm32cmd('bufascii %d'%(num*ch))
    val = stm32val()[10]
    while True:
        time.sleep(.2)
        valnew = stm32val()[10]
        if valnew == val: break
        val = valnew
    stm32cmd("COPY")
    time.sleep(.25)
    pastestr = bytes(clipboard.paste(), 'utf-8')
    data = Base64Ascii2Data(pastestr)
    return data
def ad7606sample(num, ch, period):
    stm32cmd('CLEAR')
    stm32cmd('adascii %d %d %d'%(num, ch, period))
    val = stm32val()[10]
    while True:
        time.sleep(.2)
        valnew = stm32val()[10]
        if valnew == val: break
        val = valnew
    stm32cmd("COPY")
    time.sleep(.25)
    pastestr = bytes(clipboard.paste(), 'utf-8')
    data = Base64Ascii2Data(pastestr)
    return data
#------------------------------------------------------------
#        END OF FILE : AD7606.PY
#============================================================

04示例程序


1.使用ad7606buffer采集

应用ad7606buffer的方法采集两个通道,采集频率10kHz,采集数据量10k。

▲ 使用AD7606Buffer采集两个通道数据曲线

▲ 使用AD7606Buffer采集两个通道数据曲线

#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# TEST2.PY                     -- by Dr. ZhuoQing 2020-06-04
#
# Note:
#============================================================
from headm import *
import ad7606
from tsmodule.tsstm32       import *
#------------------------------------------------------------
stm32cmd('setus 100')
data = ad7606.ad7606buffer(10000, 2, 1)
plt.plot(data[0::2])
plt.plot(data[1::2])
plt.xlabel("sample")
plt.ylabel("Voltage(V)")
plt.grid(True)
plt.show()
#------------------------------------------------------------
#        END OF FILE : TEST2.PY
#============================================================

2.使用AD7606sample采集数据

使用ad7606sample函数,采集两个通道的数据,采集频率1kHz,采集数据量1k。

▲ 使用ad7606sample采集两个通道的数据

▲ 使用ad7606sample采集两个通道的数据

#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# TEST2.PY                     -- by Dr. ZhuoQing 2020-06-04
#
# Note:
#============================================================
from headm import *
import ad7606
from tsmodule.tsstm32       import *
#------------------------------------------------------------
stm32cmd('setus 1000')
data = ad7606.ad7606sample(1000, 2, 1)
plt.plot(data[0::2])
plt.plot(data[1::2])
plt.xlabel("sample")
plt.ylabel("Voltage(V)")
plt.grid(True)
plt.show()
#------------------------------------------------------------
#        END OF FILE : TEST2.PY
#============================================================

05结论


通过本文介绍的硬软件可以方便采用AD7606八通道采集模块完成对中低频信号的采集。这位电子线路实验以及信号处理实验提供了物理信号输入接口。

猜你喜欢

转载自blog.csdn.net/zhuoqingjoking97298/article/details/106548132
今日推荐