//B. Vandeportaele 2018 #define PIN_DS 2 // pin to be used for the OneWire communication unsigned char romSensor[8]={0,0,0,0,0,0,0,0} ; //global variable: array to store the ROM Code of one sensor (its MAC Address) //////////////////////////////////////////////////////////////////////////// // Macros to access the port much faster than using the Arduino HAL (PinMode, DigitalRead and Write) #define SET_PIN(x) PORTD |= (1 << x) #define CLR_PIN(x) PORTD &= ~(1 << x) #define IN_PIN(x) DDRD &= ~(1 << x) #define OUT_PIN(x) DDRD |= (1 << x) #define GET_PIN(x) ((PIND>> x) & 0x01) //////////////////////////////////////////////////////////////////////////// // Functions to act or read on the bus // set the pin as an output at low level value static void setPinOutPut0(unsigned char x){ //Insert your code here } // set the pin as an input at high level value static void setPinInput1(unsigned char x){ //Insert your code here } // read the value of the input static unsigned char getPinInput(unsigned char x){ //Insert your code here } //////////////////////////////////////////////////////////////////////////// #define TRUE 1 #define FALSE 0 //////////////////////////////////////////////////////////////////////////// /* pin : pin number for the OneWire communication returns : 1 if a peripherals responded to the reset, 0 otherwise */ unsigned char reset_one_wire(unsigned char pin) { unsigned char r; unsigned char retries = 125; setPinInput1(pin) ; // wait until the wire is high... just in case do { if (--retries == 0) return 0; //if after 250us, the pin is still low, get out delayMicroseconds(2); } while ( !GET_PIN(pin)); setPinOutPut0(pin); // drive output low //The reset begins here: delayMicroseconds(480); setPinInput1(pin); // allow it to float delayMicroseconds(70); r = !getPinInput(pin); //the value read from the pin is inverted to obtain the return value delayMicroseconds(410); return r; } //////////////////////////////////////////////////////////////////////////// /* pin : pin number for the OneWire communication b : value to be written to the bus, only LSb of the byte is considered */ void write_bit_one_wire(unsigned char pin, unsigned char b) { //Insert your code here } //////////////////////////////////////////////////////////////////////////// /* pin : pin number for the OneWire communication returns 1 if read value is '1', 0 otherwise */ unsigned char read_bit_one_wire(unsigned char pin) { unsigned char r; //Insert your code here return( r); } //////////////////////////////////////////////////////////////////////////// /* pin : pin number for the OneWire communication B : the byte to be written to the bus */ void write_byte_one_wire(unsigned char pin, unsigned char B) { //do the write //Insert your code here setPinInput1(pin); //set the bus to idle state } //////////////////////////////////////////////////////////////////////////// /* pin : pin number for the OneWire communication buf : pointer to an array that contain the bytes to send count: number of bytes to send */ void write_bytes_on_wire(unsigned char pin, const unsigned char * buf, unsigned int count) { for (uint16_t i = 0 ; i < count ; i++) write_byte_one_wire(pin, buf[i]); setPinInput1(pin); } //////////////////////////////////////////////////////////////////////////// /* pin : pin number for the OneWire communication return the byte read from the bus */ unsigned char read_byte_one_wire(unsigned char pin) { unsigned char val=0; //Insert your code here return (val); } //////////////////////////////////////////////////////////////////////////// /* pin : pin number for the OneWire communication buf : pointer to an array to store the bytes count: number of bytes to read */ void read_bytes_one_wire(unsigned char pin, unsigned char * buf, unsigned int count) { for (uint16_t i = 0 ; i < count ; i++) buf[i] = read_byte_one_wire(pin); } //////////////////////////////////////////////////////////////////////////// /* pin : pin number for the OneWire communication rom : an array of 8 byte containning the rom code of the selected peripheral */ void select_one_wire(unsigned char pin, const unsigned char * rom) { //Insert your code here } //////////////////////////////////////////////////////////////////////////// /* pin : pin number for the OneWire communication rom : an array of 8 byte (minimum) to which the function stores the rom code */ void read_rom_one_wire(unsigned char pin, unsigned char * rom ){ //read the rom code from the device //Insert your code here //display the rom code in the console Serial.print("ROM Value: "); for(int i = 0; i < 8 ; i ++){ Serial.print(rom[i]>>4, HEX); Serial.print(rom[i]&0xf, HEX); if (i!=7) Serial.print(":"); else Serial.println(""); } } //////////////////////////////////////////////////////////////////////////// /* pin : pin number for the OneWire communication */ void skip_one_wire(unsigned char pin) { //Insert your code here } //////////////////////////////////////////////////////////////////////////// /* data : pointer to the array of bytes read from the device return the temperature in degree Celsius */ float convert_temp(unsigned char * data){ unsigned int raw = (data[1] << 8) | data[0]; float celsius = 0.0 ; byte cfg = (data[4] & 0x60); // at lower res, the low bits are undefined, so let's zero them if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms //// default is 12 bit resolution, 750 ms conversion time celsius = (float)raw / 16.0; return celsius ; } //////////////////////////////////////////////////////////////////////////// /* pin : pin number for the OneWire communication rom : an array of 8 byte (minimum) to which the function stores the rom code */ float read_scratchpad_and_get_temperature(unsigned char pin, unsigned char * rom ){ unsigned char scratchpad[9]; float celsius; //Insert your code here //display the Scratchpad in the console Serial.print("Scratchpad Value: "); for(int i = 0; i < 9 ; i ++){ Serial.print(scratchpad[i]>>4, HEX); Serial.print(scratchpad[i]&0xf, HEX); if (i!=8) Serial.print(":"); else Serial.println(""); } celsius = convert_temp(scratchpad); Serial.print(" Temperature = "); Serial.print(celsius); Serial.println(" Celsius"); return celsius; } //////////////////////////////////////////////////////////////////////////// void setup(void) { Serial.begin(9600); // for debug purpose pinMode(PIN_DS, INPUT); //set the pin as input using the Arduino HAL } //////////////////////////////////////////////////////////////////////////// void loop(void) { }