Ai đã làm vs DS18b20 rồi kiểm tra giúp em , đoạn code của e sai chỗ nào được ko ak. sao giá trị hàm ds18b20_read() thu được cứ =0 thôi ak
Code:
void onewire_write(int8 data) { unsigned int8 count; for(count = 0; count < 8; ++count) { output_low(ONE_WIRE_PIN); delay_us(2); // pull 1-wire low to initiate write time-slot. output_bit(ONE_WIRE_PIN, shift_right(&data, 1, 0)); // set output bit on 1-wire delay_us(60); // wait until end of write slot. output_float(ONE_WIRE_PIN); // set 1-wire high again, delay_us(2); // for more than 1us minimum. } } /* * onewire_read() * Description: reads and returns a byte of data from the device. */ int8 OneWire_Read() { int8 count, data=0; for (count=0; count<8; ++count) { output_low(ONE_WIRE_PIN);//Thiet lap dau ra ONE_WIRE_PIN=0; delay_us( 2 ); input(ONE_WIRE_PIN);//Thiet lap dau vao delay_us( 8 ); if(ONE_WIRE_PIN) data|=0x01<<count; delay_us( 120 ); } return( data ); } float ds18b20_read() { int8 temp1, temp2; signed int16 temp3; float result; onewire_reset(); onewire_write(0xCC); //Skip ROM, address all devices onewire_write(0x44); //Start temperature conversion delay_us(200); onewire_reset(); onewire_write(0xCC); //Skip ROM, address all devices onewire_write(0xBE); //Read scratchpad temp1 = onewire_read(); temp2 = onewire_read(); temp3 = make16(temp2, temp1); //result = (float) temp3 / 2.0; //Calculation for DS18S20 with 0.5 deg C resolution result = (float) temp3 / 16.0; //Calculation for DS18B20 with 0.1 deg C resolution delay_ms(200); // ?????? return (result); } void ds18b20_configure(int8 TH, int8 TL, int8 config) { onewire_reset(); onewire_write(0xCC); //Skip ROM, address all devices onewire_write(0x4E); //Write to scratchpad onewire_write(TH); onewire_write(TL); onewire_write(config); }