ds18b20 c51源代码
字体放大 字体缩小 字体恢复 杏仁黄 秋叶褐 胭脂红 芥末绿 天蓝 雪青 灰 银河白(默认色)
#include "reg51.h"
#include "INTRINS.H"
#include "LCD.h"

#define CLR_RI  (RI=0)
#define CLR_TI  (TI=0)
                                
unsigned char code ID[2][8]={ 0x28,0x1D,0x25,0x1D,0x00,0x00,0x00,0x80,
                              0x28,0x0e,0x9e,0x1c,0x00,0x00,0x00,0x32};
unsigned char currSensorNo=0;

sbit  TMDAT   =  P1^7;
sbit  RUN_LED =  P1^6;

/*------------------------------------------------
*
*------------------------------------------------*/
void serial_initial()
{
     TMOD=0X20;
     SCON=0X50;
     PCON=0X00;
     TL1=TH1=0XE8;
     TR1=1;
}

/*------------------------------------------------
*
*------------------------------------------------*/
void  send(unsigned char count,unsigned char SEND_Buf[])
{  
unsigned char  i; 
	for(i=0;i<count;i++)
	{
		SBUF=SEND_Buf[i];
		while(!TI);
		CLR_TI;
	}
}

/*----------------------------------------------------
*   delay N ms
*----------------------------------------------------*/
void Delay_ms (unsigned int Nms )
{
unsigned char i;
	while(Nms--)
	for(i=0; i<125; i++) ;
}

/*----------------------------------------------------
*   delay N count
*----------------------------------------------------*/
void Delay_Count (unsigned char Count )
{
    while(Count>0) Count--;
}

/*----------------------------------------------------
*   start Reset Pulse
----------------------------------------------------*/
void tmreset(void)
{
    TMDAT=0;
    Delay_Count(103);

    TMDAT=1;
    	Delay_Count(4);
}

/*----------------------------------------------------
*        ACK
*----------------------------------------------------*/
void tmpre(void)
{
    while(TMDAT);
    while(~TMDAT);
    Delay_Count(4);
}

/*----------------------------------------------------
*   Read a bit from 1820
*----------------------------------------------------*/
bit tmrbit(void)
{
    int i=0;
    bit dat;
    TMDAT=0;i++;
        TMDAT=1;i++;i++;
    dat = TMDAT;
        Delay_Count(8);
    return dat;
}

/*----------------------------------------------------
*   Read a byte from 1820
*----------------------------------------------------*/
unsigned char tmrbyte(void)
{
unsigned char i,j,dat=0;

    for(i=1;i<=8;i++)
    {
        j=tmrbit();
        dat=(j<<7)|(dat>>1);
    }
    return dat;
}

/*----------------------------------------------------
*   Read a byte from 1820
*----------------------------------------------------*/
void tmwbyte(unsigned char dat)
{
    signed char   i=0;
    unsigned char j;
    bit testb;

    for(j=1;j<=8;j++)
    {
        testb=dat & 0x01;
        dat = dat>>1;
        if(testb)
        {
            TMDAT=0;
            i++;i++;
            TMDAT=1;
            	Delay_Count(8);
        }
        else
        {
            TMDAT=0;
            	Delay_Count(8);
            TMDAT=1;
            i++;i++;
        }
    }
}

/*----------------------------------------------------
*   send convert command to  1820
*----------------------------------------------------*/
void tmstart(void)
{
unsigned char i;
	tmreset();
    tmpre();
    Delay_ms(1);
/*  tmwbyte(0xcc);*/
    tmwbyte(0x55);
    for(i=0;i<8;i++)
    	tmwbyte(ID[currSensorNo][i]);
    tmwbyte(0x44);
}


/*----------------------------------------------------
*   Read tempreature from  1820
*----------------------------------------------------*/
unsigned int tmrtemp_all(void)
{
unsigned char a,b;
unsigned int y3;
unsigned char i;

	tmreset();
    tmpre();
    Delay_ms(1);
    /*tmwbyte(0xcc);*/
    tmwbyte(0x55);
    for(i=0;i<8;i++)
    	tmwbyte(ID[currSensorNo][i]);
          
    tmwbyte(0xbe);
    a = tmrbyte();
    b = tmrbyte();
    y3 = ((unsigned int)b) << 8;
   

    return ((y3+a) & 0x7ff) ;
}

/*----------------------------------------------------
*   Start Test tempreature, All
*
*----------------------------------------------------*/
void Display_AllTemp(void )
{
unsigned int last;
unsigned char i,Dot;

	RUN_LED=0;
	Part=0;
	LED_DISPLAY();
	Delay_ms(1);

	tmstart();

	Delay_ms(800);
	last=tmrtemp_all();
	RUN_LED=1;

	Dot= (last & 0x0f)*6.25 ;
	Digit[0]= Dot%10;
	Digit[1]=Dot/10;

	last=(last>>4) & 0x7f ;
	if( (last == 0x7f) )   // erro
	{
    	for(i=0;i<5;i++)  Digit[i]=16; 
	}
    else
	{
		for(i=2;i<5;i++)
		{
			Digit[i] = last %10;
			last = last/10;
		}
		if(Digit[4]==0)Digit[4]=17;
		Part=1; 
	}
	Digit[5]=currSensorNo;

         
	LED_DISPLAY();

}

/*----------------------------------------------------
*
*----------------------------------------------------*/
/*  void  Read_Id()
  {
   unsigned char i,id[8];
    tmreset();
    tmpre();
    Delay_ms(1);
    tmwbyte(0x33);

   for(i=0;i<8;i++)
    id[i]=tmrbyte();
      send(8,id);
}*/

/*----------------------------------------------------
*
*    Main
*----------------------------------------------------*/
void main(void)
{
unsigned char id[8]={1,2,3,4,5,6,7,8};

	serial_initial();
	send(8,id);
    for(;;)
    {
		RUN_LED=!RUN_LED;
/*    	Read_Id();*/
		TX_C =!TX_C ;
		currSensorNo=(currSensorNo==1)?0:1;
		Display_AllTemp();
      /*Change();*/
		send(6,Digit);
		Delay_ms(1000);
         _nop_();
   }
}
 
日期:2005年06月04日   来源: 
相关文章:
·用超低功耗MSP430单片机设计数据采集系统 2005-06-27
·Atmega16在开关磁阻电机调速系统中的应用 2005-06-28
·火灾探测器受干扰的原因及改进方法 2005-07-02
·特殊构件的焊接技术 2005-10-18
·触摸屏的工作原理及典型应用 2005-11-25
·高质量C++/C编程指南---类的构造函数,析构函数,赋值 2005-12-13
·基于ATmega8的新型电动自行车调速控制系统设计 2005-12-20
·电子产品高温老化的原理以及一间智能温控老化室的应用 2006-01-05
·新近成功开发无霍尔传感器无刷电机控制器 2006-01-20
·归纳一下最近比较流行的电动自行车技术 2006-01-23
·电子工作台 --- workbench 电子仿真软件 2006-05-23
·液晶字模工具 ------ HZDotReader V3 2006-05-23
·51系列单片机仿真软件----伟福e6000w  2006-05-23
·无传感器无刷电机驱动控制器系统的优点 2006-06-10
·PIC单片机C语言编译器 --PICC 8.05 完整破解版 2006-07-09