源码:
main():
TIM3_PWM_Init(2500-1,108000-1); //108M/108=1MμļÆêyÆμÂ꣬×Ô¶ˉÖØ×°ÔØÎa500£¬ÄÇÃ′PWMÆμÂêÎa1M/500=2kHZ
MYDMA_Config();
MY_ADC_Init(); //3õê¼»ˉADC1í¨μà5
while(1){
LCD_ShowxNum(30,50,uhADCxConvertedValue1,4,16,0X80); //ÏÔê¾μçÑ1ÖμμÄÕûêy2¿·Ö£¬3.1111μÄ»°£¬Õaàï¾íêÇÏÔê¾3
printf("%d ",uhADCxConvertedValue[i]);
}
ADC:
void MY_ADC_Init(void)
{
ADC1_Handler.Instance=ADC1;
ADC1_Handler.Init.ClockPrescaler=ADC_CLOCK_SYNC_PCLK_DIV4; //4·ÖÆ죬ADCCLK=PCLK2/4=108/4=27MHZ
ADC1_Handler.Init.Resolution=ADC_RESOLUTION_12B; //12λÄ£ê½
ADC1_Handler.Init.ScanConvMode=DISABLE; //·Çé¨ÃèÄ£ê½
ADC1_Handler.Init.ContinuousConvMode=ENABLE; //1رÕá¬Dø×a»»
ADC1_Handler.Init.DiscontinuousConvMode=DISABLE; //½ûÖ12»á¬Dø2éÑùÄ£ê½
ADC1_Handler.Init.NbrOfDiscConversion=0; //2»á¬Dø2éÑùí¨μàêyÎa0
ADC1_Handler.Init.ExternalTrigConv=ADC_EXTERNALTRIGCONV_T3_CC4; //T3_CC4′¥·¢
ADC1_Handler.Init.ExternalTrigConvEdge=ADC_EXTERNALTRIGCONVEDGE_RISING;//ê1óÃèí¼t′¥·¢
ADC1_Handler.Init.NbrOfConversion=1; //1¸ö×a»»Ôú1æÔòDòáDÖD ò2¾íêÇÖ»×a»»1æÔòDòáD1
ADC1_Handler.Init.DataAlign=ADC_DATAALIGN_RIGHT; //óò¶ÔÆë
ADC1_Handler.Init.DMAContinuousRequests=ENABLE; //1رÕDMAÇëÇó
ADC1_Handler.Init.EOCSelection=ENABLE; //1رÕEOCÖD¶Ï
HAL_ADC_Init(&ADC1_Handler);
ADC1_ChanConf.Channel=ADC_CHANNEL_5; //í¨μà
ADC1_ChanConf.Rank=1; //1¸öDòáD
ADC1_ChanConf.SamplingTime=ADC_SAMPLETIME_480CYCLES; //2éÑùê±¼ä
ADC1_ChanConf.Offset=0;
HAL_ADC_ConfigChannel(&ADC1_Handler,&ADC1_ChanConf); //í¨μàÅäÖà //3õê¼»ˉ
HAL_ADC_Start_DMA(&ADC1_Handler, (uint32_t*)&uhADCxConvertedValue, 1);
}
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* AdcHandle)
{
/* Turn LED1 on: Transfer process is correct */
LED1(0);
delay_ms(300);
LED1(1);
z=(u32)HAL_ADC_GetValue(&ADC1_Handler);
LCD_ShowString(30,150,200,16,16,"ADC_GetValue");
LCD_ShowxNum(200,150,z,5,16,0X80); //ÏÔê¾μçÑ1ÖμμÄÕûêy2¿·Ö£¬3.1111μÄ»°£¬Õaàï¾íêÇÏÔê¾3
LCD_ShowString(30,180,200,16,16,"DMA_GetValue");
LCD_ShowxNum(200,180,uhADCxConvertedValue1,5,16,0X80); //ÏÔê¾μçÑ1ÖμμÄÕûêy2¿·Ö£¬3.1111μÄ»°£¬Õaàï¾íêÇÏÔê¾3
}
DMA:
void MYDMA_Config(void)
{
__HAL_RCC_DMA2_CLK_ENABLE();//DMA2ê±Öóê1Äü
__HAL_LINKDMA(&ADC1_Handler,DMA_Handle,ADC1DMA_Handler); //½«DMAóëUSART1áaÏμÆeà′(·¢ËíDMA)
//Tx DMAÅäÖÃ
ADC1DMA_Handler.Instance=DMA2_Stream0; //êy¾Yá÷Ñ¡Ôñ
ADC1DMA_Handler.Init.Channel=DMA_CHANNEL_0; //í¨μàÑ¡Ôñ
ADC1DMA_Handler.Init.Direction=DMA_PERIPH_TO_MEMORY; //′æ′¢Æ÷μ½íaéè
ADC1DMA_Handler.Init.PeriphInc=DMA_PINC_DISABLE; //íaéè·ÇÔöá¿Ä£ê½
ADC1DMA_Handler.Init.MemInc=DMA_MINC_ENABLE; //′æ′¢Æ÷Ôöá¿Ä£ê½
ADC1DMA_Handler.Init.PeriphDataAlignment=DMA_PDATAALIGN_WORD; //íaéèêy¾Y3¤¶è:8λ
ADC1DMA_Handler.Init.MemDataAlignment=DMA_PDATAALIGN_WORD; //′æ′¢Æ÷êy¾Y3¤¶è:8λ
ADC1DMA_Handler.Init.Mode=DMA_NORMAL; //íaéèá÷¿ØÄ£ê½
ADC1DMA_Handler.Init.Priority=DMA_PRIORITY_HIGH; //ÖDμèóÅÏ輶
ADC1DMA_Handler.Init.FIFOMode=DMA_FIFOMODE_DISABLE;
ADC1DMA_Handler.Init.FIFOThreshold=DMA_FIFO_THRESHOLD_HALFFULL;
ADC1DMA_Handler.Init.MemBurst=DMA_MBURST_SINGLE; //′æ′¢Æ÷í»·¢μ¥′Î′«êä
ADC1DMA_Handler.Init.PeriphBurst=DMA_PBURST_SINGLE; //íaéèí»·¢μ¥′Î′«êä
HAL_DMA_DeInit(&ADC1DMA_Handler);
HAL_DMA_Init(&ADC1DMA_Handler);
HAL_NVIC_SetPriority(DMA2_Stream0_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA2_Stream0_IRQn);
}
void DMA2_Stream0_IRQHandler(void){
LED0(0);
delay_ms(300);
LED0(1);
HAL_DMA_IRQHandler(&ADC1DMA_Handler);
}
这里ADC的数据是正常的,但是DMA搬运后的数始终尾部0,大约每个十个数据有一个是正常的,请问下是DMA哪里配置错了吗?
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
我参考了例程,并没有发现什么,
dma循环模式,然后在传输完成中断里面打印值,结果只能持续打印ch出adc第一次的值,不知道哪里出错了,
adc调试过了一直在工作,只是我虽然设置一秒触发一次采样,但是调试过程中它是一直在转换DR里面看的,这有问题吗?
@正点原子
一周热门 更多>