int32_t main ( void ) { System_Init(); _ADC_START_CONVERT(); // Start ADC convert while ( 1 ) { P01 = 0; P00 = 0; maintask(); UartFramePro ( &UartRcvFrame ); I2cFramePro(); } }
/*---------------------------------------------------------------------------------------------------------*/ void AdcResultMonitorTest() { printf("\n=== ADC compare function test ===\n"); printf("\nIn this test, software will compare the conversion result of channel 2.\n"); /* Set the ADC operation mode as continuous scan, input mode as single-end and enable the ADC converter */ ADC->ADCR = (ADC_ADCR_ADMD_CONTINUOUS | ADC_ADCR_DIFFEN_SINGLE_END | ADC_ADCR_ADEN_Msk); /* Set the ADC channel 2 */ _ADC_ENABLE_CHANNEL(2); /* Enable ADC comparator 0. Compare condition: conversion result < 0x800; match Count=5. */ printf(" Set the compare condition of comparator 0: channel 2 is less than 0x800; match count is 5.\n"); _ADC_SET_CMP0(2, ADC_LESS_THAN, 0x800, 5); _ADC_ENABLE_CMP(0); /* Enable ADC comparator 1. Compare condition: conversion result >= 0x800; match Count=5. */ printf(" Set the compare condition of comparator 1 : channel 2 is greater than or equal to 0x800; match count is 5.\n"); _ADC_SET_CMP1(2, ADC_GREATER_OR_EQUAL, 0x800, 5); _ADC_ENABLE_CMP(1); /* clear the ADC comparator 0 interrupt flag for safe */ _ADC_CLEAR_CMP0_INT_FLAG(); /* enable ADC comparator 0 interrupt */ _ADC_ENABLE_CMP_INT(0); /* clear the ADC comparator 1 interrupt flag for safe */ _ADC_CLEAR_CMP1_INT_FLAG(); /* enable ADC comparator 1 interrupt */ _ADC_ENABLE_CMP_INT(1); NVIC_EnableIRQ(ADC_IRQn); g_u32AdcCmp0IntFlag = 0; g_u32AdcCmp1IntFlag = 0; /* Clear the ADC interrupt flag */ ADC->ADSR = ADC_ADSR_ADF_Msk; /* start A/D conversion */ _ADC_START_CONVERT(); /* Wait ADC compare interrupt */ while((g_u32AdcCmp0IntFlag==0)&&(g_u32AdcCmp1IntFlag==0)); /* Stop A/D conversion */ _ADC_STOP_CONVERT(); /* Disable ADC comparator interrupt */ _ADC_DISABLE_CMP_INT(0); _ADC_DISABLE_CMP_INT(1); /* Disable compare function */ _ADC_DISABLE_CMP(0); _ADC_DISABLE_CMP(1); if(g_u32AdcCmp0IntFlag==1) { printf("Comparator 0 interrupt occurs.\nThe conversion result of channel 2 is less than 0x800\n"); } else { printf("Comparator 1 interrupt occurs.\nThe conversion result of channel 2 is greater than or equal to 0x800\n"); } }
/*---------------------------------------------------------------------------------------------------------*/ void AdcContScanModeTest() { uint8_t u8Option; uint32_t u32ChannelCount; int32_t i32ConversionData; printf("\n=== Continuous scan mode test ===\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 = uart_getchar(); if(u8Option=='1') { /* Set the ADC operation mode as continous scan, input mode as single-end and enable the ADC converter */ ADC->ADCR = (ADC_ADCR_ADMD_CONTINUOUS | ADC_ADCR_DIFFEN_SINGLE_END | ADC_ADCR_ADEN_CONVERTER_ENABLE); /* Enable analog input channel 0, 1, 2 and 3 */ _ADC_SET_CHANNEL(0xF); /* clear the A/D interrupt flag for safe */ ADC->ADSR = ADC_ADSR_ADF_Msk; /* start A/D conversion */ _ADC_START_CONVERT(); /* Wait conversion done */ _ADC_WAIT_COVERSION_DONE(); /* Clear the ADC interrupt flag */ ADC->ADSR = ADC_ADSR_ADF_Msk; for(u32ChannelCount=0; u32ChannelCount<4; u32ChannelCount++) { i32ConversionData = _ADC_GET_CONVERSION_DATA(u32ChannelCount); printf("Conversion result of channel %d: 0x%X (%d)\n", u32ChannelCount, i32ConversionData, i32ConversionData); } /* Wait conversion done */ _ADC_WAIT_COVERSION_DONE(); /* Stop A/D conversion */ _ADC_STOP_CONVERT(); for(u32ChannelCount=0; u32ChannelCount<4; u32ChannelCount++) { i32ConversionData = _ADC_GET_CONVERSION_DATA(u32ChannelCount); printf("Conversion result of channel %d: 0x%X (%d)\n", u32ChannelCount, i32ConversionData, i32ConversionData); } /* Clear the ADC interrupt flag */ ADC->ADSR = ADC_ADSR_ADF_Msk; } else if(u8Option=='2') { /* Set the ADC operation mode as continous scan, input mode as differential and enable the ADC converter */ ADC->ADCR = (ADC_ADCR_ADMD_CONTINUOUS | ADC_ADCR_DIFFEN_DIFFERENTIAL | ADC_ADCR_ADEN_CONVERTER_ENABLE); /* Enable analog input channel 0 and 2 */ _ADC_SET_CHANNEL(0x5); /* clear the A/D interrupt flag for safe */ ADC->ADSR = ADC_ADSR_ADF_Msk; /* start A/D conversion */ _ADC_START_CONVERT(); /* Wait conversion done */ _ADC_WAIT_COVERSION_DONE(); /* Clear the ADC interrupt flag */ ADC->ADSR = ADC_ADSR_ADF_Msk; for(u32ChannelCount=0; u32ChannelCount<2; u32ChannelCount++) { i32ConversionData = _ADC_GET_CONVERSION_DATA(u32ChannelCount*2); printf("Conversion result of differential input pair %d: 0x%X (%d)\n", u32ChannelCount, i32ConversionData, i32ConversionData); } /* Wait conversion done */ _ADC_WAIT_COVERSION_DONE(); /* Stop A/D conversion */ _ADC_STOP_CONVERT(); for(u32ChannelCount=0; u32ChannelCount<2; u32ChannelCount++) { i32ConversionData = _ADC_GET_CONVERSION_DATA(u32ChannelCount*2); printf("Conversion result of differential input pair %d: 0x%X (%d)\n", u32ChannelCount, i32ConversionData, i32ConversionData); } /* Clear the ADC interrupt flag */ ADC->ADSR = ADC_ADSR_ADF_Msk; } else return ; } }
/*---------------------------------------------------------------------------------------------------------*/ void AdcSingleModeTest() { uint8_t u8Option; int32_t i32ConversionData; printf("\n=== Single mode test ===\n"); while(1) { printf("Select input mode:\n"); printf(" [1] Single end input (channel 2 only)\n"); printf(" [2] Differential input (channel pair 1 only)\n"); printf(" Other keys: exit single mode test\n"); u8Option = uart_getchar(); if(u8Option=='1') { /* Set the ADC operation mode as single, input mode as single-end and enable the ADC converter */ ADC->ADCR = (ADC_ADCR_ADMD_SINGLE | ADC_ADCR_DIFFEN_SINGLE_END | ADC_ADCR_ADEN_CONVERTER_ENABLE); /* Enable analog input channel 2 */ _ADC_SET_CHANNEL(1<<2); /* clear the A/D interrupt flag for safe */ ADC->ADSR = ADC_ADSR_ADF_Msk; /* Enable the ADC interrupt */ _ADC_ENABLE_ADC_INT(); NVIC_EnableIRQ(ADC_IRQn); /* Reset the ADC interrupt indicator and Start A/D conversion */ g_u32AdcIntFlag = 0; _ADC_START_CONVERT(); /* Wait ADC interrupt (g_u32AdcIntFlag will be set at IRQ_Handler Fuction)*/ while(g_u32AdcIntFlag==0); /* Disable the ADC interrupt */ _ADC_DISABLE_ADC_INT(); /* Get the conversion result of the ADC channel 2 */ i32ConversionData = _ADC_GET_CONVERSION_DATA(2); printf("Conversion result of channel 2: 0x%X (%d)\n\n", i32ConversionData, i32ConversionData); } else if(u8Option=='2') { /* Set the ADC operation mode as single, input mode as differential and enable the ADC converter */ ADC->ADCR = (ADC_ADCR_ADMD_SINGLE | ADC_ADCR_DIFFEN_DIFFERENTIAL | ADC_ADCR_ADEN_CONVERTER_ENABLE); /* Enable analog input channel 2 for differential input channel pair 1 */ _ADC_SET_CHANNEL(1<<2); /* clear the A/D interrupt flag for safe */ ADC->ADSR = ADC_ADSR_ADF_Msk; /* Enable the ADC interrupt */ _ADC_ENABLE_ADC_INT(); NVIC_EnableIRQ(ADC_IRQn); /* Reset the ADC interrupt indicator and Start A/D conversion */ g_u32AdcIntFlag = 0; _ADC_START_CONVERT(); /* Wait ADC interrupt (g_u32AdcIntFlag will be set at IRQ_Handler Fuction)*/ while(g_u32AdcIntFlag==0); /* Disable the ADC interrupt */ _ADC_DISABLE_ADC_INT(); /* Get the conversion result of the specified ADC channel */ i32ConversionData = _ADC_GET_CONVERSION_DATA(2); printf("Conversion result of channel pair 1: 0x%X (%d)\n\n", i32ConversionData, i32ConversionData); } else return ; } }