打印

请问STM32多通道连续转换,怎么知道当前转换的是哪个通道?

[复制链接]
4465|11
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
我用的STM32F101做11通道扫描模式,连续转换,我怎么知道当前是哪一通道在转换?在资料上没看到相关描述。
用于AD在DMA传输时通道错位检测。
沙发
yangshaowu|  楼主 | 2012-11-8 14:25 | 只看该作者
自己顶一下

使用特权

评论回复
板凳
lut1lut| | 2012-11-9 10:22 | 只看该作者
无解

使用特权

评论回复
地板
yangshaowu|  楼主 | 2013-1-24 16:45 | 只看该作者
问了技术支持,这种方式出错无法避免,可找一固定通道判断出错后更正。

使用特权

评论回复
5
uet_cache| | 2013-1-24 17:02 | 只看该作者
AD通道按定义的顺序采集,按定义的顺序通过DMA传给内存,内存里可以直接按顺序读AD值,自然就一一对应了。。。DMA自动连续扫描AD,不需占CPU资源,程序只读取变量中的内容即可。方便的很。。。。。

使用特权

评论回复
6
logokfu| | 2013-1-24 20:09 | 只看该作者
本帖最后由 **kfu 于 2013-1-24 20:26 编辑

我来给楼主上个例子,楼主可以参考测试下。


由于时间原因代码没有整理,代码注释有点乱,你只管看代码就是了。


程序功能是:采用ADC1 不间断采集通道8,9,14共三个通道的的电压。采用自动扫描模式。
采用DMA传送ADC结果。最后通过串口1输出三个通道的数据(每一秒钟printf输出一次)。

MDK完整工程下载(已测试通过) 三通道ADC1_DMA_Printf.rar (1.69 MB)




/**
  ******************************************************************************
  * @file    USART/Printf/main.c
* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include <stdio.h>

/** @addtogroup STM32F10x_StdPeriph_Examples
  * @{
  */

/** @addtogroup USART_Printf
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/


#define ADC1_DR_Address    ((uint32_t)0x4001244C)

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
USART_InitTypeDef USART_InitStructure;

__IO uint16_t ADCConvertedValue[3];
__IO uint16_t ADC_tmp[3];


static __IO uint32_t TimingDelay;

/* Private function prototypes -----------------------------------------------*/
void Delay(__IO uint32_t nTime);
void TimingDelay_Decrement(void);

/* Private function prototypes -----------------------------------------------*/

#ifdef __GNUC__
  /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
     set to 'Yes') calls __io_putchar() */
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
  
/* Private functions ---------------------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);

/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured,
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f10x_xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f10x.c file
     */     
        RCC_Configuration();  
        GPIO_Configuration();        
        
          /* DMA1 channel1 configuration ----------------------------------------------*/
  DMA_DeInit(DMA1_Channel1);
  DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADCConvertedValue;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
// DMA_InitStructure.DMA_BufferSize = 1;
        DMA_InitStructure.DMA_BufferSize = 3;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
// DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_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_M2M = DMA_M2M_Disable;
  DMA_Init(DMA1_Channel1, &DMA_InitStructure);
  
  /* Enable DMA1 channel1 */
  DMA_Cmd(DMA1_Channel1, ENABLE);
  
  /* ADC1 configuration ------------------------------------------------------*/
  ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
