MicroPython ADX51X driver
Latest update time:2024-03-06
Reads:
from machine import Pin, SoftSPI
import time
def ID(agreement):
#txbuf 需要将16进制转换为10进制
rxbuf = bytearray(3) # create a buffer
spi = SoftSPI(baudrate=200000, polarity=1, phase=0, mosi=Pin(16), miso=Pin(4), sck=Pin(2))
spi.init(baudrate=125000) # set the baudrate
CS = Pin(17, Pin.OUT) # create output pin on GPIO16
CS.value(1) # set pin to on/high
time.sleep(1)
CS.value(0) # set pin to on/high
txbuf =bytearray([16,0,0])
spi.write_readinto(txbuf, rxbuf) # write to MOSI and read from MISO into the buffer
CS.value(1) # set pin to on/high
return rxbuf
#采集P=AIN0 N=AIN1时候的电压
def GetADCValue(agreement):
#Rxbuffs = bytearray(3) # create a buffer
spi = SoftSPI(baudrate=200000, polarity=1, phase=0, mosi=Pin(16), miso=Pin(4), sck=Pin(2))
spi.init(baudrate=125000) # set the baudrate
CS = Pin(17, Pin.OUT) # create output pin on GPIO16
CS.value(1) # set pin to on/high
time.sleep(1)
CS.value(0) # set pin to on/high
#第4位设置电压采样输入p和n
txbuf =bytearray([15,81,0,1,252,0,1])
spi.write(txbuf)
txbuf =bytearray([0,0,0])
#读取三个字节
rxbuf=spi.read(3,0)
CS.value(1) # set pin to on/high
return rxbuf
import ADX51X
import time
Value=ADX51X.GetADCValue(1)
print(str(hex(Value[0]))+"|"+str(hex(Value[1]))+"|"+str(hex(Value[2])))
The main program uses MicroPython to facilitate calling the host computer Pyvisa later.
/*
Analogysemi xutong
2024/03/06
*/
//初始化ADX51X
void ADX51X_Init(void)
{
//拉PWDN和RST
}
//获取芯片的ID号
//ADX51X default Value 0x20
uint8_t ADX51X_GetChipID(void)
{
uint8_t ID;
ID=ADX51X_GetReg(ADX51X_STATUS);
return ID;
}
//获取芯片寄存器值
uint8_t ADX51X_GetReg(uint8_t Rreg)
{
uint8_t TxData[3];
uint8_t RxData[3];
//读取命令,
TxData[0]=ADX51X_RREG|Rreg;
//选择要读取的寄存器
TxData[1]=0x00;
//读取单个寄存器
TxData[2]=0X00;
//开始读取
Lpspi_Read(3,TxData,RxData);
//返回寄存器值
return RxData[2];
}
//写单个芯片寄存器值
void ADX51X_WriteReg(uint8_t Wreg,uint8_t Data)
{
uint8_t TxData[3];
//写入命令,
TxData[0]=ADX51X_WREG|Wreg;
//选择要写入的的寄存器
TxData[1]=0x00;
//读取写入寄存器
TxData[2]=Data;
Lpspi_Write(3,TxData);
}
//写多个芯片寄存器值
//StartReg 起始寄存器地址
//RegNum 要写的寄存器数量
//*Data 寄存器参数传入
void ADX51X_WriteMultiReg(uint8_t StartReg,uint8_t RegNum,uint8_t *Data)
{
uint8_t TxData[32];
uint8_t i;
//写入命令,
TxData[0]=ADX51X_WREG|StartReg;
//选择要写入的的寄存器
TxData[1]=RegNum-1;
for(i=0;i<RegNum;i++)
{
//将指针数据赋值给Txdata
TxData[2+i]=*Data;
//指向下个数据地址
Data++;
}
//最终发送多少个数据
Lpspi_Write(2+RegNum,TxData);
}
//读多个芯片寄存器值
//StartReg 起始寄存器地址
//RegNum 要写的寄存器数量
//*Data 寄存器参数传出
void ADX51X_ReadMultiReg(uint8_t StartReg,uint8_t RegNum,uint8_t *Data)
{
//读取寄存器
uint8_t TxData[3];
//读取命令,
TxData[0]=ADX51X_RREG|StartReg;
//选择要写入的的寄存器
TxData[1]=RegNum-1;
//最终接收多少个数据
Lpspi_Read(2+RegNum,TxData,Data);
}
//打印所有寄存器地址
void ADX51X_GetAllReg(void)
{
uint8_t i,j;
for(i=0;i<20;i++)
{
j=ADX51X_GetReg(i);
}
}
//发送ADX51X 数据指令 如SDATAC或者RDATAC
//详情见头文件command define部分
void ADX51X_Command(uint8_t Com)
{
uint8_t TxData[1];
TxData[0]=Com;
Lpspi_Write(1,TxData);
}
//设定通道并且获取adc数据
//获取ADC的值
//ADC负接在AIN1上
void ADX51X_GetADC_Value(uint8_t AIN_P,uint8_t AIN_N,uint8_t *Rxdata)
{
uint8_t TxData[]={0,0,0};
// 可以在这里加DRDY的判断
ADX51X_Command(ADX51X_SDATAC);
ADX51X_WriteReg(ADX51X_MUX,AIN_P<<4|AIN_N);
ADX51X_Command(ADX51X_SYNC);
ADX51X_Command(ADX51X_WAKEUP);
ADX51X_Command(ADX51X_RDATA);
Lpspi_Read(3,TxData,Rxdata);
Lpspi_Stop();
}
/*
Analogysemi xutong
2023/03/06
*/
/*command define*/
//RREG Read n nnnn registers starting at address r rrrr
//First Byte 001r rrrr (2xh)
//Second Byte 000n nnnn
//n nnnn = number of registers to be read or written – 1. For example, to read or write three registers, set n nnnn = 0 (0010). r rrrr = the
//starting register address for read and write commands
//WREG 同上
/*
寄存器定义
*/
/*
通道配置
channel config
*/
//写命令
void ADX51X_Command(uint8_t Com);
//获取芯片的ID号
uint8_t ADX51X_GetChipID(void);
//获取寄存器值
uint8_t ADX51X_GetReg(uint8_t Rreg);
//获取多个寄存器值
void ADX51X_ReadMultiReg(uint8_t StartReg,uint8_t RegNum,uint8_t *Data);
//写入寄存器值
void ADX51X_WriteReg(uint8_t Wreg,uint8_t Data);
//写多个寄存器的值
void ADX51X_WriteMultiReg(uint8_t StartReg,uint8_t RegNum,uint8_t *Data);
//获取所有寄存器值
void ADX51X_GetAllReg(void);
//设定通道并且获取ADC的值
void ADX51X_GetADC_Value(uint8_t AIN_P,uint8_t AIN_N,uint8_t *Rxdata);
//初始化ADX51X
void ADX51X_Init(void);
ADX51X C driver
The sequence when setting AIN to equal COM