你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

STM32F0 ADC PA5 无法工作

[复制链接]
小橘仔 提问时间:2018-5-25 22:28 /

其实是可以工作的, 我用的是标准库, 范例用的是官网下载的 ADC 范例
STM32F0xx_StdPeriph_Examples<-- 这一包
无论是 ADC_BasicExample或者 ADC_DMA的ˋ范例
我都能从 Livewatch 来看到我转动 VR 的数值由 0 ~ 3300 做变化

于是我复制了可以工作的范例去到 ST 官网的电机驱动方案的
FOC 4.3的 SDK -STSW-STM32100 这包 (目前已更新到 5.0, 而我用的是 4.3)

但在范例可以工作的ADC-PA5, 却无法在电机库里面运作
ADC1ConvertedValue和

ADC1ConvertedVoltage都是 100 ~ 130 左右跳动, 不会像范例一样有 0 ~ 3300的变化

以下是我贴上来的main.c
其中
PA8,PA9,PA10,PA7,PB0,PB1 用做电机库 PWM工作
PA3 电机库电压采样
PA4 电流采样
在麻烦高手指点一下是否有什么地方需要注意

/* Includes ------------------------------------------------------------------*/
/* Pre-compiler coherency check */
#define PROJECT_CHK
#include "CrossCheck.h"
#undef PROJECT_CHK

#include "MCTuningClass.h"
#include "MCInterfaceClass.h"

#if defined(PFC_ENABLED)
  #include "PFCInit.h"
  #include "PFCApplication.h"
#endif

#include "MCTasks.h"
#include "Parameters conversion.h"
#ifdef DUALDRIVE
#include "Parameters conversion motor 2.h"
#endif
#include "Timebase.h"
#include "UITask.h"
#include "MCLibraryISRPriorityConf.h"

#include <stdio.h>
#include "stm32f0xx_adc.h"
#include "RevupCtrlClass.h"







__IO uint16_t  ADC1ConvertedValue = 0, ADC1ConvertedVoltage = 0;
__IO uint16_t RegularConvData_Tab[4];
#define ADC1_DR_Address    0x40012440


#if (defined(USE_STM32303C_EVAL))
#include "stm32303c_eval.h"
#elif USE_EVAL
#include "stm32_eval.h"
#endif

#ifdef USE_STGAP1S
#include "GAPApplication.h"
#endif

#ifdef STSPIN32F0
void STSPIN32F0_Init(void);
#endif

#define INTERNAL 0
#define EXTERNAL 1
#define STM32F3_64MHZ_INT  ((CLOCK_SOURCE == INTERNAL) && defined(STM32F30X) && defined(CPU_CLK_64_MHZ))

#if STM32F3_64MHZ_INT
void STM32F3_64MHz_Internal(void);
#endif

#define FIRMWARE_VERS "STM32 FOC SDK\0Ver.4.3.0"
const char s_fwVer[32] = FIRMWARE_VERS;

#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__ */

#if defined(EXAMPLE_SPEEDMONITOR)
  void speedmonitor_start(void);
#endif
#if defined(EXAMPLE_POTENTIOMETER)
void potentiometer_start(void);  
#endif   
#if defined(EXAMPLE_RAMP)
  void ramp_start(void);
#endif   
#if defined(EXAMPLE_PI)
  void NewPIval_start(void);
#endif   
#if defined(EXAMPLE_CONTROLMODE)
void TqSpeedMode_start(void);
#endif
   
/* Private function prototypes -----------------------------------------------*/

void SysTick_Configuration(void);


static void ADC_Config(void);
static void DMA_Config(void);



static uint32_t PA5_ADC;

/* Private variables ---------------------------------------------------------*/

CMCI oMCI[MC_NUM];
CMCT oMCT[MC_NUM];  
uint32_t wConfig[MC_NUM] = {UI_CONFIG_M1,UI_CONFIG_M2};


/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/  

/**
  * @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
     */
  
