通过设计基于DA,AD,12-bit的模拟输入输出单片机加电路可以构成更加灵活的机理测量模块。手边有两个别人剩余的器件,基于STC8G1K(SOP8)来设计对应的电路模块。
(1) 元器件
(2) 原理图1
下面接口的功能定义:
PIN1 | PIN2 | PIN3 | PIN4 | PIN5 |
---|---|---|---|---|
+5V | GND | AIN | AGND | AOUT |
+5V | GND | 3.3V | 2.5V | AGND | AIN | AOUT |
---|---|---|---|---|---|---|
5.03 | -0.00 | 3.31 | 2.42 | 0.00 | 0.24 | 0.00 |
将MAX6192的VCC增加到+5V,参考电压Vref=2.4746V。
由于MAX5353的工作电压为3.3V,所以它的输出最大为3.3V。修改RA1= 2.5k,在Vref=2.5V参考电压下,MAX5353的最大输出为3.125V。
(3) 单片机软件2
▲ 设置DAC与ADC读数
(4) 软件串口命令
else IFARG0("max5353") {
sscanf(SDA(1), "%d", &nNumber);
MAX5353Write(nNumber);
} else IFARG0("max5353x") {
sscanf(SDA(1), "%x", &nNumber);
MAX5353Write(nNumber);
} else IFARG0("max1240") {
printf("%d ", MAX1240Read());
} else IFARG0("max1240b") {
nNumber = MAX1240Read();
SendChar((unsigned char)(nNumber >> 8));
SendChar(nNumber);
} else IFARG0("max5353max1240") {
for(nNumber = 0; nNumber < 0xfff; nNumber += 0x10) {
MAX5353Write(nNumber);
WaitTime(1);
printf("%d ", MAX1240Read());
}
printf("\r\n");
} else IFARG0("maxswp") {
sscanf(SDA(1), "%d", &nStart);
sscanf(SDA(2), "%d", &nNumber);
sscanf(SDA(3), "%d", &nInc);
for(; nStart < nNumber ; nStart += nInc) {
MAX5353Write(nStart);
WaitTime(1);
printf("%d ", MAX1240Read());
}
printf("\r\n");
} else IFARG0("maxswpx") {
sscanf(SDA(1), "%x", &nStart);
sscanf(SDA(2), "%x", &nNumber);
sscanf(SDA(3), "%x", &nInc);
for(; nStart < nNumber ; nStart += nInc) {
MAX5353Write(nStart);
WaitTime(1);
printf("%d ", MAX1240Read());
}
printf("\r\n");
} else IFARG0("maxsample") {
sscanf(SDA(1), "%d", &nNumber);
sscanf(SDA(2), "%d", &nStart);
for(nInc = 0; nInc < nNumber; nInc ++) {
printf("%d ", MAX1240Read());
WaitTime(nStart);
}
}
▲ ADC 设置与输出(黄色)以及MAX8608跟随输出
set=[0.00,40.00,80.00,120.00,160.00,200.00,240.00,280.00,320.00,360.00,400.00,440.00,480.00,520.00,560.00,600.00,640.00,680.00,720.00,760.00,800.00,840.00,880.00,920.00,960.00,1000.00,1040.00,1080.00,1120.00,1160.00,1200.00,1240.00,1280.00,1320.00,1360.00,1400.00,1440.00,1480.00,1520.00,1560.00,1600.00,1640.00,1680.00,1720.00,1760.00,1800.00,1840.00,1880.00,1920.00,1960.00,2000.00,2040.00,2080.00,2120.00,2160.00,2200.00,2240.00,2280.00,2320.00,2360.00,2400.00,2440.00,2480.00,2520.00,2560.00,2600.00,2640.00,2680.00,2720.00,2760.00,2800.00,2840.00,2880.00,2920.00,2960.00,3000.00,3040.00,3080.00,3120.00,3160.00,3200.00,3240.00,3280.00,3320.00,3360.00,3400.00,3440.00,3480.00,3520.00,3560.00,3600.00,3640.00,3680.00,3720.00,3760.00,3800.00,3840.00,3880.00,3920.00,3960.00]
volt=[0.01,0.02,0.05,0.08,0.11,0.14,0.17,0.20,0.23,0.26,0.29,0.32,0.36,0.39,0.42,0.45,0.48,0.51,0.54,0.57,0.60,0.63,0.66,0.69,0.72,0.75,0.78,0.81,0.84,0.87,0.90,0.93,0.96,1.00,1.03,1.06,1.09,1.12,1.15,1.18,1.21,1.24,1.27,1.30,1.33,1.36,1.39,1.42,1.45,1.48,1.51,1.54,1.57,1.60,1.64,1.67,1.70,1.73,1.76,1.79,1.82,1.85,1.88,1.91,1.94,1.97,2.00,2.03,2.06,2.09,2.12,2.15,2.18,2.21,2.25,2.28,2.31,2.34,2.37,2.40,2.43,2.46,2.49,2.52,2.55,2.58,2.61,2.64,2.67,2.70,2.73,2.76,2.79,2.82,2.85,2.88,2.92,2.95,2.98,3.01]
adc=[-0.01,0.02,0.05,0.08,0.11,0.14,0.17,0.20,0.23,0.26,0.29,0.32,0.35,0.38,0.42,0.45,0.48,0.51,0.54,0.57,0.60,0.63,0.66,0.69,0.72,0.75,0.78,0.81,0.84,0.87,0.90,0.93,0.96,0.99,1.02,1.05,1.09,1.12,1.15,1.18,1.21,1.24,1.27,1.30,1.33,1.36,1.39,1.42,1.45,1.48,1.51,1.54,1.57,1.60,1.63,1.66,1.69,1.73,1.76,1.79,1.82,1.85,1.88,1.91,1.94,1.97,2.00,2.03,2.06,2.09,2.12,2.15,2.18,2.21,2.24,2.27,2.30,2.33,2.37,2.40,2.43,2.46,2.49,2.52,2.55,2.58,2.61,2.64,2.67,2.70,2.73,2.76,2.79,2.82,2.85,2.88,2.91,2.94,2.97,3.00]
对上述DAC的设置x与输出之间的关系通过下面线性关系拟合:
A D C = a × x + b ADC = a \times x + b ADC=a×x+b
获得参数为:[ a=0.00076168 b=-0.01134206]
A D C = 0.76168 x 1000 − 0.01134 ADC = {{0.76168x} \over {1000}} - 0.01134 ADC=10000.76168x−0.01134
#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# DRAW1.PY -- by Dr. ZhuoQing 2020-07-28
#
# Note:
#============================================================
from headm import *
from scipy.optimize import curve_fit
def func1(x, a, b):
return x * a + b
set, volt, adc = tspload('meas', 'set', 'volt', 'adc')
#------------------------------------------------------------
param = (3/4000, 0)
param, conv = curve_fit(func1, set, adc, p0=param)
printf(param)
#------------------------------------------------------------
# END OF FILE : DRAW1.PY
#============================================================
▲ ADC输入电压与转换值
最后电压为: 1.5105V,转换值为2503
那么,转换值与电压之间的比例为:0.6035/1000。
这个数值理论值为: 2.5 4095 = 0.61035 × 1 0 − 3 {{2.5} \over {4095}} = 0.61035 \times 10^{ - 3} 40952.5=0.61035×10−3
▲ 红色LED的电流与电流之间的关系
▲ 绿色色LED的电流与电流之间的关系
▲ 蓝色色LED的电流与电流之间的关系
▲ 白色LED电流与电压之间的关系
#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# TEST1.PY -- by Dr. ZhuoQing 2020-07-28
#
# Note:
#============================================================
from headm import *
from tsmodule.tsvisa import *
from tsmodule.tsstm32 import *
stm32cmd("CLEAR")
stm32cmd("max5353 0")
time.sleep(1)
stm32cmd('maxswp 0 4000 2 1')
time.sleep(3)
val = stm32memo(1)
set = list(range(0, 4000, 2))
printf(set)
volt = [0.76168*s/1000-0.01134 for s in set]
voltled = [0.6035*s/1000 for s in val]
curr = [(v1-v2) for v1,v2 in zip(volt, voltled)]
plt.plot(voltled, curr)
plt.ylabel("Current(mA)")
plt.xlabel("Voltage(V)")
plt.grid(True)
plt.tight_layout()
plt.show()
#------------------------------------------------------------
# END OF FILE : TEST1.PY
#============================================================
/*
**==============================================================================
** MAX5353.H: -- by Dr. ZhuoQing, 2020-07-28
**
** Description:
**
**==============================================================================
*/
#ifndef __MAX5353__
#define __MAX5353__
//------------------------------------------------------------------------------
#ifdef MAX5353_GLOBALS
#define MAX5353_EXT
#else
#define MAX5353_EXT extern
#endif // MAX5353_GLOBALS
//------------------------------------------------------------------------------
//==============================================================================
#define MAX5353_CS 5,5
#define MAX5353_CLK 3,3
#define MAX5353_DIO 3,2
//------------------------------------------------------------------------------
void MAX5353Init(void);
void MAX5353Write(unsigned int nNumber);
void MAX5353Delay(unsigned char ucDelay);
void MAX5353Clock(unsigned char ucClock);
//==============================================================================
// END OF THE FILE : MAX5353.H
//------------------------------------------------------------------------------
#endif // __MAX5353__
/*
**==============================================================================
** MAX5353.C: -- by Dr. ZhuoQing, 2020-07-28
**
**==============================================================================
*/
//------------------------------------------------------------------------------
#define MAX5353_GLOBALS 1 // Define the global variables
#include "MAX5353.H"
#include "C51BASIC.H"
#include "STC8G.H"
#include "INTRINS.H"
//------------------------------------------------------------------------------
void MAX5353Init(void) {
ON(MAX5353_DIO);
OFF(MAX5353_CLK);
ON(MAX5353_CS);
PM_PP(MAX5353_CS);
PM_BIDIR(MAX5353_DIO);
PM_PP(MAX5353_CLK);
}
void MAX5353Delay(unsigned char ucDelay) {
unsigned char i;
for(i = 0; i < ucDelay; i ++) {
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
}
void MAX5353Clock(unsigned char ucClock) {
unsigned char i;
for(i = 0; i < ucClock; i ++) {
ON(MAX5353_CLK);
MAX5353Delay(10);
OFF(MAX5353_CLK);
MAX5353Delay(10);
}
}
void MAX5353Write(unsigned int nNumber) {
unsigned int nMask;
unsigned char i;
OFF(MAX5353_CLK);
OFF(MAX5353_CS);
OFF(MAX5353_DIO);
MAX5353Clock(3);
nMask = 0x800;
for(i = 0; i < 12; i ++) {
if(nNumber & nMask)
ON(MAX5353_DIO);
else OFF(MAX5353_DIO);
nMask >>= 1;
MAX5353Clock(1);
}
OFF(MAX5353_DIO);
MAX5353Clock(1);
ON(MAX5353_CS);
}
//==============================================================================
// END OF THE FILE : MAX5353.C
//------------------------------------------------------------------------------
/*
**==============================================================================
** MAX1240.H: -- by Dr. ZhuoQing, 2020-07-28
**
** Description:
**
**==============================================================================
*/
#ifndef __MAX1240__
#define __MAX1240__
//------------------------------------------------------------------------------
#ifdef MAX1240_GLOBALS
#define MAX1240_EXT
#else
#define MAX1240_EXT extern
#endif // MAX1240_GLOBALS
//------------------------------------------------------------------------------
//==============================================================================
#define MAX1240_CS 5, 4
#define MAX1240_CLK 3, 3
#define MAX1240_DIO 3, 2
//------------------------------------------------------------------------------
void MAX1240Init(void);
void MAX1240Delay(unsigned char ucDelay);
unsigned int MAX1240Read(void);
//==============================================================================
// END OF THE FILE : MAX1240.H
//------------------------------------------------------------------------------
#endif // __MAX1240__
/*
**==============================================================================
** MAX1240.C: -- by Dr. ZhuoQing, 2020-07-28
**
**==============================================================================
*/
//------------------------------------------------------------------------------
#define MAX1240_GLOBALS 1 // Define the global variables
#include "intrins.h"
#include "MAX1240.H"
#include "C51BASIC.H"
#include "STC8G.H"
//------------------------------------------------------------------------------
void MAX1240Init(void) {
ON(MAX1240_CS);
OFF(MAX1240_CLK);
ON(MAX1240_DIO);
PM_PP(MAX1240_CS);
PM_BIDIR(MAX1240_DIO);
PM_PP(MAX1240_CLK);
}
void MAX1240Delay(unsigned char ucDelay) {
unsigned char j;
for(j = 0; j < ucDelay; j ++) {
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
}
unsigned int MAX1240Read(void) {
unsigned int nNumber, i;
PM_BIDIR(MAX1240_DIO);
ON(MAX1240_DIO);
OFF(MAX1240_CLK);
OFF(MAX1240_CS);
for(i = 0; i < 100; i ++) {
MAX1240Delay(100);
if(VAL(MAX1240_DIO)) break;
}
if(VAL(MAX1240_DIO) == 0) return 0x0;
nNumber = 0;
for(i = 0; i < 12; i ++) {
ON(MAX1240_CLK);
MAX1240Delay(10);
OFF(MAX1240_CLK);
MAX1240Delay(10);
nNumber <<= 1;
if(VAL(MAX1240_DIO)) nNumber |= 0x1;
}
for(i = 0; i < 4; i ++) {
ON(MAX1240_CLK);
MAX1240Delay(10);
OFF(MAX1240_CLK);
MAX1240Delay(10);
}
ON(MAX1240_CS);
return nNumber;
}
//==============================================================================
// END OF THE FILE : MAX1240.C
//------------------------------------------------------------------------------
A D C = 0.76168 x 1000 − 0.01134 ADC = {{0.76168x} \over {1000}} - 0.01134 ADC=10000.76168x−0.01134
AD设计工程文件:AD\Tools\2020\ADDASTC8G1K08\ADDASTC8G1K08.SchDoc * ↩︎
C51工程文件:C51\STC\Tools\2020\ADDAST8G1K\ADDASTC8G1k.uvproj ↩︎