Skip to content

Commit

Permalink
feat: add comparator config functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Hossein-M98 committed Nov 10, 2023
1 parent d974498 commit e000605
Show file tree
Hide file tree
Showing 2 changed files with 316 additions and 0 deletions.
201 changes: 201 additions & 0 deletions ADS1x1x.c
Original file line number Diff line number Diff line change
Expand Up @@ -455,9 +455,210 @@ ADS1x1x_SetMux(ADS1x1x_Handler_t *Handler, ADS1x1x_Mux_t Mux)
if (ADS1x1x_WriteConfigurationRegister(Handler, ConfigReg) < 0)
return ADS1X1X_FAIL;

return ADS1X1X_OK;
}

/**
* @brief Set Comparator mode
* @param Handler: Pointer to handler
* @param CompMode: Specify Comparator mode
* @retval ADS1x1x_Result_t
* - ADS1X1X_OK: Operation was successful.
* - ADS1X1X_FAIL: Failed to send or receive data.
* - ADS1X1X_INVALID_PARAM: One of parameters is invalid.
*/
ADS1x1x_Result_t
ADS1x1x_SetCompMode(ADS1x1x_Handler_t *Handler, ADS1x1x_CompMode_t CompMode)
{
uint16_t ConfigReg = 0;

if (ADS1x1x_ReadConfigurationRegister(Handler, &ConfigReg) < 0)
return ADS1X1X_FAIL;

ConfigReg &= ~(1 << ADS1X1X_CONFIG_REG_OS);
switch (CompMode)
{
case ADS1X1X_COMP_MODE_TRADITIONAL:
ConfigReg &= ~(1 << ADS1X1X_CONFIG_REG_COMP_MODE);
break;

case ADS1X1X_COMP_MODE_WINDOW:
ConfigReg |= (1 << ADS1X1X_CONFIG_REG_COMP_MODE);
break;

default:
return ADS1X1X_INVALID_PARAM;
break;
}

if (ADS1x1x_WriteConfigurationRegister(Handler, ConfigReg) < 0)
return ADS1X1X_FAIL;

return ADS1X1X_OK;
}

/**
* @brief Set Comparator polarity
* @param Handler: Pointer to handler
* @param CompPol: Specify Comparator polarity
* @retval ADS1x1x_Result_t
* - ADS1X1X_OK: Operation was successful.
* - ADS1X1X_FAIL: Failed to send or receive data.
* - ADS1X1X_INVALID_PARAM: One of parameters is invalid.
*/
ADS1x1x_Result_t
ADS1x1x_SetCompPol(ADS1x1x_Handler_t *Handler, ADS1x1x_CompPol_t CompPol)
{
uint16_t ConfigReg = 0;

if (ADS1x1x_ReadConfigurationRegister(Handler, &ConfigReg) < 0)
return ADS1X1X_FAIL;

ConfigReg &= ~(1 << ADS1X1X_CONFIG_REG_OS);
switch (CompPol)
{
case ADS1X1X_COMP_POL_LOW:
ConfigReg &= ~(1 << ADS1X1X_CONFIG_REG_COMP_POL);
break;

case ADS1X1X_COMP_POL_HIGH:
ConfigReg |= (1 << ADS1X1X_CONFIG_REG_COMP_POL);
break;

default:
return ADS1X1X_INVALID_PARAM;
break;
}

if (ADS1x1x_WriteConfigurationRegister(Handler, ConfigReg) < 0)
return ADS1X1X_FAIL;

return ADS1X1X_OK;
}

/**
* @brief Set Comparator latch
* @param Handler: Pointer to handler
* @param CompLat: Specify Comparator latch
* @retval ADS1x1x_Result_t
* - ADS1X1X_OK: Operation was successful.
* - ADS1X1X_FAIL: Failed to send or receive data.
* - ADS1X1X_INVALID_PARAM: One of parameters is invalid.
*/
ADS1x1x_Result_t
ADS1x1x_SetCompLat(ADS1x1x_Handler_t *Handler, ADS1x1x_CompLat_t CompLat)
{
uint16_t ConfigReg = 0;

if (ADS1x1x_ReadConfigurationRegister(Handler, &ConfigReg) < 0)
return ADS1X1X_FAIL;

ConfigReg &= ~(1 << ADS1X1X_CONFIG_REG_OS);
switch (CompLat)
{
case ADS1X1X_COMP_LAT_NON_LATCHING:
ConfigReg &= ~(1 << ADS1X1X_CONFIG_REG_COMP_LAT);
break;

case ADS1X1X_COMP_LAT_LATCHING:
ConfigReg |= (1 << ADS1X1X_CONFIG_REG_COMP_LAT);
break;

default:
return ADS1X1X_INVALID_PARAM;
break;
}

if (ADS1x1x_WriteConfigurationRegister(Handler, ConfigReg) < 0)
return ADS1X1X_FAIL;

return ADS1X1X_OK;
}

