当前位置:文档之家› STM32-内部温度传感器-串口显示-完整程序

STM32-内部温度传感器-串口显示-完整程序

STM32-内部温度传感器-串口显示-完整程序
STM32-内部温度传感器-串口显示-完整程序

STM32F103 内部温度传感器用串口传递到PC上显示

程序如下:

#include "stm32f10x.h"

#include "stm32_eval.h"

#include "stm32f10x_conf.h"

#include

#define DR_ADDRESS ((uint32_t)0x4001244C) //ADC1 DR寄存器基地址

USART_InitTypeDef USART_InitStructure; //串口初始化结构体声明ADC_InitTypeDef ADC_InitStructure; //ADC初始化结构体声明DMA_InitTypeDef DMA_InitStructure; //DMA初始化结构体声明__IO uint16_t ADCConvertedValue; // 在内存中声明一个可读可写变量用来存放AD的转换结果,低12 位有效

void ADC_GPIO_Configuration(void);

static void Delay_ARMJISHU(__IO uint32_t nCount)

{ for (; nCount != 0; nCount--);}

int main(void)

{

u16 ADCConvertedValueLocal;

USART_https://www.doczj.com/doc/0e9641282.html,ART_BaudRate = 115200;

USART_https://www.doczj.com/doc/0e9641282.html,ART_WordLength = USART_WordLength_8b;

USART_https://www.doczj.com/doc/0e9641282.html,ART_StopBits = USART_StopBits_1; USART_https://www.doczj.com/doc/0e9641282.html,ART_Parity = USART_Parity_No;

USART_https://www.doczj.com/doc/0e9641282.html,ART_HardwareFlowControl = USART_HardwareFlowControl_None;

USART_https://www.doczj.com/doc/0e9641282.html,ART_Mode = USART_Mode_Rx | USART_Mode_Tx;

STM_EV AL_COMInit(COM1, &USART_InitStructure);

/* Enable DMA1 clock */

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); DMA_DeInit(DMA1_Channel1); //开启DMA1的第一通道DMA_InitStructure.DMA_PeripheralBaseAddr = DR_ADDRESS; DMA_InitStructure.DMA_MemoryBaseAddr=(uint32_t)&ADCConve rtedValue;

DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;

//DMA的转换模式为SRC模式,由外设搬移到内存

DMA_InitStructure.DMA_BufferSize = 1; //DMA缓存大小,1个DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //接收一次数据后,设备地址禁止后移DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;

//关闭接收一次数据后,目标内存地址后移

DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //定义外设数据宽度为16位

DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //DMA搬移数据尺寸,HalfWord 就是为16位

DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //转换模式,循环缓存模式。

DMA_InitStructure.DMA_Priority = DMA_Priority_High; //DMA优先级高

DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;

//M2M模式禁用

DMA_Init(DMA1_Channel1, &DMA_InitStructure);

/* Enable DMA1 channel1 */

DMA_Cmd(DMA1_Channel1, ENABLE);

/* Enable ADC1 and GPIOC clock */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC, ENABLE); //使能ADC和GPIOC时钟

/* ADC1 configuration ------------------------------------------------------*/ ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //独立的转换模式

ADC_InitStructure.ADC_ScanConvMode = ENABLE; //开启扫描模式

ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //开

启连续转换模式

ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //ADC外部开关,关闭状态

ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //对齐方式,ADC为12位中,右对齐方式

ADC_InitStructure.ADC_NbrOfChannel = 1; //开启通道数,1个ADC_Init(ADC1, &ADC_InitStructure);

/* ADC1 regular channel13 configuration */

ADC_RegularChannelConfig(ADC1, ADC_Channel_16, 1, ADC_SampleTime_55Cycles5);

//ADC通道组,第11个通道采样顺序1,转换时间

/* Enable ADC1 DMA */

ADC_DMACmd(ADC1, ENABLE); //ADC命令,使能

/* Enable ADC1 */

ADC_TempSensorVrefintCmd(ENABLE); //使能温度传感器和内部参考电压通道

ADC_Cmd(ADC1, ENABLE); //开启ADC1

/* Enable ADC1 reset calibaration register */

ADC_ResetCalibration(ADC1); //重新校准

/* Check the end of ADC1 reset calibration register */

while(ADC_GetResetCalibrationStatus(ADC1)); //等待重新校准完

/* Start ADC1 calibaration */

ADC_StartCalibration(ADC1); //开始校准

/* Check the end of ADC1 calibration */

while(ADC_GetCalibrationStatus(ADC1)); //等待校准完成

/* Start ADC1 Software Conversion */

ADC_SoftwareStartConvCmd(ADC1, ENABLE); //连续转换开始,ADC通过DMA方式不断的更新RAM区。

while (1)

{

vu16 Temperature,a,b,c,d;

ADCConvertedValueLocal = ADCConvertedValue;

Temperature=(1.43-ADCConvertedValueLocal*3.3/4096)*1000/4.35 + 25;

ADCConvertedValueLocal= Temperature;

a =ADCConvertedValueLocal /1000;

b = (ADCConvertedValueLocal - a*1000)/100;

c = (ADCConvertedV alueLocal - a*1000 - b*100)/10;

d = ADCConvertedValueLocal - a*1000 - b*100 - c*10;

printf("\r\n 当前STM32芯片内部温度为:%d%d%d%d% ℃.\n\r", a, b, c, d);

Delay_ARMJISHU(8000000);

}

void ADC_GPIO_Configuration(void) //ADC配置函数

{

GPIO_InitTypeDef GPIO_InitStructure;

//PC0 作为模拟通道10输入引脚

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //管脚1

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;//输入模式GPIO_Init(GPIOC, &GPIO_InitStructure); //GPIO组

}

超级终端显示如下:

关于一些数据格式的定义解释:

#ifndef __STM32F10x_TYPE_H

#define __STM32F10x_TYPE_H

typedef signed long??s32;

typedef signed short s16;

typedef signed char??s8;

typedef signed long??const sc32;? typedef signed short const sc16;?

typedef signed char??const sc8;?? typedef volatile signed long??vs32; typedef volatile signed short vs16;

typedef volatile signed char??vs8;

typedef volatile signed long??const vsc32;? typedef volatile signed short const vsc16;? typedef volatile signed char??const vsc8;?? typedef unsigned long??u32;

typedef unsigned short u16;

typedef unsigned char??u8;

typedef unsigned long??const uc32;? typedef unsigned short const uc16;?

typedef unsigned char??const uc8;??

typedef volatile unsigned long??vu32;

typedef volatile unsigned short vu16;

typedef volatile unsigned char??vu8;

typedef volatile unsigned long??const vuc32;?

typedef volatile unsigned short const vuc16;?

typedef volatile unsigned char??const vuc8;??

typedef enum {FALSE = 0, TRUE = !FALSE} bool;

typedef enum {RESET = 0, SET = !RESET} FlagStatus, ITStatus;

typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState;

#define IS_FUNCTIONAL_STATE(STATE) (((STATE) == DISABLE) || ((STATE) == ENABLE))

typedef enum {ERROR = 0, SUCCESS = !ERROR} ErrorStatus;

#define U8_MAX?????((u8)255)

#define S8_MAX?????((s8)127)

#define S8_MIN?????((s8)-128)

#define U16_MAX????((u16)65535u)

#define S16_MAX????((s16)32767)

#define S16_MIN????((s16)-32768)

#define U32_MAX????

#define S32_MAX????

#define S32_MIN???? #endif

相关主题
文本预览
相关文档 最新文档