Article count:240 Read by:410258

Account Entry

MicroPython ADX51X driver

Latest update time:2024-03-06
    Reads:
from machine import Pin, SoftSPIimport 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 ADX51Ximport 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*/#include "ADX51X.h"#include "bsp_lpspi.h"//初始化ADX51Xvoid ADX51X_Init(void){  //拉PWDN和RST}//获取芯片的ID号//ADX51X default Value 0x20uint8_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*/#ifndef __ADX51X_H#define __ADX51X_H
#include "bsp_lpspi.h"

/*command define*/
#define ADX51X_WAKEUP 0x00#define ADX51X_RDATA 0x01#define ADX51X_RDATAC 0x03#define ADX51X_SDATAC 0x0F#define ADX51X_RREG 0x10#define ADX51X_WREG 0X50#define ADX51X_SELFCAL 0XF0#define ADX51X_SELFOCAL 0XF1#define ADX51X_SELFGCAL 0XF2#define ADX51X_SYSOCAL 0XF3#define ADX51X_SYSGCAL 0XF4#define ADX51X_SYNC 0XFC#define ADX51X_STANDBY 0xFD#define ADX51X_RESET 0xFE


//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 同上



/* 寄存器定义*/#define ADX51X_STATUS 0x00#define ADX51X_MUX 0x01#define ADX51X_ADCON 0x02#define ADX51X_DRATE 0x03#define ADX51X_IO 0x04#define ADX51X_OFC0 0x05#define ADX51X_OFC1 0x06#define ADX51X_OFC2 0x07#define ADX51X_FSC0 0x08#define ADX51X_FSC1 0x09#define ADX51X_FSC2 0x0A#define ADX51X_CFG0 0x0B#define ADX51X_CFG1 0x0C#define ADX51X_CFG2 0x0D#define ADX51X_CFG3 0x0E#define ADX51X_CFG4 0x0F#define ADX51X_CFG5 0x10#define ADX51X_DEV_CFG 0x11#define ADX51X_MOD_STAT 0x12#define ADX51X_DEV_STAT 0x13#define ADX51X_CMD_STAT 0x14

#define ADX51X_AIN0 0#define ADX51X_AIN1 1#define ADX51X_AIN2 2#define ADX51X_AIN3 3#define ADX51X_AIN4 4#define ADX51X_AIN5 5#define ADX51X_AIN6 6#define ADX51X_AIN7 7#define ADX51X_AIN_COM 8


/* 通道配置 channel config*/#define ADX51X_ADC_CH(x) x #define DRDY_PIN P00//写命令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);//初始化ADX51Xvoid ADX51X_Init(void);#endif

ADX51X C driver

The sequence when setting AIN to equal COM

 
EEWorld WeChat Subscription

 
EEWorld WeChat Service Number

 
AutoDevelopers

About Us Customer Service Contact Information Datasheet Sitemap LatestNews

Room 1530, Zhongguancun MOOC Times Building,Block B, 18 Zhongguancun Street, Haidian District,Beijing, China Tel:(010)82350740 Postcode:100190

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号