[51单片机]

关于NTC如何测出温度,请教大佬帮我指点迷津到底怎么弄

[复制链接]
1133|4
手机看帖
扫描二维码
随时随地手机跟帖
小白baibai|  楼主 | 2019-8-19 21:12 | 显示全部楼层 |阅读模式
#include <reg52.h>
#include <intrins.h>
#define MAIN_Fosc  11059200UL //宏定义主时钟HZ
#define PCF8591_ADDR 0x90  //PCF8591地址
//#define DACOUT_EN  0x40  //DAC输出使能
#define uchar unsigned char
#define uint unsigned int
#define TemprStart 0
#define Scale      1
#define TableSize  71
#define us 5000;
sbit rs=P2^2;
sbit rw=P2^3;
sbit en=P2^4;
sbit SCL=P1^2;
sbit SDA=P1^3;
uchar code table[]={'0','1','2','3','4','5','6','7','8','9'};
const code NTC_Table[71] = {          //温度
0xCA,0xCA,0xCA,0xC9,0xC9,0xC9,0xC9,0xC8,
0xC8,0xC8,0xC8,0xC7,0xC7,0xC7,0xC7,0xC7,
0xC6,0xC6,0xC6,0xC6,0xC5,0xC5,0xC5,0xC5,
0xC4,0xC4,0xC4,0xC4,0xC3,0xC3,0xC3,0xC3,
0xC2,0xC2,0xC2,0xC2,0xC1,0xC1,0xC1,0xC1,
0xC0,0xC0,0xC0,0xC0,0xBF,0xBF,0xBF,0xBF,
0xBE,0xBE,0xBE,0xBE,0xBD,0xBD,0xBD,0xBD,
0xBC,0xBC,0xBC,0xBC,0xBB,0xBB,0xBB,0xBB,
0xBA,0xBA,0xBA,0xBA,0xB9,0xB9,0xB9};
uint AD_Value;//0-65535
//int16 *pTemp;
//pTmp=NTC_Table;
//CuTemp=finetab[pTmp,TempSize,AD_Value];
//uint FineTab(uint *a,uint Tablong,uint data)
//{
// uint st,ed,m;
// uint i;
// st=0;
// ed=Tablong-1;
// i=0;
// if(data>=a[st])
//  return st;
// else if(data<=a[ed])
//  return ed;
// while(st<ed)
// {
//  m=(st+ed)/2;
//  if(data=a[m]) break;
//  if(data<a[m]&&data>a[m+1]) ed=m;
//  else st=m;
//  if(i++.TabLong)  break;
// }
// if(st>de) return 0;
// return m;
//}
float GetTempr(unsigned int AD_Value)
{
unsigned char i;
unsigned int SearchStart = 0 , SearchEnd = TableSize - 1 , SearchMax = TableSize;
if(AD_Value >= NTC_Table[SearchStart])
  return (float)(SearchStart  * Scale) + TemprStart;
if(AD_Value <= NTC_Table[SearchEnd])
  return (float)(SearchEnd  * Scale) + TemprStart;
for(i = 0 ; i < SearchMax ; i ++)
{
  if((SearchEnd - SearchStart) > 1)
  {
   if(NTC_Table[ (SearchEnd + SearchStart) / 2 ] < AD_Value)
   {
    SearchEnd = ( SearchEnd + SearchStart ) / 2;
   }
   else if(NTC_Table[ ( SearchEnd + SearchStart ) / 2 ] > AD_Value)
   {
    SearchStart = ( SearchEnd + SearchStart ) / 2;
   }
   else
   {
    return (float)((SearchEnd + SearchStart) * Scale / 2) + TemprStart;
   }
  }
  else
  {
   if((NTC_Table[SearchEnd] - AD_Value) >= (AD_Value - NTC_Table[SearchStart]))
    return (float)(SearchStart  * Scale) + TemprStart;
   else
    return (float)(SearchEnd  * Scale) + TemprStart;
  }
}
return 0;
}
void Delay_Ms(uint ms)
{
     uint i;
  do{
       i = MAIN_Fosc / 96000;
    while(--i);   //96T per loop
     }while(--ms);
}
void Delay5us()
{
#if MAIN_Fosc == 11059200
  _nop_();
#elif MAIN_Fosc == 12000000
  _nop_();
#elif MAIN_Fosc == 22118400
  _nop_(); _nop_(); _nop_();
#endif
}
void write_com(uchar com)//写指令
{
rs=0;
rw = 0;
P0=com;
Delay_Ms(5);
en=1;
Delay_Ms(5);
en=0;
}
void write_dat(uchar dat)//写数据
{
rs=1;
rw = 0;
P0=dat;
Delay_Ms(5);
en=1;
Delay_Ms(5);
en=0;
}
void I2C_Start()  
{
SCL = 1;
_nop_();
SDA = 1;
Delay5us();
SDA = 0;
Delay5us();
}
/*====================================
函数:I2C_Stop()
描述:I2C停止信号
====================================*/
void I2C_Stop()
{
SDA = 0;
_nop_();
SCL = 1;
Delay5us();
SDA = 1;
Delay5us();
}
/*====================================
函数:Master_ACK(bit i)
参数:i 为0时发送非应答 为1时发送应答
描述:I2C主机发送应答
====================================*/
void Master_ACK(bit i)
{
SCL = 0; // 拉低时钟总线允许SDA数据总线上的数据变化
_nop_(); // 让总线稳定
if (i)  //如果i = 1 那么拉低数据总线 表示主机应答
{
  SDA = 0;
}
else  
{
  SDA = 1;  //发送非应答
}
_nop_();//让总线稳定
SCL = 1;//拉高时钟总线 让从机从SDA线上读走 主机的应答信号
_nop_();
SCL = 0;//拉低时钟总线, 占用总线继续通信
_nop_();
SDA = 1;//释放SDA数据总线。
_nop_();
}
/*====================================
函数:Test_ACK()
返回:0为非应答 1为应答
描述:I2C检测从机应答
====================================*/
bit Test_ACK()  // 检测从机应答
{
SCL = 1;//时钟总线为高电平期间可以读取从机应答信号
Delay5us();
if (SDA)
{
  SCL = 0;
  I2C_Stop();
  return(0);
}
else
{
  SCL = 0;
  return(1);
}
}
/*====================================
函数:I2C_send_byte(uchar byte)
参数:byte 要发送的字节
描述:I2C发送一个字节
====================================*/
void I2C_send_byte(uchar byte)
{
uchar i;
for(i = 0 ; i < 8 ; i++)
{
  SCL = 0;
  _nop_();
  if (byte & 0x80) //
  {
   SDA = 1;
   _nop_();
  }
  else
  {
   SDA = 0;
   _nop_();
  }
  SCL = 1;
  _nop_();
  byte <<= 1;
}
SCL = 0;
_nop_();
SDA = 1;
_nop_();
}
/*====================================
函数:I2C_read_byte()
返回:读取的字节
描述:I2C读一个字节
====================================*/
uchar I2C_read_byte()
{
uchar i, dat;
SCL = 0 ;
_nop_();
SDA = 1;
_nop_();
for(i = 0 ; i < 8 ; i++)
{
  SCL = 1;
  _nop_();
  dat <<= 1;   
  if (SDA)
  {
   dat |= 0x01;  
  }
  _nop_();
  SCL = 0;
  _nop_();
}
return(dat);
}
/*DAC输出*/
//bit DAC_OUT(uchar DAT)
//{
// I2C_Start();
// I2C_send_byte(PCF8591_ADDR+0);
// if (!Test_ACK())
// {
//  return(0);
// }
// I2C_send_byte(DACOUT_EN); //DA输出使能
// if (!Test_ACK())
// {
//  return(0);
// }
// I2C_send_byte(DAT);
// if (!Test_ACK())
// {
//  return(0);
// }
// I2C_Stop();
// return(1);
//}
/*读AD数据*/
bit ADC_Read(uchar CON)
{
I2C_Start();
I2C_send_byte(PCF8591_ADDR+0);
if (!Test_ACK())
{
  return(0);
}
I2C_send_byte(CON);
Master_ACK(0);
I2C_Start();
I2C_send_byte(PCF8591_ADDR+1);
if (!Test_ACK())
{
  return(0);
}
AD_Value = I2C_read_byte();
AD_Value=(5.0/255)*AD_Value;//读出电压
Master_ACK(0);
I2C_Stop();
return(1);
}
//void lop()
//{
//  AD_Value=(AD_Value/255)*5.0;//AD值
//
//}
void init()
{
SDA = 1;   //数据总线高
_nop_();
SCL = 1;   //时钟总线高
_nop_();
// write_com(0x01);
write_com(0x38);
write_com(0x0f);
write_com(0x06);
}
void main()
{
uint a;

// int *pTemp;
//pTmp=NTC_Table;
//CuTemp=finetab[pTmp,TempSize,AD_Value];
init();
while(1)
{
  if(ADC_Read(0x00));//判断AD是否输出
  a=GetTempr(AD_Value);//带入计算
  Delay_Ms(5);
  write_com(0x80+0x00);
  write_dat(table[a/100]);
  Delay5us();
  write_com(0x80+0x01);
  write_dat(table[a%100/10]);
  Delay5us();
  write_com(0x80+0x02);
  write_dat('.');
  Delay5us();
  write_com(0x80+0x03);
  write_dat(table[a%10]);
  Delay5us();
//  write_com(0x80+0x04);
//  write_dat(table[a%100/10]);
//  Delay5us();
//  write_com(0x80+0x05);
//  write_dat(table[a%10]);
//  Delay5us();
  write_com(0x80+0x06);
  write_dat('V');
  Delay_Ms(5);
}
}


1566220155(1).jpg

使用特权

评论回复

相关帖子

yangsen| | 2019-8-20 14:26 | 显示全部楼层
知道NTC的分度表查表,或者知道NTC的B值用公式计算

使用特权

评论回复
airwill| | 2019-8-21 21:45 | 显示全部楼层
NTC的分度表查表法, 应该是这个单片机最简单方便和高效的方式

使用特权

评论回复
XZL| | 2019-8-23 11:37 | 显示全部楼层
我以前用查表法

使用特权

评论回复
phones| | 2019-9-13 10:16 | 显示全部楼层
我一般是采用查表的方法。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

5

主题

35

帖子

0

粉丝