#include "AD7124.h"
#include "communication.h"
/* Error codes */
#define INVALID_VAL -1 /* Invalid argument */
#define COMM_ERR -2 /* Communication error on receive */
#define TIMEOUT -3 /* A timeout has occured */
/*! Array holding the info for the ad7124 registers - address, initial value,
size and access type. */
ad7124_st_reg ad7124_regs[] =
{
{0x00, 0x00, 1, 2}, /* AD7124_Status */
{0x01, 0x1580, 2, 1}, /* AD7124_ADC_Control */
{0x02, 0x0000, 3, 2}, /* AD7124_Data */
{0x03, 0x8000, 3, 1}, /* AD7124_IOCon1 */
{0x04, 0x0000, 2, 1}, /* AD7124_IOCon2 */
{0x05, 0x02, 1, 2}, /* AD7124_ID */
{0x06, 0x0000, 3, 2}, /* AD7124_Error */
{0x07, 0x001F, 3, 1}, /* AD7124_Error_En */
{0x08, 0x00, 1, 2}, /* AD7124_Mclk_Count */
{0x09, 0x8011, 2, 1}, /* AD7124_Channel_0 */
// {0x0A, 0x9031, 2, 1}, /* AD7124_Channel_1 */
// {0x0B, 0xA051, 2, 1}, /* AD7124_Channel_2 */
// {0x0C, 0xB071, 2, 1}, /* AD7124_Channel_3 */
// {0x0D, 0xC091, 2, 1}, /* AD7124_Channel_4 */
// {0x0E, 0xD0B1, 2, 1}, /* AD7124_Channel_5 */
// {0x0F, 0xE0D1, 2, 1}, /* AD7124_Channel_6 */
// {0x10, 0xF0F1, 2, 1}, /* AD7124_Channel_7 */
// {0x11, 0x0001, 2, 1}, /* AD7124_Channel_8 */
// {0x12, 0x0001, 2, 1}, /* AD7124_Channel_9 */
// {0x13, 0x0001, 2, 1}, /* AD7124_Channel_10 */
// {0x14, 0x0001, 2, 1}, /* AD7124_Channel_11 */
// {0x15, 0x0001, 2, 1}, /* AD7124_Channel_12 */
// {0x16, 0x0001, 2, 1}, /* AD7124_Channel_13 */
// {0x17, 0x0001, 2, 1}, /* AD7124_Channel_14 */
// {0x18, 0x0001, 2, 1}, /* AD7124_Channel_15 */
{0x19, 0x09F0, 2, 1}, /* AD7124_Config_0 */
{0x1A, 0x09F4, 2, 1}, /* AD7124_Config_1 */
{0x1B, 0x09F4, 2, 1}, /* AD7124_Config_2 */
{0x1C, 0x09F4, 2, 1}, /* AD7124_Config_3 */
{0x1D, 0x09F4, 2, 1}, /* AD7124_Config_4 */
{0x1E, 0x09F4, 2, 1}, /* AD7124_Config_5 */
{0x1F, 0x09F4, 2, 1}, /* AD7124_Config_6 */
{0x20, 0x09F4, 2, 1}, /* AD7124_Config_7 */
{0x21, 0x1060180, 3, 1}, /* AD7124_Filter_0 */
{0x22, 0x160180, 3, 1}, /* AD7124_Filter_1 */
{0x23, 0x160180, 3, 1}, /* AD7124_Filter_2 */
{0x24, 0x160180, 3, 1}, /* AD7124_Filter_3 */
{0x25, 0x160180, 3, 1}, /* AD7124_Filter_4 */
{0x26, 0x160180, 3, 1}, /* AD7124_Filter_5 */
{0x27, 0x160180, 3, 1}, /* AD7124_Filter_6 */
{0x28, 0x160180, 3, 1}, /* AD7124_Filter_7 */
{0x29, 0x800000, 3, 1}, /* AD7124_Offset_0 */
{0x2A, 0x800000, 3, 1}, /* AD7124_Offset_1 */
{0x2B, 0x800000, 3, 1}, /* AD7124_Offset_2 */
{0x2C, 0x800000, 3, 1}, /* AD7124_Offset_3 */
{0x2D, 0x800000, 3, 1}, /* AD7124_Offset_4 */
{0x2E, 0x800000, 3, 1}, /* AD7124_Offset_5 */
{0x2F, 0x800000, 3, 1}, /* AD7124_Offset_6 */
{0x30, 0x800000, 3, 1}, /* AD7124_Offset_7 */
{0x31, 0x500000, 3, 1}, /* AD7124_Gain_0 */
{0x32, 0x500000, 3, 1}, /* AD7124_Gain_1 */
{0x33, 0x500000, 3, 1}, /* AD7124_Gain_2 */
{0x34, 0x500000, 3, 1}, /* AD7124_Gain_3 */
{0x35, 0x500000, 3, 1}, /* AD7124_Gain_4 */
{0x36, 0x500000, 3, 1}, /* AD7124_Gain_5 */
{0x37, 0x500000, 3, 1}, /* AD7124_Gain_6 */
{0x38, 0x500000, 3, 1}, /* AD7124_Gain_7 */
};
/***************************************************************************//**
* @brief Reads the value of the specified register without checking if the
* device is ready to accept user requests.
*
* @param device - The handler of the instance of the driver.
* @param pReg - Pointer to the register structure holding info about the
* register to be read. The read value is stored inside the
* register structure.
*
* @return Returns 0 for success or negative error code.
*******************************************************************************/
int32_t AD7124_NoCheckReadRegister(ad7124_device *device, ad7124_st_reg* pReg)
{
int32_t ret = 0;
uint8_t buffer[8] = {0, 0, 0, 0, 0, 0, 0, 0};
uint8_t i = 0;
uint8_t check8 = 0;
uint8_t msgBuf[8] = {0, 0, 0, 0, 0, 0, 0, 0};
if(!device || !pReg)
return INVALID_VAL;
/* Build the Command word */
buffer[0] = AD7124_COMM_REG_WEN | AD7124_COMM_REG_RD |
AD7124_COMM_REG_RA(pReg->addr);
/* Read data from the device */
ret = AD7124_SPI_Read(device->slave_select_id,
buffer,
((device->useCRC != AD7124_DISABLE_CRC) ? pReg->size + 1
: pReg->size) + 1);
if(ret < 0)
return ret;
/* Check the CRC */
if(device->useCRC == AD7124_USE_CRC)
{
msgBuf[0] = AD7124_COMM_REG_WEN | AD7124_COMM_REG_RD |
AD7124_COMM_REG_RA(pReg->addr);
for(i = 1; i < pReg->size + 2; ++i)
{
msgBuf[i] = buffer[i];
}
check8 = AD7124_ComputeCRC8(msgBuf, pReg->size + 2);
}
if(check8 != 0)
{
/* ReadRegister checksum failed. */
return COMM_ERR;
}
/* Build the result */
pReg->value = 0;
for(i = 1; i < pReg->size + 1; i++)
{
pReg->value <<= 8;
pReg->value += buffer[i];
}
return ret;
}
/***************************************************************************//**
* @brief Writes the value of the specified register without checking if the
* device is ready to accept user requests.
*
* @param device - The handler of the instance of the driver.
* @param reg - Register structure holding info about the register to be written
*
* @return Returns 0 for success or negative error code.
*******************************************************************************/
int32_t AD7124_NoCheckWriteRegister(ad7124_device *device, ad7124_st_reg reg)
{
int32_t ret = 0;
int32_t regValue = 0;
uint8_t wrBuf[8] = {0, 0, 0, 0, 0, 0, 0, 0};
uint8_t i = 0;
uint8_t crc8 = 0;
if(!device)
return INVALID_VAL;
/* Build the Command word */
wrBuf[0] = AD7124_COMM_REG_WEN | AD7124_COMM_REG_WR |
AD7124_COMM_REG_RA(reg.addr);
/* Fill the write buffer */
regValue = reg.value;
for(i = 0; i < reg.size; i++)
{
wrBuf[reg.size - i] = regValue & 0xFF;
regValue >>= 8;
}
/* Compute the CRC */
if(device->useCRC != AD7124_DISABLE_CRC)
{
crc8 = AD7124_ComputeCRC8(wrBuf, reg.size + 1);
wrBuf[reg.size + 1] = crc8;
}
/* Write data to the device */
ret = AD7124_SPI_Write(device->slave_select_id,
wrBuf,
(device->useCRC != AD7124_DISABLE_CRC) ? reg.size + 2
: reg.size + 1);
return ret;
}
/***************************************************************************//**
* @brief Reads the value of the specified register only when the device is ready
* to accept user requests. If the device ready flag is deactivated the
* read operation will be executed without checking the device state.
*
* @param device - The handler of the instance of the driver.
* @param pReg - Pointer to the register structure holding info about the
* register to be read. The read value is stored inside the
* register structure.
*
* @return Returns 0 for success or negative error code.
*******************************************************************************/
int32_t AD7124_ReadRegister(ad7124_device *device, ad7124_st_reg* pReg)
{
int32_t ret;
if (pReg->addr != AD7124_ERR_REG && device->check_ready)
{
ret = AD7124_WaitForSpiReady(device, device->spi_rdy_poll_cnt);
if (ret < 0)
return ret;
}
ret = AD7124_NoCheckReadRegister(device, pReg);
return ret;
}
/***************************************************************************//**
* @brief Writes the value of the specified register only when the device is
* ready to accept user requests. If the device ready flag is deactivated
* the write operation will be executed without checking the device state.
*
* @param device - The handler of the instance of the driver.
* @param reg - Register structure holding info about the register to be written
没有合适的资源?快使用搜索试试~ 我知道了~
AD7124参考程序
共7个文件
h:4个
c:3个
4星 · 超过85%的资源 需积分: 47 147 下载量 116 浏览量
2019-03-29
17:04:32
上传
评论 2
收藏 8KB RAR 举报
温馨提示
stm32通过软件SPI来驱动,可以直接抑制,能够正常读写
资源推荐
资源详情
资源评论
收起资源包目录
AD7124.rar (7个子文件)
AD7124
myspi.h 1003B
Communication.c 3KB
AD7124.c 16KB
myspi.c 2KB
ad7124_regs.h 10KB
AD7124.h 3KB
communication.h 725B
共 7 条
- 1
资源评论
- NEU19802019-06-22例程很典型,很有参考价值
- 8704846292020-11-15没参考价值
- 黑崎江2021-02-06不是完整工程,没有提供具体怎么使用。 把官方的库的底层完善了,思路变得清晰了,可以参考,45分的确有点小贵,如果嫌积分高的,可以先自己去看官方的库和数据手册。
clearcode
- 粉丝: 342
- 资源: 22
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功