keil中用ADC采集芯片内部温度代码
时间: 2023-09-20 11:07:11 浏览: 157
ADC温度采集实验.zip_Adc温度_stm32温度采集
5星 · 资源好评率100%
以下是一个基于STM32F4xx系列芯片的Keil项目示例,可以实现使用ADC采集芯片内部温度,并将结果通过串口输出:
```c
#include <stdio.h>
#include "stm32f4xx.h"
#define ADC_CHANNEL_TEMPSENSOR 16
void ADC_Config(void);
void USART_Config(void);
int main(void)
{
uint32_t adc_value;
float temperature;
ADC_Config();
USART_Config();
while(1)
{
ADC_SoftwareStartConv(ADC1);
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
adc_value = ADC_GetConversionValue(ADC1);
temperature = ((float)adc_value - (float)0x7FF) / ((float)0xFFFF - (float)0x7FF) * 3.3f / (float)2.5f + (float)25.0f;
printf("Temperature: %.2f\r\n", temperature);
Delay(1000);
}
}
void ADC_Config(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
ADC_CommonInit(&ADC_CommonInitStructure);
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_TempSensorVrefintCmd(ENABLE);
ADC_RegularChannelConfig(ADC1, ADC_CHANNEL_TEMPSENSOR, 1, ADC_SampleTime_480Cycles);
ADC_Cmd(ADC1, ENABLE);
}
void USART_Config(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
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_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
}
void Delay(uint32_t nCount)
{
while(nCount--)
{
__NOP();
}
}
```
上述代码中,首先定义了一个`ADC_CHANNEL_TEMPSENSOR`常量,表示温度传感器所在的ADC通道。在`ADC_Config`函数中,对ADC和GPIO进行初始化,同时使能温度传感器,并将其作为一个常规的ADC通道进行采样。在主函数中,通过`ADC_SoftwareStartConv`函数触发ADC转换,并在转换完成后对结果进行处理,最后通过串口输出温度值。需要注意的是,由于芯片内部温度传感器的精度较低,需要进行一定的数值转换才能得到准确的温度值。
阅读全文