Hi!
I wrote a program to use VADC with 5 input channels. I found a lot of useful information on XMC forum which helped me to start.
Here is my first program:
#include <xmc_vadc.h>
#include <xmc_scu.h>
#include <xmc_gpio.h>
XMC_GPIO_CONFIG_t gpio_input =
{
.mode = XMC_GPIO_MODE_INPUT_TRISTATE
};
void initPins()
{
// Initialize VADC inputs.
XMC_GPIO_Init(XMC_GPIO_PORT2,6,&gpio_input); // (VADC) Group 0, channel 0.
XMC_GPIO_Init(XMC_GPIO_PORT2,10,&gpio_input); // (VADC) Group 0, channel 3.
XMC_GPIO_Init(XMC_GPIO_PORT2,2,&gpio_input); // (VADC) Group 0, channel 7.
XMC_GPIO_Init(XMC_GPIO_PORT2,7,&gpio_input); // (VADC) Group 1, channel 1.
XMC_GPIO_Init(XMC_GPIO_PORT2,5,&gpio_input); // (VADC) Group 1, channel 7.
}
int main(void)
{
initPins();
// Unlock protect bits.
SCU_GENERAL->PASSWD = 0xC0; // Block off.
SCU_CLK->CGATCLR0 = 0x1; // Disable gating.
while((SCU_CLK->CLKCR) & 0x40000000); // wait for VDDC to stabilize.
SHS0->SHSCFG = (SHS_SHSCFG_SCWC_Msk); // Enable write access to ANOFF and AREF.
SHS0->SHSCFG &= ~(SHS_SHSCFG_ANOFF_Msk); // Toggle on converter.
*((int*)0x40010500) = 0x01; // workaround to enable converter: ADC_AI.003. (only stepAA).
VADC->CLC = 0x00000000; // Enable the module clock.
VADC->GLOBCFG = 0x80000000; // Enable Start-Up calibration.
while ((SHS0->SHSCFG & SHS_SHSCFG_ANRDY_Msk) == 0); // Wait until converter is ready.
VADC->BRSSEL[0] = 0x89; // Input channel selection of group 0. Enable inputs: 0, 3, 7.
VADC->BRSSEL[1] = 0x82; // Input channel selection of group 1. Enable inputs: 1, 7.
VADC->GLOBICLASS[0] |= (0x01UL << VADC_GLOBICLASS_CMS_Pos); // Set 10 bit conversion on group 0.
VADC->GLOBICLASS[1] |= (0x01UL << VADC_GLOBICLASS_CMS_Pos); // Set 10 bit conversion on group 1.
// Start new conversion.
VADC->BRSMR = 0x211; // Requests are issued, autoscan enable, generate load event.
int group, channel;
group = 0; channel = 0;
while(1)
{
while ((VADC->GLOBRES & 0x80000000) == 0 ; // Wait until new result write int the RESULT bits (Valid flag).
group = (VADC->GLOBRES & 0xF0000) >> 16; // See which group happened the conversion.
channel = (VADC->GLOBRES & 0x1F00000) >> 20; // See which channel happened the conversion.
result = (VADC->GLOBRES & 0xFFFF) >> 2; // Store result in a variable.
}
}
I have a prolem with GLOBRES. Every time, if I read any bits of the GLOBRES, a new conversion happens. I can see it on the BRSPND registers in the debugger. If I read the VF, GNR, CHNR, or RESULT, a new conversion happens.
I read the XMC1100 datasheet a lot to realize what is the problem, but I could not. So I wrote another code.
My second try to read everything properly.
while ((VADC->GLOBRESD & 0x80000000) == 0 ; // Wait until new result write int the RESULT bits.
group = (VADC->GLOBRESD & 0xF0000) >> 16; // See which group happened the conversion.
channel = (VADC->GLOBRESD & 0x1F00000) >> 20; // See which channel happened the conversion.
result = (VADC->GLOBRES & 0xFFFF) >> 2; // Store result in a variable.
This way is working. I can read GLOBRESD without starting new conversion. To start ONE new conversion I read only the result from GLOBRES.
Is it a bug or I overlooked something?
Rjani
I wrote a program to use VADC with 5 input channels. I found a lot of useful information on XMC forum which helped me to start.
Here is my first program:
Quote:
#include <xmc_vadc.h>
#include <xmc_scu.h>
#include <xmc_gpio.h>
XMC_GPIO_CONFIG_t gpio_input =
{
.mode = XMC_GPIO_MODE_INPUT_TRISTATE
};
void initPins()
{
// Initialize VADC inputs.
XMC_GPIO_Init(XMC_GPIO_PORT2,6,&gpio_input); // (VADC) Group 0, channel 0.
XMC_GPIO_Init(XMC_GPIO_PORT2,10,&gpio_input); // (VADC) Group 0, channel 3.
XMC_GPIO_Init(XMC_GPIO_PORT2,2,&gpio_input); // (VADC) Group 0, channel 7.
XMC_GPIO_Init(XMC_GPIO_PORT2,7,&gpio_input); // (VADC) Group 1, channel 1.
XMC_GPIO_Init(XMC_GPIO_PORT2,5,&gpio_input); // (VADC) Group 1, channel 7.
}
int main(void)
{
initPins();
// Unlock protect bits.
SCU_GENERAL->PASSWD = 0xC0; // Block off.
SCU_CLK->CGATCLR0 = 0x1; // Disable gating.
while((SCU_CLK->CLKCR) & 0x40000000); // wait for VDDC to stabilize.
SHS0->SHSCFG = (SHS_SHSCFG_SCWC_Msk); // Enable write access to ANOFF and AREF.
SHS0->SHSCFG &= ~(SHS_SHSCFG_ANOFF_Msk); // Toggle on converter.
*((int*)0x40010500) = 0x01; // workaround to enable converter: ADC_AI.003. (only stepAA).
VADC->CLC = 0x00000000; // Enable the module clock.
VADC->GLOBCFG = 0x80000000; // Enable Start-Up calibration.
while ((SHS0->SHSCFG & SHS_SHSCFG_ANRDY_Msk) == 0); // Wait until converter is ready.
VADC->BRSSEL[0] = 0x89; // Input channel selection of group 0. Enable inputs: 0, 3, 7.
VADC->BRSSEL[1] = 0x82; // Input channel selection of group 1. Enable inputs: 1, 7.
VADC->GLOBICLASS[0] |= (0x01UL << VADC_GLOBICLASS_CMS_Pos); // Set 10 bit conversion on group 0.
VADC->GLOBICLASS[1] |= (0x01UL << VADC_GLOBICLASS_CMS_Pos); // Set 10 bit conversion on group 1.
// Start new conversion.
VADC->BRSMR = 0x211; // Requests are issued, autoscan enable, generate load event.
int group, channel;
group = 0; channel = 0;
while(1)
{
while ((VADC->GLOBRES & 0x80000000) == 0 ; // Wait until new result write int the RESULT bits (Valid flag).
group = (VADC->GLOBRES & 0xF0000) >> 16; // See which group happened the conversion.
channel = (VADC->GLOBRES & 0x1F00000) >> 20; // See which channel happened the conversion.
result = (VADC->GLOBRES & 0xFFFF) >> 2; // Store result in a variable.
}
}
I read the XMC1100 datasheet a lot to realize what is the problem, but I could not. So I wrote another code.
My second try to read everything properly.
Quote:
while ((VADC->GLOBRESD & 0x80000000) == 0 ; // Wait until new result write int the RESULT bits.
group = (VADC->GLOBRESD & 0xF0000) >> 16; // See which group happened the conversion.
channel = (VADC->GLOBRESD & 0x1F00000) >> 20; // See which channel happened the conversion.
result = (VADC->GLOBRES & 0xFFFF) >> 2; // Store result in a variable.
Is it a bug or I overlooked something?
Rjani