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

📄 ds18b20.c

📁 caijiwendu,dallas ds18b20
💻 C
📖 第 1 页 / 共 2 页
字号:
//DATE 20071106
//ICCAVR ATMEGA16
 
/*DATE 20071006 crystal:3.6864MHZ;
for (i=100;i>0;i--)       250us
for (i=160;i>0;i--)       400us
for (i=190;i>0;i--)       500us
for (i=30;i>0;i--)        70us
for (i=25;i>0;i--)        60us
for (i=10;i>0;i--)        25us 
for (i=5;i>0;i--)         15us
for (i=3;i>0;i--)         10us
for (i=2;i>0;i--)         7us
nop time                  0.8us
*/
/*
Operation Description Implementation 
Write 1 bit   Send a '1' bit to the 1-Wire slaves (Write 1 time slot) 
Drive bus low, delay A(5-15us) Recommended 6us;Release bus, delay B Recommended 64us(>59us)

Write 0 bit   send a '0' bit to the 1-Wire slaves (Write 0 time slot) 
Drive bus low, delay C 60us (60-120us);Release bus, delay D 10us(>8us)

Read bit      Read a bit from the 1-Wire slaves (Read time slot) 
Drive bus low, delay A 6us(5-15us) ;Release bus, delay E 9us(5-12us);
Sample bus to read bit from slave,Delay F 55us(>50us)

Reset         Reset the 1-Wire bus slave devices and ready them for a command 
Delay G 0(<2us);Drive bus low, delay H 480us(480-640us);Release bus, delay I 70us(63-78us);
Sample bus, 0 = device(s) present, 1 = no device present,Delay J 410us(>410us) 
*/
/*
//测三个温度函数,由PORT口控制DS18B20 DATALINE(ATTENTION:NEED 4.7K Ohm),在ATMEGA8上通过
void Highds(dsnumb_temps)
{
	 switch (	dsnumb_temps	)
    	   {
    	   case  1:    	 HIGH_DSB();    	   	break;
    	   case  2:    	 HIGH_DSB2();    	   	break;
    	   case  3:    	 HIGH_DSB3();   	   	break;
    	   default: 		 break;
    	   	}    	   	    	   	
	
}
void Lowds(dsnumb_temps)
{
	 switch (	dsnumb_temps	)
    	   {
    	   case  1 :    	 LOW_DSB() ;    	   	break ;
    	   case  2 :    	 LOW_DSB2() ;    	  	break ;
    	   case  3:    	   LOW_DSB3();   	    	break;
    	   default : 		 break;

    	   	}    	   	    	   	
	
}

unsigned	char  CHKds(dsnumb_temps)
{
	 switch (	dsnumb_temps	)
    	   {
    	   case  1:      return( CHECKBIT(PINC,1) ) ;  		//break;
    	   case  2:      return( CHECKBIT(PINC,2) ) ; 	  //break;
    	   	
    	   case  3:      return( CHECKBIT(PINC,3) ) ; 	  //break;
    	   
    	   default: 		 return( 0xee) ; //break;
    	   	}    	   	    	   	
	
}
*/
#include "main.h"
extern char bitdsb[64];
extern char bytedsb[8];
extern char bittc[72];
extern char bytetc[9]; 
extern int T_count;
extern char T_counth;
extern char T_countl;
//DS18B20
//#define LOW_DSB()     CLEARBIT(PORTD, 7)
//#define HIGH_DSB()     SETBIT(PORTD, 7)
//#define DATA_DSB()     CHECKBIT(PORTE, 7)

