📄 tmp_rec.c
字号:
#pragma SMALL DB OE
#include <reg51.h>
#include "tmp_rec.h"
sbit SDA = P3^0;
sbit SCL = P3^1;
/*************************************************************************/
void pause_short (void) {
int i;
for ( i = 0; i < 5; i++ );
}
/*************************************************************************/
void StartI2C (void) {
SDA = 1;
SCL = 1;
SDA = 0;
pause_short();
SCL = 0;
}
/*************************************************************************/
void StopI2C (void) {
SCL = 0;
SDA = 0;
SCL = 1;
pause_short();
SDA = 1;
}
/*************************************************************************/
char ReadI2C (void) {
char r;
SDA = 1; /* This just sets it to high impedence, the LM75 can still pull it low */
SCL = 1;
pause_short();
r = SDA; /* Read in bit */
SCL = 0;
pause_short();
return r;
}
/*************************************************************************/
void WriteI2C (char write_bit) {
SDA = write_bit;
SCL = 1;
pause_short();
SCL = 0;
pause_short();
}
/*************************************************************************/
void Address_LM75_read (void) {
WriteI2C(1);
WriteI2C(0);
WriteI2C(0);
WriteI2C(1);
WriteI2C(0);/* Note: This function only works for I2C devices at address 000 */
WriteI2C(0);/* A2 = 0 */
WriteI2C(0);/* A1 = 0 */
WriteI2C(1);/* A0 = 0 */
}
/*************************************************************************/
char temperature (void) {
char in_bit;
char check;
char t_8;
char i;
StartI2C();
Address_LM75_read();
check = ReadI2C(); /* Check for LM75 acknowledgement */
if ( check == 0 ) {
t_8 = 0;
for ( i = 7; i >= 0; i-- ) {
in_bit = ReadI2C();
t_8 = t_8 | ( in_bit << i );
}
WriteI2C(0);
for ( i = 7; i > 0; i-- ) {
in_bit = ReadI2C(); /* Read in lower 8 bits */
}
WriteI2C(0);
}
else {
t_8 = 127;
}
StopI2C();
return t_8;
}
/*************************************************************************/
unsigned char PrintTemperature (void)
{
unsigned char temp;
float fahr;
float remainder;
unsigned char rem;
temp = temperature();
// Convert the char to an int
fahr = 1.8 * (int)temp + 32;
remainder = fahr;
remainder = fahr - (int)fahr;
remainder = remainder * 10;
rem = ((int)remainder % 10) + 48;
GotoXY(0,0);
PrintString("Temperat");
GotoXY(1,0);
PrintString("ure:");
PrintInt( (int)fahr );
PutChar('.');
PutChar( rem );
return temp;
}
/*************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -