RT1052 (10) 驱动 ADX3208
<div class='showpostmsg'><pre><code class="language-cpp">/*
Analogysemi xutong
2023/02/15
*/
#include "ADX3208.h"
#include "board.h"
#include "bsp_lpspi.h"
#include "rtthread.h"
//初始化ADX3208
void ADX3208_Init(void)
{
// START_L;
// XTAL_H;
// PWDN_H;
// RST_H;
// rt_thread_mdelay(150);
// RST_L;
// rt_thread_mdelay(1);
// RST_H;
}
//获取芯片的ID号
uint8_t ADX3208_GetChipID(void)
{
uint8_t ID;
ID=ADX3208_GetReg(ADX3208_ID);
return ID;
}
//获取芯片寄存器值
uint8_t ADX3208_GetReg(uint8_t Rreg)
{
uint8_t TxData;
uint8_t RxData;
//读取命令,
TxData=ADX3208_RREG|Rreg;
//选择要读取的寄存器
TxData=0x00;
//读取单个寄存器
TxData=0X00;
//开始读取
Lpspi_Read(3,TxData,RxData);
//返回寄存器值
return RxData;
}
//写单个芯片寄存器值
void ADX3208_WriteReg(uint8_t Wreg,uint8_t Data)
{
uint8_t TxData;
//写入命令,
TxData=ADX3208_WREG|Wreg;
//选择要写入的的寄存器
TxData=0x00;
//读取写入寄存器
TxData=Data;
Lpspi_Write(3,TxData);
}
//写多个芯片寄存器值
//StartReg 起始寄存器地址
//RegNum 要写的寄存器数量
//*Data寄存器参数传入
void ADX3208_WriteMultiReg(uint8_t StartReg,uint8_t RegNum,uint8_t *Data)
{
uint8_t TxData;
uint8_t i;
//写入命令,
TxData=ADX3208_WREG|StartReg;
//选择要写入的的寄存器
TxData=RegNum-1;
for(i=0;i<RegNum;i++)
{
//将指针数据赋值给Txdata
TxData=*Data;
//指向下个数据地址
Data++;
}
//最终发送多少个数据
Lpspi_Write(2+RegNum,TxData);
}
//读多个芯片寄存器值
//StartReg 起始寄存器地址
//RegNum 要写的寄存器数量
//*Data寄存器参数传出
void ADX3208_ReadMultiReg(uint8_t StartReg,uint8_t RegNum,uint8_t *Data)
{
//读取寄存器
uint8_t TxData;
//读取命令,
TxData=ADX3208_RREG|StartReg;
//选择要写入的的寄存器
TxData=RegNum-1;
//最终接收多少个数据
Lpspi_Read(2+RegNum,TxData,Data);
}
//打印所有寄存器地址
void ADX3208_GetAllReg(void)
{
uint8_t i,j;
for(i=0;i<24;i++)
{
j=ADX3208_GetReg(i);
//打印所有寄存器值
//rt_kprintf("reg addr: %d reg default Value: %x\r\n",i,j);
}
}
//发送ADX3208 数据指令 如SDATAC或者RDATAC
//详情见头文件command define部分
void ADX3208_Command(uint8_t Com)
{
uint8_t TxData[]={Com};
Lpspi_Write(1,TxData);
}
//获取ADC的值
//数据一共216位 / 152位
//24位状态位1100FaultStatusPFaultStatusNGPIO
//8通道*ADC位数 数据
void ADX3208_GetADC_Value(uint8_t ADC_Resolution,uint8_t *Rxdata)
{
if (ADC_Resolution==16)
{
Lpspi_Read(152,NULL,Rxdata);
}
else
{
Lpspi_Read(216,NULL,Rxdata);
}
}</code></pre>
<pre>
<code>/*
Analogysemi xutong
2023/02/15
*/
#ifndef __ADX3208_H
#define __ADX3208_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的时序并不一定满足ADX3208的配置,以下配置经过验证
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 ADX3208_WAKEUP 0x20
#define ADX3208_STANDBY 0x04
#define ADX3208_RESET 0x06
#define ADX3208_START 0x08
#define ADX3208_STOP 0x0a
#define ADX3208_OFFSETCAL 0x1a
#define ADX3208_RDATAC0x10
#define ADX3208_SDATAC0x11
#define ADX3208_RDATA 0x12
//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 ADX3208_RREG 0x20
#define ADX3208_WREG 0X40
/*
寄存器定义
*/
#defineADX3208_ID 0x00
#defineADX3208_CONFIG1 0x01
#defineADX3208_CONFIG2 0x02
#defineADX3208_CONFIG3 0x03
#defineADX3208_LOFF 0x04
#defineADX3208_CH1SET 0x05
#defineADX3208_CH2SET 0x06
#defineADX3208_CH3SET 0x07
#defineADX3208_CH4SET 0x08
#defineADX3208_CH5SET 0x09
#defineADX3208_CH6SET 0x0A
#defineADX3208_CH7SET 0x0B
#defineADX3208_CH8SET 0x0C
#defineADX3208_OPAMP_SENSP 0x0D
#defineADX3208_OPAMP_SENSN 0x0E
#defineADX3208_LOFF_SENSP 0x0F
#defineADX3208_LOFF_SENSN 0x10
#defineADX3208_LOFF_FLIP 0x11
#defineADX3208_LOFF_STATP 0x12
#defineADX3208_LOFF_STATN 0x13
#defineADX3208_GPIO 0x14
/*
寄存器Deafult 值
寄存器地址10进制:寄存器值16进制:
reg addr: 0 reg default Value: 0e
reg addr: 1 reg default Value: 91
reg addr: 2 reg default Value: e0
reg addr: 3 reg default Value: 40
reg addr: 4 reg default Value: 00
reg addr: 5 reg default Value: 10
reg addr: 6 reg default Value: 10
reg addr: 7 reg default Value: 10
reg addr: 8 reg default Value: 10
reg addr: 9 reg default Value: 10
reg addr: 10 reg default Value: 10
reg addr: 11 reg default Value: 10
reg addr: 12 reg default Value: 10
reg addr: 13 reg default Value: 00
reg addr: 14 reg default Value: 00
reg addr: 15 reg default Value: 00
reg addr: 16 reg default Value: 00
reg addr: 17 reg default Value: 00
reg addr: 18 reg default Value: 00
reg addr: 19 reg default Value: 00
reg addr: 20 reg default Value: 0f
reg addr: 21 reg default Value: 00
reg addr: 22 reg default Value: 00
reg addr: 23 reg default Value: 00
*/
#define ADX3208_ADC_Resolution_16 16
#define ADX3208_ADC_Resolution_24 24
//写命令
void ADX3208_Command(uint8_t Com);
//获取芯片的ID号
uint8_t ADX3208_GetChipID(void);
//获取寄存器值
uint8_t ADX3208_GetReg(uint8_t Rreg);
//获取多个寄存器值
void ADX3208_ReadMultiReg(uint8_t StartReg,uint8_t RegNum,uint8_t *Data);
//写入寄存器值
void ADX3208_WriteReg(uint8_t Wreg,uint8_t Data);
//写多个寄存器的值
void ADX3208_WriteMultiReg(uint8_t StartReg,uint8_t RegNum,uint8_t *Data);
//获取所有寄存器值
void ADX3208_GetAllReg(void);
//获取ADC的值
void ADX3208_GetADC_Value(uint8_t ADC_Resolution,uint8_t *Rxdata);
//初始化ADX3208
void ADX3208_Init(void);
#endif</code></pre>
<p>ADX3208 兼容ADS131E 国产替代一个新选择 <a href="http://www.analogysemi.com/app/" target="_blank">类比半导体</a></p>
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script> <pre>
<code class="language-cpp">#include "bsp_lpspi.h"
SPI_HandleTypeDef SpiHandle;
void Spi_delay(uint32_t count)
{
volatile uint32_t i = 0;
for (i = 0; i < count; ++i)
{
__asm("NOP"); /* 调用nop空指令 */
__asm("NOP"); /* 调用nop空指令 */
__asm("NOP"); /* 调用nop空指令 */
__asm("NOP"); /* 调用nop空指令 */
__asm("NOP"); /* 调用nop空指令 */
}
}
void LPSPI_Init(void)
{
GPIO_InitTypeDefGPIO_InitStruct;
/* Enable SPI1 clock */
SPIx_CLK_ENABLE();
SPIx_SCK_GPIO_CLK_ENABLE();
SPIx_MISO_GPIO_CLK_ENABLE();
SPIx_MOSI_GPIO_CLK_ENABLE();
SPIx_CS_GPIO_CLK_ENABLE();
/* 初始化GPIO 配置*/
/*##-1- Configure peripheral GPIO ##########################################*/
/* SPI SCK GPIO pin configuration*/
GPIO_InitStruct.Pin = SPIx_SCK_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = SPIx_SCK_AF;
HAL_GPIO_Init(SPIx_SCK_GPIO_PORT, &GPIO_InitStruct);
/* SPI MISO GPIO pin configuration*/
GPIO_InitStruct.Pin = SPIx_MISO_PIN;
GPIO_InitStruct.Alternate = SPIx_MISO_AF;
HAL_GPIO_Init(SPIx_MISO_GPIO_PORT, &GPIO_InitStruct);
/* SPI MOSI GPIO pin configuration*/
GPIO_InitStruct.Pin = SPIx_MOSI_PIN;
GPIO_InitStruct.Alternate = SPIx_MOSI_AF;
HAL_GPIO_Init(SPIx_MOSI_GPIO_PORT, &GPIO_InitStruct);
/* SPI CS GPIO pin configuration*/
GPIO_InitStruct.Pin = SPIx_CS_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(SPIx_CS_GPIO_PORT, &GPIO_InitStruct);
/* 初始化SPI 配置*/
/*##-2- Configure the SPI peripheral #######################################*/
/* Set the SPI parameters */
SpiHandle.Instance = SPIx;
SpiHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
SpiHandle.Init.Direction = SPI_DIRECTION_2LINES;
SpiHandle.Init.CLKPhase = SPI_PHASE_1EDGE;
SpiHandle.Init.CLKPolarity = SPI_POLARITY_HIGH;
SpiHandle.Init.DataSize = SPI_DATASIZE_8BIT;
SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB;
SpiHandle.Init.TIMode = SPI_TIMODE_DISABLE;
SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
SpiHandle.Init.CRCPolynomial = 7;
SpiHandle.Init.NSS = SPI_NSS_SOFT;
SpiHandle.Init.Mode = SPI_MODE_MASTER;
//初始化SPI
HAL_SPI_Init(&SpiHandle);
//默认拉高CS引脚
HAL_GPIO_WritePin(SPIx_CS_GPIO_PORT,SPIx_CS_PIN,GPIO_PIN_SET);
}
void Lpspi_Write(uint8_t datasize,uint8_t *data)
{
//写数据
HAL_GPIO_WritePin(SPIx_CS_GPIO_PORT,SPIx_CS_PIN,GPIO_PIN_RESET);
HAL_SPI_Transmit(&SpiHandle,data, datasize, 5000);
Spi_delay(30);
HAL_GPIO_WritePin(SPIx_CS_GPIO_PORT,SPIx_CS_PIN,GPIO_PIN_SET);
}
void Lpspi_Read(uint8_t datasize,uint8_t *data,uint8_t *Rxdata)
{
//读写数据
HAL_GPIO_WritePin(SPIx_CS_GPIO_PORT,SPIx_CS_PIN,GPIO_PIN_RESET);
HAL_SPI_TransmitReceive(&SpiHandle,data,Rxdata, datasize, 5000);
Spi_delay(30);
HAL_GPIO_WritePin(SPIx_CS_GPIO_PORT,SPIx_CS_PIN,GPIO_PIN_SET);
}
</code></pre>
<pre>
<code class="language-cpp">/*
analogysemi
xutong 2023/10/19
*/
#ifndef __BSP_LPSPI_H
#define __BSP_LPSPI_H
#include "stm32h7xx_hal.h"
#define SPIx SPI1
#define SPIx_CLK_ENABLE() __HAL_RCC_SPI1_CLK_ENABLE()
#define SPIx_SCK_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
#define SPIx_MISO_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
#define SPIx_MOSI_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
#define SPIx_CS_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
/* Definition for SPIx Pins */
#define SPIx_SCK_PIN GPIO_PIN_3
#define SPIx_SCK_GPIO_PORT GPIOB
#define SPIx_SCK_AF GPIO_AF5_SPI1
#define SPIx_MISO_PIN GPIO_PIN_4
#define SPIx_MISO_GPIO_PORT GPIOB
#define SPIx_MISO_AF GPIO_AF5_SPI1
#define SPIx_MOSI_PIN GPIO_PIN_5
#define SPIx_MOSI_GPIO_PORT GPIOB
#define SPIx_MOSI_AF GPIO_AF5_SPI1
#define SPIx_CS_PIN GPIO_PIN_6
#define SPIx_CS_GPIO_PORT GPIOB
void LPSPI_Init(void);
void Lpspi_Write(uint8_t datasize,uint8_t *data);
void Lpspi_Read(uint8_t datasize,uint8_t *data,uint8_t *Rxdata);
#endif /* __BSP_LPSPI_H */</code></pre>
<p>转换成STM32能用的驱动</p>
<p> </p>
页:
[1]