// ADC_InitStructure.ADC_NbrOfChannel = 1;
        ADC_InitStructure.ADC_NbrOfChannel = 3;
  ADC_Init(ADC1, &ADC_InitStructure);

  /* ADC1 regular channel14 configuration */
  ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 3, ADC_SampleTime_239Cycles5);
        
        
        ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_239Cycles5);
        ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 2, ADC_SampleTime_239Cycles5);

  /* Enable ADC1 DMA */
  ADC_DMACmd(ADC1, ENABLE);
  
  /* Enable ADC1 */
  ADC_Cmd(ADC1, ENABLE);

  /* Enable ADC1 reset calibration register */   
  ADC_ResetCalibration(ADC1);
  /* Check the end of ADC1 reset calibration register */
  while(ADC_GetResetCalibrationStatus(ADC1));

  /* Start ADC1 calibration */
  ADC_StartCalibration(ADC1);
  /* Check the end of ADC1 calibration */
  while(ADC_GetCalibrationStatus(ADC1));
     
  /* Start ADC1 Software Conversion */
  ADC_SoftwareStartConvCmd(ADC1, ENABLE);
        

  /* USARTx configured as follow:
        - BaudRate = 115200 baud  
        - Word Length = 8 Bits
        - One Stop Bit
        - No parity
        - Hardware flow control disabled (RTS and CTS signals)
        - Receive and transmit enabled
  */
  USART_InitStructure.USART_BaudRate = 115200;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;


  /* USART configuration */
  USART_Init(USART1, &USART_InitStructure);
   
  /* Enable USART */
  USART_Cmd(USART1, ENABLE);


  /* Output a message on Hyperterminal using printf function */
  printf("\n\rUSART Printf Example: retarget the C library printf function to the USART\n\r");
        
        
        
        if (SysTick_Config(SystemCoreClock /1000))//1ms中断一次
  {
    /* Capture error */
    while (1);
  }
        
  while (1)
  {
        /*                if(ADC_tmp[0]>=ADCConvertedValue[0]+300||ADCConvertedValue[0]>=ADC_tmp[0]+300)
                        {
                                         ADC_tmp[0]=ADCConvertedValue[0];
                                   
                                  printf("\n\r ADC测试: %d,%d,%d",ADCConvertedValue[0],ADCConvertedValue[1],ADCConvertedValue[2]);
                        }
                */
               
                Delay(1000);
                ADC_tmp[0]=ADCConvertedValue[0];
                ADC_tmp[1]=ADCConvertedValue[1];
                ADC_tmp[2]=ADCConvertedValue[2];
                printf("\r                                                                ");
                printf("\rADC测试: CH8=%d,CH9=%d,CH14=%d",ADC_tmp[0],ADC_tmp[1],ADC_tmp[2]);
               
  }
}



/**
  * @brief  Configures the different system clocks.
  * @param  None
  * @retval None
  */
void RCC_Configuration(void)
{

  /* ADCCLK = PCLK2/4 */
  RCC_ADCCLKConfig(RCC_PCLK2_Div4);
  /* Enable peripheral clocks ------------------------------------------------*/
  /* Enable DMA1 clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

  /* Enable ADC1 and GPIOC ,GPIOB clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOB, ENABLE);
        
        /* Enable GPIOA USART1 AFIO clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1 | RCC_APB2Periph_AFIO, ENABLE);
}


/**
  * @brief  Configures the different GPIO ports.
  * @param  None
  * @retval None
  */
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

  /* Configure PC.04 (ADC Channel14) as analog input -------------------------*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
        
        
        /* Configure PB.00 (ADC Channel8) ,PB.01(ADC Channel9)as analog input -------------------------*/
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
        GPIO_Init(GPIOB, &GPIO_InitStructure);
        
        /* Configure USART Tx as alternate function push-pull */
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* Configure USART Rx as input floating */
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
}




/**
  * @brief  Inserts a delay time.
  * @param  nTime: specifies the delay time length, in milliseconds.
  * @retval None
  */
void Delay(__IO uint32_t nTime)
{
  TimingDelay = nTime;

  while(TimingDelay != 0);
}

/**
  * @brief  Decrements the TimingDelay variable.
  * @param  None
  * @retval None
  */
void TimingDelay_Decrement(void)
{
  if (TimingDelay != 0x00)
  {
    TimingDelay--;
  }
}



/**
  * @brief  Retargets the C library printf function to the USART.
  * @param  None
  * @retval None
  */
PUTCHAR_PROTOTYPE
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART */
  USART_SendData(USART1, (uint8_t) ch);

  /* Loop until the end of transmission */
  while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
  {}

  return ch;
}

#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}

#endif

/**
  * @}
  */

/**
  * @}
  */

/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/

使用特权

评论回复
7
shenglong127| | 2013-8-19 16:47 | 只看该作者
好,这个例子好!!!!

使用特权

评论回复
8
keasen| | 2013-8-19 17:35 | 只看该作者
mark

使用特权

评论回复
9
liumingyang| | 2014-8-12 18:47 | 只看该作者
// DMA_InitStructure.DMA_BufferSize = 1;
        DMA_InitStructure.DMA_BufferSize = 3;
为什么是3不是1?不是每一个通道转换完成都启动一次DMA传输,接着内存地址自加?

使用特权

评论回复
10
lattice1| | 2017-4-10 11:51 | 只看该作者
不错

使用特权

评论回复
11
huzi2099| | 2017-4-10 17:24 | 只看该作者
从DMA的地址计数可以推断出来,不过一般dma都是开启adc的连续转换,所以即便有错也及其不好处理。

使用特权

评论回复
12
木英山下| | 2017-6-23 19:09 | 只看该作者
那要是不用DMA呢

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

46

主题

88

帖子

0

粉丝