电子说
ADX516兼容SGM58601
/*
Analogysemi xutong
2023/04/20
*/
#include "ADX516.h"
#include "board.h"
#include "bsp_lpspi.h"
#include "rtthread.h"
//初始化ADX516
void ADX516_Init(void)
{
//拉PWDN和RST
}
//获取芯片的ID号
//Adx516 default Value 0x20
uint8_t ADX516_GetChipID(void)
{
uint8_t ID;
ID=ADX516_GetReg(ADX516_STATUS);
return ID;
}
//获取芯片寄存器值
uint8_t ADX516_GetReg(uint8_t Rreg)
{
uint8_t TxData[3];
uint8_t RxData[3];
//读取命令,
TxData[0]=ADX516_RREG|Rreg;
//选择要读取的寄存器
TxData[1]=0x00;
//读取单个寄存器
TxData[2]=0X00;
//开始读取
Lpspi_Read(3,TxData,RxData);
//返回寄存器值
return RxData[2];
}
//写单个芯片寄存器值
void ADX516_WriteReg(uint8_t Wreg,uint8_t Data)
{
uint8_t TxData[3];
//写入命令,
TxData[0]=ADX516_WREG|Wreg;
//选择要写入的的寄存器
TxData[1]=0x00;
//读取写入寄存器
TxData[2]=Data;
Lpspi_Write(3,TxData);
}
//写多个芯片寄存器值
//StartReg 起始寄存器地址
//RegNum 要写的寄存器数量
//*Data 寄存器参数传入
void ADX516_WriteMultiReg(uint8_t StartReg,uint8_t RegNum,uint8_t *Data)
{
uint8_t TxData[32];
uint8_t i;
//写入命令,
TxData[0]=ADX516_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 ADX516_ReadMultiReg(uint8_t StartReg,uint8_t RegNum,uint8_t *Data)
{
//读取寄存器
uint8_t TxData[3];
//读取命令,
TxData[0]=ADX516_RREG|StartReg;
//选择要写入的的寄存器
TxData[1]=RegNum-1;
//最终接收多少个数据
Lpspi_Read(2+RegNum,TxData,Data);
}
//打印所有寄存器地址
void ADX516_GetAllReg(void)
{
uint8_t i,j;
for(i=0;i< 20;i++)
{
j=ADX516_GetReg(i);
//打印所有寄存器值
rt_kprintf("reg addr hex: %x reg default Value: %xrn",i,j);
}
}
//发送ADX516 数据指令 如SDATAC或者RDATAC
//详情见头文件command define部分
void ADX516_Command(uint8_t Com)
{
uint8_t TxData[]={Com};
Lpspi_Write(1,TxData);
}
//设定通道并且获取adc数据
//获取ADC的值
void ADX516_GetADC_Value(uint8_t Ch,uint8_t *Rxdata)
{
uint8_t TxData[]={ADX516_RDATA};
ADX516_Command(ADX516_SDATAC);
ADX516_WriteReg(ADX516_MUX,Ch< < 4|0X0F);
ADX516_Command(ADX516_SYNC);
ADX516_Command(ADX516_WAKEUP);
//rt_thread_mdelay(1);
Lpspi_Read(4,TxData,Rxdata);
}
/*
Analogysemi xutong
2023/02/15
*/
#ifndef __ADX516_H
#define __ADX516_H
#include "bsp_lpspi.h"
/*
device 需要先写入SDTAC 启动寄存器写入才可以对寄存器进行操作
在此之前需要检查PWDN以及Reset引脚的状态是否为 高
在操作完寄存器后,需要发送RDATAC 才能连续 读取数据
bsp_lpspi 的接口函数如下:
Lpspi_Write(uint8_t datasize,uint8_t *data);
Lpspi_Read(uint8_t datasize,uint8_t *data,uint8_t *Rxdata);
注意:
Lpspi的时序并不一定满足ADX516的配置,以下配置经过验证
lpspi_master_config_t config;
LPSPI_MasterGetDefaultConfig(&config);
config.whichPcs=kLPSPI_Pcs0;
config.baudRate=50000;
config.betweenTransferDelayInNanoSec = 1000000000 /500;
config.pcsToSckDelayInNanoSec=1000000000 /500;
config.lastSckToPcsDelayInNanoSec=1000000000 /500;
config.cpha=kLPSPI_ClockPhaseSecondEdge;
LPSPI_MasterInit(LPSPI3, &config, LPSPI3_CLOCK_FREQ);
*/
/*command define*/
#define ADX516_WAKEUP 0x00
#define ADX516_RDATA 0x12
#define ADX516_RDATAC 0x10
#define ADX516_SDATAC 0x11
#define ADX516_RREG 0x10
#define ADX516_WREG 0X50
#define ADX516_SELFCAL 0XF0
#define ADX516_SELFOCAL 0XF1
#define ADX516_SELFGCAL 0XF2
#define ADX516_SYSOCAL 0XF3
#define ADX516_SYSGCAL 0XF4
#define ADX516_SYNC 0XFC
#define ADX516_STANDBY 0xFD
#define ADX516_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 ADX516_STATUS 0x00
#define ADX516_MUX 0x01
#define ADX516_ADCON 0x02
#define ADX516_DRATE 0x03
#define ADX516_IO 0x04
#define ADX516_OFC0 0x05
#define ADX516_OFC1 0x06
#define ADX516_OFC2 0x07
#define ADX516_FSC0 0x08
#define ADX516_FSC1 0x09
#define ADX516_FSC2 0x0A
#define ADX516_CFG0 0x0B
#define ADX516_CFG1 0x0C
#define ADX516_CFG2 0x0D
#define ADX516_CFG3 0x0E
#define ADX516_CFG4 0x0F
#define ADX516_CFG5 0x10
#define ADX516_DEV_CFG 0x11
#define ADX516_MOD_STAT 0x12
#define ADX516_DEV_STAT 0x13
#define ADX516_CMD_STAT 0x14
/*
通道配置
channel config
*/
#define ADX516_ADC_CH0
//写命令
void ADX516_Command(uint8_t Com);
//获取芯片的ID号
uint8_t ADX516_GetChipID(void);
//获取寄存器值
uint8_t ADX516_GetReg(uint8_t Rreg);
//获取多个寄存器值
void ADX516_ReadMultiReg(uint8_t StartReg,uint8_t RegNum,uint8_t *Data);
//写入寄存器值
void ADX516_WriteReg(uint8_t Wreg,uint8_t Data);
//写多个寄存器的值
void ADX516_WriteMultiReg(uint8_t StartReg,uint8_t RegNum,uint8_t *Data);
//获取所有寄存器值
void ADX516_GetAllReg(void);
//设定通道并且获取ADC的值
void ADX516_GetADC_Value(uint8_t Ch,uint8_t *Rxdata);
//初始化ADX516
void ADX516_Init(void);
#endif
全部0条评论
快来发表一下你的评论吧 !