#if !defined(STM32F0XX)
  /*NVIC Priority group configuration.
    Default option is NVIC_PriorityGroup_3.
  */
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);
#endif
  
#ifdef USE_STGAP1S
  GAPboot();
#endif

#if STM32F3_64MHZ_INT
STM32F3_64MHz_Internal();
#endif

#ifdef STSPIN32F0
  STSPIN32F0_Init();
#endif
   
  /*MCInterface and MCTuning boot*/
  MCboot(oMCI,oMCT);
  
  #if defined(PFC_ENABLED)
    PFC_Boot(oMCT[0],(CMCT)MC_NULL, (int16_t *)MC_NULL);
  #endif
   
  /*Systick configuration.*/
  SysTick_Configuration();
  
  /* Start here ***************************************************************/
  /* GUI, this section is present only if LCD, DAC or serial communication is */
  /* enabled.                                                                 */
#if (defined(LCD_FUNCTIONALITY) | defined(DAC_FUNCTIONALITY) | defined(SERIAL_COMMUNICATION))
  UI_TaskInit(UI_INIT_CFG,wConfig,MC_NUM,oMCI,oMCT,s_fwVer);
#endif  
  
#ifdef ENABLE_START_STOP_BUTTON
  /* Init Key input (Start/Stop button) */
  {
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_StructInit(&GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = START_STOP_GPIO_PIN;
    GPIO_Init(START_STOP_GPIO_PORT, &GPIO_InitStructure);
  }
#endif
  /* End here******************************************************************/  
  /* DMA configuration */
  DMA_Config();
  /* ADC1 configuration */
  ADC_Config();
  
  

  
  
  while(1)
  {        
#ifdef SERIAL_COMMUNICATION
    /* Start here ***********************************************************/
    /* GUI, this section is present only if serial communication is enabled.*/
    if (UI_SerialCommunicationTimeOutHasElapsed())
    {
      // Send timeout message
      Exec_UI_IRQ_Handler(UI_IRQ_USART,3,0); // Flag 3 = Send timeout error*/
    }
    if (UI_SerialCommunicationATRTimeHasElapsed())
    {
      // Send ATR message
      Exec_UI_IRQ_Handler(UI_IRQ_USART,4,0); // Flag 4 = Send ATR message*/
    }
    /* End here**************************************************************/
#endif

#if (defined(LCD_FUNCTIONALITY) || defined(ENABLE_START_STOP_BUTTON))
    /* Start here ***********************************************************/
    /* GUI, this section is present only if LCD or start/stop button is enabled. */
    if (UI_IdleTimeHasElapsed())
    {  
      UI_SetIdleTime(UI_TASK_OCCURENCE_TICKS);
      
#ifdef LCD_FUNCTIONALITY
      UI_LCDRefresh();
#endif
      
#ifdef ENABLE_START_STOP_BUTTON
      {
        /* Chek status of Start/Stop button and performs debounce management */
        static uint16_t hKeyButtonDebounceCounter = 0u;
        if ((GPIO_ReadInputDataBit(START_STOP_GPIO_PORT, START_STOP_GPIO_PIN) == START_STOP_POLARITY) &&
            (hKeyButtonDebounceCounter == 0))
        {

#ifdef SINGLEDRIVE
          /* Queries the STM and a command start or stop depending on the state. */
          /* It can be added to MCI functionality */
          if (MCI_GetSTMState(oMCI[M1]) == IDLE)
          {
            MCI_StartMotor(oMCI[M1]);
          }
          else
          {
            MCI_StopMotor(oMCI[M1]);
          }
#endif
        
#ifdef DUALDRIVE
          /* Stop both motors */
          MCI_StopMotor(oMCI[M1]);
          MCI_StopMotor(oMCI[M2]);
#endif

          hKeyButtonDebounceCounter = 4u; /* Debounce time xx * LCD Clock */
        }
        if (hKeyButtonDebounceCounter > 0)
        {
          hKeyButtonDebounceCounter--;
        }
      }
#endif
      
    }
    /* End here**************************************************************/  
#endif

/********************************   EXAMPLE AREA ******************************/
#if defined(EXAMPLE_POTENTIOMETER)
   potentiometer_start();  
#endif   
#if defined(EXAMPLE_RAMP)
   ramp_start();
#endif   
#if defined(EXAMPLE_PI)
   NewPIval_start();
#endif   
#if defined(EXAMPLE_CONTROLMODE)
   TqSpeedMode_start();
#endif
#if defined(EXAMPLE_SPEEDMONITOR)
   speedmonitor_start();
#endif
/*****************************************************************************/
   
#ifdef USE_STGAP1S
   GAPSchedule();
#endif
   
   
       /* Test EOC flag */
    //while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
   
   
    /* Get ADC1 converted data */
    ADC1ConvertedValue = ADC_GetConversionValue(ADC1);
   
    /* Compute the voltage */
    ADC1ConvertedVoltage = (ADC1ConvertedValue *3300)/0xFFF;
    //ADC1ConvertedVoltage = (ADC1ConvertedValue *3300);
   
    //ADConv_t ADConv_struct;
    //ADConv_struct.Channel = ADC_Channel_5;
   
   


   
   
   
   
   
  }
}


