/*******************************************************************************
* 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);
}
}
/*******************************************************************************
* File Name : main.c
*
* Description : This source file contains the main routine for non-secure
* application in the CM33 CPU
*
* Related Document : See README.md
*
********************************************************************************
* Copyright 2024-2025, Cypress Semiconductor Corporation (an Infineon company) or
* an affiliate of Cypress Semiconductor Corporation. All rights reserved.
*
* This software, including source code, documentation and related
* materials ("Software") is owned by Cypress Semiconductor Corporation
* or one of its affiliates ("Cypress") and is protected by and subject to
* worldwide patent protection (United States and foreign),
* United States copyright laws and international treaty provisions.
* Therefore, you may use this Software only as provided in the license
* agreement accompanying the software package from which you
* obtained this Software ("EULA").
* If no EULA applies, Cypress hereby grants you a personal, non-exclusive,
* non-transferable license to copy, modify, and compile the Software
* source code solely for use in connection with Cypress's
* integrated circuit products. Any reproduction, modification, translation,
* compilation, or representation of this Software except as specified
* above is prohibited without the express written permission of Cypress.
*
* Disclaimer: THIS SOFTWARE IS PROVIDED AS-IS, WITH NO WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT, IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Cypress
* reserves the right to make changes to the Software without notice. Cypress
* does not assume any liability arising out of the application or use of the
* Software or any product or circuit described in the Software. Cypress does
* not authorize its products for use in any products where a malfunction or
* failure of the Cypress product may reasonably be expected to result in
* significant property damage, injury or death ("High Risk Product"). By
* including Cypress's product in a High Risk Product, the manufacturer
* of such system or application assumes all risk of such use and in doing
* so agrees to indemnify Cypress against all liability.
*******************************************************************************/
/*******************************************************************************
* Header Files
*******************************************************************************/
#include "cybsp.h"
#include "retarget_io_init.h"
#include <stdbool.h>
/*******************************************************************************
* Macros
******************************************************************************/
#define CM55_BOOT_WAIT_TIME_USEC (10U)
#define ADC_READ_INTERVAL_MS (100U)
#define SAR_ADC_INDEX (0U)
#define SAR_ADC_SEQUENCER (0U)
#define SAR_ADC_CHANNEL (0U)
#define SAR_ADC_VREF_MV (1800U)
/*******************************************************************************
* Global Variables
*******************************************************************************/
static bool adc_initialized = false;
/*******************************************************************************
* Function Prototypes
*******************************************************************************/
static cy_rslt_t init_adc(void);
static int16_t read_potentiometer_mv(void);
static uint8_t get_pot_percentage(int16_t voltage_mv);
/*******************************************************************************
* 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;
}
/*******************************************************************************
* Function Name: main
*******************************************************************************
* Summary:
* Main function - entry point for the ADC potentiometer demo.
*
* This demo reads the potentiometer connected to P15_1 and displays:
* - Raw voltage in millivolts (0-1800 mV)
* - Percentage value (0-100%)
*
* The LED toggles with each ADC reading to indicate activity.
*
* Parameters:
* none
*
* Return:
* int
*
******************************************************************************/
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);
}
}
/* [] END OF FILE */