unsigned char DS18B20_InitC(void)
{   int i=0;
    for(i=0;i<5;i++)
    {   
      HIGH_DSB();      
      NOP();
      LOW_DSB();
      for (i=190;i>0;i-- )//480us-960us 
        { NOP(); }  
      HIGH_DSB();          //Release bus, delay  70us(63-78us)
      for (i=0;i<30;i++)
        {NOP();}
          
      for(i=0;i<200;i++)
      { 
      //if( CHECKBIT(PIND,7) ==0 )
        if(!DATA_DSB()) 
        {
           i=200;
            return(0x01);
        }
      else 
        {
          if(i<180)
            NOP();
          if(i>180)
            {
              i=200;		
            return(0xff);
            }
        }	
      }                 
        
     }
}
void DS18B20_Init(void)
{ 
  unsigned char i=0;
  unsigned char init=0;
  init=DS18B20_InitC();
  if(init==1)
  for(i=0;i<3;i++)
  {
    {
    HIGH_DSB();       
    NOP();
    LOW_DSB();
    for (i=190;i>0;i-- )						//480us-960us 
    { NOP(); }  
    HIGH_DSB();           //Release bus, delay  70us(63-78us)
    
    for (i=0;i<30;i++)
    {NOP();}
    
    for(i=0;i<200;i++) 
    {    
      //if( CHECKBIT(PIND,7) ==0 )
      if(!DATA_DSB()) 
      { 
        i=200;
      break;
      }
    else
    NOP();              
    }
    
    } 
               
  for (i=165;i>0;i--)
  { NOP();}
  i=200;
  if(i==200)
  break;  
  }       
}
void DSBWriteByte( unsigned char dscommand )
{
  unsigned char i,j,k;
  for(i=0; i<8; i++)
    {  
      if ( (dscommand & 0x01 ))              //from low bit to high
          { 
          HIGH_DSB();
          NOP();
          LOW_DSB();			//Drive bus low, delay (5-15us) Recommended 6us
          for (k=2;k>0;k--)
          {NOP();}
          HIGH_DSB();
          for (j=0;j<30;j++)    
          { NOP(); }
          NOP();      //Recovery Time
          NOP();
          NOP();
          NOP();
          NOP();
          NOP();
          }   
      else
          {   
          HIGH_DSB();
          NOP();
          LOW_DSB();			//Drive bus low, delay  60us (60-120us)
          for (j=0;j<30;j++)    
          { NOP();}  
          HIGH_DSB();   // Release bus, delay  10us(>8us)       
          for (k=3;k>0;k--)
          {NOP();}
          }
      dscommand >>= 1;
   }	
}
/*
For a read data time slot, if a “0” is to be transmitted, 
the delay circuit determines how long the DS1990A will hold the data line
low overriding the 1 generated by the master. 
If the data bit is a “1”, the iButton will leave the read data time slot unchanged.
Drive bus low, delay A 6us(5-15us) ;Release bus, delay E 9us(5-12us);
Sample bus to read bit from slave,Delay F 55us(>50us)
 */
void Read_DsB(void )
{ 
  int i,j;
  for(j=0;j<64;j++)
     {  HIGH_DSB();
     		NOP();
        LOW_DSB();
        for (i=2;i>0;i--)
           {NOP();}
        HIGH_DSB();
        for (i=2;i>0;i--)
          NOP();
        //if( CHECKBIT(PIND,7) )  
        if(DATA_DSB()) 
        	   bitdsb[j]=1;   
       	else
          	 bitdsb[j]=0;
         for (i=25;i>0;i--)
        NOP();
        NOP();
        NOP();
        NOP();
        //putchar(bitdsb[j]);
        //return(bitdsb[j]);
     }
     
}
void Read_Dstc( void )
{ 
  int i=0,j=0;
 int m=0,n=0;
  for(j=0;j<72;j++)
     { HIGH_DSB();
     	// Highds(dsnumb_temps);
       NOP();
        LOW_DSB();
       //Lowds(dsnumb_temps);
       for (i=2;i>0;i--)
           {NOP();}
        HIGH_DSB();
      
        for (i=2;i>0;i--)
            NOP();
      //if( CHECKBIT(PIND,7))  
        if(DATA_DSB()) 
        	bittc[j]=1;   
       else
          bittc[j]=0;     
        for (i=25;i>0;i--)
        NOP();
        NOP();
        NOP();
        NOP();
        //putchar(bittc[j]);

     }
  for(m=8;m>=0;m--)
      { 
        for(n=7;n>=0;n--)
            {  
              bytetc[m] <<=1;
            if(bittc[m*8+n]==1)
            bytetc[m] |=0x01;
            }
      }

   if(bytetc[1]&0x10)
       {bytetc[1] = ~bytetc[1];
        bytetc[0] = 0x10-bytetc[0];
        T_count = (bytetc[1] << 8) + bytetc[0];
                      T_count = (T_count>>4)-1;
                      T_count|=0xff00; //ff msb sign'-'
                      //T_count = -T_count;
        }
       else
        { T_count =  (bytetc[1] << 8) + bytetc[0];
          T_count = T_count >> 4;
         }
  /*
     for(j=8;j>=0;j--)
        { 
          for(k=7;k>=0;k--)
            {  bytetc[j] <<=1;
               if(bittc[j*8+k]==1)
               bytetc[j] |=0x01;
            }
         put0_char( bytetc[j]);
        }
    if(bytetc[1]&0x10)
   	 {
           //-25.0625   0xfe6f bytetc[1]=0xfe,bytetc[0]=0x6f
           //msb is sign'-',lsb is decimal digits
           //example ~0xfe=0x01,0x10-0x6f=0xa1;(0x01<<8)+0xa1=0x01a1;
           //(0x01a1>>4)-1 = 0x001a-1=0x0019;(25)
           //the least Significant bit 0x0001 is decimal digits(0.0625)
          bytetc[1] = ~bytetc[1];
          bytetc[0] = 0x10-bytetc[0];
          T_count = (bytetc[1] << 8) + bytetc[0];
          T_count = (T_count>>4)-1;
          T_count = -T_count;
   	  }
	 else

⌨️ 快捷键说明

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