/* Dallas Semiconductor DS1620 Digital Thermometer and Thermostat http://www.maxim-ic.com/quick_view2.cfm/qv_pk/2735 Pin Assignment: 1 - DQ 8 - VDD (2.7V - 5.5V) 2 - CLK/CONV 7 - T HIGH 3 - RST 6 - T LOW 4 - GND 5 - T COM Serial Interface: (1) Activate RST by taking it high. (2) Send an instruction (protocol) to the DS1620 telling it what you want to do. (3) If you are reading data, shift it into the controller. (4) If you are writing data, shift it out to the DS1620. (5) Deactivate RST by taking it low. */ // DS1620 to Arduino pins #define DQ 5 #define CLK 4 #define RST 3 // DS1620 Commands #define READ_TEMP 0xAA // read temperature register #define WRITE_TH 0x01 // write high temperature register #define WRITE_TL 0x02 // write low temperature register #define READ_TH 0xA1 // read high temperature register #define READ_TL 0xA2 // read low temperature register #define READ_CNTR 0xA0 // read counter register #define READ_SLOPE 0xA9 // read slope register #define START_CNV 0xEE // start temperature conversion #define STOP_CNV 0x22 // stop temperature conversion #define WRITE_CFG 0x0C // write configuration register #define READ_CFG 0xAC // read configuration register // DS1620 configuration bits #define DONE B10000000 // Conversion Bit Done #define THF B01000000 // Temperature High Flag #define TLF B00100000 // Temperature Low Flag #define NVB B00010000 // Nonvolatile Memory Busy Flag #define CPU B00000010 // CPU Use Bit #define ONE_SHOT B00000001 // One-Shot Mode // RST HIGH to start communication // RST LOW to end communication #include LiquidCrystal lcd(7, 8, 9, 10, 11, 12, 13); void setup() { Serial.begin(9600); pinMode(DQ, OUTPUT); pinMode(RST, OUTPUT); pinMode(CLK, OUTPUT); // Write config and set TH alarm temp write_config(10); write_th(30); write_tl(15); //Serial.print("CONFIG: "); Serial.println(read_config()); //Serial.print("TH = "); Serial.println(read_th()); //Serial.print("TL = "); Serial.println(read_tl()); // Start temperature conversions (continuous) start_conv(); lcd.clear(); lcd.home(); lcd.print("DS1620"); lcd.setCursor(0,1); lcd.print("TH = "); lcd.print(read_th()); lcd.setCursor(10,1); lcd.print("TL = "); lcd.print(read_tl()); lcd.setCursor(0,2); lcd.print("TEMP = "); // Set pin to read TH ALARM pinMode(2, INPUT); } char heartbeat = '*'; void loop() { analogWrite(6, 70); lcd.setCursor(7,2); lcd.print(read_temp()); lcd.print(" deg C"); lcd.print(" "); delay(1000); lcd.setCursor(0,3); if(digitalRead(2) == HIGH) { lcd.print("T HIGH"); } else { lcd.print(" "); } if(heartbeat == '*'){heartbeat = ' ';} else {heartbeat = '*';} lcd.setCursor(19,3); lcd.print(heartbeat); Serial.println(heartbeat); } // Read Temperature [AAh] int read_temp(){ int t; rst_start(); send_command(READ_TEMP); // Next 9 clock cycles are last temperature conversion result t = receive_data()/2; rst_stop(); return(t); } // Write TH [01h] void write_th(int high_temp){ int bit; high_temp = high_temp * 2; rst_start(); send_command(WRITE_TH); // Next 9 clock cycles clock in value of the high temp limit for(int n=0; n<9; n++){ // Send all nine bits of temperature bit = (high_temp >> n) & 0x01; digitalWrite(DQ, bit); // DQ HIGH or LOW based on bit digitalWrite(CLK, LOW); // CLK LOW then HIGH to make one cycle digitalWrite(CLK, HIGH); } delay(50); // Write can take up to 10ms rst_stop(); } // Write TL [02h] void write_tl(int temp){ int bit; temp = temp * 2; rst_start(); send_command(WRITE_TL); // Next 9 clock cycles clock in value of the high temp limit for(int n=0; n<9; n++){ // Send all nine bits of temperature bit = (temp >> n) & 0x01; digitalWrite(DQ, bit); // DQ HIGH or LOW based on bit digitalWrite(CLK, LOW); // CLK LOW then HIGH to make one cycle digitalWrite(CLK, HIGH); } delay(50); // Write can take up to 10ms rst_stop(); } // Read TH [A1h] int read_th(){ int temp = 0; rst_start(); send_command(READ_TH); // Next 8 clock cycles output value of config register temp = receive_data()/2; rst_stop(); return(temp); } // Read TL [A2h] int read_tl(){ int temp = 0; rst_start(); send_command(READ_TL); // Next 8 clock cycles output value of config register temp = receive_data()/2; rst_stop(); return(temp); } // Read Counter [A0h] int read_counter(){ int counter = 0; rst_start(); send_command(READ_CNTR); // Next 9 clock cycles output value of counter counter = receive_data(); rst_stop(); return(counter); } // Read Slope [A9h] int read_slope(){ int slope = 0; rst_start(); send_command(READ_SLOPE); // Next 9 clock cycles output value of counter slope = receive_data(); rst_stop(); return(slope); } // Start Convert T [EEh] void start_conv(){ rst_start(); send_command(START_CNV); // Begins temperature conversion, depends on 1-shot mode rst_stop(); } // Stop Convert T [22h] void stop_conv(){ rst_start(); send_command(STOP_CNV); // Stops temperature conversion rst_stop(); } // Write Config [0Ch] int write_config(int config_register){ // Configuration/Status Register // DONE, THF, TLF, NVB, 1, 0, CPU, 1-SHOT // return 0 if successful // return 1 if fail if(config_register > 0) { rst_start(); send_command(WRITE_CFG); // Next 8 clock cycles clock in value of config register send_command(config_register); delay(50); // Write can take up to 10ms rst_stop(); // Confirm that config was properly written if(read_config() == config_register) { return 0; } else { return 1; } } } // Read Config [ACh] int read_config(){ int config_register = 0; rst_start(); send_command(READ_CFG); // Next 8 clock cycles output value of config register config_register = receive_data(); rst_stop(); return(config_register); } int receive_data(){ int data = 0; int n; int bit; pinMode(DQ, INPUT); // Change Data/DQ pin mode to accept INPUT for(n=0; n<9; n++) { // Always receive 9 bits of data digitalWrite(CLK, LOW); bit = digitalRead(DQ); digitalWrite(CLK, HIGH); data = data | bit << n; } pinMode(DQ, OUTPUT); // Done reading, set back to OUTPUT return(data); } void rst_start(){ digitalWrite(RST, LOW); digitalWrite(CLK, HIGH); digitalWrite(RST, HIGH); // All communications start by taking RST HIGH } void rst_stop(){ digitalWrite(RST, LOW); // Taking RST LOW will terminate any communication } void send_command(int command){ int n; int bit; for(n=0; n<8; n++){ // Always send 8 bits of data // Arithmetic bitwise shift command to the right and bitwise AND with // bitmask (00000001) to return next bit, least significant (rightmost) first bit = (command >> n) & 0x01; digitalWrite(DQ, bit); // DQ HIGH or LOW based on bit digitalWrite(CLK, LOW); // CLK LOW then HIGH to make one cycle digitalWrite(CLK, HIGH); } }