http://blog.**/sunke9/139675/message.aspx 这个程序是在万利的ADCDemo的基础上修改来的,咱们也试一下STM32F的温度测试好不好用.这里和原来的程序的区别在于ADC通道用到了16,还需要打开温度测试功能.从实际运行的结果来看这个温度传感器也只能是测试CPU的温度,根本没办法测量环境温度,因为CPU在工作的时候要发热,环境温度在30度的时候CPU温度是36度,当把一片散热器按到CPU上的时候LCD显示温度值下降2度,如果想用CPU测环境温度的话可能需要给CPU加一个足够大的散热片了.由于本人水平有限,有不对的地方欢迎指正,本帖要参加博客大赛,如果您觉得我的帖子还说得过去请到左上角顶我一下.
/******************** (C) COPYRIGHT 2007 STMicroelectronics ******************** * File Name : main.c * Author : MCD Application Team * Version : V1.0 * Date : 10/08/2007 * Description : Main program body ******************************************************************************** * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. *******************************************************************************/
/* Includes ------------------------------------------------------------------*/ #include "stm32f10x_lib.h" #include "lcd.h"
/* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ #define ADC1_DR_Address ((u32)0x4001244C)
#define V25 ((1.42*4096)/3.3) #define Avg_slope ((4.35*4096)/(1000*3.3)) /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ ADC_InitTypeDef ADC_InitStructure; DMA_InitTypeDef DMA_InitStructure; vu16 ADC_ConvertedValue; ErrorStatus HSEStartUpStatus;
/* Private function prototypes -----------------------------------------------*/ void RCC_Configuration(void); void GPIO_Configuration(void); void NVIC_Configuration(void); void SysTick_Config(void); void LcdShow_Init(void);
/* Private functions ---------------------------------------------------------*/ void delay(void); void delay() { int i; for (i=0; i<0xfffff; i++) ; }
char display[4]={0,0}; /******************************************************************************* * Function Name : main * Description : Main program * Input : None * Output : None * Return : None *******************************************************************************/ int main(void) { #ifdef DEBUG debug(); #endif
/* System clocks configuration 系统时钟配置*/ RCC_Configuration();
/* NVIC configuration 内部中断配置*/ NVIC_Configuration();
/* GPIO configuration 输入输出端口配置*/ GPIO_Configuration();
/* Configure the systick 系统节拍定时器配置*/ SysTick_Config(); /* LCD初始化*/ LcdShow_Init();
/* DMA channel1 configuration DMA通道1配置*/ DMA_DeInit(DMA_Channel1);//复位DMA通道1 DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;//DMA外设基地址=ADC DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue;//DMA 通道的存储器基地址=ADC转换值 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;//指定该外设是DMA 的源 DMA_InitStructure.DMA_BufferSize = 1;//缓冲区的数据单元的大小=1 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//当前外设寄存器不变 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;//当前存储器寄存器不变 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;//外设的数据宽度=32位 DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;//存储器的数据宽度=16位 DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//使用环形缓冲模式 DMA_InitStructure.DMA_Priority = DMA_Priority_High;//DMA通道优先级高 DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;//DMA通道未配置存储器到存储器传输 DMA_Init(DMA_Channel1, &DMA_InitStructure);//配置dma通道1
/* Enable DMA channel1 使能DMA通道1*/ DMA_Cmd(DMA_Channel1, ENABLE);
/* ADC1 configuration ------------------------------------------------------*/ ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;//ADC1和ADC2工作在独立模式 ADC_InitStructure.ADC_ScanConvMode = ENABLE;//指定转换工作在扫描(多信道)模式 ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;//指定转换工作在连续模式 ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//由软件控制开始转换,而不是外部触发器。 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//ADC数据右对齐 ADC_InitStructure.ADC_NbrOfChannel = 1;// 指定将要进行转换的ADC 信道号1 ADC_Init(ADC1, &ADC_InitStructure); /* ADC1 regular channel10 configuration */ ADC_RegularChannelConfig(ADC1, ADC_Channel_16, 1, ADC_SampleTime_239Cycles5);
/* Enable ADC1 DMA 使能ADC1 DMA请求*/ ADC_DMACmd(ADC1, ENABLE);
/* Enable ADC1 使能ADC1外围模块*/ ADC_Cmd(ADC1, ENABLE);
/* Enable ADC1 reset calibaration register 重置ADC1校准寄存器*/ ADC_ResetCalibration(ADC1); /* Check the end of ADC1 reset calibration register 等待ADC1校准寄存器准备就绪*/ while(ADC_GetResetCalibrationStatus(ADC1));
/* Start ADC1 calibaration 开始ADC1校准过程*/ ADC_StartCalibration(ADC1); /* Check the end of ADC1 calibration 等待ADC1校准过程结束*/ while(ADC_GetCalibrationStatus(ADC1)); /* Enable the temperature sensor and vref internal channel */ ADC_TempSensorVrefintCmd(ENABLE);
/* Start ADC1 Software Conversion 使能ADC1由软件控制开始转换*/ ADC_SoftwareStartConvCmd(ADC1, ENABLE);
//#if 1 while(1) { vu16 value = ADC_ConvertedValue; vu16 Temperature = (V25-value)/Avg_slope+25; u8 num1 = Temperature % 10; u8 num2 = (Temperature % 100) / 10; u8 num3 = (Temperature % 1000) / 100; u8 num4 = (Temperature % 10000) / 1000; display[3] = num1+(48 - 0); display[2] = num2+(48 - 0); display[1] = num3+(48 - 0); display[0] = num4+(48 - 0); write_string(display); delay(); delay(); delay(); delay(); delay(); delay(); delay(); delay(); } //#endif }
相关链接:http://http://blog.**/sunke9/139675/message.aspx |