1wire.c

来自「Pic Nic 16f877/18f452控制 rtl8019as 原代码」· C语言 代码 · 共 81 行

C
81
字号
/*
 * 1WIRE.C - 1-wire communication library
 *
 * By F醔io Pereira
 *
 * Adapted by Rafael
 *
 */

#define ONE_WIRE_PIN pin_b5	  // Default 1 Wire Communication Pin

#inline
void seta_saida_1w (void) {
	output_float (ONE_WIRE_PIN);
}

#inline
void limpa_saida_1w (void) {
	output_low (ONE_WIRE_PIN);
}

//Reset devices on the 1Wire Bus
boolean reset_1w(void) {
	boolean presente;
	limpa_saida_1w ();
	delay_us(480);
	seta_saida_1w ();
	delay_us(60);
	presente = input (ONE_WIRE_PIN);
	delay_us (240);
	return (presente);
   //Returns:
	// 0 = a device is present
	// 1 = no detected device
}

//Gives power to the Bus for devices on parasite mode
void alimenta_barramento_1w (void) {
	output_high (ONE_WIRE_PIN);
	delay_ms(1000);
	seta_saida_1w();
}

//Read a bit from the Bus
boolean le_bit_1w (void) {
	limpa_saida_1w ();
	seta_saida_1w ();
	delay_us (25);
	return (input(ONE_WIRE_PIN));
}

//Write a bit to the Bus
void escreve_bit_1w (boolean bit) {
	limpa_saida_1w ();
	if (bit) seta_saida_1w ();
	delay_us (120);
	seta_saida_1w ();
}

//Read a byte from the Bus
byte le_byte_1w (void) {
	byte i, dado = 0;
	// Read 8 bits starting from the lsb
	for (i=0;i<8;i++)	{
		if (le_bit_1w ()) dado|=0x01<<i;
		delay_us(90);
	}
	return (dado);
}

//Write a byte to the Bys
void escreve_byte_1w (char dado) {
	byte i, temp;

	for (i=0; i<8; i++) {
		temp = dado>>i;
		temp &= 0x01;
		escreve_bit_1w (temp);
	}
}

⌨️ 快捷键说明

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