📄 t.c
字号:
#include "msstate_lrwpan.h"
typedef unsigned char byte;
typedef unsigned int word;
//延时
#define DQ P1_4
void delay(word useconds)
{
for(;useconds>0;useconds--);
}
//复位
byte ow_reset(void)
{
byte presence;
IO_DIR_PORT_PIN(1, 4, IO_OUT);
DQ = 0; //pull DQ line low
delay(500); // leave it low for 480us
//DQ = 1; // allow line to return high
IO_DIR_PORT_PIN(1, 4, IO_IN);
delay(35); // wait for presence
//IO_DIR_PORT_PIN(1, 4, IO_IN);
presence = DQ; // get presence signal
//IO_DIR_PORT_PIN(1, 4, IO_OUT);
//DQ = 1;
delay(465); // wait for end of timeslot
return(presence); // presence signal returned
} // 0=presence, 1 = no part
//从 1-wire 总线上读取一个字节
byte read_byte(void)
{
byte i;
byte value = 0;
for (i=8;i>0;i--)
{
value>>=1;
IO_DIR_PORT_PIN(1, 4, IO_OUT);
DQ = 0; // pull DQ low to start timeslot
delay(1);
IO_DIR_PORT_PIN(1, 4, IO_IN);
//DQ = 1; // then return high
delay(3); //for (i=0; i<3; i++);
//IO_DIR_PORT_PIN(1, 4, IO_IN);
if(DQ)value|=0x80;
delay(60); //wait for rest of timeslot
IO_DIR_PORT_PIN(1, 4, IO_OUT);
delay(1);
}
return(value);
}
//向 1-WIRE 总线上写一个字节
void write_byte(char val)
{
byte i;
for (i=8; i>0; i--) // writes byte, one bit at a time
{
IO_DIR_PORT_PIN(1, 4, IO_OUT);
DQ = 0;
delay(5);// pull DQ low to start timeslot
DQ = val&0x01;
delay(60); // hold value for remainder of timeslot
DQ = 1;
delay(1);
val=val/2;
}
delay(55);
}
//读取温度
int Read_Temperature(void)
{
union{
byte c[2];
int x;
}temp;
ow_reset();
write_byte(0xCC); // Skip ROM
//DQ = 1;
//delay(1);
write_byte(0xBE); // Read Scratch Pad
temp.c[0]=read_byte();
delay(1);
temp.c[1]=read_byte();
ow_reset();
write_byte(0xCC); //Skip ROM
//IO_DIR_PORT_PIN(1, 4, IO_OUT);
//DQ = 1;
//delay(1);
write_byte(0x44); // Start Conversion
temp.x>>=4;
return temp.x;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -