当前位置:网站首页>Stm32f4 ll library multi-channel ADC

Stm32f4 ll library multi-channel ADC

2022-06-12 05:10:00 Cocoa core

One 、 Reference resources

  1. Refer to this big man :https://blog.csdn.net/qq_45100839/article/details/114399506

Two 、 step

contrast STM32F0,ADC We need to add Number Of Conversion Configuration of , Then some other configurations are rarely different

  1. ADC To configure , I am here 2 Channels
     Insert picture description here
     Insert picture description here

  2. DMA To configure
     Insert picture description here

  3. I don't use interrupts here , Direct query waiting , You can refer to the link reference above to add an interrupt

  4. Add code

    void adc_dma_init()
    {
          
    /* Set DMA transfer addresses of source and destination */
    	LL_DMA_ConfigAddresses(DMA2,
    						   LL_DMA_STREAM_0,
    						   LL_ADC_DMA_GetRegAddr(ADC1, LL_ADC_DMA_REG_REGULAR_DATA),
    						   (uint32_t)&adcBuf,
    						   LL_DMA_DIRECTION_PERIPH_TO_MEMORY);// To configure DMA, take DMA And ADC1 Link together 
     
    	/* Set DMA transfer size */
    	LL_DMA_SetDataLength(DMA2,LL_DMA_STREAM_0,ADC_DMA_BUF_SIZE);// Set up DMA The length of , With storage ADC The value array is equal in length 
     
    	/* Enable the DMA transfer */
    	LL_DMA_EnableStream(DMA2,LL_DMA_STREAM_0);// Can make DMA transmission 
     
    	LL_ADC_Enable(ADC1);// open ADC
     
    	/* Start ADC group regular conversion */
    	LL_ADC_REG_StartConversionSWStart(ADC1);// start-up ADC Group general conversion ,SWStart  And cubmx Configuration in progress  ADCs_Regular_ConversionMode  Of  External Trigger Conversion Source ADC Corresponding 
     
    	LL_ADC_REG_SetDMATransfer(ADC1,LL_ADC_REG_DMA_TRANSFER_UNLIMITED);// Start conversion 
        
    }
    
    
    ///> adc  Some macro definitions 
    #define ADC_DMA_BUF_CH 2 /*  altogether 2 Channels  */
    #define ADC_DMA_BUF_LEN 8 /*  Every channel 8 A data cache  */
    #define ADC_DMA_BUF_SIZE (ADC_DMA_BUF_LEN*ADC_DMA_BUF_CH) /*  total BUF size  */
    uint8_t adc_get( uint16_t *adc_data_buf)
    {
          
        if ( LL_DMA_IsActiveFlag_TC0(DMA2) )
        {
          
            uint16_t i,j;
            uint16_t * tadBuf;
    
            tadBuf = & adcBuf[0];
    
            for(i = 0; i < ADC_DMA_BUF_CH; i++)
            {
          
                tAD[i] = 0;
            }
    
            for(i = 0; i < ADC_DMA_BUF_CH; i++)
            {
          
                for(j = 0; j < ADC_DMA_BUF_LEN; j++)
                {
          
                    tAD[i] = tAD[i] + tadBuf[j*ADC_DMA_BUF_CH + i];
                }
                tAD[i] = tAD[i] / ADC_DMA_BUF_LEN;
            }
    
            memcpy(adc_data_buf,tAD,ADC_DMA_BUF_CH*2);  /*  Be careful memcpy  Or in accordance with 8 position copy  So we need to *2 Conduct copy */
            
            return 1;
        }
        return 0;
    }
    
    
原网站

版权声明
本文为[Cocoa core]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010620503500.html