/**
  * @brief  ADC1 channel configuration
  * @param  None
  * @retval None
  */
static void ADC_Config(void)
{
  ADC_InitTypeDef     ADC_InitStructure;
  GPIO_InitTypeDef    GPIO_InitStructure;
  /* ADC1 DeInit */  
  ADC_DeInit(ADC1);
  
  /* GPIOC Periph clock enable */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  
   /* ADC1 Periph clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
  
  
  
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 ;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  
  /* Initialize ADC structure */
  ADC_StructInit(&ADC_InitStructure);
  
  /* Configure the ADC1 in continuous mode withe a resolution equal to 12 bits  */
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  //ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Backward;
  ADC_Init(ADC1, &ADC_InitStructure);

  
  ADC_ChannelConfig(ADC1, ADC_Channel_5 , ADC_SampleTime_55_5Cycles);
  /* Convert the ADC1 temperature sensor  with 55.5 Cycles as sampling time */
//  ADC_ChannelConfig(ADC1, ADC_Channel_TempSensor , ADC_SampleTime_55_5Cycles);  
//  ADC_TempSensorCmd(ENABLE);
  
  /* Convert the ADC1 Vref  with 55.5 Cycles as sampling time */
  //ADC_ChannelConfig(ADC1, ADC_Channel_Vrefint , ADC_SampleTime_55_5Cycles);
  //ADC_VrefintCmd(ENABLE);
  
  /* Convert the ADC1 Vbat with 55.5 Cycles as sampling time */
  //ADC_ChannelConfig(ADC1, ADC_Channel_Vbat , ADC_SampleTime_55_5Cycles);  
  //ADC_VbatCmd(ENABLE);
  
  /* ADC Calibration */
  ADC_GetCalibrationFactor(ADC1);
  
  /* ADC DMA request in circular mode */
  ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular);
  //ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_OneShot);
                                               
  /* Enable ADC_DMA */
  ADC_DMACmd(ADC1, ENABLE);  
  
  /* Enable the ADC peripheral */
  ADC_Cmd(ADC1, ENABLE);     
  
  /* Wait the ADRDY flag */
  while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY));
  
  /* ADC1 regular Software Start Conv */
  ADC_StartOfConversion(ADC1);
}

/**
  * @brief  DMA channel1 configuration
  * @param  None
  * @retval None
  */
static void DMA_Config(void)
{
  DMA_InitTypeDef   DMA_InitStructure;
  /* DMA1 clock enable */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE);
  
  /* DMA1 Channel1 Config */
  DMA_DeInit(DMA1_Channel1);
  //DMA_DeInit(DMA1_Channel5);
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address;
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RegularConvData_Tab;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  DMA_InitStructure.DMA_BufferSize = 4;
  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_Mode = ADC_DMAMode_OneShot;
  
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  DMA_Init(DMA1_Channel1, &DMA_InitStructure);
  //DMA_Init(DMA1_Channel5, &DMA_InitStructure);
  /* DMA1 Channel1 enable */
  DMA_Cmd(DMA1_Channel1, ENABLE);
  //DMA_Cmd(DMA1_Channel5, ENABLE);
  
}












