|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区
您需要 登录 才可以下载或查看,没有账号?立即注册
×
#include "tsensor.h"
#include "delay.h"
#include "sys.h"
void T_Adc_Init(void) //ADCͨµÀ³õʼ»¯
{
ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_ADC1 , ENABLE ); //ʹÄÜGPIOA,ADC1ͨµÀʱÖÓ
RCC_ADCCLKConfig(RCC_PCLK2_Div6); //·ÖƵÒò×Ó6ʱÖÓΪ72M/6=12MHz
ADC_DeInit(ADC1); //½«ÍâÉè ADC1 µÄÈ«²¿¼Ä´æÆ÷ÖØÉèΪȱʡֵ
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC¹¤×÷ģʽ:ADC1ºÍADC2¹¤×÷ÔÚ¶ÀÁ¢Ä£Ê½
ADC_InitStructure.ADC_ScanConvMode = DISABLE; //Ä£Êýת»»¹¤×÷ÔÚµ¥Í¨µÀģʽ
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //Ä£Êýת»»¹¤×÷ÔÚµ¥´Îת»»Ä£Ê½
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //ת»»ÓÉÈí¼þ¶ø²»ÊÇÍⲿ´¥·¢Æô¶¯
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //ADCÊý¾ÝÓÒ¶ÔÆë
ADC_InitStructure.ADC_NbrOfChannel = 1; //˳Ðò½øÐйæÔòת»»µÄADCͨµÀµÄÊýÄ¿
ADC_Init(ADC1, &ADC_InitStructure); //¸ù¾ÝADC_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèADCxµÄ¼Ä´æÆ÷
ADC_TempSensorVrefintCmd(ENABLE); //¿ªÆôÄÚ²¿Î¶ȴ«¸ÐÆ÷
ADC_Cmd(ADC1, ENABLE); //ʹÄÜÖ¸¶¨µÄADC1
ADC_ResetCalibration(ADC1); //ÖØÖÃÖ¸¶¨µÄADC1µÄ¸´Î»¼Ä´æÆ÷
while(ADC_GetResetCalibrationStatus(ADC1)); //»ñÈ¡ADC1ÖØÖÃУ׼¼Ä´æÆ÷µÄ״̬,ÉèÖÃ״̬ÔòµÈ´ý
ADC_StartCalibration(ADC1); //
while(ADC_GetCalibrationStatus(ADC1)); //»ñÈ¡Ö¸¶¨ADC1µÄУ׼³ÌÐò,ÉèÖÃ״̬ÔòµÈ´ý
}
u16 T_Get_Adc(u8 ch)
{
ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_239Cycles5 ); //ADC1,ADCͨµÀ3,µÚÒ»¸öת»»,²ÉÑùʱ¼äΪ239.5ÖÜÆÚ
ADC_SoftwareStartConvCmd(ADC1, ENABLE); //ʹÄÜÖ¸¶¨µÄADC1µÄÈí¼þת»»Æô¶¯¹¦ÄÜ
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));//µÈ´ýת»»½áÊø
return ADC_GetConversionValue(ADC1); //·µ»Ø×î½üÒ»´ÎADC1¹æÔò×éµÄת»»½á¹û
}
//µÃµ½ADC²ÉÑùÄÚ²¿Î¶ȴ«¸ÐÆ÷µÄÖµ
//È¡10´Î,È»ºóƽ¾ù
u16 T_Get_Temp(void)
{
u16 temp_val=0;
u8 t;
for(t=0;t<10;t++)
{
temp_val+=T_Get_Adc(ADC_Channel_16); //TampSensor
delay_ms(5);
}
return temp_val/10;
}
//»ñȡͨµÀchµÄת»»Öµ
//È¡times´Î,È»ºóƽ¾ù
u16 T_Get_Adc_Average(u8 ch,u8 times)
{
u32 temp_val=0;
u8 t;
for(t=0;t<times;t++)
{
temp_val+=T_Get_Adc(ch);
delay_ms(5);
}
return temp_val/times;
}
//µÃµ½Î¶ÈÖµ
//·µ»ØÖµ£ºÎ¶ÈÖµ£¨À©´óÁË100±¶£¬µ¥Î»£º¡æ£©
short Get_Temprate(void)
{
u32 adcx;
short result;
double temperate;
adcx=T_Get_Adc_Average(ADC_Channel_16,20);//¶ÁȡͨµÀ16,20´Îȡƽ¾ù
temperate=(float)adcx*(3.3/4096);//µçѹֵ
temperate=(1.43-temperate)/0.0043+25;//ת»»ÎªÎ¶ÈÖµ
result=temperate*=100;//À©´óÁË100±¶
return result;
}
#ifndef __TSENSOR_H#define __TSENSOR_H
#include "stm32f10x.h"
#define ADC_CH_TEMP ADC_Channel_16 //ζȴ«¸ÐÆ÷ͨµÀ
u16 T_Get_Temp(void); //È¡µÃζÈÖµ
void T_Adc_Init(void); //ADCͨµÀ³õʼ»¯
u16 T_Get_Adc(u8 ch); //»ñµÃij¸öͨµÀÖµ
u16 T_Get_Adc_Average(u8 ch,u8 times);//µÃµ½Ä³¸öͨµÀ10´Î²ÉÑùµÄƽ¾ùÖµ
short Get_Temprate(void);
#endif
#include "led.h"#include "delay.h"
#include "sys.h"#include "usart.h"
#include "lcd.h"#include "tsensor.h"
int main(void) { u16 adcx; float temp; float temperate; delay_init(); //ÑÓʱº¯Êý³õʼ»¯ uart_init(9600); //´®¿Ú³õʼ»¯Îª9600 LED_Init(); //³õʼ»¯ÓëLEDÁ¬½ÓµÄÓ²¼þ½Ó¿Ú LCD_Init(); T_Adc_Init(); //ADC³õʼ»¯ POINT_COLOR=RED;//ÉèÖÃ×ÖÌåΪºìÉ« LCD_ShowString(60,50,200,16,16,"Mini STM32"); LCD_ShowString(60,70,200,16,16,"Temperature TEST"); LCD_ShowString(60,90,200,16,16,"ATOM@ALIENTEK"); LCD_ShowString(60,110,200,16,16,"2014/3/9"); //ÏÔʾÌáʾÐÅÏ¢ POINT_COLOR=BLUE;//ÉèÖÃ×ÖÌåΪÀ¶É« LCD_ShowString(60,130,200,16,16,"TEMP_VAL:"); LCD_ShowString(60,150,200,16,16,"TEMP_VOL:0.000V"); LCD_ShowString(60,170,200,16,16,"TEMPERATE:00.00C"); while(1) { adcx=T_Get_Adc_Average(ADC_CH_TEMP,10); LCD_ShowxNum(132,130,adcx,4,16,0);//ÏÔʾADCµÄÖµ temp=(float)adcx*(3.3/4096); temperate=temp;//±£´æζȴ«¸ÐÆ÷µÄµçѹֵ adcx=temp; LCD_ShowxNum(132,150,adcx,1,16,0); //ÏÔʾµçѹֵÕûÊý²¿·Ö temp-=(u8)temp; //¼õµôÕûÊý²¿·Ö LCD_ShowxNum(148,150,temp*1000,3,16,0X80); //ÏÔʾµçѹСÊý²¿·Ö temperate=(1.43-temperate)/0.0043+25; //¼ÆËã³öµ±Ç°Î¶ÈÖµ LCD_ShowxNum(140,170,(u8)temperate,2,16,0); //ÏÔʾζÈÕûÊý²¿·Ö temperate-=(u8)temperate; LCD_ShowxNum(164,170,temperate*100,2,16,0X80);//ÏÔʾζÈСÊý²¿·Ö LED0=!LED0; delay_ms(250); } } |
|