/**
* @brief Set Comparator queue
* @param Handler: Pointer to handler
* @param CompQue: Specify Comparator queue
* @retval ADS1x1x_Result_t
* - ADS1X1X_OK: Operation was successful.
* - ADS1X1X_FAIL: Failed to send or receive data.
* - ADS1X1X_INVALID_PARAM: One of parameters is invalid.
*/
ADS1x1x_Result_t
ADS1x1x_SetCompQue(ADS1x1x_Handler_t *Handler, ADS1x1x_CompQue_t CompQue)
{
uint16_t ConfigReg = 0;

if (ADS1x1x_ReadConfigurationRegister(Handler, &ConfigReg) < 0)
return ADS1X1X_FAIL;

ConfigReg &= ~(1 << ADS1X1X_CONFIG_REG_OS);
switch (CompQue)
{
case ADS1X1X_COMP_QUE_ASSERT_1:
ConfigReg &= ~(1 << ADS1X1X_CONFIG_REG_COMP_QUE1);
ConfigReg &= ~(1 << ADS1X1X_CONFIG_REG_COMP_QUE0);
break;

case ADS1X1X_COMP_QUE_ASSERT_2:
ConfigReg &= ~(1 << ADS1X1X_CONFIG_REG_COMP_QUE1);
ConfigReg |= (1 << ADS1X1X_CONFIG_REG_COMP_QUE0);
break;

case ADS1X1X_COMP_QUE_ASSERT_4:
ConfigReg |= (1 << ADS1X1X_CONFIG_REG_COMP_QUE1);
ConfigReg &= ~(1 << ADS1X1X_CONFIG_REG_COMP_QUE0);
break;

case ADS1X1X_COMP_QUE_DISABLE:
ConfigReg |= (1 << ADS1X1X_CONFIG_REG_COMP_QUE1);
ConfigReg |= (1 << ADS1X1X_CONFIG_REG_COMP_QUE0);
break;

default:
return ADS1X1X_INVALID_PARAM;
break;
}

if (ADS1x1x_WriteConfigurationRegister(Handler, ConfigReg) < 0)
return ADS1X1X_FAIL;

return ADS1X1X_OK;
}

/**
* @brief Set Lo-Threshold
* @param Handler: Pointer to handler
* @param Threshold: Specify Lo-Threshold
* @retval ADS1x1x_Result_t
* - ADS1X1X_OK: Operation was successful.
* - ADS1X1X_FAIL: Failed to send or receive data.
* - ADS1X1X_INVALID_PARAM: One of parameters is invalid.
*/
ADS1x1x_Result_t
ADS1x1x_SetLoThresh(ADS1x1x_Handler_t *Handler, int16_t Threshold)
{
if (ADS1x1x_WriteReg(Handler, ADS1X1X_REG_ADDR_LO_THRESH, (uint16_t)Threshold) < 0)
return ADS1X1X_FAIL;

return ADS1X1X_OK;
}

/**
* @brief Set Hi-Threshold
* @param Handler: Pointer to handler
* @param Threshold: Specify Hi-Threshold
* @retval ADS1x1x_Result_t
* - ADS1X1X_OK: Operation was successful.
* - ADS1X1X_FAIL: Failed to send or receive data.
* - ADS1X1X_INVALID_PARAM: One of parameters is invalid.
*/
ADS1x1x_Result_t
ADS1x1x_SetHiThresh(ADS1x1x_Handler_t *Handler, int16_t Threshold)
{
if (ADS1x1x_WriteReg(Handler, ADS1X1X_REG_ADDR_HI_THRESH, (uint16_t)Threshold) < 0)
return ADS1X1X_FAIL;

return ADS1X1X_OK;
}

Expand Down
115 changes: 115 additions & 0 deletions ADS1x1x.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,43 @@ typedef enum ADS1x1x_Rate_e
ADS1X1X_RATE_860SPS = 7
} ADS1x1x_Rate_t;

/**
* @brief Comparator mode
*/
typedef enum ADS1x1x_CompMode_e
{
ADS1X1X_COMP_MODE_TRADITIONAL = 0,
ADS1X1X_COMP_MODE_WINDOW = 1
} ADS1x1x_CompMode_t;

