int main(void) { RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA, GPIO_PinSource2,GPIO_AF_1); GPIO_PinAFConfig(GPIOA, GPIO_PinSource3,GPIO_AF_1); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStructure); ADC_InitStructure.ADC_Resolution=ADC_Resolution_12b; ADC_InitStructure.ADC_ContinuousConvMode=ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge=ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_ExternalTrigConv=DISABLE; ADC_InitStructure.ADC_DataAlign=ADC_DataAlign_Right; ADC_InitStructure.ADC_ScanDirection=ADC_ScanDirection_Upward; ADC_Init(ADC1, &ADC_InitStructure); ADC_DMACmd(ADC1, ENABLE); ADC_Cmd(ADC1, ENABLE); ADC_ChannelConfig(ADC1, ADC_Channel_0, ADC_SampleTime_239_5Cycles); ADC_StartOfConversion(ADC1); USART_InitStructure.USART_BaudRate=9600; USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode=USART_Mode_Tx|USART_Mode_Rx; USART_InitStructure.USART_Parity=USART_Parity_No; USART_InitStructure.USART_StopBits=USART_StopBits_1; USART_InitStructure.USART_WordLength=USART_WordLength_8b; USART_Init(USART2, &USART_InitStructure); DMA_InitStructure.DMA_PeripheralBaseAddr=0x40012440; DMA_InitStructure.DMA_MemoryBaseAddr=(uint32_t)&adcdata; DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize=DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_PeripheralInc=DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc=DMA_MemoryInc_Disable; 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_M2M=DMA_M2M_Disable; DMA_Init(DMA1_Channel1, &DMA_InitStructure); DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE); DMA_Cmd(DMA1_Channel1, ENABLE); USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); USART_ClearITPendingBit(USART2, USART_IT_RXNE); USART_ClearFlag(USART2,USART_FLAG_TC); USART_Cmd(USART2, ENABLE); NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPriority=0; NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; NVIC_Init(&NVIC_InitStructure); NVIC_InitStructure.NVIC_IRQChannel=DMA1_Channel1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPriority=1; NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; NVIC_Init(&NVIC_InitStructure); while (1) { voltage=adcdata*3.3/4096; printf("\r\nV=%f\r\n",voltage); delay();delay();delay();delay();delay();delay();delay(); delay();delay();delay();delay();delay();delay();delay(); delay();delay();delay();delay();delay();delay();delay(); delay();delay();delay();delay();delay();delay();delay(); delay();delay();delay();delay();delay();delay();delay(); delay();delay();delay();delay();delay();delay();delay(); delay();delay();delay();delay();delay();delay();delay(); delay();delay();delay();delay();delay();delay();delay(); } } |
ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular);
这个函数配置的是ADCx的CFGR1寄存器。
而DMA初始化中的连续模式配置的是配置的是DMA的CCR寄存器,DMA_CCR_CIRC ((uint32_t)0x00000020)
没贴出来中断函数,中断中清理标志位了。
void DMA1_Channel1_IRQHandler(void)
{
if(DMA_GetFlagStatus(DMA1_FLAG_TC1)==1)
{
DMA_ClearFlag(DMA1_FLAG_TC1);
ADC_ClearFlag(ADC1, ADC_FLAG_EOC);
}
}