美文网首页
temperature

temperature

作者: 昨天剩下的一杯冷茶 | 来源:发表于2018-08-31 10:48 被阅读1次
#include "temperature.h"

#define HAL_ADC_REF_125V        0x00
#define ADC_12_BIT              0x30
#define ADC_TEMP_SENS           0x0e
#define ADC_TO_CELSIUS(ADC_VALUE)       ((ADC_VALUE>>4)-334);



void Temperature_Init(void)
{
  TR0 |= 0x01;
  ATEST |= 0x01;
}

unsigned int Read_Temperature(void)
{
  unsigned int value;
  unsigned char tmpADCCON3 = ADCCON3;
  ADCIF = 0;
  ADCCON3 = (HAL_ADC_REF_125V|ADC_12_BIT|ADC_TEMP_SENS);
  while(!ADCIF);
  value = ADCL>>2;
  value |= ((unsigned int )ADCH)<<6;
  ADCCON3 = tmpADCCON3;
  
  return ADC_TO_CELSIUS(value);
}

//头文件=================================
#ifndef __TEMPERATURE_H__
#define __TEMPERATURE_H__

#include "all.h"



void Temperature_Init(void);
unsigned int Read_Temperature(void);



#endif

//例子==============================
#include "all.h" 
#include "led.h"
#include "timer.h"
#include "uart.h"
#include "adc.h"
#include "temperature.h"


typedef unsigned char uint8;
typedef unsigned short int  uint16;

void DelayMS(uint16 msec)
{ 
    uint16 i,j;
    
    for (i=0; i<msec; i++)
        for (j=0; j<536; j++);
}
void main(void)
{   
  char p_buf[]="hello world\r\n";
  char buf[30];
  unsigned int tempvalue;
  CLKCONCMD &= ~0x40;               //设置系统时钟源为32MHZ晶振
  while(CLKCONSTA & 0x40);          //等待晶振稳定
  CLKCONCMD &= ~0x47;               //设置系统主时钟频率为32MHZ   

  InitGPIO();
  Uart_Init(UART_BAUDRATE_115200);
  Uart_String(p_buf);
  Temperature_Init();
  while(1)                     //死循环
  {
    tempvalue = Read_Temperature();
    sprintf(buf,"T=%03dC\r\n",tempvalue);
    Uart_String(buf);
    DelayMS(1000);
  }    
}



QQ图片20180831104734.png

相关文章

网友评论

      本文标题:temperature

      本文链接:https://www.haomeiwen.com/subject/ohhswftx.html