#define F_CPU 1000000UL //Your clock speed in Hz #include #include #include #include #include ///////////////////////One wire bus Lib/////////////////////////////////// inline __attribute__((gnu_inline)) void therm_delay(uint16_t delay){ while(delay--) asm volatile("nop"); } #define LOOP_CYCLES 8 //Number of cycles that the loop takes #define us(num) (num/(LOOP_CYCLES*(1/(F_CPU/1000000.0)))) /* Thermometer Connections (At your choice) */ #define THERM_PORT PORTA #define THERM_DDR DDRA #define THERM_PIN PINA #define THERM_DQ PA0 /* Utils */ #define THERM_INPUT_MODE() THERM_DDR&=~(1<>=1; n|=(therm_read_bit()<<7); } return n; } void therm_write_byte(uint8_t byte){ uint8_t i=8; while(i--){ //Write actual bit and shift one position right to make //the next bit ready therm_write_bit(byte&1); byte>>=1; } } void therm_read_temperature(char *buffer){ // Buffer length must be at least 12bytes long! ["+XXX.XXXX C"] uint8_t temperature[2]; int8_t digit; uint16_t decimal; //Reset, skip ROM and start temperature conversion therm_reset(); therm_write_byte(THERM_CMD_SKIPROM); therm_write_byte(THERM_CMD_CONVERTTEMP); //Wait until conversion is complete while(!therm_read_bit()); //Reset, skip ROM and send command to read Scratchpad therm_reset(); therm_write_byte(THERM_CMD_SKIPROM); therm_write_byte(THERM_CMD_RSCRATCHPAD); //Read Scratchpad (only 2 first bytes) temperature[0]=therm_read_byte(); temperature[1]=therm_read_byte(); therm_reset(); //Store temperature integer digits and decimal digits digit=temperature[0]>>4; digit|=(temperature[1]&0x7)<<4; //Store decimal digits decimal=temperature[0]&0xf; decimal*=THERM_DECIMAL_STEPS_12BIT; //Format temperature into a string [+XXX.XXXX C] sprintf(buffer, "%+d.%04u C", digit, decimal); } ////////////////////////////////////////////////////////////////////////// int main (void) { DDRC = 0xFF; MCUCSR=(1<