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

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

STM32F103内部温度传感器用串口传递到PC上显示程序如下:#includestm32f10x.h#includestm32_eval.h#includestm32f10x_conf.h#includestdio.h#defineDR_ADDRESS((uint32_t)0x4001244C)//ADC1DR寄存器基地址USART_InitTypeDefUSART_InitStructure;//串口初始化结构体声明ADC_InitTypeDefADC_InitStructure;//ADC初始化结构体声明DMA_InitTypeDefDMA_InitStructure;//DMA初始化结构体声明__IOuint16_tADCConvertedValue;//在内存中声明一个可读可写变量用来存放AD的转换结果,低12位有效voidADC_GPIO_Configuration(void);staticvoidDelay_ARMJISHU(__IOuint32_tnCount){for(;nCount!=0;nCount--);}intmain(void){u16ADCConvertedValueLocal;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;STM_EVAL_COMInit(COM1,&USART_InitStructure);/*EnableDMA1clock*/RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);DMA_DeInit(DMA1_Channel1);//开启DMA1的第一通道DMA_InitStructure.DMA_PeripheralBaseAddr=DR_ADDRESS;DMA_InitStructure.DMA_MemoryBaseAddr=(uint32_t)&ADCConvertedValue;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);/*EnableDMA1channel1*/DMA_Cmd(DMA1_Channel1,ENABLE);/*EnableADC1andGPIOCclock*/RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1|RCC_APB2Periph_GPIOC,ENABLE);//使能ADC和GPIOC时钟/*ADC1configuration------------------------------------------------------*/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);/*ADC1regularchannel13configuration*/ADC_RegularChannelConfig(ADC1,ADC_Channel_16,1,ADC_SampleTime_55Cycles5);//ADC通道组,第11个通道采样顺序1,转换时间/*EnableADC1DMA*/ADC_DMACmd(ADC1,ENABLE);//ADC命令,使能/*EnableADC1*/ADC_TempSensorVrefintCmd(ENABLE);//使能温度传感器和内部参考电压通道ADC_Cmd(ADC1,ENABLE);//开启ADC1/*EnableADC1resetcalibarationregister*/ADC_ResetCalibration(ADC1);//重新校准/*ChecktheendofADC1resetcalibrationregister*/while(ADC_GetResetCalibrationStatus(ADC1));//等待重新校准完成/*StartADC1calibaration*/ADC_StartCalibration(ADC1);//开始校准/*ChecktheendofADC1calibration*/while(ADC_GetCalibrationStatus(ADC1));//等待校准完成/*StartADC1SoftwareConversion*/ADC_SoftwareStartConvCmd(ADC1,ENABLE);//连续转换开始,ADC通过DMA方式不断的更新RAM区。while(1){vu16Temperature,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=(ADCConvertedValueLocal-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);}}voidADC_GPIO_Configuration(void)//ADC配置函数{GPIO_InitTypeDefGPIO_InitStructure;//PC0作为模拟通道10输入引脚GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;//管脚1GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN;//输入模式GPIO_Init(GPIOC,&GPIO_InitStructure);//GPIO组}超级终端显示如下:关于一些数据格式的定义解释:#ifndef__STM32F10x_TYPE_H#define__STM32F10x_TYPE_Htypedefsignedlongs32;typedefsignedshorts16;typedefsignedchars8;typedefsignedlongconstsc32;typedefsignedshortconstsc16;typedefsignedcharconstsc8;typedefvolatilesignedlongvs32;typedefvolatilesignedshortvs16;typedefvolatilesignedcharvs8;typedefvolatilesignedlongconstvsc32;typedefvolatilesignedshortconstvsc16;typedefvolatilesignedcharconstvsc8;typedefunsignedlongu32;typedefunsignedshortu16;typedefunsignedcharu8;typedefunsignedlongconstuc32;typedefunsignedshortconstuc16;typedefunsignedcharconstuc8;typedefvolatileunsignedlongvu32;typedefvolatileunsignedshortvu16;typedefvolatileunsignedcharvu8;typedefvolatileunsignedlongconstvuc32;typedefvolatileunsignedshortconstvuc16;typedefvolatileunsignedcharconstvuc8;typedefenum{FALSE=0,TRUE=!FALSE}bool;typedefenum{RESET=0,SET=!RESET}FlagStatus,ITStatus;typedefenum{DISABLE=0,ENABLE=!DISABLE}FunctionalState;#defineIS_FUNCTIONAL_STATE(STATE)(((STATE)==DISABLE)||((STATE)==ENABLE))typedefenum{ERROR=0,SUCCESS=!ERROR}ErrorStatus;#defineU8_MAX((u8)255)#defineS8_MAX((s8)127)#defineS8_MIN((s8)-128)#defineU16_MAX((u16)65535u)#defineS16_MAX((s16)32767)#defineS16_MIN((s16)-32768)#defineU32_MAX((u32)4294967295uL)#defineS32_MAX((s32)2147483647)#defin

1 / 9
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功