Skip to content
Permalink
31d9be986b
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
184 lines (163 sloc) 6.88 KB
/******************************************************************************
* File Name: main.c
*
* Description: This is the source code for the XMC MCU: VADC SCAN Example
* for ModusToolbox. This example shows how to configure ADC for
* Continuous Scan mode.ADC result is used to change status of LED on kit.
*
* Related Document: See README.md
*
******************************************************************************
*
* Copyright (c) 2015-2022, Infineon Technologies AG
* All rights reserved.
*
* Boost Software License - Version 1.0 - August 17th, 2003
*
* Permission is hereby granted, free of charge, to any person or organization
* obtaining a copy of the software and accompanying documentation covered by
* this license (the "Software") to use, reproduce, display, distribute,
* execute, and transmit the Software, and to prepare derivative works of the
* Software, and to permit third-parties to whom the Software is furnished to
* do so, all subject to the following:
*
* The copyright notices in the Software and this entire statement, including
* the above license grant, this restriction and the following disclaimer,
* must be included in all copies of the Software, in whole or in part, and
* all derivative works of the Software, unless such copies or derivative
* works are solely in the form of machine-executable object code generated by
* a source language processor.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
* SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
* FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*****************************************************************************/
#include "cybsp.h"
#include "cy_utils.h"
#include "cy_retarget_io.h"
/*******************************************************************************
* Macros
*******************************************************************************/
/* Define macros for XMC12x, XMC13x and XMC14x Boot kits */
#if (UC_SERIES == XMC12) || (UC_SERIES == XMC13) || (UC_SERIES == XMC14)
#define RES_REG_NUMBER (10)
#define CHANNEL_NUMBER (7U)
#define ADC_CONVERSION_EVENT_HANDLER VADC_SR0_INTERRUPT_HANDLER
#define INTERRUPT_PRIORITY_NODE_ID VADC_SR0_IRQN
#endif
/* Define macros for XMC48x, XMC47x and XMC45x Relax kits and for XMC4200 and XMC4400 PLT2GO kits */
#if (UC_FAMILY == XMC4)
#if (UC_SERIES == XMC48) || (UC_SERIES == XMC47)
#define CHANNEL_NUMBER (5U)
#else
#define CHANNEL_NUMBER (0U)
#endif
#define RES_REG_NUMBER (4)
#define ADC_CONVERSION_EVENT_HANDLER VADC_SR2_INTERRUPT_HANDLER
#define INTERRUPT_PRIORITY_NODE_ID VADC_SR2_IRQN
#endif
#define ADC_MEASUREMENT_ICLASS_NUM (0U)
/* Define macro to enable/disable printing of debug messages */
#define ENABLE_XMC_DEBUG_PRINT (0)
#if ENABLE_XMC_DEBUG_PRINT
static bool LED_TOGGLE = false;
#endif
/*******************************************************************************
* Global Variables
*******************************************************************************/
static volatile uint32_t g_result_adc_measurement;
/*******************************************************************************
* Function Name: ADC_CONVERSION_EVENT_HANDLER
********************************************************************************
* Summary:
* This is the interrupt handler function for the ADC after conversion.
*
* Parameters:
* void
*
* Return:
* void
*
*******************************************************************************/
void ADC_CONVERSION_EVENT_HANDLER(void)
{
/*Read out conversion results*/
g_result_adc_measurement=XMC_VADC_GROUP_GetResult(VADC_GROUP_HW, RES_REG_NUMBER);
#if !(ENABLE_XMC_DEBUG_PRINT)
/* Prints the result in UART Terminal */
printf("ADC Result value is %lx \r\n", g_result_adc_measurement);
#endif
if(g_result_adc_measurement >= 2000)
{
/*Compare the result counts */
#if (UC_SERIES == XMC12) || (UC_SERIES == XMC13) || (UC_SERIES == XMC14)
XMC_GPIO_SetOutputLow(CYBSP_USER_LED_PORT, CYBSP_USER_LED_PIN);
#endif
#if (UC_SERIES == XMC45) || (UC_SERIES == XMC47) || (UC_SERIES == XMC48) \
|| (UC_SERIES == XMC42) || (UC_SERIES == XMC43) || (UC_SERIES == XMC44)
XMC_GPIO_SetOutputHigh(CYBSP_USER_LED_PORT, CYBSP_USER_LED_PIN);
#endif
}
else
{
#if ENABLE_XMC_DEBUG_PRINT
if(!LED_TOGGLE)
{
printf("LED Toggled\r\n");
LED_TOGGLE = true;
}
#endif
#if (UC_SERIES == XMC12) || (UC_SERIES == XMC13) || (UC_SERIES == XMC14)
XMC_GPIO_SetOutputHigh(CYBSP_USER_LED_PORT, CYBSP_USER_LED_PIN);
#endif
#if (UC_SERIES == XMC45) || (UC_SERIES == XMC47) || (UC_SERIES == XMC48) \
|| (UC_SERIES == XMC42) || (UC_SERIES == XMC43) || (UC_SERIES == XMC44)
XMC_GPIO_SetOutputLow(CYBSP_USER_LED_PORT, CYBSP_USER_LED_PIN);
#endif
}
}
/*******************************************************************************
* Function Name: main
********************************************************************************
* Summary:
* This is the main function.
* It sets up a VADC for continuous scan using background scan source.
* ADC result is available after conversion is completed inside event handler function.
* ADC result register value is compared inside event handler function
* On board LED glows high when ADC counts are more than 2000.
*
* Parameters:
* none
*
* Return:
* int
*
*******************************************************************************/
int main(void)
{
cy_rslt_t result;
/*Initialize the device and board peripherals*/
result = cybsp_init();
if (result != CY_RSLT_SUCCESS)
{
CY_ASSERT(0);
}
/* Initialize printf retarget */
cy_retarget_io_init(CYBSP_DEBUG_UART_HW);
#if ENABLE_XMC_DEBUG_PRINT
printf("Initialization done\r\n");
#endif
/*Add all channels into the Background Request Source Channel Select Register*/
XMC_VADC_GLOBAL_BackgroundAddChannelToSequence(VADC, VADC_GROUP_NUM, CHANNEL_NUMBER);
/*Enable Background Continuous Scan Request source IRQ*/
NVIC_EnableIRQ(INTERRUPT_PRIORITY_NODE_ID);
/*Generate a load event to start background request source continuous conversion*/
XMC_VADC_GLOBAL_BackgroundTriggerConversion(VADC);
while(1);
}
/* [] END OF FILE */