Not applicable
Nov 23, 2014
08:58 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Nov 23, 2014
08:58 PM
Hi,
I am working on workaround solution of ADC for xmc1100,in interrupt array stores 100 values ADC_Result=VADC->GLOBRES&0xFFF; how to sum these values from array and make average of it
result_sum = 0;
ADC_Result=VADC->GLOBRES&0xFFF;
result_sum += ADC_Result;
if(i==100)
{
result_sum = result_sum/100;
i=0;
}
else{i++;}
i have written code like this,is this right way to write kindly guide me if iam wrong??
I am working on workaround solution of ADC for xmc1100,in interrupt array stores 100 values ADC_Result=VADC->GLOBRES&0xFFF; how to sum these values from array and make average of it
result_sum = 0;
ADC_Result=VADC->GLOBRES&0xFFF;
result_sum += ADC_Result;
if(i==100)
{
result_sum = result_sum/100;
i=0;
}
else{i++;}
i have written code like this,is this right way to write kindly guide me if iam wrong??
22 Replies
Not applicable
Nov 23, 2014
10:04 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Nov 23, 2014
10:04 PM
Hi prathibha,
Yes, your code could is correct.
Yes, your code could is correct.
Attachments are accessible only for community members.
Not applicable
Nov 23, 2014
11:10 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Nov 23, 2014
11:10 PM
thank you Jackson,
As per my project based on value of ADC_Result i need to on and off relay.where i supposed to perform on/off relays.to do this where i supposed to do in isr routine or in while(1) loop.As per architecture of xmc1100 which is the right way to do please guide me.here i am attaching the code what i have written, i have written in while(1) loop about on/off relays .Kindly guide me if this part is complete i will finish my project kindly guide me.
As per my project based on value of ADC_Result i need to on and off relay.where i supposed to perform on/off relays.to do this where i supposed to do in isr routine or in while(1) loop.As per architecture of xmc1100 which is the right way to do please guide me.here i am attaching the code what i have written, i have written in while(1) loop about on/off relays .Kindly guide me if this part is complete i will finish my project kindly guide me.
Not applicable
Nov 23, 2014
11:34 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Nov 23, 2014
11:34 PM
Hi prathibha,
Did you upload the wrong project?
I don't see any code in the while(1) loop.
By the way, the project you have uploaded is used for XMC1300 device.
The ADC for XMC1100 has different ways to initialize.
If you are working on XMC1100, please download the ADC example project for XMC1100.
As a general guide, you can always toggle the pins in ISR as it doesn't took much time.
Usually, only long function are not encourage to do in ISR.
Did you upload the wrong project?
I don't see any code in the while(1) loop.
By the way, the project you have uploaded is used for XMC1300 device.
The ADC for XMC1100 has different ways to initialize.
If you are working on XMC1100, please download the ADC example project for XMC1100.
As a general guide, you can always toggle the pins in ISR as it doesn't took much time.
Usually, only long function are not encourage to do in ISR.
Not applicable
Nov 23, 2014
11:55 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Nov 23, 2014
11:55 PM
Hi jackson this is the code
#include //SFR declarations of the selected device
//#include
#include "GPIO.h"
/*******************************************************************************
** Macro Definitions **
*******************************************************************************/
#define CALIB00 0x480340E0
#define CALIB01 0x480340E4
/*******************************************************************************
** Private Variable Definitions **
*******************************************************************************/
uint32_t * SHS0_CALOC0 = (uint32_t *) CALIB00;
uint32_t * SHS0_CALOC1 = (uint32_t *) CALIB01;
/*******************************************************************************
** Function Declarations **
*******************************************************************************/
void delay (uint32_t delay);
void VADC_Init();
void ADC_SU_Calibration (void);
void ADC_PG_Calibration (void);
/*******************************************************************************
** Global Variable Definitions **
*******************************************************************************/
uint32_t ADC_Result[101]={0};
uint32_t i=0, j=0;
uint32_t result_sum=0;
uint32_t flag_condition1=0;
uint32_t flag_condition2=0;
int main(void)
{
SCU_GENERAL->PASSWD = 0x000000C0UL; // disable bit protection
SCU_CLK->CLKCR = 0x3ff10200UL; // 32 MHz MCLK, 64 MHz PCLK
while((SCU_CLK->CLKCR & SCU_CLK_CLKCR_VDDC2LOW_Msk));
SCU_GENERAL->PASSWD = 0x000000C3UL; // enable bit protection
P0_5_set_mode(OUTPUT_PP_GP); // pin set up p0.5
P0_6_set_mode(OUTPUT_PP_GP); //pin setup p0.6
/* Relay1 on for power on condition*/
P0_5_reset();
P0_6_set();
// Initialize ADC
VADC_Init();
// Generate load event to start conversion
VADC->BRSMR |= (1 << VADC_BRSMR_LDEV_Pos & VADC_BRSMR_LDEV_Msk);
while(1)
{
/*threshold voltage 1.26v*/
if(result_sum<=1032)
{
if (flag_condition2==1)
{
delay(1000);
flag_condition1=1;
flag_condition2=0;
}
//flag_condition1=1;
/*Relay1 on <1032 count*/
P0_5_reset();
P0_6_set();
}
else if (result_sum >1032)
{
if (flag_condition1==1)
{
delay(1000);
flag_condition1=0;
flag_condition2=1;
}
//flag_condition2=1;
/*Relay2 on >1032 count*/
P0_6_reset();
P0_5_set();
}
} //end of while
} // end of main
void VADC0_C0_1_IRQHandler(void)
{
//result_sum = 0;
ADC_Result=VADC->GLOBRES&0xFFF;
result_sum += ADC_Result;
if(i==100)
{
result_sum = result_sum/100;
i=0;
}
else{i++;}
// Clear offset calibration values
*SHS0_CALOC0 = 0x8000;
*SHS0_CALOC1 = 0x8000;
}
void VADC_Init()
{
// Disable clock gating to VADC
SCU_GENERAL->PASSWD = 0x000000C0U; // Disable bit protection
SET_BIT(SCU_CLK->CGATCLR0, SCU_CLK_CGATCLR0_VADC_Pos); // Disables clock gating for VADC kernel
while (SCU_CLK->CLKCR & (SCU_CLK_CLKCR_VDDC2LOW_Msk)); // Wait for the VDDC to stable
SCU_GENERAL->PASSWD = 0x000000C3UL; // Enable bit protection
//GLOBAL VADC INITIALIZATION
VADC->CLC = (0 << VADC_CLC_DISR_Pos & VADC_CLC_DISR_Msk) | // On request: enable the module clock
(0 << VADC_CLC_DISS_Pos & VADC_CLC_DISS_Msk) | // Module clock is enabled
(0 << VADC_CLC_EDIS_Pos & VADC_CLC_EDIS_Msk); // Sleep mode request is enabled and functional
while(VADC->CLC!=0); // wait for module clock enabled
// Converter ON
SHS0->SHSCFG = (0 << SHS_SHSCFG_ANOFF_Pos & SHS_SHSCFG_ANOFF_Msk)|SHS_SHSCFG_SCWC_Msk;
*(uint32_t *)0x40010500 = 0X0001; //Errata ADC_AI.003
// Wait until analog converter is on (SHSCFG.ANRDY = 1)
while (!(SHS0->SHSCFG & SHS_SHSCFG_ANRDY_Msk)) ;
delay(12); // Added ~15usec wait time for ADC power to stable
// Trigger Start Up calibration
ADC_SU_Calibration();
// Trigger gain calibration
ADC_PG_Calibration(); //do not modify DPCAL and SUCAL bitfields form GLOBCFG from here onwards to avoid wrong calibration configurations
// Background source configuration (include 1 channel -ch 7 group 0 w/o auto scan (continuous conversions).
VADC->BRSSEL[0] = (1 << VADC_BRSSEL_CHSELG7_Pos & VADC_BRSSEL_CHSELG7_Msk);
VADC->BRSMR = (1 << VADC_BRSMR_ENGT_Pos & VADC_BRSMR_ENGT_Msk)| //Enable gating always
(1 << VADC_BRSMR_SCAN_Pos & VADC_BRSMR_SCAN_Msk); //Enable autoscan
// Enable interrupt for global result register
VADC->GLOBRCR = (0x1 << VADC_GLOBRCR_SRGEN_Pos & VADC_GLOBRCR_SRGEN_Msk);
// Route event trigger to service request line (interrupt node)
VADC->GLOBEVNP = (0x1 << VADC_GLOBEVNP_REV0NP_Pos & VADC_GLOBEVNP_REV0NP_Msk);
// Enable nvic node for SR1
NVIC_EnableIRQ(VADC0_C0_1_IRQn);
}
// Startup calibration
void ADC_SU_Calibration (void)
{
uint16_t wait;
// Enable calibration
*SHS0_CALOC0 = 0x00;
*SHS0_CALOC1 = 0x00;
// Enable SU calibration ADC
VADC->GLOBCFG |= (1 << VADC_GLOBCFG_SUCAL_Pos & VADC_GLOBCFG_SUCAL_Msk)|
(1 << VADC_GLOBCFG_DPCAL0_Pos & VADC_GLOBCFG_DPCAL0_Msk);
// Wait for 1920cycles or 60us for the startup calibration to complete
wait = 20;
while(wait > 0)
{
wait--;
*SHS0_CALOC0 = 0x8000; // Clear offset calibration values
*SHS0_CALOC1 = 0x8000;
}
} // End of ADC_SU_Calibration
// Gain calibration routine
void ADC_PG_Calibration (void)
{
uint16_t i, j, adc_result_aux;
*(uint32_t *)0x480340BC = 0X3F100400; //Errata ADC_AI.004
VADC->BRSSEL[0] = (1 << VADC_BRSSEL_CHSELG0_Pos & VADC_BRSSEL_CHSELG0_Msk);//set G0_Ch0 for dummy conversions
// SUCAL=0, DPCAL0=0, DPCAL1=0 (DPCAL1 has to be kept to 0)
VADC->GLOBCFG |= 0x00;
*SHS0_CALOC0 = 0x8000; // Clear offset calibration values
*SHS0_CALOC1 = 0x8000;
// Trigger dummy conversions (9 x 2000 times)
for (i=0; i < 2000; i++)
{
for (j=0; j < 9; j++)
{
VADC->BRSMR |= (1 << VADC_BRSMR_ENGT_Pos & VADC_BRSMR_ENGT_Msk)| //Enable gating always
(0 << VADC_BRSMR_SCAN_Pos & VADC_BRSMR_SCAN_Msk)| //No autoscan-->only 1 conversion
(1 << VADC_BRSMR_LDEV_Pos & VADC_BRSMR_LDEV_Msk); //Generate a load event
// wait for valid result
while (!(VADC->GLOBRES & VADC_GLOBRES_VF_Msk));
//read result
adc_result_aux = (VADC->GLOBRES & VADC_GLOBRES_RESULT_Msk);
// Clear offset calibration values
*SHS0_CALOC0 = 0x8000;
*SHS0_CALOC1 = 0x8000;
}
}
// Redundant code to avoid warning
if(adc_result_aux == 0);
// Wait until last gain cal step is finished
while ( (SHS0->SHSCFG & SHS_SHSCFG_STATE_Msk) != 0 )
{
// Clear offset calibration values
*SHS0_CALOC0 = 0x8000;
*SHS0_CALOC1 = 0x8000;
}
VADC->GLOBCFG = 0x00; // Ensure Post-Calibration enable (SUCAL=0, DPCAL0=0, DPCAL1=0)
// Reset used register to reset value
VADC->GLOBRES = 0x00;
VADC->BRSMR = 0x00;
VADC->BRSSEL[0] = 0x00;
} // End of ADC_PG_Calibration
void delay (uint32_t delay)
{
while(delay > 0)
{
delay--;
}
} // End of delay
#include
//#include
#include "GPIO.h"
/*******************************************************************************
** Macro Definitions **
*******************************************************************************/
#define CALIB00 0x480340E0
#define CALIB01 0x480340E4
/*******************************************************************************
** Private Variable Definitions **
*******************************************************************************/
uint32_t * SHS0_CALOC0 = (uint32_t *) CALIB00;
uint32_t * SHS0_CALOC1 = (uint32_t *) CALIB01;
/*******************************************************************************
** Function Declarations **
*******************************************************************************/
void delay (uint32_t delay);
void VADC_Init();
void ADC_SU_Calibration (void);
void ADC_PG_Calibration (void);
/*******************************************************************************
** Global Variable Definitions **
*******************************************************************************/
uint32_t ADC_Result[101]={0};
uint32_t i=0, j=0;
uint32_t result_sum=0;
uint32_t flag_condition1=0;
uint32_t flag_condition2=0;
int main(void)
{
SCU_GENERAL->PASSWD = 0x000000C0UL; // disable bit protection
SCU_CLK->CLKCR = 0x3ff10200UL; // 32 MHz MCLK, 64 MHz PCLK
while((SCU_CLK->CLKCR & SCU_CLK_CLKCR_VDDC2LOW_Msk));
SCU_GENERAL->PASSWD = 0x000000C3UL; // enable bit protection
P0_5_set_mode(OUTPUT_PP_GP); // pin set up p0.5
P0_6_set_mode(OUTPUT_PP_GP); //pin setup p0.6
/* Relay1 on for power on condition*/
P0_5_reset();
P0_6_set();
// Initialize ADC
VADC_Init();
// Generate load event to start conversion
VADC->BRSMR |= (1 << VADC_BRSMR_LDEV_Pos & VADC_BRSMR_LDEV_Msk);
while(1)
{
/*threshold voltage 1.26v*/
if(result_sum<=1032)
{
if (flag_condition2==1)
{
delay(1000);
flag_condition1=1;
flag_condition2=0;
}
//flag_condition1=1;
/*Relay1 on <1032 count*/
P0_5_reset();
P0_6_set();
}
else if (result_sum >1032)
{
if (flag_condition1==1)
{
delay(1000);
flag_condition1=0;
flag_condition2=1;
}
//flag_condition2=1;
/*Relay2 on >1032 count*/
P0_6_reset();
P0_5_set();
}
} //end of while
} // end of main
void VADC0_C0_1_IRQHandler(void)
{
//result_sum = 0;
ADC_Result=VADC->GLOBRES&0xFFF;
result_sum += ADC_Result;
if(i==100)
{
result_sum = result_sum/100;
i=0;
}
else{i++;}
// Clear offset calibration values
*SHS0_CALOC0 = 0x8000;
*SHS0_CALOC1 = 0x8000;
}
void VADC_Init()
{
// Disable clock gating to VADC
SCU_GENERAL->PASSWD = 0x000000C0U; // Disable bit protection
SET_BIT(SCU_CLK->CGATCLR0, SCU_CLK_CGATCLR0_VADC_Pos); // Disables clock gating for VADC kernel
while (SCU_CLK->CLKCR & (SCU_CLK_CLKCR_VDDC2LOW_Msk)); // Wait for the VDDC to stable
SCU_GENERAL->PASSWD = 0x000000C3UL; // Enable bit protection
//GLOBAL VADC INITIALIZATION
VADC->CLC = (0 << VADC_CLC_DISR_Pos & VADC_CLC_DISR_Msk) | // On request: enable the module clock
(0 << VADC_CLC_DISS_Pos & VADC_CLC_DISS_Msk) | // Module clock is enabled
(0 << VADC_CLC_EDIS_Pos & VADC_CLC_EDIS_Msk); // Sleep mode request is enabled and functional
while(VADC->CLC!=0); // wait for module clock enabled
// Converter ON
SHS0->SHSCFG = (0 << SHS_SHSCFG_ANOFF_Pos & SHS_SHSCFG_ANOFF_Msk)|SHS_SHSCFG_SCWC_Msk;
*(uint32_t *)0x40010500 = 0X0001; //Errata ADC_AI.003
// Wait until analog converter is on (SHSCFG.ANRDY = 1)
while (!(SHS0->SHSCFG & SHS_SHSCFG_ANRDY_Msk)) ;
delay(12); // Added ~15usec wait time for ADC power to stable
// Trigger Start Up calibration
ADC_SU_Calibration();
// Trigger gain calibration
ADC_PG_Calibration(); //do not modify DPCAL and SUCAL bitfields form GLOBCFG from here onwards to avoid wrong calibration configurations
// Background source configuration (include 1 channel -ch 7 group 0 w/o auto scan (continuous conversions).
VADC->BRSSEL[0] = (1 << VADC_BRSSEL_CHSELG7_Pos & VADC_BRSSEL_CHSELG7_Msk);
VADC->BRSMR = (1 << VADC_BRSMR_ENGT_Pos & VADC_BRSMR_ENGT_Msk)| //Enable gating always
(1 << VADC_BRSMR_SCAN_Pos & VADC_BRSMR_SCAN_Msk); //Enable autoscan
// Enable interrupt for global result register
VADC->GLOBRCR = (0x1 << VADC_GLOBRCR_SRGEN_Pos & VADC_GLOBRCR_SRGEN_Msk);
// Route event trigger to service request line (interrupt node)
VADC->GLOBEVNP = (0x1 << VADC_GLOBEVNP_REV0NP_Pos & VADC_GLOBEVNP_REV0NP_Msk);
// Enable nvic node for SR1
NVIC_EnableIRQ(VADC0_C0_1_IRQn);
}
// Startup calibration
void ADC_SU_Calibration (void)
{
uint16_t wait;
// Enable calibration
*SHS0_CALOC0 = 0x00;
*SHS0_CALOC1 = 0x00;
// Enable SU calibration ADC
VADC->GLOBCFG |= (1 << VADC_GLOBCFG_SUCAL_Pos & VADC_GLOBCFG_SUCAL_Msk)|
(1 << VADC_GLOBCFG_DPCAL0_Pos & VADC_GLOBCFG_DPCAL0_Msk);
// Wait for 1920cycles or 60us for the startup calibration to complete
wait = 20;
while(wait > 0)
{
wait--;
*SHS0_CALOC0 = 0x8000; // Clear offset calibration values
*SHS0_CALOC1 = 0x8000;
}
} // End of ADC_SU_Calibration
// Gain calibration routine
void ADC_PG_Calibration (void)
{
uint16_t i, j, adc_result_aux;
*(uint32_t *)0x480340BC = 0X3F100400; //Errata ADC_AI.004
VADC->BRSSEL[0] = (1 << VADC_BRSSEL_CHSELG0_Pos & VADC_BRSSEL_CHSELG0_Msk);//set G0_Ch0 for dummy conversions
// SUCAL=0, DPCAL0=0, DPCAL1=0 (DPCAL1 has to be kept to 0)
VADC->GLOBCFG |= 0x00;
*SHS0_CALOC0 = 0x8000; // Clear offset calibration values
*SHS0_CALOC1 = 0x8000;
// Trigger dummy conversions (9 x 2000 times)
for (i=0; i < 2000; i++)
{
for (j=0; j < 9; j++)
{
VADC->BRSMR |= (1 << VADC_BRSMR_ENGT_Pos & VADC_BRSMR_ENGT_Msk)| //Enable gating always
(0 << VADC_BRSMR_SCAN_Pos & VADC_BRSMR_SCAN_Msk)| //No autoscan-->only 1 conversion
(1 << VADC_BRSMR_LDEV_Pos & VADC_BRSMR_LDEV_Msk); //Generate a load event
// wait for valid result
while (!(VADC->GLOBRES & VADC_GLOBRES_VF_Msk));
//read result
adc_result_aux = (VADC->GLOBRES & VADC_GLOBRES_RESULT_Msk);
// Clear offset calibration values
*SHS0_CALOC0 = 0x8000;
*SHS0_CALOC1 = 0x8000;
}
}
// Redundant code to avoid warning
if(adc_result_aux == 0);
// Wait until last gain cal step is finished
while ( (SHS0->SHSCFG & SHS_SHSCFG_STATE_Msk) != 0 )
{
// Clear offset calibration values
*SHS0_CALOC0 = 0x8000;
*SHS0_CALOC1 = 0x8000;
}
VADC->GLOBCFG = 0x00; // Ensure Post-Calibration enable (SUCAL=0, DPCAL0=0, DPCAL1=0)
// Reset used register to reset value
VADC->GLOBRES = 0x00;
VADC->BRSMR = 0x00;
VADC->BRSSEL[0] = 0x00;
} // End of ADC_PG_Calibration
void delay (uint32_t delay)
{
while(delay > 0)
{
delay--;
}
} // End of delay
Not applicable
Nov 24, 2014
12:05 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Nov 24, 2014
12:05 AM
Hi prathibha,
I think you need to put some condition before you check the result_sum in your while loop.
Since you only want the average result, you should only check the result_sum in the while loop after you got the average result.
So, perhaps you need to set some condition/flag to indicate whether you should proceed the check result function.
I think you need to put some condition before you check the result_sum in your while loop.
Since you only want the average result, you should only check the result_sum in the while loop after you got the average result.
So, perhaps you need to set some condition/flag to indicate whether you should proceed the check result function.
Not applicable
Nov 24, 2014
12:43 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Nov 24, 2014
12:43 AM
Thankyou jackson,
i have put condition in while(1) please check . initially Init_Conv_Comp_flag = 0; i put condition flag in ISR Inside ISR after result_sum Init_Conv_Comp_flag = 1 becomes 1,in while(1) check condition Init_Conv_Comp_flag = 1; then i on/off relays.kindly guide me
/*******************************************************************************
** INCLUDE FILES **
******************************************************************************/
#include //SFR declarations of the selected device
//#include
#include "GPIO.h"
/*******************************************************************************
** Macro Definitions **
*******************************************************************************/
#define CALIB00 0x480340E0
#define CALIB01 0x480340E4
/*******************************************************************************
** Private Variable Definitions **
*******************************************************************************/
uint32_t * SHS0_CALOC0 = (uint32_t *) CALIB00;
uint32_t * SHS0_CALOC1 = (uint32_t *) CALIB01;
/*******************************************************************************
** Function Declarations **
*******************************************************************************/
void delay (uint32_t delay);
void VADC_Init();
void ADC_SU_Calibration (void);
void ADC_PG_Calibration (void);
/*******************************************************************************
** Global Variable Definitions **
*******************************************************************************/
uint32_t ADC_Result[101]={0};
uint32_t i=0, j=0;
uint32_t result_sum=0;
uint32_t flag_condition1=0;
uint32_t flag_condition2=0;
uint32_t Init_Conv_Comp_flag = 0;
int main(void)
{
SCU_GENERAL->PASSWD = 0x000000C0UL; // disable bit protection
SCU_CLK->CLKCR = 0x3ff10200UL; // 32 MHz MCLK, 64 MHz PCLK
while((SCU_CLK->CLKCR & SCU_CLK_CLKCR_VDDC2LOW_Msk));
SCU_GENERAL->PASSWD = 0x000000C3UL; // enable bit protection
P0_5_set_mode(OUTPUT_PP_GP); // pin set up p0.5
P0_6_set_mode(OUTPUT_PP_GP); //pin setup p0.6
/* Relay1 on for power on condition*/
P0_5_reset();
P0_6_set();
// Initialize ADC
VADC_Init();
// Generate load event to start conversion
VADC->BRSMR |= (1 << VADC_BRSMR_LDEV_Pos & VADC_BRSMR_LDEV_Msk);
while(1)
{
if(Init_Conv_Comp_flag == 1) //condition check
{
/*threshold voltage 1.26v*/
if(result_sum<=1032)
{
if (flag_condition2==1)
{
delay(1000);
flag_condition1=1;
flag_condition2=0;
}
//flag_condition1=1;
/*Relay1 on <1032 count*/
P0_5_reset();
P0_6_set();
}
else if (result_sum >1032)
{
if (flag_condition1==1)
{
delay(1000);
flag_condition1=0;
flag_condition2=1;
}
//flag_condition2=1;
/*Relay2 on >1032 count*/
P0_6_reset();
P0_5_set();
} //end of else part
} //end of con_flag
} //end of while
} // end of main
void VADC0_C0_1_IRQHandler(void)
{
//result_sum = 0;
ADC_Result=VADC->GLOBRES&0xFFF;
result_sum += ADC_Result;
if(i==100)
{
result_sum = result_sum/100;
i=0;
}
else{i++;}
//Init_Conv_Comp_flag = 1;
// Clear offset calibration values
*SHS0_CALOC0 = 0x8000;
*SHS0_CALOC1 = 0x8000;
Init_Conv_Comp_flag = 1;
}
void VADC_Init()
{
// Disable clock gating to VADC
SCU_GENERAL->PASSWD = 0x000000C0U; // Disable bit protection
SET_BIT(SCU_CLK->CGATCLR0, SCU_CLK_CGATCLR0_VADC_Pos); // Disables clock gating for VADC kernel
while (SCU_CLK->CLKCR & (SCU_CLK_CLKCR_VDDC2LOW_Msk)); // Wait for the VDDC to stable
SCU_GENERAL->PASSWD = 0x000000C3UL; // Enable bit protection
//GLOBAL VADC INITIALIZATION
VADC->CLC = (0 << VADC_CLC_DISR_Pos & VADC_CLC_DISR_Msk) | // On request: enable the module clock
(0 << VADC_CLC_DISS_Pos & VADC_CLC_DISS_Msk) | // Module clock is enabled
(0 << VADC_CLC_EDIS_Pos & VADC_CLC_EDIS_Msk); // Sleep mode request is enabled and functional
while(VADC->CLC!=0); // wait for module clock enabled
// Converter ON
SHS0->SHSCFG = (0 << SHS_SHSCFG_ANOFF_Pos & SHS_SHSCFG_ANOFF_Msk)|SHS_SHSCFG_SCWC_Msk;
*(uint32_t *)0x40010500 = 0X0001; //Errata ADC_AI.003
// Wait until analog converter is on (SHSCFG.ANRDY = 1)
while (!(SHS0->SHSCFG & SHS_SHSCFG_ANRDY_Msk)) ;
delay(12); // Added ~15usec wait time for ADC power to stable
// Trigger Start Up calibration
ADC_SU_Calibration();
// Trigger gain calibration
ADC_PG_Calibration(); //do not modify DPCAL and SUCAL bitfields form GLOBCFG from here onwards to avoid wrong calibration configurations
// Background source configuration (include 1 channel -ch 7 group 0 w/o auto scan (continuous conversions).
VADC->BRSSEL[0] = (1 << VADC_BRSSEL_CHSELG7_Pos & VADC_BRSSEL_CHSELG7_Msk);
VADC->BRSMR = (1 << VADC_BRSMR_ENGT_Pos & VADC_BRSMR_ENGT_Msk)| //Enable gating always
(1 << VADC_BRSMR_SCAN_Pos & VADC_BRSMR_SCAN_Msk); //Enable autoscan
// Enable interrupt for global result register
VADC->GLOBRCR = (0x1 << VADC_GLOBRCR_SRGEN_Pos & VADC_GLOBRCR_SRGEN_Msk);
// Route event trigger to service request line (interrupt node)
VADC->GLOBEVNP = (0x1 << VADC_GLOBEVNP_REV0NP_Pos & VADC_GLOBEVNP_REV0NP_Msk);
// Enable nvic node for SR1
NVIC_EnableIRQ(VADC0_C0_1_IRQn);
}
// Startup calibration
void ADC_SU_Calibration (void)
{
uint16_t wait;
// Enable calibration
*SHS0_CALOC0 = 0x00;
*SHS0_CALOC1 = 0x00;
// Enable SU calibration ADC
VADC->GLOBCFG |= (1 << VADC_GLOBCFG_SUCAL_Pos & VADC_GLOBCFG_SUCAL_Msk)|
(1 << VADC_GLOBCFG_DPCAL0_Pos & VADC_GLOBCFG_DPCAL0_Msk);
// Wait for 1920cycles or 60us for the startup calibration to complete
wait = 20;
while(wait > 0)
{
wait--;
*SHS0_CALOC0 = 0x8000; // Clear offset calibration values
*SHS0_CALOC1 = 0x8000;
}
} // End of ADC_SU_Calibration
// Gain calibration routine
void ADC_PG_Calibration (void)
{
uint16_t i, j, adc_result_aux;
*(uint32_t *)0x480340BC = 0X3F100400; //Errata ADC_AI.004
VADC->BRSSEL[0] = (1 << VADC_BRSSEL_CHSELG0_Pos & VADC_BRSSEL_CHSELG0_Msk);//set G0_Ch0 for dummy conversions
// SUCAL=0, DPCAL0=0, DPCAL1=0 (DPCAL1 has to be kept to 0)
VADC->GLOBCFG |= 0x00;
*SHS0_CALOC0 = 0x8000; // Clear offset calibration values
*SHS0_CALOC1 = 0x8000;
// Trigger dummy conversions (9 x 2000 times)
for (i=0; i < 2000; i++)
{
for (j=0; j < 9; j++)
{
VADC->BRSMR |= (1 << VADC_BRSMR_ENGT_Pos & VADC_BRSMR_ENGT_Msk)| //Enable gating always
(0 << VADC_BRSMR_SCAN_Pos & VADC_BRSMR_SCAN_Msk)| //No autoscan-->only 1 conversion
(1 << VADC_BRSMR_LDEV_Pos & VADC_BRSMR_LDEV_Msk); //Generate a load event
// wait for valid result
while (!(VADC->GLOBRES & VADC_GLOBRES_VF_Msk));
//read result
adc_result_aux = (VADC->GLOBRES & VADC_GLOBRES_RESULT_Msk);
// Clear offset calibration values
*SHS0_CALOC0 = 0x8000;
*SHS0_CALOC1 = 0x8000;
}
}
// Redundant code to avoid warning
if(adc_result_aux == 0);
// Wait until last gain cal step is finished
while ( (SHS0->SHSCFG & SHS_SHSCFG_STATE_Msk) != 0 )
{
// Clear offset calibration values
*SHS0_CALOC0 = 0x8000;
*SHS0_CALOC1 = 0x8000;
}
VADC->GLOBCFG = 0x00; // Ensure Post-Calibration enable (SUCAL=0, DPCAL0=0, DPCAL1=0)
// Reset used register to reset value
VADC->GLOBRES = 0x00;
VADC->BRSMR = 0x00;
VADC->BRSSEL[0] = 0x00;
} // End of ADC_PG_Calibration
void delay (uint32_t delay)
{
while(delay > 0)
{
delay--;
}
} // End of delay
i have put condition in while(1) please check . initially Init_Conv_Comp_flag = 0; i put condition flag in ISR Inside ISR after result_sum Init_Conv_Comp_flag = 1 becomes 1,in while(1) check condition Init_Conv_Comp_flag = 1; then i on/off relays.kindly guide me
/*******************************************************************************
** INCLUDE FILES **
******************************************************************************/
#include
//#include
#include "GPIO.h"
/*******************************************************************************
** Macro Definitions **
*******************************************************************************/
#define CALIB00 0x480340E0
#define CALIB01 0x480340E4
/*******************************************************************************
** Private Variable Definitions **
*******************************************************************************/
uint32_t * SHS0_CALOC0 = (uint32_t *) CALIB00;
uint32_t * SHS0_CALOC1 = (uint32_t *) CALIB01;
/*******************************************************************************
** Function Declarations **
*******************************************************************************/
void delay (uint32_t delay);
void VADC_Init();
void ADC_SU_Calibration (void);
void ADC_PG_Calibration (void);
/*******************************************************************************
** Global Variable Definitions **
*******************************************************************************/
uint32_t ADC_Result[101]={0};
uint32_t i=0, j=0;
uint32_t result_sum=0;
uint32_t flag_condition1=0;
uint32_t flag_condition2=0;
uint32_t Init_Conv_Comp_flag = 0;
int main(void)
{
SCU_GENERAL->PASSWD = 0x000000C0UL; // disable bit protection
SCU_CLK->CLKCR = 0x3ff10200UL; // 32 MHz MCLK, 64 MHz PCLK
while((SCU_CLK->CLKCR & SCU_CLK_CLKCR_VDDC2LOW_Msk));
SCU_GENERAL->PASSWD = 0x000000C3UL; // enable bit protection
P0_5_set_mode(OUTPUT_PP_GP); // pin set up p0.5
P0_6_set_mode(OUTPUT_PP_GP); //pin setup p0.6
/* Relay1 on for power on condition*/
P0_5_reset();
P0_6_set();
// Initialize ADC
VADC_Init();
// Generate load event to start conversion
VADC->BRSMR |= (1 << VADC_BRSMR_LDEV_Pos & VADC_BRSMR_LDEV_Msk);
while(1)
{
if(Init_Conv_Comp_flag == 1) //condition check
{
/*threshold voltage 1.26v*/
if(result_sum<=1032)
{
if (flag_condition2==1)
{
delay(1000);
flag_condition1=1;
flag_condition2=0;
}
//flag_condition1=1;
/*Relay1 on <1032 count*/
P0_5_reset();
P0_6_set();
}
else if (result_sum >1032)
{
if (flag_condition1==1)
{
delay(1000);
flag_condition1=0;
flag_condition2=1;
}
//flag_condition2=1;
/*Relay2 on >1032 count*/
P0_6_reset();
P0_5_set();
} //end of else part
} //end of con_flag
} //end of while
} // end of main
void VADC0_C0_1_IRQHandler(void)
{
//result_sum = 0;
ADC_Result=VADC->GLOBRES&0xFFF;
result_sum += ADC_Result;
if(i==100)
{
result_sum = result_sum/100;
i=0;
}
else{i++;}
//Init_Conv_Comp_flag = 1;
// Clear offset calibration values
*SHS0_CALOC0 = 0x8000;
*SHS0_CALOC1 = 0x8000;
Init_Conv_Comp_flag = 1;
}
void VADC_Init()
{
// Disable clock gating to VADC
SCU_GENERAL->PASSWD = 0x000000C0U; // Disable bit protection
SET_BIT(SCU_CLK->CGATCLR0, SCU_CLK_CGATCLR0_VADC_Pos); // Disables clock gating for VADC kernel
while (SCU_CLK->CLKCR & (SCU_CLK_CLKCR_VDDC2LOW_Msk)); // Wait for the VDDC to stable
SCU_GENERAL->PASSWD = 0x000000C3UL; // Enable bit protection
//GLOBAL VADC INITIALIZATION
VADC->CLC = (0 << VADC_CLC_DISR_Pos & VADC_CLC_DISR_Msk) | // On request: enable the module clock
(0 << VADC_CLC_DISS_Pos & VADC_CLC_DISS_Msk) | // Module clock is enabled
(0 << VADC_CLC_EDIS_Pos & VADC_CLC_EDIS_Msk); // Sleep mode request is enabled and functional
while(VADC->CLC!=0); // wait for module clock enabled
// Converter ON
SHS0->SHSCFG = (0 << SHS_SHSCFG_ANOFF_Pos & SHS_SHSCFG_ANOFF_Msk)|SHS_SHSCFG_SCWC_Msk;
*(uint32_t *)0x40010500 = 0X0001; //Errata ADC_AI.003
// Wait until analog converter is on (SHSCFG.ANRDY = 1)
while (!(SHS0->SHSCFG & SHS_SHSCFG_ANRDY_Msk)) ;
delay(12); // Added ~15usec wait time for ADC power to stable
// Trigger Start Up calibration
ADC_SU_Calibration();
// Trigger gain calibration
ADC_PG_Calibration(); //do not modify DPCAL and SUCAL bitfields form GLOBCFG from here onwards to avoid wrong calibration configurations
// Background source configuration (include 1 channel -ch 7 group 0 w/o auto scan (continuous conversions).
VADC->BRSSEL[0] = (1 << VADC_BRSSEL_CHSELG7_Pos & VADC_BRSSEL_CHSELG7_Msk);
VADC->BRSMR = (1 << VADC_BRSMR_ENGT_Pos & VADC_BRSMR_ENGT_Msk)| //Enable gating always
(1 << VADC_BRSMR_SCAN_Pos & VADC_BRSMR_SCAN_Msk); //Enable autoscan
// Enable interrupt for global result register
VADC->GLOBRCR = (0x1 << VADC_GLOBRCR_SRGEN_Pos & VADC_GLOBRCR_SRGEN_Msk);
// Route event trigger to service request line (interrupt node)
VADC->GLOBEVNP = (0x1 << VADC_GLOBEVNP_REV0NP_Pos & VADC_GLOBEVNP_REV0NP_Msk);
// Enable nvic node for SR1
NVIC_EnableIRQ(VADC0_C0_1_IRQn);
}
// Startup calibration
void ADC_SU_Calibration (void)
{
uint16_t wait;
// Enable calibration
*SHS0_CALOC0 = 0x00;
*SHS0_CALOC1 = 0x00;
// Enable SU calibration ADC
VADC->GLOBCFG |= (1 << VADC_GLOBCFG_SUCAL_Pos & VADC_GLOBCFG_SUCAL_Msk)|
(1 << VADC_GLOBCFG_DPCAL0_Pos & VADC_GLOBCFG_DPCAL0_Msk);
// Wait for 1920cycles or 60us for the startup calibration to complete
wait = 20;
while(wait > 0)
{
wait--;
*SHS0_CALOC0 = 0x8000; // Clear offset calibration values
*SHS0_CALOC1 = 0x8000;
}
} // End of ADC_SU_Calibration
// Gain calibration routine
void ADC_PG_Calibration (void)
{
uint16_t i, j, adc_result_aux;
*(uint32_t *)0x480340BC = 0X3F100400; //Errata ADC_AI.004
VADC->BRSSEL[0] = (1 << VADC_BRSSEL_CHSELG0_Pos & VADC_BRSSEL_CHSELG0_Msk);//set G0_Ch0 for dummy conversions
// SUCAL=0, DPCAL0=0, DPCAL1=0 (DPCAL1 has to be kept to 0)
VADC->GLOBCFG |= 0x00;
*SHS0_CALOC0 = 0x8000; // Clear offset calibration values
*SHS0_CALOC1 = 0x8000;
// Trigger dummy conversions (9 x 2000 times)
for (i=0; i < 2000; i++)
{
for (j=0; j < 9; j++)
{
VADC->BRSMR |= (1 << VADC_BRSMR_ENGT_Pos & VADC_BRSMR_ENGT_Msk)| //Enable gating always
(0 << VADC_BRSMR_SCAN_Pos & VADC_BRSMR_SCAN_Msk)| //No autoscan-->only 1 conversion
(1 << VADC_BRSMR_LDEV_Pos & VADC_BRSMR_LDEV_Msk); //Generate a load event
// wait for valid result
while (!(VADC->GLOBRES & VADC_GLOBRES_VF_Msk));
//read result
adc_result_aux = (VADC->GLOBRES & VADC_GLOBRES_RESULT_Msk);
// Clear offset calibration values
*SHS0_CALOC0 = 0x8000;
*SHS0_CALOC1 = 0x8000;
}
}
// Redundant code to avoid warning
if(adc_result_aux == 0);
// Wait until last gain cal step is finished
while ( (SHS0->SHSCFG & SHS_SHSCFG_STATE_Msk) != 0 )
{
// Clear offset calibration values
*SHS0_CALOC0 = 0x8000;
*SHS0_CALOC1 = 0x8000;
}
VADC->GLOBCFG = 0x00; // Ensure Post-Calibration enable (SUCAL=0, DPCAL0=0, DPCAL1=0)
// Reset used register to reset value
VADC->GLOBRES = 0x00;
VADC->BRSMR = 0x00;
VADC->BRSSEL[0] = 0x00;
} // End of ADC_PG_Calibration
void delay (uint32_t delay)
{
while(delay > 0)
{
delay--;
}
} // End of delay
Not applicable
Nov 24, 2014
01:33 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Nov 24, 2014
01:33 AM
Hi prathibha,
You should put Init_Conv_Comp_flag = 1; in the if(i==100) { ..} because this is where you divide the result_sum to get your average result.
You should put Init_Conv_Comp_flag = 1; in the if(i==100) { ..} because this is where you divide the result_sum to get your average result.
Not applicable
Nov 24, 2014
01:57 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Nov 24, 2014
01:57 AM
Thank you jackson ,is this is the way i need to put condition inside if(i==100) .kindly guide me
if(i==100)
{
result_sum = result_sum/100;
i=0;
Init_Conv_Comp_flag = 1;
}
if(i==100)
{
result_sum = result_sum/100;
i=0;
Init_Conv_Comp_flag = 1;
}
Not applicable
Nov 24, 2014
05:21 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Nov 24, 2014
05:21 PM
Hi prathibha,
Yes, and you should set the Init_Conv_Comp_flag = 0; before the end of if(Init_Conv_Comp_flag == 1){...} routine.
Yes, and you should set the Init_Conv_Comp_flag = 0; before the end of if(Init_Conv_Comp_flag == 1){...} routine.
Attachments are accessible only for community members.
Not applicable
Nov 24, 2014
08:38 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Nov 24, 2014
08:38 PM
Thank you Jackson i have done the changes as you told.please find the attachment.
#include //SFR declarations of the selected device
//#include
#include "GPIO.h"
/*******************************************************************************
** Macro Definitions **
*******************************************************************************/
#define CALIB00 0x480340E0
#define CALIB01 0x480340E4
/*******************************************************************************
** Private Variable Definitions **
*******************************************************************************/
uint32_t * SHS0_CALOC0 = (uint32_t *) CALIB00;
uint32_t * SHS0_CALOC1 = (uint32_t *) CALIB01;
/*******************************************************************************
** Function Declarations **
*******************************************************************************/
void delay (uint32_t delay);
void VADC_Init();
void ADC_SU_Calibration (void);
void ADC_PG_Calibration (void);
/*******************************************************************************
** Global Variable Definitions **
*******************************************************************************/
uint32_t ADC_Result[101]={0};
uint32_t i=0, j=0;
uint32_t result_sum=0;
uint32_t flag_condition1=0;
uint32_t flag_condition2=0;
uint32_t Init_Conv_Comp_flag = 0;
int main(void)
{
SCU_GENERAL->PASSWD = 0x000000C0UL; // disable bit protection
SCU_CLK->CLKCR = 0x3ff10200UL; // 32 MHz MCLK, 64 MHz PCLK
while((SCU_CLK->CLKCR & SCU_CLK_CLKCR_VDDC2LOW_Msk));
SCU_GENERAL->PASSWD = 0x000000C3UL; // enable bit protection
P0_5_set_mode(OUTPUT_PP_GP); // pin set up p0.5 for relay2
P0_6_set_mode(OUTPUT_PP_GP); //pin setup p0.6 for relay1
/* Relay1 on for power on condition*/
P0_5_reset();
P0_6_set();
// Initialize ADC
VADC_Init();
// Generate load event to start conversion
VADC->BRSMR |= (1 << VADC_BRSMR_LDEV_Pos & VADC_BRSMR_LDEV_Msk);
while(1)
{
if(Init_Conv_Comp_flag == 1)
{
/*threshold voltage 1.26v*/
if(result_sum<=1032)
{
if (flag_condition2==1)
{
delay(1000);
flag_condition1=1;
flag_condition2=0;
}
//flag_condition1=1;
/*Relay1 on <1032 count*/
P0_5_reset();
P0_6_set();
}
else if (result_sum >1032)
{
if (flag_condition1==1)
{
delay(1000);
flag_condition1=0;
flag_condition2=1;
}
//flag_condition2=1;
/*Relay2 on >1032 count*/
P0_6_reset();
P0_5_set();
} //end of else part
Init_Conv_Comp_flag = 0;
} //end of con_Comp_flag
} //end of while
} // end of main
void VADC0_C0_1_IRQHandler(void)
{
//result_sum = 0;
ADC_Result=VADC->GLOBRES&0xFFF;
result_sum += ADC_Result;
if(i==100)
{
result_sum = result_sum/100;
i=0;
Init_Conv_Comp_flag = 1;
}
else{i++;}
//Init_Conv_Comp_flag = 1;
// Clear offset calibration values
*SHS0_CALOC0 = 0x8000;
*SHS0_CALOC1 = 0x8000;
}
void VADC_Init()
{
// Disable clock gating to VADC
SCU_GENERAL->PASSWD = 0x000000C0U; // Disable bit protection
SET_BIT(SCU_CLK->CGATCLR0, SCU_CLK_CGATCLR0_VADC_Pos); // Disables clock gating for VADC kernel
while (SCU_CLK->CLKCR & (SCU_CLK_CLKCR_VDDC2LOW_Msk)); // Wait for the VDDC to stable
SCU_GENERAL->PASSWD = 0x000000C3UL; // Enable bit protection
//GLOBAL VADC INITIALIZATION
VADC->CLC = (0 << VADC_CLC_DISR_Pos & VADC_CLC_DISR_Msk) | // On request: enable the module clock
(0 << VADC_CLC_DISS_Pos & VADC_CLC_DISS_Msk) | // Module clock is enabled
(0 << VADC_CLC_EDIS_Pos & VADC_CLC_EDIS_Msk); // Sleep mode request is enabled and functional
while(VADC->CLC!=0); // wait for module clock enabled
// Converter ON
SHS0->SHSCFG = (0 << SHS_SHSCFG_ANOFF_Pos & SHS_SHSCFG_ANOFF_Msk)|SHS_SHSCFG_SCWC_Msk;
*(uint32_t *)0x40010500 = 0X0001; //Errata ADC_AI.003
// Wait until analog converter is on (SHSCFG.ANRDY = 1)
while (!(SHS0->SHSCFG & SHS_SHSCFG_ANRDY_Msk)) ;
delay(12); // Added ~15usec wait time for ADC power to stable
// Trigger Start Up calibration
ADC_SU_Calibration();
// Trigger gain calibration
ADC_PG_Calibration(); //do not modify DPCAL and SUCAL bitfields form GLOBCFG from here onwards to avoid wrong calibration configurations
// Background source configuration (include 1 channel -ch 7 group 0 w/o auto scan (continuous conversions).
VADC->BRSSEL[0] = (1 << VADC_BRSSEL_CHSELG7_Pos & VADC_BRSSEL_CHSELG7_Msk);
VADC->BRSMR = (1 << VADC_BRSMR_ENGT_Pos & VADC_BRSMR_ENGT_Msk)| //Enable gating always
(1 << VADC_BRSMR_SCAN_Pos & VADC_BRSMR_SCAN_Msk); //Enable autoscan
// Enable interrupt for global result register
VADC->GL2OBRCR = (0x1 << VADC_GLOBRCR_SRGEN_Pos & VADC_GLOBRCR_S2RGEN_Msk);
// Route event trigger to service request line (interrupt node)
VADC->GLOBEVNP = (0x1 << VADC_GLOBEVNP_REV0NP_Pos & VADC_GLOBEVNP_REV0NP_Msk);
// Enable nvic node for SR1
NVIC_EnableIRQ(VADC0_C0_1_IRQn);
}
// Startup calibration
void ADC_SU_Calibration (void)
{
uint16_t wait;
// Enable calibration
*SHS0_CALOC0 = 0x00;
*SHS0_CALOC1 = 0x00;
// Enable SU calibration ADC
VADC->GLOBCFG |= (1 << VADC_GLOBCFG_SUCAL_Pos & VADC_GLOBCFG_SUCAL_Msk)|
(1 << VADC_GLOBCFG_DPCAL0_Pos & VADC_GLOBCFG_DPCAL0_Msk);
// Wait for 1920cycles or 60us for the startup calibration to complete
wait = 20;
while(wait > 0)
{
wait--;
*SHS0_CALOC0 = 0x8000; // Clear offset calibration values
*SHS0_CALOC1 = 0x8000;
}
} // End of ADC_SU_Calibration
// Gain calibration routine
void ADC_PG_Calibration (void)
{
uint16_t i, j, adc_result_aux;
*(uint32_t *)0x480340BC = 0X3F100400; //Errata ADC_AI.004
VADC->BRSSEL[0] = (1 << VADC_BRSSEL_CHSELG0_Pos & VADC_BRSSEL_CHSELG0_Msk);//set G0_Ch0 for dummy conversions
// SUCAL=0, DPCAL0=0, DPCAL1=0 (DPCAL1 has to be kept to 0)
VADC->GLOBCFG |= 0x00;
*SHS0_CALOC0 = 0x8000; // Clear offset calibration values
*SHS0_CALOC1 = 0x8000;
// Trigger dummy conversions (9 x 2000 times)
for (i=0; i < 2000; i++)
{
for (j=0; j < 9; j++)
{
VADC->BRSMR |= (1 << VADC_BRSMR_ENGT_Pos & VADC_BRSMR_ENGT_Msk)| //Enable gating always
(0 << VADC_BRSMR_SCAN_Pos & VADC_BRSMR_SCAN_Msk)| //No autoscan-->only 1 conversion
(1 << VADC_BRSMR_LDEV_Pos & VADC_BRSMR_LDEV_Msk); //Generate a load event
// wait for valid result
while (!(VADC->GLOBRES & VADC_GLOBRES_VF_Msk));
//read result
adc_result_aux = (VADC->GLOBRES & VADC_GLOBRES_RESULT_Msk);
// Clear offset calibration values
*SHS0_CALOC0 = 0x8000;
*SHS0_CALOC1 = 0x8000;
}
}
// Redundant code to avoid warning
if(adc_result_aux == 0);
// Wait until last gain cal step is finished
while ( (SHS0->SHSCFG & SHS_SHSCFG_STATE_Msk) != 0 )
{
// Clear offset calibration values
*SHS0_CALOC0 = 0x8000;
*SHS0_CALOC1 = 0x8000;
}
VADC->GLOBCFG = 0x00; // Ensure Post-Calibration enable (SUCAL=0, DPCAL0=0, DPCAL1=0)
// Reset used register to reset value
VADC->GLOBRES = 0x00;
VADC->BRSMR = 0x00;
VADC->BRSSEL[0] = 0x00;
} // End of ADC_PG_Calibration
void delay (uint32_t delay)
{
while(delay > 0)
{
delay--;
}
} // End of delay
#include
//#include
#include "GPIO.h"
/*******************************************************************************
** Macro Definitions **
*******************************************************************************/
#define CALIB00 0x480340E0
#define CALIB01 0x480340E4
/*******************************************************************************
** Private Variable Definitions **
*******************************************************************************/
uint32_t * SHS0_CALOC0 = (uint32_t *) CALIB00;
uint32_t * SHS0_CALOC1 = (uint32_t *) CALIB01;
/*******************************************************************************
** Function Declarations **
*******************************************************************************/
void delay (uint32_t delay);
void VADC_Init();
void ADC_SU_Calibration (void);
void ADC_PG_Calibration (void);
/*******************************************************************************
** Global Variable Definitions **
*******************************************************************************/
uint32_t ADC_Result[101]={0};
uint32_t i=0, j=0;
uint32_t result_sum=0;
uint32_t flag_condition1=0;
uint32_t flag_condition2=0;
uint32_t Init_Conv_Comp_flag = 0;
int main(void)
{
SCU_GENERAL->PASSWD = 0x000000C0UL; // disable bit protection
SCU_CLK->CLKCR = 0x3ff10200UL; // 32 MHz MCLK, 64 MHz PCLK
while((SCU_CLK->CLKCR & SCU_CLK_CLKCR_VDDC2LOW_Msk));
SCU_GENERAL->PASSWD = 0x000000C3UL; // enable bit protection
P0_5_set_mode(OUTPUT_PP_GP); // pin set up p0.5 for relay2
P0_6_set_mode(OUTPUT_PP_GP); //pin setup p0.6 for relay1
/* Relay1 on for power on condition*/
P0_5_reset();
P0_6_set();
// Initialize ADC
VADC_Init();
// Generate load event to start conversion
VADC->BRSMR |= (1 << VADC_BRSMR_LDEV_Pos & VADC_BRSMR_LDEV_Msk);
while(1)
{
if(Init_Conv_Comp_flag == 1)
{
/*threshold voltage 1.26v*/
if(result_sum<=1032)
{
if (flag_condition2==1)
{
delay(1000);
flag_condition1=1;
flag_condition2=0;
}
//flag_condition1=1;
/*Relay1 on <1032 count*/
P0_5_reset();
P0_6_set();
}
else if (result_sum >1032)
{
if (flag_condition1==1)
{
delay(1000);
flag_condition1=0;
flag_condition2=1;
}
//flag_condition2=1;
/*Relay2 on >1032 count*/
P0_6_reset();
P0_5_set();
} //end of else part
Init_Conv_Comp_flag = 0;
} //end of con_Comp_flag
} //end of while
} // end of main
void VADC0_C0_1_IRQHandler(void)
{
//result_sum = 0;
ADC_Result=VADC->GLOBRES&0xFFF;
result_sum += ADC_Result;
if(i==100)
{
result_sum = result_sum/100;
i=0;
Init_Conv_Comp_flag = 1;
}
else{i++;}
//Init_Conv_Comp_flag = 1;
// Clear offset calibration values
*SHS0_CALOC0 = 0x8000;
*SHS0_CALOC1 = 0x8000;
}
void VADC_Init()
{
// Disable clock gating to VADC
SCU_GENERAL->PASSWD = 0x000000C0U; // Disable bit protection
SET_BIT(SCU_CLK->CGATCLR0, SCU_CLK_CGATCLR0_VADC_Pos); // Disables clock gating for VADC kernel
while (SCU_CLK->CLKCR & (SCU_CLK_CLKCR_VDDC2LOW_Msk)); // Wait for the VDDC to stable
SCU_GENERAL->PASSWD = 0x000000C3UL; // Enable bit protection
//GLOBAL VADC INITIALIZATION
VADC->CLC = (0 << VADC_CLC_DISR_Pos & VADC_CLC_DISR_Msk) | // On request: enable the module clock
(0 << VADC_CLC_DISS_Pos & VADC_CLC_DISS_Msk) | // Module clock is enabled
(0 << VADC_CLC_EDIS_Pos & VADC_CLC_EDIS_Msk); // Sleep mode request is enabled and functional
while(VADC->CLC!=0); // wait for module clock enabled
// Converter ON
SHS0->SHSCFG = (0 << SHS_SHSCFG_ANOFF_Pos & SHS_SHSCFG_ANOFF_Msk)|SHS_SHSCFG_SCWC_Msk;
*(uint32_t *)0x40010500 = 0X0001; //Errata ADC_AI.003
// Wait until analog converter is on (SHSCFG.ANRDY = 1)
while (!(SHS0->SHSCFG & SHS_SHSCFG_ANRDY_Msk)) ;
delay(12); // Added ~15usec wait time for ADC power to stable
// Trigger Start Up calibration
ADC_SU_Calibration();
// Trigger gain calibration
ADC_PG_Calibration(); //do not modify DPCAL and SUCAL bitfields form GLOBCFG from here onwards to avoid wrong calibration configurations
// Background source configuration (include 1 channel -ch 7 group 0 w/o auto scan (continuous conversions).
VADC->BRSSEL[0] = (1 << VADC_BRSSEL_CHSELG7_Pos & VADC_BRSSEL_CHSELG7_Msk);
VADC->BRSMR = (1 << VADC_BRSMR_ENGT_Pos & VADC_BRSMR_ENGT_Msk)| //Enable gating always
(1 << VADC_BRSMR_SCAN_Pos & VADC_BRSMR_SCAN_Msk); //Enable autoscan
// Enable interrupt for global result register
VADC->GL2OBRCR = (0x1 << VADC_GLOBRCR_SRGEN_Pos & VADC_GLOBRCR_S2RGEN_Msk);
// Route event trigger to service request line (interrupt node)
VADC->GLOBEVNP = (0x1 << VADC_GLOBEVNP_REV0NP_Pos & VADC_GLOBEVNP_REV0NP_Msk);
// Enable nvic node for SR1
NVIC_EnableIRQ(VADC0_C0_1_IRQn);
}
// Startup calibration
void ADC_SU_Calibration (void)
{
uint16_t wait;
// Enable calibration
*SHS0_CALOC0 = 0x00;
*SHS0_CALOC1 = 0x00;
// Enable SU calibration ADC
VADC->GLOBCFG |= (1 << VADC_GLOBCFG_SUCAL_Pos & VADC_GLOBCFG_SUCAL_Msk)|
(1 << VADC_GLOBCFG_DPCAL0_Pos & VADC_GLOBCFG_DPCAL0_Msk);
// Wait for 1920cycles or 60us for the startup calibration to complete
wait = 20;
while(wait > 0)
{
wait--;
*SHS0_CALOC0 = 0x8000; // Clear offset calibration values
*SHS0_CALOC1 = 0x8000;
}
} // End of ADC_SU_Calibration
// Gain calibration routine
void ADC_PG_Calibration (void)
{
uint16_t i, j, adc_result_aux;
*(uint32_t *)0x480340BC = 0X3F100400; //Errata ADC_AI.004
VADC->BRSSEL[0] = (1 << VADC_BRSSEL_CHSELG0_Pos & VADC_BRSSEL_CHSELG0_Msk);//set G0_Ch0 for dummy conversions
// SUCAL=0, DPCAL0=0, DPCAL1=0 (DPCAL1 has to be kept to 0)
VADC->GLOBCFG |= 0x00;
*SHS0_CALOC0 = 0x8000; // Clear offset calibration values
*SHS0_CALOC1 = 0x8000;
// Trigger dummy conversions (9 x 2000 times)
for (i=0; i < 2000; i++)
{
for (j=0; j < 9; j++)
{
VADC->BRSMR |= (1 << VADC_BRSMR_ENGT_Pos & VADC_BRSMR_ENGT_Msk)| //Enable gating always
(0 << VADC_BRSMR_SCAN_Pos & VADC_BRSMR_SCAN_Msk)| //No autoscan-->only 1 conversion
(1 << VADC_BRSMR_LDEV_Pos & VADC_BRSMR_LDEV_Msk); //Generate a load event
// wait for valid result
while (!(VADC->GLOBRES & VADC_GLOBRES_VF_Msk));
//read result
adc_result_aux = (VADC->GLOBRES & VADC_GLOBRES_RESULT_Msk);
// Clear offset calibration values
*SHS0_CALOC0 = 0x8000;
*SHS0_CALOC1 = 0x8000;
}
}
// Redundant code to avoid warning
if(adc_result_aux == 0);
// Wait until last gain cal step is finished
while ( (SHS0->SHSCFG & SHS_SHSCFG_STATE_Msk) != 0 )
{
// Clear offset calibration values
*SHS0_CALOC0 = 0x8000;
*SHS0_CALOC1 = 0x8000;
}
VADC->GLOBCFG = 0x00; // Ensure Post-Calibration enable (SUCAL=0, DPCAL0=0, DPCAL1=0)
// Reset used register to reset value
VADC->GLOBRES = 0x00;
VADC->BRSMR = 0x00;
VADC->BRSSEL[0] = 0x00;
} // End of ADC_PG_Calibration
void delay (uint32_t delay)
{
while(delay > 0)
{
delay--;
}
} // End of delay
Not applicable
Nov 24, 2014
10:35 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Nov 24, 2014
10:35 PM
Hi prathibha,
Does it work as you required now?
Does it work as you required now?
Attachments are accessible only for community members.
Not applicable
Nov 25, 2014
04:18 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Nov 25, 2014
04:18 AM
Thank you Jackson .
when i checked in step over function in debug window instruction pointer contionously pointing between if(Init_Conv_Comp_flag == 1) {..} .when i press suspend button its showing result_sum value but relays are not on/off according to threshold voltage else if (result_sum >1638) relay 2 has to be on but its not happening.I can conclude that i am not able to do step by step execution inside while(1) loop .only power on condition is executing /* Relay1 on for power on condition*/
P0_5_reset(); //Relay2 off
P0_6_reset(); //Relay1 onKindly guide me i am attaching the code.
If i set flag condition Instruction pointer is between if(int_conv){...}
if i dont set flag its directly entering while(1) loop
result_sum is>1638 as per threshold condition relays are not operating
in between the execution this message is coming oftenly
Kindly guide me to fix up this problems.
Thanks&Regards
Prathibha
when i checked in step over function in debug window instruction pointer contionously pointing between if(Init_Conv_Comp_flag == 1) {..} .when i press suspend button its showing result_sum value but relays are not on/off according to threshold voltage else if (result_sum >1638) relay 2 has to be on but its not happening.I can conclude that i am not able to do step by step execution inside while(1) loop .only power on condition is executing /* Relay1 on for power on condition*/
P0_5_reset(); //Relay2 off
P0_6_reset(); //Relay1 onKindly guide me i am attaching the code.
If i set flag condition Instruction pointer is between if(int_conv){...}
if i dont set flag its directly entering while(1) loop
result_sum is>1638 as per threshold condition relays are not operating
in between the execution this message is coming oftenly
Kindly guide me to fix up this problems.
Thanks&Regards
Prathibha
Not applicable
Nov 25, 2014
09:41 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Nov 25, 2014
09:41 PM
Hi prathibha,
It could be the ADC interrupt is too frequent that the CPU doesn't able to execute the while loop in main.
Perhaps you can try to stop the ADC when it hits Init_Conv_Comp_flag = 1; so the CPU can execute the code in the while loop.
Then only restart the ADC after the relay on/off (before Init_Conv_Comp_flag = 0).
It could be the ADC interrupt is too frequent that the CPU doesn't able to execute the while loop in main.
Perhaps you can try to stop the ADC when it hits Init_Conv_Comp_flag = 1; so the CPU can execute the code in the while loop.
Then only restart the ADC after the relay on/off (before Init_Conv_Comp_flag = 0).
Not applicable
Nov 25, 2014
09:45 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Nov 25, 2014
09:45 PM
Hi Jackson,
I also checked by giving power on condition in different way P0_6_reset(); //Relay1 off P0_5_set(); //Relay2 on at this time relay2 is on ,if(result_sum<1638) for this condition the value in the result_sum =226 relay1 has to be on but this is not happening.Kindly guide me how can i on/off relays based on ADC count.
Thanks&Regards
Prathibha
I also checked by giving power on condition in different way P0_6_reset(); //Relay1 off P0_5_set(); //Relay2 on at this time relay2 is on ,if(result_sum<1638) for this condition the value in the result_sum =226 relay1 has to be on but this is not happening.Kindly guide me how can i on/off relays based on ADC count.
Thanks&Regards
Prathibha
Not applicable
Nov 25, 2014
09:54 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Nov 25, 2014
09:54 PM
Hi Jackson,
In terms of coding kindly can u explain where i need to made the changes
In terms of coding kindly can u explain where i need to made the changes
Not applicable
Nov 25, 2014
10:05 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Nov 25, 2014
10:05 PM
Thankyou Jackson
I have done the changes As you told please check .kindly guide me
/*******************************************************************************
** INCLUDE FILES **
******************************************************************************/
#include //SFR declarations of the selected device
//#include
#include "GPIO.h"
/*******************************************************************************
** Macro Definitions **
*******************************************************************************/
#define CALIB00 0x480340E0
#define CALIB01 0x480340E4
/*******************************************************************************
** Private Variable Definitions **
*******************************************************************************/
uint32_t * SHS0_CALOC0 = (uint32_t *) CALIB00;
uint32_t * SHS0_CALOC1 = (uint32_t *) CALIB01;
/*******************************************************************************
** Function Declarations **
*******************************************************************************/
void delay (uint32_t delay);
void VADC_Init();
void ADC_SU_Calibration (void);
void ADC_PG_Calibration (void);
/*******************************************************************************
** Global Variable Definitions **
*******************************************************************************/
uint32_t ADC_Result[101]={0};
uint32_t i=0, j=0;
uint32_t result_sum=0;
uint32_t flag_condition1=0;
uint32_t flag_condition2=0;
uint32_t Init_Conv_Comp_flag = 0;
int main(void)
{
SCU_GENERAL->PASSWD = 0x000000C0UL; // disable bit protection
SCU_CLK->CLKCR = 0x3ff10200UL; // 32 MHz MCLK, 64 MHz PCLK
while((SCU_CLK->CLKCR & SCU_CLK_CLKCR_VDDC2LOW_Msk));
SCU_GENERAL->PASSWD = 0x000000C3UL; // enable bit protection
P0_5_set_mode(OUTPUT_PP_GP); // pin set up p0.5 for relay2
P0_6_set_mode(OUTPUT_PP_GP); //pin setup p0.6 for relay1
/* Relay1 on for power on condition*/
P0_5_set(); //Relay2 off
P0_6_reset(); //Relay1 on
delay(1000);
// Initialize ADC
VADC_Init();
// Generate load event to start conversion
VADC->BRSMR |= (1 << VADC_BRSMR_LDEV_Pos & VADC_BRSMR_LDEV_Msk);
while(1)
{
if(Init_Conv_Comp_flag == 0)
{
/*threshold voltage 2v*/
if(result_sum<=1638)
{
if (flag_condition2==1)
{
delay(1000);
flag_condition1=1;
flag_condition2=0;
}
//flag_condition1=1;
/*Relay1 on <1032 count*/
P0_5_reset(); //Relay2 off
P0_6_set(); //Relay1 on
Init_Conv_Comp_flag = 1;
}
else if (result_sum >1638)
{
if (flag_condition1==1)
{
delay(1000);
flag_condition1=0;
flag_condition2=1;
}
//flag_condition2=1;
/*Relay2 on >1032 count*/
P0_6_reset(); //Relay1 off
P0_5_set(); //Relay2 on
Init_Conv_Comp_flag = 1;
} //end of else part
//Init_Conv_Comp_flag = 0;
} //end of con_Comp_flag
} //end of while
return 0;
} // end of main
void VADC0_C0_1_IRQHandler(void)
{
static int result_temp = 0;
ADC_Result=VADC->GLOBRES&0xFFF;
//result_sum += ADC_Result;
result_temp = ADC_Result+result_temp;
if(i==100)
{
result_sum = result_temp/100;
//result_sum = result_sum/100;
i=0;
result_temp=0;
Init_Conv_Comp_flag = 1;
Init_Conv_Comp_flag = 0;
}
else{i++;}
//Init_Conv_Comp_flag = 1;
// Clear offset calibration values
*SHS0_CALOC0 = 0x8000;
*SHS0_CALOC1 = 0x8000;
}
void VADC_Init()
{
// Disable clock gating to VADC
SCU_GENERAL->PASSWD = 0x000000C0U; // Disable bit protection
SET_BIT(SCU_CLK->CGATCLR0, SCU_CLK_CGATCLR0_VADC_Pos); // Disables clock gating for VADC kernel
while (SCU_CLK->CLKCR & (SCU_CLK_CLKCR_VDDC2LOW_Msk)); // Wait for the VDDC to stable
SCU_GENERAL->PASSWD = 0x000000C3UL; // Enable bit protection
//GLOBAL VADC INITIALIZATION
VADC->CLC = (0 << VADC_CLC_DISR_Pos & VADC_CLC_DISR_Msk) | // On request: enable the module clock
(0 << VADC_CLC_DISS_Pos & VADC_CLC_DISS_Msk) | // Module clock is enabled
(0 << VADC_CLC_EDIS_Pos & VADC_CLC_EDIS_Msk); // Sleep mode request is enabled and functional
while(VADC->CLC!=0); // wait for module clock enabled
// Converter ON
SHS0->SHSCFG = (0 << SHS_SHSCFG_ANOFF_Pos & SHS_SHSCFG_ANOFF_Msk)|SHS_SHSCFG_SCWC_Msk;
*(uint32_t *)0x40010500 = 0X0001; //Errata ADC_AI.003
// Wait until analog converter is on (SHSCFG.ANRDY = 1)
while (!(SHS0->SHSCFG & SHS_SHSCFG_ANRDY_Msk)) ;
delay(12); // Added ~15usec wait time for ADC power to stable
// Trigger Start Up calibration
ADC_SU_Calibration();
// Trigger gain calibration
ADC_PG_Calibration(); //do not modify DPCAL and SUCAL bitfields form GLOBCFG from here onwards to avoid wrong calibration configurations
// Background source configuration (include 1 channel -ch 7 group 0 w/o auto scan (continuous conversions).
VADC->BRSSEL[0] = (1 << VADC_BRSSEL_CHSELG7_Pos & VADC_BRSSEL_CHSELG7_Msk);
VADC->BRSMR = (1 << VADC_BRSMR_ENGT_Pos & VADC_BRSMR_ENGT_Msk)| //Enable gating always
(1 << VADC_BRSMR_SCAN_Pos & VADC_BRSMR_SCAN_Msk); //Enable autoscan
// Enable interrupt for global result register
VADC->GLOBRCR = (0x1 << VADC_GLOBRCR_SRGEN_Pos & VADC_GLOBRCR_SRGEN_Msk);
// Route event trigger to service request line (interrupt node)
VADC->GLOBEVNP = (0x1 << VADC_GLOBEVNP_REV0NP_Pos & VADC_GLOBEVNP_REV0NP_Msk);
// Enable nvic node for SR1
NVIC_EnableIRQ(VADC0_C0_1_IRQn);
}
// Startup calibration
void ADC_SU_Calibration (void)
{
uint16_t wait;
// Enable calibration
*SHS0_CALOC0 = 0x00;
*SHS0_CALOC1 = 0x00;
// Enable SU calibration ADC
VADC->GLOBCFG |= (1 << VADC_GLOBCFG_SUCAL_Pos & VADC_GLOBCFG_SUCAL_Msk)|
(1 << VADC_GLOBCFG_DPCAL0_Pos & VADC_GLOBCFG_DPCAL0_Msk);
// Wait for 1920cycles or 60us for the startup calibration to complete
wait = 20;
while(wait > 0)
{
wait--;
*SHS0_CALOC0 = 0x8000; // Clear offset calibration values
*SHS0_CALOC1 = 0x8000;
}
} // End of ADC_SU_Calibration
// Gain calibration routine
void ADC_PG_Calibration (void)
{
uint16_t i, j, adc_result_aux;
*(uint32_t *)0x480340BC = 0X3F100400; //Errata ADC_AI.004
VADC->BRSSEL[0] = (1 << VADC_BRSSEL_CHSELG0_Pos & VADC_BRSSEL_CHSELG0_Msk);//set G0_Ch0 for dummy conversions
// SUCAL=0, DPCAL0=0, DPCAL1=0 (DPCAL1 has to be kept to 0)
VADC->GLOBCFG |= 0x00;
*SHS0_CALOC0 = 0x8000; // Clear offset calibration values
*SHS0_CALOC1 = 0x8000;
// Trigger dummy conversions (9 x 2000 times)
for (i=0; i < 2000; i++)
{
for (j=0; j < 9; j++)
{
VADC->BRSMR |= (1 << VADC_BRSMR_ENGT_Pos & VADC_BRSMR_ENGT_Msk)| //Enable gating always
(0 << VADC_BRSMR_SCAN_Pos & VADC_BRSMR_SCAN_Msk)| //No autoscan-->only 1 conversion
(1 << VADC_BRSMR_LDEV_Pos & VADC_BRSMR_LDEV_Msk); //Generate a load event
// wait for valid result
while (!(VADC->GLOBRES & VADC_GLOBRES_VF_Msk));
//read result
adc_result_aux = (VADC->GLOBRES & VADC_GLOBRES_RESULT_Msk);
// Clear offset calibration values
*SHS0_CALOC0 = 0x8000;
*SHS0_CALOC1 = 0x8000;
}
}
// Redundant code to avoid warning
if(adc_result_aux == 0);
// Wait until last gain cal step is finished
while ( (SHS0->SHSCFG & SHS_SHSCFG_STATE_Msk) != 0 )
{
// Clear offset calibration values
*SHS0_CALOC0 = 0x8000;
*SHS0_CALOC1 = 0x8000;
}
VADC->GLOBCFG = 0x00; // Ensure Post-Calibration enable (SUCAL=0, DPCAL0=0, DPCAL1=0)
// Reset used register to reset value
VADC->GLOBRES = 0x00;
VADC->BRSMR = 0x00;
VADC->BRSSEL[0] = 0x00;
} // End of ADC_PG_Calibration
void delay (uint32_t delay)
{
while(delay > 0)
{
delay--;
}
} // End of delay
I have done the changes As you told please check .kindly guide me
/*******************************************************************************
** INCLUDE FILES **
******************************************************************************/
#include
//#include
#include "GPIO.h"
/*******************************************************************************
** Macro Definitions **
*******************************************************************************/
#define CALIB00 0x480340E0
#define CALIB01 0x480340E4
/*******************************************************************************
** Private Variable Definitions **
*******************************************************************************/
uint32_t * SHS0_CALOC0 = (uint32_t *) CALIB00;
uint32_t * SHS0_CALOC1 = (uint32_t *) CALIB01;
/*******************************************************************************
** Function Declarations **
*******************************************************************************/
void delay (uint32_t delay);
void VADC_Init();
void ADC_SU_Calibration (void);
void ADC_PG_Calibration (void);
/*******************************************************************************
** Global Variable Definitions **
*******************************************************************************/
uint32_t ADC_Result[101]={0};
uint32_t i=0, j=0;
uint32_t result_sum=0;
uint32_t flag_condition1=0;
uint32_t flag_condition2=0;
uint32_t Init_Conv_Comp_flag = 0;
int main(void)
{
SCU_GENERAL->PASSWD = 0x000000C0UL; // disable bit protection
SCU_CLK->CLKCR = 0x3ff10200UL; // 32 MHz MCLK, 64 MHz PCLK
while((SCU_CLK->CLKCR & SCU_CLK_CLKCR_VDDC2LOW_Msk));
SCU_GENERAL->PASSWD = 0x000000C3UL; // enable bit protection
P0_5_set_mode(OUTPUT_PP_GP); // pin set up p0.5 for relay2
P0_6_set_mode(OUTPUT_PP_GP); //pin setup p0.6 for relay1
/* Relay1 on for power on condition*/
P0_5_set(); //Relay2 off
P0_6_reset(); //Relay1 on
delay(1000);
// Initialize ADC
VADC_Init();
// Generate load event to start conversion
VADC->BRSMR |= (1 << VADC_BRSMR_LDEV_Pos & VADC_BRSMR_LDEV_Msk);
while(1)
{
if(Init_Conv_Comp_flag == 0)
{
/*threshold voltage 2v*/
if(result_sum<=1638)
{
if (flag_condition2==1)
{
delay(1000);
flag_condition1=1;
flag_condition2=0;
}
//flag_condition1=1;
/*Relay1 on <1032 count*/
P0_5_reset(); //Relay2 off
P0_6_set(); //Relay1 on
Init_Conv_Comp_flag = 1;
}
else if (result_sum >1638)
{
if (flag_condition1==1)
{
delay(1000);
flag_condition1=0;
flag_condition2=1;
}
//flag_condition2=1;
/*Relay2 on >1032 count*/
P0_6_reset(); //Relay1 off
P0_5_set(); //Relay2 on
Init_Conv_Comp_flag = 1;
} //end of else part
//Init_Conv_Comp_flag = 0;
} //end of con_Comp_flag
} //end of while
return 0;
} // end of main
void VADC0_C0_1_IRQHandler(void)
{
static int result_temp = 0;
ADC_Result=VADC->GLOBRES&0xFFF;
//result_sum += ADC_Result;
result_temp = ADC_Result+result_temp;
if(i==100)
{
result_sum = result_temp/100;
//result_sum = result_sum/100;
i=0;
result_temp=0;
Init_Conv_Comp_flag = 1;
Init_Conv_Comp_flag = 0;
}
else{i++;}
//Init_Conv_Comp_flag = 1;
// Clear offset calibration values
*SHS0_CALOC0 = 0x8000;
*SHS0_CALOC1 = 0x8000;
}
void VADC_Init()
{
// Disable clock gating to VADC
SCU_GENERAL->PASSWD = 0x000000C0U; // Disable bit protection
SET_BIT(SCU_CLK->CGATCLR0, SCU_CLK_CGATCLR0_VADC_Pos); // Disables clock gating for VADC kernel
while (SCU_CLK->CLKCR & (SCU_CLK_CLKCR_VDDC2LOW_Msk)); // Wait for the VDDC to stable
SCU_GENERAL->PASSWD = 0x000000C3UL; // Enable bit protection
//GLOBAL VADC INITIALIZATION
VADC->CLC = (0 << VADC_CLC_DISR_Pos & VADC_CLC_DISR_Msk) | // On request: enable the module clock
(0 << VADC_CLC_DISS_Pos & VADC_CLC_DISS_Msk) | // Module clock is enabled
(0 << VADC_CLC_EDIS_Pos & VADC_CLC_EDIS_Msk); // Sleep mode request is enabled and functional
while(VADC->CLC!=0); // wait for module clock enabled
// Converter ON
SHS0->SHSCFG = (0 << SHS_SHSCFG_ANOFF_Pos & SHS_SHSCFG_ANOFF_Msk)|SHS_SHSCFG_SCWC_Msk;
*(uint32_t *)0x40010500 = 0X0001; //Errata ADC_AI.003
// Wait until analog converter is on (SHSCFG.ANRDY = 1)
while (!(SHS0->SHSCFG & SHS_SHSCFG_ANRDY_Msk)) ;
delay(12); // Added ~15usec wait time for ADC power to stable
// Trigger Start Up calibration
ADC_SU_Calibration();
// Trigger gain calibration
ADC_PG_Calibration(); //do not modify DPCAL and SUCAL bitfields form GLOBCFG from here onwards to avoid wrong calibration configurations
// Background source configuration (include 1 channel -ch 7 group 0 w/o auto scan (continuous conversions).
VADC->BRSSEL[0] = (1 << VADC_BRSSEL_CHSELG7_Pos & VADC_BRSSEL_CHSELG7_Msk);
VADC->BRSMR = (1 << VADC_BRSMR_ENGT_Pos & VADC_BRSMR_ENGT_Msk)| //Enable gating always
(1 << VADC_BRSMR_SCAN_Pos & VADC_BRSMR_SCAN_Msk); //Enable autoscan
// Enable interrupt for global result register
VADC->GLOBRCR = (0x1 << VADC_GLOBRCR_SRGEN_Pos & VADC_GLOBRCR_SRGEN_Msk);
// Route event trigger to service request line (interrupt node)
VADC->GLOBEVNP = (0x1 << VADC_GLOBEVNP_REV0NP_Pos & VADC_GLOBEVNP_REV0NP_Msk);
// Enable nvic node for SR1
NVIC_EnableIRQ(VADC0_C0_1_IRQn);
}
// Startup calibration
void ADC_SU_Calibration (void)
{
uint16_t wait;
// Enable calibration
*SHS0_CALOC0 = 0x00;
*SHS0_CALOC1 = 0x00;
// Enable SU calibration ADC
VADC->GLOBCFG |= (1 << VADC_GLOBCFG_SUCAL_Pos & VADC_GLOBCFG_SUCAL_Msk)|
(1 << VADC_GLOBCFG_DPCAL0_Pos & VADC_GLOBCFG_DPCAL0_Msk);
// Wait for 1920cycles or 60us for the startup calibration to complete
wait = 20;
while(wait > 0)
{
wait--;
*SHS0_CALOC0 = 0x8000; // Clear offset calibration values
*SHS0_CALOC1 = 0x8000;
}
} // End of ADC_SU_Calibration
// Gain calibration routine
void ADC_PG_Calibration (void)
{
uint16_t i, j, adc_result_aux;
*(uint32_t *)0x480340BC = 0X3F100400; //Errata ADC_AI.004
VADC->BRSSEL[0] = (1 << VADC_BRSSEL_CHSELG0_Pos & VADC_BRSSEL_CHSELG0_Msk);//set G0_Ch0 for dummy conversions
// SUCAL=0, DPCAL0=0, DPCAL1=0 (DPCAL1 has to be kept to 0)
VADC->GLOBCFG |= 0x00;
*SHS0_CALOC0 = 0x8000; // Clear offset calibration values
*SHS0_CALOC1 = 0x8000;
// Trigger dummy conversions (9 x 2000 times)
for (i=0; i < 2000; i++)
{
for (j=0; j < 9; j++)
{
VADC->BRSMR |= (1 << VADC_BRSMR_ENGT_Pos & VADC_BRSMR_ENGT_Msk)| //Enable gating always
(0 << VADC_BRSMR_SCAN_Pos & VADC_BRSMR_SCAN_Msk)| //No autoscan-->only 1 conversion
(1 << VADC_BRSMR_LDEV_Pos & VADC_BRSMR_LDEV_Msk); //Generate a load event
// wait for valid result
while (!(VADC->GLOBRES & VADC_GLOBRES_VF_Msk));
//read result
adc_result_aux = (VADC->GLOBRES & VADC_GLOBRES_RESULT_Msk);
// Clear offset calibration values
*SHS0_CALOC0 = 0x8000;
*SHS0_CALOC1 = 0x8000;
}
}
// Redundant code to avoid warning
if(adc_result_aux == 0);
// Wait until last gain cal step is finished
while ( (SHS0->SHSCFG & SHS_SHSCFG_STATE_Msk) != 0 )
{
// Clear offset calibration values
*SHS0_CALOC0 = 0x8000;
*SHS0_CALOC1 = 0x8000;
}
VADC->GLOBCFG = 0x00; // Ensure Post-Calibration enable (SUCAL=0, DPCAL0=0, DPCAL1=0)
// Reset used register to reset value
VADC->GLOBRES = 0x00;
VADC->BRSMR = 0x00;
VADC->BRSSEL[0] = 0x00;
} // End of ADC_PG_Calibration
void delay (uint32_t delay)
{
while(delay > 0)
{
delay--;
}
} // End of delay
Not applicable
Nov 25, 2014
10:51 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Nov 25, 2014
10:51 PM
Hi Jackson
Kindly reply me to solve the issues
Kindly reply me to solve the issues
Not applicable
Nov 26, 2014
12:57 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Nov 26, 2014
12:57 AM
Hi prathibha,
You can stop the conversion by gating the ADC.
This can be done by writing 0 to BRSMR.ENGT register.
So you should gate the ADC when if(i==100){...} and enable the ADC again before end of if(flag_condition1==1){...}.
You can stop the conversion by gating the ADC.
This can be done by writing 0 to BRSMR.ENGT register.
So you should gate the ADC when if(i==100){...} and enable the ADC again before end of if(flag_condition1==1){...}.
Not applicable
Nov 26, 2014
01:26 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Nov 26, 2014
01:26 AM
Thank you Jackson
These are the changes i made please check.
#include //SFR declarations of the selected device
//#include
#include "GPIO.h"
/*******************************************************************************
** Macro Definitions **
*******************************************************************************/
#define CALIB00 0x480340E0
#define CALIB01 0x480340E4
/*******************************************************************************
** Private Variable Definitions **
*******************************************************************************/
uint32_t * SHS0_CALOC0 = (uint32_t *) CALIB00;
uint32_t * SHS0_CALOC1 = (uint32_t *) CALIB01;
/*******************************************************************************
** Function Declarations **
*******************************************************************************/
void delay (uint32_t delay);
void VADC_Init();
void ADC_SU_Calibration (void);
void ADC_PG_Calibration (void);
/*******************************************************************************
** Global Variable Definitions **
*******************************************************************************/
uint32_t ADC_Result[101]={0};
uint32_t i=0, j=0;
uint32_t result_sum=0;
uint32_t flag_condition1=0;
uint32_t flag_condition2=0;
uint32_t Init_Conv_Comp_flag = 0;
int main(void)
{
SCU_GENERAL->PASSWD = 0x000000C0UL; // disable bit protection
SCU_CLK->CLKCR = 0x3ff10200UL; // 32 MHz MCLK, 64 MHz PCLK
while((SCU_CLK->CLKCR & SCU_CLK_CLKCR_VDDC2LOW_Msk));
SCU_GENERAL->PASSWD = 0x000000C3UL; // enable bit protection
P0_5_set_mode(OUTPUT_PP_GP); // pin set up p0.5 for relay2
P0_6_set_mode(OUTPUT_PP_GP); //pin setup p0.6 for relay1
/* Relay1 on for power on condition*/
P0_5_set(); //Relay2 off
P0_6_reset(); //Relay1 on
delay(1000);
// Initialize ADC
VADC_Init();
// Generate load event to start conversion
VADC->BRSMR |= (1 << VADC_BRSMR_LDEV_Pos & VADC_BRSMR_LDEV_Msk);
while(1)
{
if(Init_Conv_Comp_flag == 1)
{
/*threshold voltage 2v*/
if(result_sum<=1638)
{
if (flag_condition2==1)
{
delay(1000);
flag_condition1=1;
flag_condition2=0;
}
//flag_condition1=1;
/*Relay1 on <1032 count*/
P0_5_reset(); //Relay2 off
P0_6_set(); //Relay1 on
//Init_Conv_Comp_flag = 1;
}
else if (result_sum >1638)
{
if (flag_condition1==1)
{
delay(1000);
flag_condition1=0;
flag_condition2=1;
}
//flag_condition2=1;
/*Relay2 on >1032 count*/
P0_6_reset(); //Relay1 off
P0_5_set(); //Relay2 on
//Init_Conv_Comp_flag = 0;
} //end of else part
WR_REG(VADC->BRSMR, VADC_BRSMR_ENGT_Msk, VADC_BRSMR_ENGT_Pos, 1); //gating enabled
//Init_Conv_Comp_flag = 0;
} //end of con_Comp_flag
} //end of while
return 0;
} // end of main
void VADC0_C0_1_IRQHandler(void)
{
static int result_temp = 0;
ADC_Result=VADC->GLOBRES&0xFFF;
//result_sum += ADC_Result;
result_temp = ADC_Result+result_temp;
if(i==100)
{
result_sum = result_temp/100;
//result_sum = result_sum/100;
i=0;
result_temp=0;
Init_Conv_Comp_flag = 1;
WR_REG(VADC->BRSMR, VADC_BRSMR_ENGT_Msk, VADC_BRSMR_ENGT_Pos, 0); // Diable gating
//Init_Conv_Comp_flag = 0;
}
else{i++;}
//Init_Conv_Comp_flag = 1;
// Clear offset calibration values
*SHS0_CALOC0 = 0x8000;
*SHS0_CALOC1 = 0x8000;
}
void VADC_Init()
{
// Disable clock gating to VADC
SCU_GENERAL->PASSWD = 0x000000C0U; // Disable bit protection
SET_BIT(SCU_CLK->CGATCLR0, SCU_CLK_CGATCLR0_VADC_Pos); // Disables clock gating for VADC kernel
while (SCU_CLK->CLKCR & (SCU_CLK_CLKCR_VDDC2LOW_Msk)); // Wait for the VDDC to stable
SCU_GENERAL->PASSWD = 0x000000C3UL; // Enable bit protection
//GLOBAL VADC INITIALIZATION
VADC->CLC = (0 << VADC_CLC_DISR_Pos & VADC_CLC_DISR_Msk) | // On request: enable the module clock
(0 << VADC_CLC_DISS_Pos & VADC_CLC_DISS_Msk) | // Module clock is enabled
(0 << VADC_CLC_EDIS_Pos & VADC_CLC_EDIS_Msk); // Sleep mode request is enabled and functional
while(VADC->CLC!=0); // wait for module clock enabled
// Converter ON
SHS0->SHSCFG = (0 << SHS_SHSCFG_ANOFF_Pos & SHS_SHSCFG_ANOFF_Msk)|SHS_SHSCFG_SCWC_Msk;
*(uint32_t *)0x40010500 = 0X0001; //Errata ADC_AI.003
// Wait until analog converter is on (SHSCFG.ANRDY = 1)
while (!(SHS0->SHSCFG & SHS_SHSCFG_ANRDY_Msk)) ;
delay(12); // Added ~15usec wait time for ADC power to stable
// Trigger Start Up calibration
ADC_SU_Calibration();
// Trigger gain calibration
ADC_PG_Calibration(); //do not modify DPCAL and SUCAL bitfields form GLOBCFG from here onwards to avoid wrong calibration configurations
// Background source configuration (include 1 channel -ch 7 group 0 w/o auto scan (continuous conversions).
VADC->BRSSEL[0] = (1 << VADC_BRSSEL_CHSELG7_Pos & VADC_BRSSEL_CHSELG7_Msk);
VADC->BRSMR = (1 << VADC_BRSMR_ENGT_Pos & VADC_BRSMR_ENGT_Msk)| //Enable gating always
(1 << VADC_BRSMR_SCAN_Pos & VADC_BRSMR_SCAN_Msk); //Enable autoscan
// Enable interrupt for global result register
VADC->GLOBRCR = (0x1 << VADC_GLOBRCR_SRGEN_Pos & VADC_GLOBRCR_SRGEN_Msk);
// Route event trigger to service request line (interrupt node)
VADC->GLOBEVNP = (0x1 << VADC_GLOBEVNP_REV0NP_Pos & VADC_GLOBEVNP_REV0NP_Msk);
// Enable nvic node for SR1
NVIC_EnableIRQ(VADC0_C0_1_IRQn);
}
// Startup calibration
void ADC_SU_Calibration (void)
{
uint16_t wait;
// Enable calibration
*SHS0_CALOC0 = 0x00;
*SHS0_CALOC1 = 0x00;
// Enable SU calibration ADC
VADC->GLOBCFG |= (1 << VADC_GLOBCFG_SUCAL_Pos & VADC_GLOBCFG_SUCAL_Msk)|
(1 << VADC_GLOBCFG_DPCAL0_Pos & VADC_GLOBCFG_DPCAL0_Msk);
// Wait for 1920cycles or 60us for the startup calibration to complete
wait = 20;
while(wait > 0)
{
wait--;
*SHS0_CALOC0 = 0x8000; // Clear offset calibration values
*SHS0_CALOC1 = 0x8000;
}
} // End of ADC_SU_Calibration
// Gain calibration routine
void ADC_PG_Calibration (void)
{
uint16_t i, j, adc_result_aux;
*(uint32_t *)0x480340BC = 0X3F100400; //Errata ADC_AI.004
VADC->BRSSEL[0] = (1 << VADC_BRSSEL_CHSELG0_Pos & VADC_BRSSEL_CHSELG0_Msk);//set G0_Ch0 for dummy conversions
// SUCAL=0, DPCAL0=0, DPCAL1=0 (DPCAL1 has to be kept to 0)
VADC->GLOBCFG |= 0x00;
*SHS0_CALOC0 = 0x8000; // Clear offset calibration values
*SHS0_CALOC1 = 0x8000;
// Trigger dummy conversions (9 x 2000 times)
for (i=0; i < 2000; i++)
{
for (j=0; j < 9; j++)
{
VADC->BRSMR |= (1 << VADC_BRSMR_ENGT_Pos & VADC_BRSMR_ENGT_Msk)| //Enable gating always
(0 << VADC_BRSMR_SCAN_Pos & VADC_BRSMR_SCAN_Msk)| //No autoscan-->only 1 conversion
(1 << VADC_BRSMR_LDEV_Pos & VADC_BRSMR_LDEV_Msk); //Generate a load event
// wait for valid result
while (!(VADC->GLOBRES & VADC_GLOBRES_VF_Msk));
//read result
adc_result_aux = (VADC->GLOBRES & VADC_GLOBRES_RESULT_Msk);
// Clear offset calibration values
*SHS0_CALOC0 = 0x8000;
*SHS0_CALOC1 = 0x8000;
}
}
// Redundant code to avoid warning
if(adc_result_aux == 0);
// Wait until last gain cal step is finished
while ( (SHS0->SHSCFG & SHS_SHSCFG_STATE_Msk) != 0 )
{
// Clear offset calibration values
*SHS0_CALOC0 = 0x8000;
*SHS0_CALOC1 = 0x8000;
}
VADC->GLOBCFG = 0x00; // Ensure Post-Calibration enable (SUCAL=0, DPCAL0=0, DPCAL1=0)
// Reset used register to reset value
VADC->GLOBRES = 0x00;
VADC->BRSMR = 0x00;
VADC->BRSSEL[0] = 0x00;
} // End of ADC_PG_Calibration
void delay (uint32_t delay)
{
while(delay > 0)
{
delay--;
}
} // End of delay
These are the changes i made please check.
#include
//#include
#include "GPIO.h"
/*******************************************************************************
** Macro Definitions **
*******************************************************************************/
#define CALIB00 0x480340E0
#define CALIB01 0x480340E4
/*******************************************************************************
** Private Variable Definitions **
*******************************************************************************/
uint32_t * SHS0_CALOC0 = (uint32_t *) CALIB00;
uint32_t * SHS0_CALOC1 = (uint32_t *) CALIB01;
/*******************************************************************************
** Function Declarations **
*******************************************************************************/
void delay (uint32_t delay);
void VADC_Init();
void ADC_SU_Calibration (void);
void ADC_PG_Calibration (void);
/*******************************************************************************
** Global Variable Definitions **
*******************************************************************************/
uint32_t ADC_Result[101]={0};
uint32_t i=0, j=0;
uint32_t result_sum=0;
uint32_t flag_condition1=0;
uint32_t flag_condition2=0;
uint32_t Init_Conv_Comp_flag = 0;
int main(void)
{
SCU_GENERAL->PASSWD = 0x000000C0UL; // disable bit protection
SCU_CLK->CLKCR = 0x3ff10200UL; // 32 MHz MCLK, 64 MHz PCLK
while((SCU_CLK->CLKCR & SCU_CLK_CLKCR_VDDC2LOW_Msk));
SCU_GENERAL->PASSWD = 0x000000C3UL; // enable bit protection
P0_5_set_mode(OUTPUT_PP_GP); // pin set up p0.5 for relay2
P0_6_set_mode(OUTPUT_PP_GP); //pin setup p0.6 for relay1
/* Relay1 on for power on condition*/
P0_5_set(); //Relay2 off
P0_6_reset(); //Relay1 on
delay(1000);
// Initialize ADC
VADC_Init();
// Generate load event to start conversion
VADC->BRSMR |= (1 << VADC_BRSMR_LDEV_Pos & VADC_BRSMR_LDEV_Msk);
while(1)
{
if(Init_Conv_Comp_flag == 1)
{
/*threshold voltage 2v*/
if(result_sum<=1638)
{
if (flag_condition2==1)
{
delay(1000);
flag_condition1=1;
flag_condition2=0;
}
//flag_condition1=1;
/*Relay1 on <1032 count*/
P0_5_reset(); //Relay2 off
P0_6_set(); //Relay1 on
//Init_Conv_Comp_flag = 1;
}
else if (result_sum >1638)
{
if (flag_condition1==1)
{
delay(1000);
flag_condition1=0;
flag_condition2=1;
}
//flag_condition2=1;
/*Relay2 on >1032 count*/
P0_6_reset(); //Relay1 off
P0_5_set(); //Relay2 on
//Init_Conv_Comp_flag = 0;
} //end of else part
WR_REG(VADC->BRSMR, VADC_BRSMR_ENGT_Msk, VADC_BRSMR_ENGT_Pos, 1); //gating enabled
//Init_Conv_Comp_flag = 0;
} //end of con_Comp_flag
} //end of while
return 0;
} // end of main
void VADC0_C0_1_IRQHandler(void)
{
static int result_temp = 0;
ADC_Result=VADC->GLOBRES&0xFFF;
//result_sum += ADC_Result;
result_temp = ADC_Result+result_temp;
if(i==100)
{
result_sum = result_temp/100;
//result_sum = result_sum/100;
i=0;
result_temp=0;
Init_Conv_Comp_flag = 1;
WR_REG(VADC->BRSMR, VADC_BRSMR_ENGT_Msk, VADC_BRSMR_ENGT_Pos, 0); // Diable gating
//Init_Conv_Comp_flag = 0;
}
else{i++;}
//Init_Conv_Comp_flag = 1;
// Clear offset calibration values
*SHS0_CALOC0 = 0x8000;
*SHS0_CALOC1 = 0x8000;
}
void VADC_Init()
{
// Disable clock gating to VADC
SCU_GENERAL->PASSWD = 0x000000C0U; // Disable bit protection
SET_BIT(SCU_CLK->CGATCLR0, SCU_CLK_CGATCLR0_VADC_Pos); // Disables clock gating for VADC kernel
while (SCU_CLK->CLKCR & (SCU_CLK_CLKCR_VDDC2LOW_Msk)); // Wait for the VDDC to stable
SCU_GENERAL->PASSWD = 0x000000C3UL; // Enable bit protection
//GLOBAL VADC INITIALIZATION
VADC->CLC = (0 << VADC_CLC_DISR_Pos & VADC_CLC_DISR_Msk) | // On request: enable the module clock
(0 << VADC_CLC_DISS_Pos & VADC_CLC_DISS_Msk) | // Module clock is enabled
(0 << VADC_CLC_EDIS_Pos & VADC_CLC_EDIS_Msk); // Sleep mode request is enabled and functional
while(VADC->CLC!=0); // wait for module clock enabled
// Converter ON
SHS0->SHSCFG = (0 << SHS_SHSCFG_ANOFF_Pos & SHS_SHSCFG_ANOFF_Msk)|SHS_SHSCFG_SCWC_Msk;
*(uint32_t *)0x40010500 = 0X0001; //Errata ADC_AI.003
// Wait until analog converter is on (SHSCFG.ANRDY = 1)
while (!(SHS0->SHSCFG & SHS_SHSCFG_ANRDY_Msk)) ;
delay(12); // Added ~15usec wait time for ADC power to stable
// Trigger Start Up calibration
ADC_SU_Calibration();
// Trigger gain calibration
ADC_PG_Calibration(); //do not modify DPCAL and SUCAL bitfields form GLOBCFG from here onwards to avoid wrong calibration configurations
// Background source configuration (include 1 channel -ch 7 group 0 w/o auto scan (continuous conversions).
VADC->BRSSEL[0] = (1 << VADC_BRSSEL_CHSELG7_Pos & VADC_BRSSEL_CHSELG7_Msk);
VADC->BRSMR = (1 << VADC_BRSMR_ENGT_Pos & VADC_BRSMR_ENGT_Msk)| //Enable gating always
(1 << VADC_BRSMR_SCAN_Pos & VADC_BRSMR_SCAN_Msk); //Enable autoscan
// Enable interrupt for global result register
VADC->GLOBRCR = (0x1 << VADC_GLOBRCR_SRGEN_Pos & VADC_GLOBRCR_SRGEN_Msk);
// Route event trigger to service request line (interrupt node)
VADC->GLOBEVNP = (0x1 << VADC_GLOBEVNP_REV0NP_Pos & VADC_GLOBEVNP_REV0NP_Msk);
// Enable nvic node for SR1
NVIC_EnableIRQ(VADC0_C0_1_IRQn);
}
// Startup calibration
void ADC_SU_Calibration (void)
{
uint16_t wait;
// Enable calibration
*SHS0_CALOC0 = 0x00;
*SHS0_CALOC1 = 0x00;
// Enable SU calibration ADC
VADC->GLOBCFG |= (1 << VADC_GLOBCFG_SUCAL_Pos & VADC_GLOBCFG_SUCAL_Msk)|
(1 << VADC_GLOBCFG_DPCAL0_Pos & VADC_GLOBCFG_DPCAL0_Msk);
// Wait for 1920cycles or 60us for the startup calibration to complete
wait = 20;
while(wait > 0)
{
wait--;
*SHS0_CALOC0 = 0x8000; // Clear offset calibration values
*SHS0_CALOC1 = 0x8000;
}
} // End of ADC_SU_Calibration
// Gain calibration routine
void ADC_PG_Calibration (void)
{
uint16_t i, j, adc_result_aux;
*(uint32_t *)0x480340BC = 0X3F100400; //Errata ADC_AI.004
VADC->BRSSEL[0] = (1 << VADC_BRSSEL_CHSELG0_Pos & VADC_BRSSEL_CHSELG0_Msk);//set G0_Ch0 for dummy conversions
// SUCAL=0, DPCAL0=0, DPCAL1=0 (DPCAL1 has to be kept to 0)
VADC->GLOBCFG |= 0x00;
*SHS0_CALOC0 = 0x8000; // Clear offset calibration values
*SHS0_CALOC1 = 0x8000;
// Trigger dummy conversions (9 x 2000 times)
for (i=0; i < 2000; i++)
{
for (j=0; j < 9; j++)
{
VADC->BRSMR |= (1 << VADC_BRSMR_ENGT_Pos & VADC_BRSMR_ENGT_Msk)| //Enable gating always
(0 << VADC_BRSMR_SCAN_Pos & VADC_BRSMR_SCAN_Msk)| //No autoscan-->only 1 conversion
(1 << VADC_BRSMR_LDEV_Pos & VADC_BRSMR_LDEV_Msk); //Generate a load event
// wait for valid result
while (!(VADC->GLOBRES & VADC_GLOBRES_VF_Msk));
//read result
adc_result_aux = (VADC->GLOBRES & VADC_GLOBRES_RESULT_Msk);
// Clear offset calibration values
*SHS0_CALOC0 = 0x8000;
*SHS0_CALOC1 = 0x8000;
}
}
// Redundant code to avoid warning
if(adc_result_aux == 0);
// Wait until last gain cal step is finished
while ( (SHS0->SHSCFG & SHS_SHSCFG_STATE_Msk) != 0 )
{
// Clear offset calibration values
*SHS0_CALOC0 = 0x8000;
*SHS0_CALOC1 = 0x8000;
}
VADC->GLOBCFG = 0x00; // Ensure Post-Calibration enable (SUCAL=0, DPCAL0=0, DPCAL1=0)
// Reset used register to reset value
VADC->GLOBRES = 0x00;
VADC->BRSMR = 0x00;
VADC->BRSSEL[0] = 0x00;
} // End of ADC_PG_Calibration
void delay (uint32_t delay)
{
while(delay > 0)
{
delay--;
}
} // End of delay
Attachments are accessible only for community members.
Not applicable
Nov 26, 2014
03:21 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Nov 26, 2014
03:21 AM
Hi Jackson,
I have checked with the code mentioned above enable and disable of gating .By doing this Based on count Relays are changing if ADC_Result=40 relay1 is on.
When i sum Values of ADC_Result i am getting manual calculations as 4012 but sum of result_temp variable is showing 2552(result_temp = ADC_Result+result_temp)
result_sum variable=41(result_sum = result_temp/100),and the value of i is 61.How i can get the proper values of i,result_temp,result_sum variables
Please find the attachment of ADC_Result from debug window.This is the system behavior .Kindly guide me
I have checked with the code mentioned above enable and disable of gating .By doing this Based on count Relays are changing if ADC_Result=40 relay1 is on.
When i sum Values of ADC_Result i am getting manual calculations as 4012 but sum of result_temp variable is showing 2552(result_temp = ADC_Result+result_temp)
result_sum variable=41(result_sum = result_temp/100),and the value of i is 61.How i can get the proper values of i,result_temp,result_sum variables
Please find the attachment of ADC_Result from debug window.This is the system behavior .Kindly guide me
Not applicable
Nov 26, 2014
11:10 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Nov 26, 2014
11:10 PM
Hi prathibha,
Please place you breakpoint in if(1==100){...} and you will see the correct result.
Please place you breakpoint in if(1==100){...} and you will see the correct result.
Attachments are accessible only for community members.
Not applicable
Nov 30, 2014
09:25 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Nov 30, 2014
09:25 AM