⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serie_tempe_high_accur.c

📁 使用C所做的电子温度计,单片机采用89C51,使用LCD液晶显示,温度精度达小数点后两位,可根据实际室温对使用者进行温馨提示
💻 C
字号:
#include <reg51.h>//用DS18B20检测温度,并用OCMJ2X8C串行显示程序!

#define uint  unsigned int
#define uchar unsigned char
#define x     0x80
#define y     0x80
#define comm  0
#define dat   1

sbit cs   = P3^6;
sbit std  = P3^5;
sbit sclk = P3^2;
sbit psb  = P3^3;   //H=并口; L=串口;
sbit rst  = P3^4;   //Reset Signal 低电平有效
sbit TMDAT= P2^6;

bit tsign;
bit flag;

void wr_lcd (uchar dat_comm,uchar content);
void delay (uint us);

uchar code tab1[]={
"中国石油大学北京"};

uchar code tab2[]={
"2005年10月制作  "
};

uchar code tab3[]={
"  当前温度为    "
};

uchar code tab4[]={
"  目前室温较高  "};

uchar code tab5[]={
"  请您注意防暑  "};

uchar code tab6[]={
"  目前温度适宜  "};

uchar code tab7[]={
"  目前温度较低  "};

uchar code tab8[]={
"  请您注意保暖  "};






/*------------------初始化-----------------*/
void init_lcd (void)
{
  rst=1;
  psb=0;                  //串行
  wr_lcd (comm,0x30);  /*30---基本指令动作*/
  wr_lcd (comm,0x01);  /*清屏,地址指针指向00H*/
  delay (80);
  wr_lcd (comm,0x06);  /*光标的移动方向*/
  wr_lcd (comm,0x0c);  /*开显示,关游标*/
}
/*---------------显示汉字或字符----------------*/
void chn_disp1 (uchar code *chn)//在第一行写字
{
  uchar i,j;
  wr_lcd (comm,0x30);
  wr_lcd (comm,0x80);
  j=0;
  for (i=0;i<16;i++)
  wr_lcd (dat,chn[j*16+i]);
}

void chn_disp2 (uchar code *chn)//在第二行写字
{
  uchar i,j;
  wr_lcd (comm,0x30);
  wr_lcd (comm,0x90);
  j=0;
  for (i=0;i<16;i++)
  wr_lcd (dat,chn[j*16+i]);
}

/*---------------------------------------*/
void wr_lcd (uchar dat_comm,uchar content)
{
  uchar a,i,j;
  delay (40);
  a=content;
  cs=1;
  sclk=0;
  std=1;
  for(i=0;i<5;i++)
  {
    sclk=1;
    sclk=0;
  }
  std=0;
  sclk=1;
  sclk=0;
  if(dat_comm)
    std=1;   //data
  else
    std=0;   //command
  sclk=1;
  sclk=0;
  std=0;
  sclk=1;
  sclk=0;
  for(j=0;j<2;j++)
  {
    for(i=0;i<4;i++)
    {
      a=a<<1;
      std=CY;
      sclk=1;
      sclk=0;
    }
    std=0;
    for(i=0;i<4;i++)
    {
      sclk=1;
      sclk=0;
    }
  }
}
/*---------------------------------------*/
void delay (uint us)   //delay time
{
  while(us--);
}


void dmsec(unsigned int count)
{
  unsigned int i;
  while(count--){
  for(i=0;i<125;i++){}
      }
}

void tmreset(void)
{
  unsigned int i;
  TMDAT=0;
  i=103;while(i>0) i--;
  TMDAT=1;
  i=4;while(i>0) i--;
}

void tmpre(void)
{
  unsigned int i;
  flag=0;
  while(TMDAT);
  i=32;//可能数字不对!!!!!!!!!!!!
  while(i--)
  {
    if(TMDAT) {flag=1;break;}
  }
  i=4;while(i>0) i--;
}

bit tmrbit(void)  //read one bit
{
  bit tdat;
  unsigned int i;

  TMDAT=1;
  i++;
  TMDAT=0;i++;
  TMDAT=1;i++;i++;i++;
  tdat=TMDAT;
  i=8;
  while(i>0) i--;
  return(tdat);
}

