Hi Adrian, Thanks a lot for your detailed answer. It was extremely helpful. I have better understanding about the registers now. Though I still have more questions about the overall workflow of the Analog to Digital Converter.
I extracted a few lines of code here.
Question 1:
How did they define those channels. From the reference manual, I found the channel address goes as things like "4003_B000". But from the source code, where do those "0x0d|0x40" come from?
Question 2:
Can you explain how the main function works? I don't quite understand the difference between "m_bDMA2Flag" and "m_bDMA0Flag"
Question 3:
Can you explain what method "StartADCScan" does? I don't quite understand how DMA_DCR2 |= DMA_DCR_START_MASK; does any scanning...
I didn't include the implementation of
void Dma0_init(void);
void Dma2_init(void);
void DMA0_IRQHandler(void);
void DMA2_IRQHandler(void);
void StartADCScan( void );
But if you need them I will include them. Thanks again!
#include "common.h"
#include "adc.h"
#ifdef CMSIS
#include "start.h"
#endif
#define DMA_REQUEST_SOURCE_ADC0 40
#define DMA_REQUEST_SOURCE_ALWAYS 60
uint8_t m_uiADCChannelBuff[6] = {
0x0d|0x40, //channel 0x0d -- external channel 13
0x1d|0x40, //channel 0x0d -- Vrefh
0x1e|0x40, //channel 0x1e -- Vrefl
0x1d|0x40, //channel 0x1d -- Vrefh
0x1a|0x40, //channel 0x1a -- temperature sensor
0x1f|0x40 //channel disable,last time,disable ADC channel to avoid to generate new ADC interrupt.
};
uint16_t m_uiADCResultBuff[8] = {0,0,0,0,0,0,0};
uint8_t m_bDMA0Flag;
uint8_t m_bDMA2Flag;
void Dma0_init(void);
void Dma2_init(void);
void DMA0_IRQHandler(void);
void DMA2_IRQHandler(void);
void StartADCScan( void );
/********************************************************************/
int main (void)
{
uint8_t i;
uint32_t uiSum;
printf("\nRunning the AdcDemoWithDMA project.\n");
InitADC();
if( ADC_Cal(ADC0_BASE_PTR) )
{
printf("ADC calibration fail!\n");
}
Dma0_init();
Dma2_init();
// disable ADC module
ADC0_SC1A = 0x1f;
// enable DMA transfer for ADC module
ADC0_SC2 |= ADC_SC2_DMAEN_MASK;
printf("input char 's', trigger new ADC scan with DMA...\n");
while(1)
{
if( m_bDMA0Flag )
{
m_bDMA0Flag = 0;
printf("ADC conversion success!\n");
uiSum = 0;
for(i=0;i<5;i++)
{
printf("0x%x,",m_uiADCResultBuff[i]);
uiSum += m_uiADCResultBuff[i];
}
printf("\n");
// printf("new value for input voltage is: 0x%x\n",uiSum);
}
if( m_bDMA2Flag )
{
m_bDMA2Flag = 0;
//printf("DMA channel 2 transfer completed!\n");
}
if( UART0_S1 & UART0_S1_RDRF_MASK )
{
if( 's' == UART0_D )
{
StartADCScan();
}
}
}
}
void StartADCScan( void )
{
// write start to DMA channel to start DMA transfer, so that write channel data to ADC_SCA
DMA_DCR2 |= DMA_DCR_START_MASK;
}