• 起航了
    了解作者
  • C/C++
    开发工具
  • 10KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 0
    下载次数
  • 2021-03-11 15:46
    上传日期
AD公司脉搏传感器,红光,绿光,红外光adpd174驱动程序
adpd174.rar
  • adpd174.h
    18.4KB
  • adpd174.c
    35.7KB
内容介绍
/** ============================================================================== * @file adpd174.c * @author * @version V1.0 * @date 2017-5-15 * @brief ADPD174驱动程序 * @verbatim ============================================================================== */ #include "adpd174.h" #include "hard_ctrl.h" /*------------------------------------------------------------------------------- 修改记录 --------------------------------------------------------------------------------- -------------------------------------------------------------------------------*/ //定义ADPD174通讯出错统计 static uint8_t adpd174_error_times = 0; //定义ADPD174工作模式 static ADPD174_MODE adpd174_work_mode; //定义ADPD174内部晶振校正需要使用RTC中断来计1秒时间,需要外部中断来计数产生中断次数 static struct { volatile uint16_t int_count; //定义32K校正时,统计一段时间内产生的中断计数 volatile uint16_t CalTimer; //定义32K校正时1秒时基计数 }adpd174_Calibrate; extern RTC_HandleTypeDef hrtc; //定义ADPD174 I2C端口号 I2C_HandleTypeDef *pADPD174_I2cHandle; //------------------------------------------------------------------------------ //私有函数 static void ADPD174_error_deal(void); static uint8_t adpd174_wait_i2c_ready(void); static uint8_t adpd174_NormalToStandby(void); static uint8_t adpd174_AfeOffSet(uint16_t SlotA_Offset, uint16_t SlotB_Offset); static uint8_t adpd174_OutDataRateSet(uint16_t SampleFreq, uint16_t SlotA_AverNum, uint16_t SlotB_AverNum); static uint8_t adpd174_OutDataMode(uint8_t OutDataMode, uint16_t fifo_th); static uint8_t adpd174_NormalMode(void); static uint8_t adpd174_FIFO_ReadDataNum(uint16_t *pData); static uint8_t adpd174_Reg_Init(void); static uint8_t adpd174_32K_Calibration(void); static uint8_t adpd174_32M_Calibration(void); //外部接口函数 /*------------------------------------------------------------------------------------------------- 功能: 读取ADPD174的通讯状态 输入: 无 返回: 0-连接正常 其余-异常 --------------------------------------------------------------------------------------------------*/ uint8_t adpd174_get_status(void) { if(adpd174_error_times >= ADPD174_ERROR_TIMES) { return ADPD174_ERROR; } return ADPD174_OK; } /*------------------------------------------------------------------------------------------------- 功能: 读取ADPD174当前的工作模式 输入: 无 返回: 当前工作模式 --------------------------------------------------------------------------------------------------*/ ADPD174_MODE adpd174_get_work_mode(void) { return adpd174_work_mode; } /*------------------------------------------------------------------------------------------------- 功能: ADPD174 寄存器读 输入: reg_addr--寄存器地址 reg_data--读取数据存放指针 cnt-------读取数据长度 返回: 0-成功 1-失败 --------------------------------------------------------------------------------------------------*/ uint8_t adpd174_I2C_read(uint8_t reg_addr, uint8_t *reg_data, uint8_t cnt) { uint8_t i; if(adpd174_wait_i2c_ready() != ADPD174_OK) return ADPD174_ERROR; //读取数据,尝试三次 for(i=0; i<3; i++) { if(HAL_I2C_Mem_Read(pADPD174_I2cHandle, ADPD174_I2C_ADD, reg_addr, I2C_MEMADD_SIZE_8BIT, reg_data, cnt, ADPD174_I2C_TIMEOUT) == HAL_OK) { adpd174_error_times = 0; return ADPD174_OK; } HAL_Delay(1); } ADPD174_error_deal(); return ADPD174_ERROR; } /*------------------------------------------------------------------------------------------------- 功能: ADPD174 寄存器写 输入: reg_addr--寄存器地址 reg_data--写入数据存放指针 cnt-------写入数据长度 返回: 0-成功 1-失败 --------------------------------------------------------------------------------------------------*/ uint8_t adpd174_I2C_write(uint8_t reg_addr, uint8_t *reg_data, uint8_t cnt) { uint8_t i; if(adpd174_wait_i2c_ready() != ADPD174_OK) return ADPD174_ERROR; //写入数据,尝试三次 for(i=0; i<3; i++) { if(HAL_I2C_Mem_Write(pADPD174_I2cHandle, ADPD174_I2C_ADD, reg_addr, I2C_MEMADD_SIZE_8BIT, reg_data, cnt, ADPD174_I2C_TIMEOUT) == HAL_OK) { adpd174_error_times = 0; return ADPD174_OK; } HAL_Delay(1); } ADPD174_error_deal(); return ADPD174_ERROR; } /*------------------------------------------------------------------------------------------------- 功能: ADPD174 寄存器读DMA方式 输入: reg_addr--寄存器地址 reg_data--读取数据存放指针 cnt-------读取数据长度 返回: 0-成功 1-失败 --------------------------------------------------------------------------------------------------*/ uint8_t adpd174_I2C_read_DMA(uint8_t reg_addr, uint8_t *reg_data, uint8_t cnt) { uint8_t i; if(adpd174_wait_i2c_ready() != ADPD174_OK) return ADPD174_ERROR; for(i=0; i<3; i++) { if(HAL_I2C_Mem_Read_DMA(pADPD174_I2cHandle, ADPD174_I2C_ADD, reg_addr, I2C_MEMADD_SIZE_8BIT, reg_data, cnt) == HAL_OK) { adpd174_error_times = 0; return ADPD174_OK; } HAL_Delay(1); } ADPD174_error_deal(); return ADPD174_ERROR; } /*------------------------------------------------------------------------------------------------- 功能: ADPD174 寄存器写DMA方式 输入: reg_addr--寄存器地址 reg_data--写入数据存放指针 cnt-------写入数据长度 返回: 0-成功 1-失败 --------------------------------------------------------------------------------------------------*/ uint8_t adpd174_I2C_write_DMA(uint8_t reg_addr, uint8_t *reg_data, uint8_t cnt) { uint8_t i; if(adpd174_wait_i2c_ready() != ADPD174_OK) return ADPD174_ERROR; //读取数据,尝试三次 //adpd174_int_disable(); for(i=0; i<3; i++) { if(HAL_I2C_Mem_Write_DMA(pADPD174_I2cHandle, ADPD174_I2C_ADD, reg_addr, I2C_MEMADD_SIZE_8BIT, reg_data, cnt) == HAL_OK) { adpd174_error_times = 0; return ADPD174_OK; } HAL_Delay(1); } //adpd174_int_enable(); ADPD174_error_deal(); return ADPD174_ERROR; } /*------------------------------------------------------------------------------------------------- 功能: ADPD174 单个寄存器读出,16位 输入: reg_addr--寄存器地址 reg_data--读取数据存放指针 返回: 0-成功 1-失败 --------------------------------------------------------------------------------------------------*/ uint8_t adpd174_Reg_Read(uint8_t reg_addr, uint16_t *reg_data) { uint8_t status,read_data[2]; status = adpd174_I2C_read (reg_addr, read_data, 2); if(status == ADPD174_OK) { *reg_data = (uint16_t)read_data[0] *0x100 + read_data[1]; } return status; } /*------------------------------------------------------------------------------------------------- 功能: ADPD174 单个寄存器写入,16位 输入: reg_addr--寄存器地址 reg_data--读取数据存放指针 返回: 0-成功 1-失败 -------------------------------------
评论
    相关推荐