volatile UINT8 nSendFlag = 0xFF;
#define ADC_BUFFER_LEN_HALF 256
UINT16 g_ADC_Buffer[ADC_BUFFER_LEN_HALF] = {0};
UINT16 g_ADC_Buffer_2[ADC_BUFFER_LEN_HALF] = {0};
void ADC1_DMA_Config()
{
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
DMA_DeInit(DMA2_Stream0);
while(DMA_GetCmdStatus(DMA2_Stream0) != DISABLE){}
// DMA SET
DMA_InitStructure.DMA_Channel = DMA_Channel_0;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR,
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)g_ADC_Buffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = ADC_BUFFER_LEN;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_DoubleBufferModeConfig(DMA2_Stream0,(uint32_t)&g_ADC_Buffer_2,DMA_Memory_0);//DMA_Memory_0?????
DMA_DoubleBufferModeCmd(DMA2_Stream0,ENABLE);
DMA_Init(DMA2_Stream0, &DMA_InitStructure);
// NVIC
//DMA_ClearITPendingBit(DMA2_Stream0,DMA_IT_HTIF0);
//DMA_ClearITPendingBit(DMA2_Stream0,DMA_IT_TCIF0);
//DMA_ITConfig(DMA2_Stream0,DMA_IT_TC,ENABLE);
//DMA_ITConfig(DMA2_Stream0,DMA_IT_HT,ENABLE);
NVIC_InitStructure.NVIC_IRQChannel=DMA2_Stream0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x02;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x02;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
DMA_ClearITPendingBit(DMA2_Stream0,DMA_IT_TCIF0);
DMA_ITConfig(DMA2_Stream0,DMA_IT_TC,ENABLE);
//while (DMA_GetCmdStatus(DMA2_Stream0) != DISABLE){}
DMA_Cmd(DMA2_Stream0, ENABLE);
}
void ADC1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
ADC_InitTypeDef ADC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
// PA5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;//GPIO_Pin_5, PA5, PA6
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//ADC1
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1, ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1, DISABLE);
// Common Set
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1;
// ADC Set
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//ADC_DataAlign_Left;//ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_Cmd(ADC1, ENABLE);
ADC1_DMA_Config();
ADC_DMACmd(ADC1, ENABLE);
ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 1, ADC_SampleTime_15Cycles); //ADC_SampleTime_3Cycles
ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
}
void DMA2_Stream0_IRQHandler(void)
{
if (DMA_GetITStatus(DMA2_Stream0, DMA_IT_TCIF0) == SET)
{
DMA_ClearITPendingBit(DMA2_Stream0, DMA_IT_TCIF0);
if(DMA_GetCurrentMemoryTarget(DMA2_Stream0) == 1) //
{
nSendFlag = 1;
}else{
nSendFlag = 2;
}
}
}
void Net_Send(....)
{
.....
}
void Send_ADC_Data(void){
if(ADC_Status.nSFlag == 1){
Net_Send(CMD_DATA_TEST_1 ADC_Status.nID, g_ADC_Buffer);
ADC_Status.nSFlag = 0xFF;
memset(g_ADC_Buffer, 0, ADC_BUFFER_LEN_HALF);
}else if(ADC_Status.nSFlag == 2){{
Net_Send(CMD_DATA_TEST_2, ADC_Status.nID, g_ADC_Buffer_2);
ADC_Status.nSFlag = 0xFF;
memset(g_ADC_Buffer_2, 0, ADC_BUFFER_LEN_HALF);
}
}
int main(void)
{
........
ADC1_Init();
......
ADC_SoftwareStartConv(ADC1);
while(1)
{
Send_ADC_Data();
}
}