/*******************************************************************************
* Function Name: init_adc
********************************************************************************
* Summary:
* Initializes the SAR ADC using autonomous analog control.
* Sets up interrupt masks and enables autonomous operation.
*
* Parameters:
* none
*
* Return:
* cy_rslt_t - CY_RSLT_SUCCESS if initialization successful
*
*******************************************************************************/
static cy_rslt_t init_adc(void)
{
cy_rslt_t result;
/* Initialize SAR ADC with autonomous analog configuration */
result = Cy_AutAnalog_Init(&autonomous_analog_init);
if (CY_AUTANALOG_SUCCESS != result)
{
return result;
}
/* Set interrupt mask to enable SAR0 result interrupt */
Cy_AutAnalog_SetInterruptMask(CY_AUTANALOG_INT_SAR0_RESULT);
/* Start autonomous control of the ADC */
Cy_AutAnalog_StartAutonomousControl();
adc_initialized = true;
return CY_RSLT_SUCCESS;
}
/*******************************************************************************
* Function Name: read_potentiometer_mv
********************************************************************************
* Summary:
* Reads the potentiometer value from ADC and converts to millivolts.
* Uses the autonomous analog SAR ADC API.
*
* Parameters:
* none
*
* Return:
* int16_t - Voltage in millivolts (0-1800 mV)
*
*******************************************************************************/
static int16_t read_potentiometer_mv(void)
{
int32_t adc_count;
int16_t voltage_mv;
/* Read ADC result (raw counts) */
adc_count = Cy_AutAnalog_SAR_ReadResult(SAR_ADC_INDEX,
CY_AUTANALOG_SAR_INPUT_GPIO,
SAR_ADC_CHANNEL);
/* Convert ADC counts to millivolts */
voltage_mv = Cy_AutAnalog_SAR_CountsTo_mVolts(SAR_ADC_INDEX,
false,
SAR_ADC_SEQUENCER,
CY_AUTANALOG_SAR_INPUT_GPIO,
SAR_ADC_CHANNEL,
SAR_ADC_VREF_MV,
adc_count);
return voltage_mv;
}
/*******************************************************************************
* Function Name: get_pot_percentage
********************************************************************************
* Summary:
* Converts potentiometer voltage to percentage (0-100%).
* Maps the voltage range (0 to VREF_MV) to percentage scale.
*
* Parameters:
* voltage_mv - Input voltage in millivolts
*
* Return:
* uint8_t - Percentage value (0-100)
*
*******************************************************************************/
static uint8_t get_pot_percentage(int16_t voltage_mv)
{
int32_t percentage;
/* Ensure voltage is within valid range */
if (voltage_mv < 0)
{
voltage_mv = 0;
}
else if (voltage_mv > SAR_ADC_VREF_MV)
{
voltage_mv = SAR_ADC_VREF_MV;
}
/* Calculate percentage: (voltage / VREF) * 100 */
percentage = ((int32_t)voltage_mv * 100) / SAR_ADC_VREF_MV;
return (uint8_t)percentage;
}
int main(void)
{
cy_rslt_t result = CY_RSLT_SUCCESS;
int16_t voltage_mv;
uint8_t percentage;
/* Initialize the device and board peripherals. */
result = cybsp_init();
/* Board initialization failed. Stop program execution. */
if (CY_RSLT_SUCCESS != result)
{
handle_app_error();
}
/* Enable global interrupts */
__enable_irq();
/* Initialize retarget-io middleware */
init_retarget_io();
/* \x1b[2J\x1b[;H - ANSI ESC sequence for clear screen. */
printf("\x1b[2J\x1b[;H");
printf("***********************************************************\r\n");
printf(" PSOC Edge MCU: Potentiometer ADC \r\n");
printf("***********************************************************\r\n\n");
/* Initialize SAR ADC using helper function */
result = init_adc();
if (CY_RSLT_SUCCESS != result)
{
printf("ADC initialization failed!\r\n");
handle_app_error();
}
/* Inform user about potentiometer connection */
printf("ADC initialized successfully!\r\n");
printf("Reading potentiometer on pin P15_1\r\n");
printf("VREF = %d mV (12-bit resolution)\r\n\n", SAR_ADC_VREF_MV);
/* CY_CM55_APP_BOOT_ADDR must be updated if CM55 memory layout is changed.*/
Cy_SysEnableCM55(MXCM55, CY_CM55_APP_BOOT_ADDR, CM55_BOOT_WAIT_TIME_USEC);
for(;;)
{
/* Read potentiometer voltage using helper function */
voltage_mv = read_potentiometer_mv();
/* Convert to percentage using helper function */
percentage = get_pot_percentage(voltage_mv);
/* Print ADC output in millivolts and percentage */
printf("POT: %4d mV | %3d%%\r\n", voltage_mv, percentage);
/* Toggle user LED to indicate activity */
Cy_GPIO_Inv(CYBSP_USER_LED_PORT, CYBSP_USER_LED_PIN);
/* Delay between ADC readings */
Cy_SysLib_Delay(ADC_READ_INTERVAL_MS);
}
}