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調試程序
03 PYTHON軟件包
1.軟件包接口介紹
AD7606.py集成了採集數據的兩個主要接口:
- ad7606buffer(num, ch, period)
- ad7606sample(num, ch, period)
這兩個接口的基本功能是相同的。只是AD7606Buffer採用了板上的23LC1024作爲緩衝,可以採用高達20kHz的採樣率。而AD7606Sample則只能最高在1kHz左右的採樣率。
函數返回值都是一樣的。是包括有 個有符號的整形數。
除了前面兩個主要接口之外,還有兩個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採集兩個通道數據曲線
#!/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採集兩個通道的數據
#!/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八通道採集模塊完成對中低頻信號的採集。這位電子線路實驗以及信號處理實驗提供了物理信號輸入接口。