// ---------------------------------------------------------------------------------------- // Start ADC conversion // ---------------------------------------------------------------------------------------- void GetBattery(void) { // Clear the ADC INT0 interrupt flag EADC_CLR_INT_FLAG(EADC, 0x2); //Trigger sample module 0 to start A/D conversion EADC_START_CONV(EADC, 0x2); //Wait ADC interrupt (g_u32AdcIntFlag will be set at IRQ_Handler function) while(EADC_GET_INT_FLAG(EADC, 0x2) == 0); //Trigger sample module 0 to start A/D conversion BatteryData = ((EADC_GET_CONV_DATA(EADC, 1))*100/4096); /* Update TID value */ BatDev.Input.data1.value = BatteryData; if(BatDev.Input.data1.value < BatDev.Feature.data2.value) { BatDev.Input.data2.value = 1; BatOverTimeCounter = getTickCount()+ BatDev.Feature.data3.value*1000; } /* reset alerm flag after 10s */ if(BatDev.Input.data2.value == 1) { if(getTickCount() > BatOverTimeCounter) BatDev.Input.data2.value = 0; if(BatDev.Output.data1.value == 1) BatDev.Input.data2.value = 0; } }
void GetID() { static uint16_t u16Range[][2] = { { 125, 310}, { 373, 558}, { 621, 806}, { 869, 1055}, {1118, 1303}, {1366, 1551}, {1616, 1799}, {1861, 2048}, {2111, 2296}, {2359, 2544}, {2607, 2792}, {2855, 3040}, {3103, 3289}, {3351, 3537}, {3600, 3785} }; uint16_t adcData; uint32_t i; // Clear the ADC INT0 interrupt flag EADC_CLR_INT_FLAG(EADC, 0x1); // Clear the ADC INT0 interrupt flag EADC_START_CONV(EADC, 0x1); //Trigger sample module 0 to start A/D conversion while(EADC_GET_INT_FLAG(EADC, 0x1) == 0); //Wait ADC interrupt (g_u32AdcIntFlag will be set at IRQ_Handler function) adcData = EADC_GET_CONV_DATA(EADC, 0); //check module ID for(i=0; i<(sizeof(u16Range)/sizeof(uint16_t)/2); i++) { if (adcData > u16Range[i][0] && adcData < u16Range[i][1]) { devNum = i; break; } } if (i >= (sizeof(u16Range)/sizeof(uint16_t)/2)) devNum = 100; }
//============================================================================= //----- (0000184C) -------------------------------------------------------- __myevic__ uint32_t ADC_Read( uint32_t module ) { EADC_Open( EADC, EADC_CTL_DIFFEN_SINGLE_END ); EADC_SetInternalSampleTime( EADC, 6 ); // 0.67 us EADC_ConfigSampleModule( EADC, module, EADC_SOFTWARE_TRIGGER, module ); EADC_CLR_INT_FLAG( EADC, 1 << 0 ); EADC_ENABLE_INT( EADC, 1 << 0 ); EADC_ENABLE_SAMPLE_MODULE_INT( EADC, 0, 1 << module ); NVIC_EnableIRQ( ADC00_IRQn ); ADC00_IRQ_Flag = 0; EADC_START_CONV( EADC, 1 << module ); while ( !ADC00_IRQ_Flag ) ; EADC_DISABLE_INT( EADC, 1 << 0 ); return EADC_GET_CONV_DATA( EADC, module ); }
void UpdateBattery() { EADC_START_CONV(EADC, 0x1); AdcData = EADC_GET_CONV_DATA(EADC, 0); }
/*---------------------------------------------------------------------------------------------------------*/ void EADC_FunctionTest() { uint8_t u8Option, u32SAMPLECount = 0; int32_t i32ConversionData[8] = {0}; printf("\n"); printf("+----------------------------------------------------------------------+\n"); printf("| ADINT trigger mode test |\n"); printf("+----------------------------------------------------------------------+\n"); printf("\nIn this test, software will get 2 cycles of conversion result from the specified channels.\n"); while(1) { printf("\n\nSelect input mode:\n"); printf(" [1] Single end input (channel 0, 1, 2 and 3)\n"); printf(" [2] Differential input (input channel pair 0 and 1)\n"); printf(" Other keys: exit continuous scan mode test\n"); u8Option = getchar(); if(u8Option == '1') { /* Set the ADC internal sampling time, input mode as single-end and enable the A/D converter */ EADC_Open(EADC, EADC_CTL_DIFFEN_SINGLE_END); EADC_SetInternalSampleTime(EADC, 6); /* Configure the sample 4 module for analog input channel 0 and enable ADINT0 trigger source */ EADC_ConfigSampleModule(EADC, 4, EADC_ADINT0_TRIGGER, 0); /* Configure the sample 5 module for analog input channel 1 and enable ADINT0 trigger source */ EADC_ConfigSampleModule(EADC, 5, EADC_ADINT0_TRIGGER, 1); /* Configure the sample 6 module for analog input channel 2 and enable ADINT0 trigger source */ EADC_ConfigSampleModule(EADC, 6, EADC_ADINT0_TRIGGER, 2); /* Configure the sample 7 module for analog input channel 3 and enable ADINT0 trigger source */ EADC_ConfigSampleModule(EADC, 7, EADC_ADINT0_TRIGGER, 3); /* Clear the A/D ADINT0 interrupt flag for safe */ EADC_CLR_INT_FLAG(EADC, 0x1); /* Enable the sample module 7 interrupt */ EADC_ENABLE_INT(EADC, 0x1);//Enable sample module A/D ADINT0 interrupt. EADC_ENABLE_SAMPLE_MODULE_INT(EADC, 0, (0x1 << 7));//Enable sample module 7 interrupt. NVIC_EnableIRQ(ADC00_IRQn); /* Reset the ADC indicator and trigger sample module 7 to start A/D conversion */ g_u32AdcIntFlag = 0; g_u32COVNUMFlag = 0; EADC_START_CONV(EADC, (0x1 << 7)); /* Wait EADC interrupt (g_u32AdcIntFlag will be set at IRQ_Handler function) */ while(g_u32AdcIntFlag == 0); /* Reset the EADC interrupt indicator */ g_u32AdcIntFlag = 0; /* Wait EADC interrupt (g_u32AdcIntFlag will be set at IRQ_Handler function) */ while(g_u32AdcIntFlag == 0); /* Reset the EADC interrupt indicator */ g_u32AdcIntFlag = 0; /* Disable the sample module 7 interrupt */ EADC_DISABLE_SAMPLE_MODULE_INT(EADC, 0, (0x1 << 7)); /* Get the conversion result of the sample module */ for(u32SAMPLECount = 0; u32SAMPLECount < 4; u32SAMPLECount++) i32ConversionData[u32SAMPLECount] = EADC_GET_CONV_DATA(EADC, (u32SAMPLECount + 4)); /* Wait conversion done */ while(EADC_GET_DATA_VALID_FLAG(EADC, 0xF0) != 0xF0); /* Get the conversion result of the sample module */ for(u32SAMPLECount = 4; u32SAMPLECount < 8; u32SAMPLECount++) i32ConversionData[u32SAMPLECount] = EADC_GET_CONV_DATA(EADC, u32SAMPLECount); for(g_u32COVNUMFlag = 0; (g_u32COVNUMFlag) < 8; g_u32COVNUMFlag++) printf("Conversion result of channel %d: 0x%X (%d)\n", (g_u32COVNUMFlag % 4), i32ConversionData[g_u32COVNUMFlag], i32ConversionData[g_u32COVNUMFlag]); } else if(u8Option == '2') { /* Set the ADC internal sampling time, input mode as differential and enable the A/D converter */ EADC_Open(EADC, EADC_CTL_DIFFEN_DIFFERENTIAL); EADC_SetInternalSampleTime(EADC, 6); /* Configure the sample module 4 for analog input channel 0 and enable ADINT0 trigger source */ EADC_ConfigSampleModule(EADC, 4, EADC_ADINT0_TRIGGER, 0); /* Configure the sample module 5 for analog input channel 1 and enable ADINT0 trigger source */ EADC_ConfigSampleModule(EADC, 5, EADC_ADINT0_TRIGGER, 1); /* Configure the sample module 6 for analog input channel 2 and enable ADINT0 trigger source */ EADC_ConfigSampleModule(EADC, 6, EADC_ADINT0_TRIGGER, 2); /* Configure the sample module 7 for analog input channel 3 and enable ADINT0 trigger source */ EADC_ConfigSampleModule(EADC, 7, EADC_ADINT0_TRIGGER, 3); /* Clear the A/D ADINT0 interrupt flag for safe */ EADC_CLR_INT_FLAG(EADC, 0x1); /* Enable the sample module 7 interrupt */ EADC_ENABLE_INT(EADC, 0x1);//Enable sample module A/D ADINT0 interrupt. EADC_ENABLE_SAMPLE_MODULE_INT(EADC, 0, (0x1 << 7));//Enable sample module 7 interrupt. NVIC_EnableIRQ(ADC00_IRQn); /* Reset the ADC indicator and trigger sample module 7 to start A/D conversion */ g_u32AdcIntFlag = 0; g_u32COVNUMFlag = 0; EADC_START_CONV(EADC, (0x1 << 7)); /* Wait EADC interrupt (g_u32AdcIntFlag will be set at IRQ_Handler function) */ while(g_u32AdcIntFlag == 0); /* Reset the EADC interrupt indicator */ g_u32AdcIntFlag = 0; /* Wait EADC interrupt (g_u32AdcIntFlag will be set at IRQ_Handler function) */ while(g_u32AdcIntFlag == 0); /* Reset the EADC interrupt indicator */ g_u32AdcIntFlag = 0; /* Disable the sample module 7 interrupt */ EADC_DISABLE_SAMPLE_MODULE_INT(EADC, 0, (0x1 << 7)); /* Get the conversion result of the sample module */ for(u32SAMPLECount = 0; u32SAMPLECount < 4; u32SAMPLECount++) i32ConversionData[u32SAMPLECount] = EADC_GET_CONV_DATA(EADC, (u32SAMPLECount + 4)); /* Wait conversion done */ while(EADC_GET_DATA_VALID_FLAG(EADC, 0xF0) != 0xF0); /* Get the conversion result of the sample module */ for(u32SAMPLECount = 4; u32SAMPLECount < 8; u32SAMPLECount++) i32ConversionData[u32SAMPLECount] = EADC_GET_CONV_DATA(EADC, u32SAMPLECount); for(g_u32COVNUMFlag = 0; (g_u32COVNUMFlag) < 8; g_u32COVNUMFlag++) printf("Conversion result of channel %d: 0x%X (%d)\n", (g_u32COVNUMFlag % 4), i32ConversionData[g_u32COVNUMFlag], i32ConversionData[g_u32COVNUMFlag]); } else return ; } }