c - 模拟输入引脚 PA8、PA11 和 PA12 在 STM32F103RB 上不工作

标签 c embedded stm32 adc

正在开发一个简单的 ADC 项目,从多个 channel 上的分压器读取电压。我在商业且制作精良的 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);
}
//*****************************************************************************

最佳答案

我从来没有使用过这个芯片,但是在查看了硬件之后data sheet花了 5 分钟,我得出了以下幼稚的结论:

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

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

关于c - 模拟输入引脚 PA8、PA11 和 PA12 在 STM32F103RB 上不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21753553/

相关文章:

php - 我可以用非自由软件重新分发 Phing 吗?

c - MailKit 将 HtmlBody 以字符串形式写入数据库

c - FatFS f_write 不工作

c - 使用 DMA 生成 STM32F3 DAC 信号

c - STM32F3如何使用MODBUS TCP?

c - EXTI_Init 函数中访问越界错误

c - 退出函数后,使用 realloc 不会将值存储在数组中

在 C 中创建按字母顺序排列的 .txt 文件的二进制搜索

Linux 2.6.23 。接收错误。读取函数返回-1

c++ - 如何正确地将字节数组反序列化回 C++ 中的对象?