main.c

来自「51单片机的C语言编写的DS18B20的驱动程序」· C语言 代码 · 共 62 行

C
62
字号
//DELAY -with an 11.059MHz crystal
//Calling the routine takes about 24us,and then 
//each cout takes another 16 us
//delay function
void delay(int us)
{
	int s;
	for(s=0;s<us;s++);
}
//reset function
unsigned char ow_reset(void)
{
	unsigned char presence;
	DQ=0;			//pull DQ line low
	delay(29);		//leave it low for 480us
	DQ=1;			//allow line to return high
	delay(3);		//wait for presence
	presence=DQ;	//get presence signal
	delay(25);
	return(presence);//presence signal returned
}					//presence=0,no part=1

//read bit function
unsigned char read_bit(void)
{
	unsigned char i;
	DQ=0;			//pull DQ low to start timeslot
	DQ=1;			//then return high
	for(i=0;i<3;i++);	//delay 15 us from start of times lot
	return(DQ);		//return value of DQ line
}
void write_bit(char bitval)
{
	DQ=0;			//pull DQ low to start timeslot
	if(bitval==1)DQ=1;//return DQ high if write 1
	delay(5);		//hold value for remainder of timeslot
	DQ=1;
}//Delay provides 16us per loop ,plus 24 us therefore
//,delay(5)=104 us
unsigned char read_byte(void)
{
	unsigned char i;
	unsigned char value=0;
	for(i=0;i<8;i++){
		if(read_bit())value|=0x01<<i;
//reads byte in,one byte at a time and then shifts it left
		delay(6);//wait for rest os timeslot
	}
	return (value);
}
void write_byte(char val)
{
	unsigned char i;
	unsigned char temp;
	for(i=0;i<8;i++){//writes byte,one bit at a time		
		temp=val>>i;//shifts val right 'i' spaces
		temp &=0x01;//copy that bit to temp
		write_bit(temp);//write bit in temp into
	}
	delay(5);
}

⌨️ 快捷键说明

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