2

处理一个简单的 ADC 项目,从多个通道上的分压器读取电压。我在商业且制作精良的 PCB 上使用 STM32f103rb。我正在读取 PORTA 上的引脚,以下引脚工作并返回电压:

PA0、PA1、PA4、PA5、PA6、PA7。

但是,以下引脚不起作用并返回大约 2000-2700 的原始值:

PA8、PA11 和 PA12。

项目的性质和 PCB 是固定设计的事实意味着我们被这些引脚选择所困扰。数据表非常具体地说明这些引脚可用作 AIN。所有设置和配置均按照标准 STM32,取自基本示例代码并为我们的目的而修改。包含的代码是我们为了寻找原因而制作的调试版本,但无济于事。

已测量引脚上的电压,并且对于分压器的类型是正确的。

任何帮助将不胜感激。

    /* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stdio.h"
#include "stdlib.h"

// Standard STM peripheral config functions
void RCC_Configuration(void);
void GPIO_Configuration(void);

// ADC config - STM example code
void NEW_ADC_Configuration(void);

// Function to read ADC channel and output value
u16 NEW_readADC1(u8 channel);


// Variables
double voltage_su;          // Variable to store supply voltage
double voltage7;
double voltage8;

//*****************************************************************************
// Main program
int main(void)
{
    // Initialise peripheral modules
    RCC_Configuration();
    GPIO_Configuration();
    NEW_ADC_Configuration();

    // Infinate loop
    while (1)
    {       

        // Get value of supply voltage and convert
        voltage_su =  (((3.3 * NEW_readADC1(ADC_Channel_12)) / 4095));

    }
}
//*****************************************************************************
// STM RCC config
void RCC_Configuration(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);   // Connect PORTC
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);   // Connect PORTB...
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);   // Connect PORTA...
}
//*****************************************************************************
// STM GPIO config
void GPIO_Configuration(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;    // Value for setting up the pins

    // Sup
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);  

}
//*****************************************************************************
void NEW_ADC_Configuration(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;

    ADC_InitTypeDef ADC_InitStructure;  // Varibale used to setup the ADC
    RCC_ADCCLKConfig(RCC_PCLK2_Div4);   // Set ADC clock to /4

    // Enable ADC 1 so we can use it
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
    // Conenct the port A to peripheral clock
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    // Restore to defaults
    ADC_DeInit(ADC1);

    /* ADC1 Configuration ----------------------------------------------------*/
    ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
    // Scan 1 at a time
    ADC_InitStructure.ADC_ScanConvMode = DISABLE;
    // No continuous conversions - do them on demand
    ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
    // Start conversion on software request - not bu external trigger
    ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
    // Conversions are 12 bit - put them in the lower 12 bits of the result 
    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
    // How many channels are to be sued by the sequencer
    ADC_InitStructure.ADC_NbrOfChannel = 1;

    // Setup ADC
    ADC_Init(ADC1, &ADC_InitStructure);
    // Enable ADC 1
    ADC_Cmd(ADC1, ENABLE);

    // Enable ADC1 reset calibaration register
    ADC_ResetCalibration(ADC1);
    // Check end of reset calib reg
    while(ADC_GetResetCalibrationStatus(ADC1));
    //Start ADC1 calib
    ADC_StartCalibration(ADC1);
    // Check the end of ADC1 calib
    while(ADC_GetCalibrationStatus(ADC1));
}
//*****************************************************************************
// Function to return the value of ADC ch
u16 NEW_readADC1(u8 channel)
{
    // Config the channel to be sampled
    ADC_RegularChannelConfig(ADC1, channel, 1, ADC_SampleTime_239Cycles5);
    // Start the conversion
    ADC_SoftwareStartConvCmd(ADC1, ENABLE);
    // Wait until conversion complete
    while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
    // Get the conversion value
    return ADC_GetConversionValue(ADC1);
}
//*****************************************************************************
4

2 回答 2

5

我从未使用过这款芯片,但在查看了 5 分钟的硬件数据表后,我得出了以下幼稚的结论:

  • 该芯片有 2 个 ADC,每个 8 个通道,共 16 个。
  • PA0、PA1、PA4、PA5、PA6、PA7 听起来好像它们将属于第一个 ADC。
  • 然后我假设 PA8、PA11 和 PA12 属于第二个 ADC,ST 称为 ADC2。
  • 您的代码似乎只提到并初始化 ADC1。没有 ADC2 的踪迹。

也许这些引脚不起作用,因为您甚至没有初始化或激活它们所属的 ADC?

于 2014-02-13T12:57:56.340 回答
0

此外,由于这些引脚被用作“备用功能”,您应该检查数据表是否存在争用。

通常,他们会列出需要完成/断开的操作,以便在没有争用的情况下使用引脚(例如断开一些电阻)

于 2014-02-14T06:34:09.297 回答