stm32_ota/SYSTEM/STM32TEMP/stm32Temp.c

120 lines
5.5 KiB
C
Raw Normal View History

2023-12-15 06:28:06 +00:00
#include "stm32Temp.h"
#include "stm32f10x_adc.h"
#include "sysport.h"
/********************<2A><>*********************<2A><>*******************************
* STM32<EFBFBD>ڲ<EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD>ȴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ͨ<EFBFBD><EFBFBD>ADC1<EFBFBD><EFBFBD><EFBFBD>Ի<EFBFBD>ȡ<EFBFBD><EFBFBD>MCU<EFBFBD><EFBFBD>
* ͨ<EFBFBD><EFBFBD>STM32<EFBFBD>ڲ<EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡMCU<EFBFBD><EFBFBD>
****************************************************************************/
/********************************************************************************
* @file stm32Temp.c
* @author <EFBFBD>̳ϿƼ<EFBFBD> Mr.Wang
* @version V1.0.0
* @date 11-Dec-2018
* @brief STM32<EFBFBD>ڲ<EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD>ȴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ͨ<EFBFBD><EFBFBD>ADC1<EFBFBD><EFBFBD><EFBFBD>Ի<EFBFBD>ȡ<EFBFBD><EFBFBD>MCU<EFBFBD><EFBFBD>
******************************************************************************
*******************************************************************************/
/**************************************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD>ƣ<EFBFBD> void T_Adc_Init(void) //ADCͨ<43><CDA8><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD><EFBFBD><EFBFBD> ADC1<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>
*************************************************************************************************/
void T_Adc_Init(void) //ADCͨ<43><CDA8><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>
{
ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_ADC1 , ENABLE ); //ʹ<><CAB9>GPIOA,ADC1ͨ<31><CDA8>ʱ<EFBFBD><CAB1>
RCC_ADCCLKConfig(RCC_PCLK2_Div6); //<2F><>Ƶ<EFBFBD><C6B5><EFBFBD><EFBFBD><36><CAB1>Ϊ72M/6=12MHz
ADC_DeInit(ADC1); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ADC1 <20><>ȫ<EFBFBD><C8AB><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊȱʡֵ
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent ; //ADC<44><43><EFBFBD><EFBFBD>ģʽ:ADC1<43><31>ADC2<43><32><EFBFBD><EFBFBD><EFBFBD>ڶ<EFBFBD><DAB6><EFBFBD>ģʽ
ADC_InitStructure.ADC_ScanConvMode = DISABLE ; //ģ<><C4A3>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>ͨ<EFBFBD><CDA8>ģʽ
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE ; //ģ<><C4A3>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD>ת<EFBFBD><D7AA>ģʽ
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None ; //ת<><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ⲿ<EFBFBD><E2B2BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right ; //ADC<44><43><EFBFBD><EFBFBD><EFBFBD>Ҷ<EFBFBD><D2B6><EFBFBD>
ADC_InitStructure.ADC_NbrOfChannel = 1 ; //˳<><CBB3><EFBFBD><EFBFBD><EFBFBD>й<EFBFBD><D0B9><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>ADCͨ<43><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ
ADC_Init(ADC1, &ADC_InitStructure) ; //<2F><><EFBFBD><EFBFBD>ADC_InitStruct<63><74>ָ<EFBFBD><D6B8><EFBFBD>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ADCx<43>ļĴ<C4BC><C4B4><EFBFBD>
ADC_TempSensorVrefintCmd(ENABLE) ; //<2F><><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><DAB2>¶ȴ<C2B6><C8B4><EFBFBD><EFBFBD><EFBFBD>
ADC_Cmd(ADC1, ENABLE) ; //ʹ<><CAB9>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>ADC1
ADC_ResetCalibration(ADC1) ; //<2F><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>ADC1<43>ĸ<EFBFBD>λ<EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD>
while(ADC_GetResetCalibrationStatus(ADC1)) ; //<2F><>ȡADC1<43><31><EFBFBD><EFBFBD>У׼<D0A3>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>״̬,<2C><><EFBFBD><EFBFBD>״̬<D7B4><CCAC><EFBFBD>ȴ<EFBFBD>
ADC_StartCalibration(ADC1) ;
while(ADC_GetCalibrationStatus(ADC1)) ; //<2F><>ȡָ<C8A1><D6B8>ADC1<43><31>У׼<D0A3><D7BC><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD>״̬<D7B4><CCAC><EFBFBD>ȴ<EFBFBD>
}
/**************************************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD>ƣ<EFBFBD> uint16_t T_Get_Adc(uint8_t ch)
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD>Ӧͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ADCת<EFBFBD><EFBFBD>ֵ
* <EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param1 ch: ͨ<EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param1 uint16_t: chͨ<EFBFBD><EFBFBD>ADCת<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
*************************************************************************************************/
uint16_t T_Get_Adc(uint8_t ch)
{
ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_239Cycles5 ); //ADC1,ADCͨ<43><CDA8>3,<2C><>һ<EFBFBD><D2BB>ת<EFBFBD><D7AA>,<2C><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>Ϊ239.5<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
ADC_SoftwareStartConvCmd(ADC1, ENABLE); //ʹ<><CAB9>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>ADC1<43><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC )); //<2F>ȴ<EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
return ADC_GetConversionValue(ADC1); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>ADC1<43><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
/**************************************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD>ƣ<EFBFBD> uint16_t T_Get_Temp(void)
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD>ADC1ͨ<EFBFBD><EFBFBD>16<EFBFBD><EFBFBD>ADCת<EFBFBD><EFBFBD>5<EFBFBD><EFBFBD>ȡƽ<EFBFBD><EFBFBD>ֵ
* <EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param1 uint16_t: ADC1ͨ<EFBFBD><EFBFBD>16<EFBFBD><EFBFBD>ADCת<EFBFBD><EFBFBD>5<EFBFBD>η<EFBFBD><EFBFBD>ص<EFBFBD>ƽ<EFBFBD><EFBFBD>ֵ
*************************************************************************************************/
uint16_t T_Get_Temp(void)
{
uint16_t temp_val = 0 ;
uint8_t t;
for( t=0; t<5; t++ )
{
temp_val += T_Get_Adc(ADC_Channel_16); //TampSensor
Wait_For_Nms(10);
}
return temp_val/5;
}
/**************************************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD>ƣ<EFBFBD> uint16_t T_Get_Adc_Average( uint8_t ch, uint8_t times)
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD>ADC1ͨ<EFBFBD><EFBFBD>ch<EFBFBD><EFBFBD>ADCת<EFBFBD><EFBFBD>times<EFBFBD><EFBFBD>ȡƽ<EFBFBD><EFBFBD>ֵ
* <EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param1 ch: ͨ<EFBFBD><EFBFBD>
* @param2 times: <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param1 uint16_t: <EFBFBD><EFBFBD>ADC1ͨ<EFBFBD><EFBFBD>ch<EFBFBD><EFBFBD>ADCת<EFBFBD><EFBFBD>times<EFBFBD>η<EFBFBD><EFBFBD>ص<EFBFBD>ƽ<EFBFBD><EFBFBD>ֵ
*************************************************************************************************/
uint16_t T_Get_Adc_Average( uint8_t ch, uint8_t times)
{
uint32_t temp_val=0;
uint8_t t;
for( t=0; t<times; t++ )
{
temp_val += T_Get_Adc(ch) ;
Wait_For_Nms(10) ;
}
return temp_val/times ;
}
/**************************************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD>ƣ<EFBFBD> short Get_Temprate(void) //<2F><>ȡ<EFBFBD>ڲ<EFBFBD><DAB2>¶ȴ<C2B6><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD>ȡ<EFBFBD>ڲ<EFBFBD><EFBFBD>ȴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD>100<EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> short<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ(<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>100<EFBFBD><EFBFBD>,<EFBFBD><EFBFBD>λ:<EFBFBD><EFBFBD>.)
*************************************************************************************************/
short Get_Temprate(void) //<2F><>ȡ<EFBFBD>ڲ<EFBFBD><DAB2>¶ȴ<C2B6><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
{
uint32_t adcx ;
short result ;
double temperate ;
adcx=T_Get_Adc_Average(ADC_Channel_16,10) ; //<2F><>ȡͨ<C8A1><CDA8>16,20<32><30>ȡƽ<C8A1><C6BD>
temperate = (float)adcx*(3.3/4096) ; //<2F><>ѹֵ
temperate = (1.43-temperate)/0.0043+25 ; //ת<><D7AA>Ϊ<EFBFBD><EFBFBD>ֵ
result = temperate*=100 ; //<2F><><EFBFBD><EFBFBD>100<30><30>.
return result ;
}