unsigned char tmrbyte(void) //read one byte
{
  unsigned char i,j;
  unsigned char tdat=0;
  for(i=1;i<=8;i++)
  {
    j=tmrbit();
    tdat=(j<<7)|(tdat>>1);
  }
  return(tdat); 
}

void tmwbyte(unsigned char tdat)
{
  unsigned int i;
  unsigned char j;
  bit testb;
  TMDAT=1;i++;i++;
  for(j=1;j<=8;j++)
  {
    testb=tdat&0x01;
    tdat=tdat>>1;
    if(testb)
    {
      TMDAT=0;  //write 1
      i++;i++;
      TMDAT=1;
      i=8;while(i>0) i--;
    }
    else
    {
      TMDAT=0;   //write 0
      i=8;while(i>0) i--;
      TMDAT=1;
      i++;i++;
    } 
  } 
}

void tmstart(void)  //ds18b20 start convert
{
   do{ tmreset();
       tmpre();
	  }while(~flag);
   dmsec(1);
   tmwbyte(0xcc);   //skip rom
   tmwbyte(0x44);   //convert
}

unsigned int tmrtemp(void)  //read temp
{
   unsigned char a,b, y1,y2,y3;
   unsigned int t1=0x0000;
   float tt;
   unsigned int t;
   do{ tmreset();
       tmpre();
	 }while(~flag);
   dmsec(1);
   tmwbyte(0xcc);
   tmwbyte(0xbe);
   a=tmrbyte();
   b=tmrbyte();
   y1=a>>4;
   y2=b<<4;
   y3=y1|y2;
   if((y3>>7)==0x01) tsign=1;
   else tsign=0;
   t1=((t1|b)<<8)|(a&0x00ff)&0x07ff;
   tt=t1*0.0625;
   t=tt*100;
   return(t); 
}
/*------------------主程序--------------------*/
void main ()
{
  unsigned int last;
  unsigned char hsb,msb,lsb,dsb,gsb;
  
  SP=0x5f;
  init_lcd ();
  chn_disp1(tab1);
  chn_disp2(tab2);
  dmsec(400);
  while (1)
  {
    tmstart();
	dmsec(1000);
	last=tmrtemp();
//	last=last&0x7f;
	hsb=last/10000+0x30;
	msb=(last%10000)/1000+0x30;
	lsb=(last%1000)/100+0x30;
    dsb=(last%100)/10+0x30;
    gsb=(last%10)+0x30;
    chn_disp1(tab3);

    wr_lcd (comm,0x30);
    wr_lcd (comm,0x90);
	wr_lcd(dat,' ');
	 if(tsign)  wr_lcd(dat,'-');
	 else       wr_lcd(dat,'+');
	wr_lcd(dat,' ');
	if(hsb=='0') wr_lcd(dat,' ');
	else       wr_lcd(dat,'1');
    wr_lcd(dat,msb);
 	wr_lcd(dat,lsb);
    wr_lcd(dat,'.');
	wr_lcd(dat,dsb);
   	wr_lcd(dat,gsb); 
 	wr_lcd(dat,' ');   
	wr_lcd(dat,'o');
	wr_lcd(dat,'C');
    wr_lcd(dat,' ');
	wr_lcd(dat,' ');
	wr_lcd(dat,' ');
	wr_lcd(dat,' ');
//	if(tsign)
//	tab3[32]={"  - ",hsb,msb,lsb," 摄氏度  "//编译不能通过,因为字符数组只能在主函数之外定义,成全局的
//	          "-**************-"};
//    else
//    tab3[32]={"  + ",hsb,msb,lsb," 摄氏度  "
//	          "-**************-"};
	dmsec(2000);
    if(last>3000)
	{
	chn_disp1(tab4);
    chn_disp2(tab5);
	}
	else if(last>2000)
	{
    chn_disp1(tab6);
	}
    else if(last<=2000)
    {
	chn_disp1(tab7);
    chn_disp2(tab8);
	}
	dmsec(1700);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -