STM32 - Electronic Thermometer

Experimental requirements

write picture description here

renderings

renderings

Peripheral circuit

The thermistor is used, the resistance value is measured by the voltage division method, and then the actual temperature value is obtained linearly segment by segment according to the curve of the thermistor value and temperature. When measuring the voltage, the ADC sampling of STM32 is used. The op amp is used here to reduce the Effect of leakage current
write picture description here

ADC sampling of STM32

STM32 uses a 12-bit successive approximation analog-to-digital conversion ADC. It can measure 16 external and 2 internal signal sources.

write picture description here

The reference voltage sampled by the ADC is determined by the voltage supplied to the ADC. The voltage of the power supply part on the development board is 3.3V. Therefore, the accuracy of the ADC is 3.3/(2^12)=3.3/4096 V

The process of configuring the ADC

  • Turn on the corresponding peripheral clock (GPIO, ADC, AFIO)
  • Configure ADC sampling IO port as analog input mode
  • Configure ADC parameters (generally use single sampling mode)
  • Configure timers and respond to interrupts
  • Single sampling in the timer interrupt function to flexibly control the sampling frequency of the ADC
void ADC()
{                                        
    ADC_InitTypeDef ADC_InitStructure;

  ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;   //设置ADC1和ADC2工作在独立工作模式
  ADC_InitStructure.ADC_ScanConvMode = DISABLE;             // 设置ADC工作在扫描模式(多通道),如果只需要一个通道,配置为单通道模式 DISABLE
  ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;  //设置ADC工作在单次模式(使能一次,转换一次;也可以配置连续模式,自动周期性采样
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;  //设置转换由软件触发,不用外部触发
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;              //ADC数据右对齐
  ADC_InitStructure.ADC_NbrOfChannel = 1;                          //设置进行转换的通道数目为5
  ADC_Init(ADC1, &ADC_InitStructure);                              //初始化ADC1

  ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1,ADC_SampleTime_239Cycles5);  //设置ADC1的通道0第一个进行转换,采样时钟周期239.5个

  ADC_Cmd(ADC1, ENABLE);                           //使能ADC1

  ADC_ResetCalibration(ADC1);                        //重置ADC1的校准寄存器
  while(ADC_GetResetCalibrationStatus(ADC1));        //等待ADC1校准重置完成

  ADC_StartCalibration(ADC1);                        //开始ADC1校准
  while(ADC_GetCalibrationStatus(ADC1));             //等待ADC1校准完成
  ADC_SoftwareStartConvCmd(ADC1, ENABLE);            //使能ADC1软件开始转换
}


void TIM3_IRQHandler(void)      //TIM3的溢出更新中断响应函数
{     
            float Vx;
        TIM_ClearITPendingBit(TIM3,TIM_IT_Update);   //清空TIM3溢出中断响应函数标志位
            ADC_SoftwareStartConvCmd(ADC1, ENABLE); //开始单次模式数据转换
        AD1_Value=ADC_GetConversionValue(ADC1);
      Vx = (AD1_Value * 3.3) / 4095;
      resistence =(10*Vx) / (3.3-Vx);   //计算此时电阻
}           

Program implementation

main

int main(void)
{
    int kk=0;
    RCC_Configuration();                                                                    
    GPIO_Configuration();   
        ADC();
        nvic(); 
        tim3();

        Nixie_Configuration();
        delay_nms(500); 

    while(1)
    {
            int ii,jj,ans=0,count=0;
            float deta,origin;
            for(ii=0;ii<55;ii++){       
                count =0;
                if(resistence <= RESLIST[ii] && resistence >= RESLIST[ii+1])
                    {
            ans =(ii-10)*10 + 10*(RESLIST[ii] - resistence) /(RESLIST[ii] - RESLIST[ii+1]);
                        break;
                    }
            }
      for(kk=0;kk<200;kk++){
          NumDisplay(ans);
      } 
  }     
}   

tim3 configuration

void tim3()                           //配置TIM3为基本定时器模式 ,约100Hz
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;   //定义格式为TIM_TimeBaseInitTypeDef的结构体的名字为TIM_TimeBaseStructure  

    TIM_TimeBaseStructure. TIM_Period =9999;          //配置计数阈值为9999,超过时,自动清零,并触发中断
        TIM_TimeBaseStructure.TIM_Prescaler =71;       //    时钟预分频值,定时器的计数的频率等于主时钟频率除以该预分频值
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;  // 时钟分频倍数(用于数字滤波,暂时无用)
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  // 计数方式为向上计数

    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);      //  初始化tim3
    TIM_ClearITPendingBit(TIM3,TIM_IT_Update); //清除TIM3溢出中断标志
    TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE); //  使能TIM3的溢出更新中断
    TIM_Cmd(TIM3,ENABLE);                     //           使能TIM3
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324408237&siteId=291194637