/**
  * @brief  Configures the SysTick.
  * @param  None
  * @retval None
  */
void SysTick_Configuration(void)
{
  /* Setup SysTick Timer for 500 usec interrupts  */
  if (SysTick_Config((SystemCoreClock) / SYS_TICK_FREQUENCY))
  {
    /* Capture error */
    while (1);
  }
  
  NVIC_SetPriority(SysTick_IRQn, SYSTICK_PRIORITY);
  NVIC_SetPriority(PendSV_IRQn, PENDSV_PRIORITY);
}

#if STM32F3_64MHZ_INT
void STM32F3_64MHz_Internal()
{
#warning "Internal"
  /* Cleaning of Source clock register */
  RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  
  /* Disable PLL */
  RCC_PLLCmd((FunctionalState)DISABLE);
  
  /* Wait untill PLL is cleared */
  while((RCC->CR & RCC_CR_PLLRDY) == 1)
  {
  }
  
  /* Setting of system clock to 64 MHz */
  RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_16); //8/2*16 = 64MHz
  
  /* Enable PLL */
  RCC_PLLCmd((FunctionalState)ENABLE);
  
  /* Wait till PLL is ready */
  while((RCC->CR & RCC_CR_PLLRDY) == 0)
  {
  }
  
  /* Select PLL as system clock source */
  RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;   
  
  /* Wait till PLL is used as system clock source */
  while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_PLL)
  {
  }
  
  /* HSE disabling */
  RCC_HSEConfig(RCC_HSE_OFF);
  
  /* Wait the disabling of HSE */
  while(RCC_GetFlagStatus(RCC_FLAG_HSERDY)==1)
  {
  }
}
#endif

#ifdef STSPIN32F0
void STSPIN32F0_Init(void)
{
        /** This function is dedicated to the manual setting for STSPIN32F0. **/
       
        /** Setting of internal clock source **/
       
  /* Cleaning of Source clock register */
  RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  
  /* Disable PLL */
  RCC_PLLCmd((FunctionalState)DISABLE);
  
  /* Wait untill PLL is cleared */
  while((RCC->CR & RCC_CR_PLLRDY) == 1)
  {
  }
  
  /* Setting of system clock to 48 MHz */
  RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_CFGR_PLLMUL12); //8/2*12 = 48MHz
  
  /* Enable PLL */
  RCC_PLLCmd((FunctionalState)ENABLE);
  
  /* Wait till PLL is ready */
  while((RCC->CR & RCC_CR_PLLRDY) == 0)
  {
  }
  
  /* Select PLL as system clock source */
  RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;   
  
  /* Wait till PLL is used as system clock source */
  while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_PLL)
  {
  }
  
  /* HSE disabling */
  RCC_HSEConfig(RCC_HSE_OFF);
  
  /* Wait the disabling of HSE */
  while(RCC_GetFlagStatus(RCC_FLAG_HSERDY)==1)
  {
  }
  
  /** Setting for OC protection th **/
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOF,(FunctionalState) ENABLE);
  GPIO_InitTypeDef GPIO_InitStructure;
  GPIO_StructInit(&GPIO_InitStructure);
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOF, &GPIO_InitStructure);
  GPIOF->BSRR = GPIO_Pin_7;
  GPIOF->BRR = GPIO_Pin_6;
}
#endif

#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





收藏 评论2 发布时间:2018-5-25 22:28

举报

2个回答
feixiang20 回答时间:2018-5-26 00:34:14
你有尝试过一点点修改吗
butterflyspring 回答时间:2018-5-30 10:54:50
实测一下电压,比较一下硬件的参考电压
关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版