📄 ee.c
字号:
ee_delay();
SDA=0; /* Stop condition */
TRIS1=0; /* SDA OUTPUT */
ee_delay();
SCL = 1;
ee_delay();
SDA = 1;
ee_delay();
return(TRUE);
}
/******************************************************************************/
bit ReadTemperatureFromDS1775(byte *MSByteRead, byte *LSByteRead)
{
if(!SetDataPointerDS1775(0)) /* Set the data pointer to Temperature */
return(FALSE);
/* Returns True if write was successfully acknowledged */
ee_start(); /* Send start sequence */
ee_byte(READ_CONTROL_BYTE); /* Control byte (READ) */
if (ack())
{
ReadError=3;
return(FALSE);
}
ee_data(MSByteRead); /* Get the data MSbyte */
MasterAck(); /* Acknowledge the data received */
ee_data(LSByteRead); /* Get the data LSbyte */
ack(); /* Master NAck */
SCL=0; /* Stop condition */
ee_delay();
SDA=0;
TRIS1=0; /* SDA OUTPUT */
ee_delay();
SCL=1;
ee_delay();
SDA=1;
return(TRUE);
}
/******************************************************************************/
bit SetDataPointerDS1775(byte NewDataPointer)
{
/* Returns True if write was successfully acknowledged */
ee_start(); /* Send start sequence */
ee_byte(WRITE_CONTROL_BYTE); /* Control byte (WRITE) */
if (ack())
{
ReadError=1;
return(FALSE); /* Test Acknowledge */
}
ee_byte(NewDataPointer); /* Set Data Pointer to Config register */
if (ack())
{
ReadError=2;
return(FALSE); /* Test Acknowledge */
}
SCL=0;
ee_delay();
SDA=0; /* Stop condition */
TRIS1=0; /* SDA OUTPUT */
ee_delay();
SCL = 1;
ee_delay();
SDA = 1;
ee_delay();
return(TRUE);
}
/******************************************************************************/
void MasterAck(void)
{
SCL=0; /* Acknowledge */
ee_delay();
SDA=0;
TRIS1=0; /* SDA Output */
ee_delay();
SCL=1;
ee_delay();
SCL=0;
}
/******************************************************************************/
/******************** I2C comms to the DS1721 *******************************/
bit ConfigByteDS1721(byte NewConfig)
{
/* Returns True if write was successfully acknowledged */
ee_start(); /* Send start sequence */
ee_byte(WRITE_CONTROL_BYTE); /* Control byte (WRITE) */
if (ack())
return(FALSE); /* Test Acknowledge */
ee_byte(ACCESS_CONFIG); /* Write Config Command */
if (ack())
return(FALSE); /* Test Acknowledge */
ee_byte(NewConfig); /* Write New configuration */
if (ack())
return(FALSE); /* Test Acknowledge */
SCL=0;
ee_delay();
SDA=0; /* Stop condition */
TRIS1=0; /* SDA OUTPUT */
ee_delay();
SCL = 1;
ee_delay();
SDA = 1;
ee_delay();
return(TRUE);
}
/******************************************************************************/
bit StartTemperatureConversionDS1721(void)
{
/* Returns True if write was successfully acknowledged */
ee_start(); /* Send start sequence */
ee_byte(WRITE_CONTROL_BYTE); /* Control byte (WRITE) */
if (ack())
return(FALSE); /* Test Acknowledge */
ee_byte(START_TEMP_CONVERT); /* Write Start Temperature Conversion command */
if (ack())
return(FALSE); /* Test Acknowledge */
SCL=0;
ee_delay();
SDA=0; /* Stop condition */
TRIS1=0; /* SDA OUTPUT */
ee_delay();
SCL = 1;
ee_delay();
SDA = 1;
ee_delay();
return(TRUE);
}
/******************************************************************************/
bit ReadTemperatureConversionStatusDS1721(byte *IsBusy)
{
/* Returns True if write was successfully acknowledged */
ee_start(); /* Send start sequence */
ee_byte(WRITE_CONTROL_BYTE); /* Control byte (WRITE) */
if (ack())
return(FALSE); /* Test Acknowledge */
ee_byte(ACCESS_CONFIG); /* Write Config Command */
if (ack())
return(FALSE); /* Test Acknowledge */
ee_RepeatStart(); /* Restart Comms command to read */
ee_byte(READ_CONTROL_BYTE); /* Control byte (READ) */
if (ack())
return(FALSE); /* Test Acknowledge */
ee_data(IsBusy); /* Get the DS1721 status */
ack(); /* Master NAck */
SCL=0; /* Stop condition */
ee_delay();
SDA=0;
TRIS1=0; /* SDA OUTPUT */
ee_delay();
SCL=1;
ee_delay();
SDA=1;
return(TRUE);
}
/******************************************************************************/
void ee_RepeatStart(void)
{
SCL = 0;
ee_delay();
TRIS &= 0b11111001; /* make SCL and SDA outputs */
ee_delay();
SDA = 1;
ee_delay();
SCL = 1;
ee_delay();
SDA = 0; /* Restart now! */
}
/******************************************************************************/
bit IsDS1721Busy(void)
{unsigned char cRetries=0, Busy=0;
while(cRetries < 254)
{ /* Read the DS1721 status */
if(ReadTemperatureConversionStatusDS1721(&Busy))
break;
else
cRetries++;
}
if(cRetries < 254)
{
if(Busy & 0x80) /* Check if DS1721 is still busy */
return(FALSE); /* DS1721 is done */
else
return(TRUE); /* DS1721 is still */
}
else
return(TRUE); /* There was an error reading the status */
}
/******************************************************************************/
bit ReadTemperatureFromDS1721(byte *MSByteRead, byte *LSByteRead)
{
/* Returns True if write was successfully acknowledged */
ee_start(); /* Send start sequence */
ee_byte(WRITE_CONTROL_BYTE); /* Control byte (WRITE) */
if (ack())
return(FALSE); /* Test Acknowledge */
ee_byte(READ_TEMPERATURE); /* Write READ_TEMPERATURE Command */
if (ack())
return(FALSE); /* Test Acknowledge */
ee_RepeatStart(); /* Restart Comms command to read */
ee_byte(READ_CONTROL_BYTE); /* Control byte (READ) */
if (ack())
return(FALSE); /* Test Acknowledge */
ee_data(MSByteRead); /* Get the data MSbyte */
MasterAck(); /* Acknowledge the data received */
ee_data(LSByteRead); /* Get the data LSbyte */
ack(); /* Master NAck */
SCL=0; /* Stop condition */
ee_delay();
SDA=0;
TRIS1=0; /* SDA OUTPUT */
ee_delay();
SCL=1;
ee_delay();
SDA=1;
return(TRUE);
}
/******************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -