在STM32上进行ADC单次转换

5

我正在学习在STM32 F103x上的ADC编程,并从最简单的情况 - 单次转换开始。内部温度传感器(连接到ADC1)的值通过使用USART测量,并将其发送到COM端口。虽然目标很清晰,但当我尝试下载源代码到闪存时,它不会向COM端口发送任何数据。USART函数运作良好,我猜问题来自ADC配置部分,因为我被困在等待完成转换的循环中。

while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); //Wail for conversion complete

这是我目前的源代码。

/* Includes ------------------------------------------------------------*/
#include "stm32f10x.h"
#include <stdio.h>

    uint16_t AD_value;
    const uint16_t V25 = 1750; //when V25=1.41V
    const uint16_t Avg_Slope = 5; //when avg_slc
    uint16_t TemperatureC;

//Define output device
PUTCHAR_PROTOTYPE
{
    USART_SendData(USART1, (uint8_t) ch);
    while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
    {}
    return ch;
}

void Usart1Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);

    /* COnfig PA9 for USART Tx as alternate function push-pull */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

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

    /* USARTx configured as follow:
        - BaudRate = 9600 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 = 9600;
    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);
    USART_Cmd(USART1, ENABLE);  

}

int main(void)
{

    ADC_InitTypeDef ADC_InitStructure;
    Usart1Init();

    RCC_ADCCLKConfig(RCC_PCLK2_Div6); //ADCCLK = PCLK22/6 = 72/6=12MHz
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //Enable ADC1 Clock

    /* ADC1 configuration */
    ADC_DeInit(ADC1); //Power-on default
    ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //Independent conversion mode (single)
    ADC_InitStructure.ADC_ScanConvMode = DISABLE; //Convert single channel only
    ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //Convert 1 time
    ADC_InitStructure.ADC_ExternalTrigConv = DISABLE; //No external triggering
    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //Right 12-bit data alignment
    ADC_InitStructure.ADC_NbrOfChannel = 1; //single channel conversion
    ADC_Init(ADC1, &ADC_InitStructure);
    ADC_TempSensorVrefintCmd(ENABLE); //wake up temperature sensor
    ADC_Cmd(ADC1, ENABLE); //Enable ADC1
    ADC_ResetCalibration(ADC1); //Enable ADC1 reset calibration register
    while(ADC_GetResetCalibrationStatus(ADC1)); //check the end of ADC1 reset calibration register  
    ADC_StartCalibration(ADC1); //Start ADC1 calibration
    while(ADC_GetCalibrationStatus(ADC1)); //Check the end of ADC1 calibration
    ADC_RegularChannelConfig(ADC1, ADC_Channel_16, 1, ADC_SampleTime_1Cycles5); //Select 1.5 cycles conversion for channel 16
    ADC_SoftwareStartConvCmd(ADC1, ENABLE); //Start ADC1 software conversion
    while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); //Wail for conversion complete
    AD_value = ADC_GetConversionValue(ADC1); //Read ADC value
    ADC_ClearFlag(ADC1, ADC_FLAG_EOC); //Clear EOC flag

    printf("\r\n ADC value: %d \r\n", AD_value);
    TemperatureC = (uint16_t)((V25-AD_value)/Avg_Slope+25);
    printf("Temperature: %d%cC\r\n", TemperatureC, 176);
    while (1)
    {}  
}

非常感谢您的建议!

3个回答

6
我会尝试解释源代码的内容。
1- disable 的定义为零: typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState;
2- 当你写 "ADC_InitStructure.ADC_ExternalTrigConv = DISABLE;" 时,实际上是将它分配为零,如此分配 "ADC_InitStructure.ADC_ExternalTrigConv = 0;
3- 这意味着您将 ADC_CR2 寄存器的 EXTSEL 分配为零。
4- 如果 EXTSEL 为零,则 ADC 转换取决于计时器启动。
5- 如果您想要 ADC 由软件启动,则 EXTSEL 必须为 0x000E0000;
6- 可以猜到 "ADC_ExternalTrigConv_None" 的值定义为 0x000E0000;
因此,while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); 代码将在此处挂起,因为它依赖于计时器启动,如果没有启动,它将无法结束。

4
问题解决了!禁用外部触发器是我的错。不要使用以下方式:
ADC_InitStructure.ADC_ExternalTrigConv = DISABLE;

应该是这样的:
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;

多么愚蠢啊!


0

我不会重复发明轮子,告诉你你正处于这三个while()循环之一中。在每个循环之前输出一些内容,然后你就会知道自己卡在哪里了。

如果你有调试器(它在每个ST开发板上都有,并且可能你正在使用它来下载程序),请毫不犹豫地使用它 :)


谢谢您的评论。您说得对,我卡在了一个步骤上:等待转换完成: while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); 我不知道源代码发生了什么 :( - lft

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接