/**
* @brief Comparator polarity
*/
typedef enum ADS1x1x_CompPol_e
{
ADS1X1X_COMP_POL_LOW = 0,
ADS1X1X_COMP_POL_HIGH = 1
} ADS1x1x_CompPol_t;

/**
* @brief Comparator latch
*/
typedef enum ADS1x1x_CompLat_e
{
ADS1X1X_COMP_LAT_NON_LATCHING = 0,
ADS1X1X_COMP_LAT_LATCHING = 1
} ADS1x1x_CompLat_t;

/**
* @brief Comparator queue
*/
typedef enum ADS1x1x_CompQue_e
{
ADS1X1X_COMP_QUE_ASSERT_1 = 0,
ADS1X1X_COMP_QUE_ASSERT_2 = 1,
ADS1X1X_COMP_QUE_ASSERT_4 = 2,
ADS1X1X_COMP_QUE_DISABLE = 3
} ADS1x1x_CompQue_t;


/**
Expand Down Expand Up @@ -309,6 +346,84 @@ ADS1x1x_Result_t
ADS1x1x_SetMux(ADS1x1x_Handler_t *Handler, ADS1x1x_Mux_t Mux);


/**
* @brief Set Comparator mode
* @param Handler: Pointer to handler
* @param CompMode: Specify Comparator mode
* @retval ADS1x1x_Result_t
* - ADS1X1X_OK: Operation was successful.
* - ADS1X1X_FAIL: Failed to send or receive data.
* - ADS1X1X_INVALID_PARAM: One of parameters is invalid.
*/
ADS1x1x_Result_t
ADS1x1x_SetCompMode(ADS1x1x_Handler_t *Handler, ADS1x1x_CompMode_t CompMode);


/**
* @brief Set Comparator polarity
* @param Handler: Pointer to handler
* @param CompPol: Specify Comparator polarity
* @retval ADS1x1x_Result_t
* - ADS1X1X_OK: Operation was successful.
* - ADS1X1X_FAIL: Failed to send or receive data.
* - ADS1X1X_INVALID_PARAM: One of parameters is invalid.
*/
ADS1x1x_Result_t
ADS1x1x_SetCompPol(ADS1x1x_Handler_t *Handler, ADS1x1x_CompPol_t CompPol);


/**
* @brief Set Comparator latch
* @param Handler: Pointer to handler
* @param CompLat: Specify Comparator latch
* @retval ADS1x1x_Result_t
* - ADS1X1X_OK: Operation was successful.
* - ADS1X1X_FAIL: Failed to send or receive data.
* - ADS1X1X_INVALID_PARAM: One of parameters is invalid.
*/
ADS1x1x_Result_t
ADS1x1x_SetCompLat(ADS1x1x_Handler_t *Handler, ADS1x1x_CompLat_t CompLat);


/**
* @brief Set Comparator queue
* @param Handler: Pointer to handler
* @param CompQue: Specify Comparator queue
* @retval ADS1x1x_Result_t
* - ADS1X1X_OK: Operation was successful.
* - ADS1X1X_FAIL: Failed to send or receive data.
* - ADS1X1X_INVALID_PARAM: One of parameters is invalid.
*/
ADS1x1x_Result_t
ADS1x1x_SetCompQue(ADS1x1x_Handler_t *Handler, ADS1x1x_CompQue_t CompQue);


/**
* @brief Set Lo-Threshold
* @param Handler: Pointer to handler
* @param Threshold: Specify Lo-Threshold
* @retval ADS1x1x_Result_t
* - ADS1X1X_OK: Operation was successful.
* - ADS1X1X_FAIL: Failed to send or receive data.
* - ADS1X1X_INVALID_PARAM: One of parameters is invalid.
*/
ADS1x1x_Result_t
ADS1x1x_SetLoThresh(ADS1x1x_Handler_t *Handler, int16_t Threshold);


/**
* @brief Set Hi-Threshold
* @param Handler: Pointer to handler
* @param Threshold: Specify Hi-Threshold
* @retval ADS1x1x_Result_t
* - ADS1X1X_OK: Operation was successful.
* - ADS1X1X_FAIL: Failed to send or receive data.
* - ADS1X1X_INVALID_PARAM: One of parameters is invalid.
*/
ADS1x1x_Result_t
ADS1x1x_SetHiThresh(ADS1x1x_Handler_t *Handler, int16_t Threshold);


/**
* @brief Start conversion (single shot)
* @param Handler: Pointer to handler
Expand Down

0 comments on commit e000605

Please sign in to comment.