Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:examples:sensor:1-wire [2012/05/14 15:44] heikopikneren:examples:sensor:1-wire [2020/07/20 09:00] (current) – external edit 127.0.0.1
Line 1: Line 1:
-<note important>Spell check</note>+====== 1-wire temperature sensor ======
  
-====== 1-wire temperature sensor  ====== 
 //The necessary knowledge: [HW] [[en:hardware:homelab:sensor]], [HW] [[en:hardware:homelab:digi]], //The necessary knowledge: [HW] [[en:hardware:homelab:sensor]], [HW] [[en:hardware:homelab:digi]],
 [HW] [[en:hardware:homelab:controller]], [LIB] [[en:software:homelab:library:pin]], [LIB] [[en:software:homelab:library:module:lcd_graphic]], [LIB] [[en:software:homelab:library:module:sensor]]// [HW] [[en:hardware:homelab:controller]], [LIB] [[en:software:homelab:library:pin]], [LIB] [[en:software:homelab:library:module:lcd_graphic]], [LIB] [[en:software:homelab:library:module:sensor]]//
Line 7: Line 6:
 ===== Theory ===== ===== Theory =====
  
-==== 1-wire protocol ==== +[{{  :examples:sensor:1wire:1-wire_temp_sensor.jpg?200|Temperature sensor}}] 
-[{{  :examples:sensor:1wire:1-wire_temp_sensor.jpg?200|}}]+
 Dallas Semiconductor Corp. worked out a communication bus system for simple sensors and other equipments called 1-wire protocol. This protocol provides low-speed data exchange, signaling and power over single signal wire. It is possible to connect up to 75 devices to one bus, forming MicroLan networks. MicroLan networks have one master unit, what controls network's traffic and ensures that one device at a time uses the bus. 1-wire protocol is similar to I2C protocol, but has lower data rates, longer distance range and only one wire for all communication. Dallas Semiconductor Corp. worked out a communication bus system for simple sensors and other equipments called 1-wire protocol. This protocol provides low-speed data exchange, signaling and power over single signal wire. It is possible to connect up to 75 devices to one bus, forming MicroLan networks. MicroLan networks have one master unit, what controls network's traffic and ensures that one device at a time uses the bus. 1-wire protocol is similar to I2C protocol, but has lower data rates, longer distance range and only one wire for all communication.
  
 1-wire communication is mostly used for communicating between different sensors and memory units. Bus data transfer rate is approximately 16.3 kbit/s. Communication is started by a master with the "reset" pulse, which pulls the wire to 0 volts for at least 480 µs. This signal resets all devices on the bus, simply taking the power out from the bus. After that, any device on the bus, shows that it exists with a "presence" pulse by holding the wire to ground at least 60 µs after the master releases the bus. With following 8-bit command and then data can be sent or received in groups of 8-bits. Each device on the bus has a unique 64-bit serial identification number. 1-wire communication is mostly used for communicating between different sensors and memory units. Bus data transfer rate is approximately 16.3 kbit/s. Communication is started by a master with the "reset" pulse, which pulls the wire to 0 volts for at least 480 µs. This signal resets all devices on the bus, simply taking the power out from the bus. After that, any device on the bus, shows that it exists with a "presence" pulse by holding the wire to ground at least 60 µs after the master releases the bus. With following 8-bit command and then data can be sent or received in groups of 8-bits. Each device on the bus has a unique 64-bit serial identification number.
  
-To find all the devices, the master send an enumeration command, and then an address. For each bit it listens to the answer. If a slave has all the right address bits it returns a 0. The master uses this simple behavior to search for valid sequences of address bits. An enumeration of 10 or 15 devices finishes very quickly.+[{{  :et:examples:sensor:ds_network2.jpg?500  |1-wire sensors connection}}]
  
-[{{:et:examples:sensor:ds_network2.jpg?500|1-wire sensor connection}}]+[{{  :en:examples:sensor:1_wire.png?300Data transfer on a 1-wire bus}}]
  
-<note warning>?</note> +To find all devices, master send an enumeration command, and an address. For each bit master listens the answer. If slave device has all right address bits it returns a 0. Master uses this simple behavior to search for valid sequences of address bits. An enumeration of 10 or 15 devices finishes very quickly.
-Bits are generated with the length of the pulse. To send "1" there will be generated 15 µs and to send "0" pulse+
  
-Example communication batween device and master+A read-time is initiated by master device pulling the 1-wire bus low for a minimum of 1 µs and then releasing the bus. Slave device transmits a 1 by leaving the bus high and transmits a 0 by pulling the bus low. 
  
-[{{:en:examples:sensor:homelab_1_wire.png?600| Data transfer on 1 Wire bus}}]+When transmitting 0, slave device releases the bus by the end of the time, and the bus will be pulled back to its high idle state by pull-up resistor. Output data from the slave is valid for 15 µs after the falling edge which initiated read-time. 
  
 ===== Practice ===== ===== Practice =====
-The DS18S20 digital thermometer provides 9–bit centigrade temperature measurements and has an  + 
-alarm function with nonvolatile user-programmable upper and lower trigger points. A digital thermometer DS18S20 with a 1-wire communication protocol can be connected with Robotic HomeLab Sensor module external sensor connectors. +The DS18S20 digital thermometer provides 9–bit centigrade temperature measurements and has an alarm function with nonvolatile user-programmable upper and lower trigger points. A digital thermometer DS18S20 with a 1-wire communication protocol can be connected with Robotic HomeLab Sensor module external sensor connectors. 
 Sensors's technical properties are following: Sensors's technical properties are following:
  
Line 36: Line 34:
 In the example below, 1-wire temperature sensor takes measurements and displays the results on HomeLab's User Interface board LCD display. With the present configuration and provided example code only 5 sensors can be connected to one bus. Sensors will be found automatically. In the example below, 1-wire temperature sensor takes measurements and displays the results on HomeLab's User Interface board LCD display. With the present configuration and provided example code only 5 sensors can be connected to one bus. Sensors will be found automatically.
  
-1-wire temperature sensor must be connected to sensor board ADC3 pin group. After loading the example program to the controller appears sensors queue number and temperature in Celsius on the User Interface board LCD display. If more than one sensor are connected on the bus, then sensor measurements are displayed in a row. Sensor and power supply type are also displayed. Homelab sensor's type is DS18S20 and power supply type is "externally". It's possible to connect sensor only with 2 wires, in that case sensor receives it's power supply from data bus and on display is displayed "parasite". "ERROR!" is shown in the display if error occurs, in this case most likely problem is in the wiring.+1-wire temperature sensor must be connected to sensor board ADC3 pin group. After loading the example program to the controller appears sensors queue number and temperature in Celsius on the User Interface board LCD display. If more than one sensor are connected on the bus, then sensor measurements are displayed in a row. Sensor and power supply type are also displayed. Homelab sensor's type is DS18S20 and power supply type is "externally". It's possible to connect sensor only with 2 wires, in that case sensor receives it's power supply from data bus and on display "parasite" is displayed. "ERROR!" is shown in the display if error occurs, in this case the problem is most likely related with wiring.
  
-The colors of the wires for connecting the sensor:+Colors of the wires for connecting the sensor:
   * Green - Optional VDD pin. VDD must be grounded for operation in parasite power mode   * Green - Optional VDD pin. VDD must be grounded for operation in parasite power mode
   * White - Data Input/Output pin. Open-drain 1-wire interface pin. Also provides power to the device when used in parasite power mode.   * White - Data Input/Output pin. Open-drain 1-wire interface pin. Also provides power to the device when used in parasite power mode.
Line 45: Line 43:
 Example code enabling to read the temperature with 1-wire protocol is shown below. It is important to include  "onewire.h" and "onewire.c" to the program. Sensor-specific code is located in "ds18x20.h" and "ds18x20.c" files. Example code enabling to read the temperature with 1-wire protocol is shown below. It is important to include  "onewire.h" and "onewire.c" to the program. Sensor-specific code is located in "ds18x20.h" and "ds18x20.c" files.
  
-{{:et:examples:sensor:ds18s20demo.rar|1-wire Example code}} +<pagebreak>
- +
-~~PB~~+
  
 <code c> <code c>
Line 53: Line 49:
 #include <string.h> #include <string.h>
 #include <stdio.h> #include <stdio.h>
- 
- 
 #include "onewire.h" #include "onewire.h"
 #include "ds18x20.h" #include "ds18x20.h"
- 
 #include <homelab/module/lcd_gfx.h> #include <homelab/module/lcd_gfx.h>
 #include <homelab/delay.h> #include <homelab/delay.h>
 #include <homelab/pin.h> #include <homelab/pin.h>
  
-// 
 // Sensor queue number and sensor's specific code are stored here // Sensor queue number and sensor's specific code are stored here
-// 
 extern uint8_t gSensorIDs[MAXSENSORS][OW_ROMCODE_SIZE]; extern uint8_t gSensorIDs[MAXSENSORS][OW_ROMCODE_SIZE];
  
 +// Main program
 int main( void ) int main( void )
 { {
- uint8_t nSensors, i; +  uint8_t nSensors, i; 
- int16_t decicelsius; +  int16_t decicelsius; 
- uint8_t error; +  uint8_t error; 
- char s[10]; +  char s[10]; 
- char sensor_nr[1];+  char sensor_nr[1];
   
- // Pin configuration of the Sensor board's multiplexer. +  // Pin configuration of the Sensor board's multiplexer. 
- pin multiplexer_pin = PIN(G, 0);+  pin multiplexer_pin = PIN(G, 0);
  
- // LCD display initialization  +  // LCD display initialization  
- lcd_gfx_init();+  lcd_gfx_init();
    
- //  LCD display clear +  //  LCD display clear 
- lcd_gfx_clear();+  lcd_gfx_clear();
    
- // Switching back light on  +  // Switching back light on  
- lcd_gfx_backlight(true); +  lcd_gfx_backlight(true);
    
-  // Multiplexer's pin configuration as a output and then switchover it   +  // Multiplexer's pin configuration as a output and then switchover it   
- // to connect external sensor with the controller . +  // to connect external sensor with the controller. 
- pin_setup_output(multiplexer_pin); +  pin_setup_output(multiplexer_pin); 
- pin_set(multiplexer_pin);  +  pin_set(multiplexer_pin);  
-  +  
- // 1-Wire bus configuration. In sensor board the ADC3 pin group is same as PF3. +  // 1-Wire bus configuration. In sensor board the ADC3 pin group is same as PF3. 
- ow_set_bus(&PINF,&PORTF,&DDRF,PF3);+  ow_set_bus(&PINF,&PORTF,&DDRF,PF3);
  
- // Searching for sensors. For variable nSensors is attributed the sum  +  // Searching for sensors. For variable nSensors is attributed the sum  
- // of all found sensors. +  // of all found sensors. 
- nSensors = search_sensors();+  nSensors = search_sensors();
  
- while(1)  +  while(1)  
-   +  
-  +    error = 0;
- error = 0;+
  
- // If no sensors found the error flag is set. +    // If no sensors found the error flag is set. 
- if ( nSensors == 0 )  +    if ( nSensors == 0 )  
- +    
- error++; +      error++; 
- }+    }
  
- // All the sensor are displayed starting at the bottom. +    // All the sensor are displayed starting at the bottom. 
- for ( i = nSensors; i > 0; i-- )  +    for ( i = nSensors; i > 0; i-- )  
- +    
- // Taking the measurements. In case of error, the error flag is set. + // Taking the measurements. In case of error, the error flag is set. 
- if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, &gSensorIDs[i-1][0] ) == DS18X20_OK )  + if (DS18X20_start_meas(DS18X20_POWER_PARASITE,&gSensorIDs[i-1][0])==DS18X20_OK)  
- +
- sw_delay_ms( 750 ); +   sw_delay_ms( 750 ); 
-  +  
- // Measurements are saved in decicelsius. In case of error, the error flag is set. +   // Measurements are saved in decicelsius. In case of error, 
- if ( DS18X20_read_decicelsius( &gSensorIDs[i-1][0], &decicelsius) == DS18X20_OK )  +   // the error flag is set. 
- +   if (DS18X20_read_decicelsius(&gSensorIDs[i-1][0],&decicelsius)==DS18X20_OK)  
-  +   
- // Displaying the word "TEMP"+     // Displaying the word "TEMP"
- lcd_gfx_goto_char_xy(2, 1); +     lcd_gfx_goto_char_xy(2, 1); 
- lcd_gfx_write_string("TEMP");+     lcd_gfx_write_string("TEMP");
  
- // Displaying degree sign +        // Displaying degree sign 
- lcd_gfx_goto_char_xy(13, 1); +     lcd_gfx_goto_char_xy(13, 1); 
- lcd_gfx_write_string("C"); +     lcd_gfx_write_string("C");
-  +
- // Making the readings to strings and adding +/-. +
- DS18X20_format_from_decicelsius( decicelsius, s, 10 );+
  
- // If is needed maximum accuracy use this function:  +     // Making the readings to strings and adding +/-
- // DS18X20_format_from_maxres( tval, s, 10 ); +     DS18X20_format_from_decicelsiusdecicelsiuss10 );
-  +
- // Displaying the temprerature +
- lcd_gfx_goto_char_xy(7, 1);  +
- lcd_gfx_write_string(s); +
-  +
- // Displaying sensors queue number. Firstly it's converted to a string. +
- lcd_gfx_goto_char_xy(0, 1); +
- sprintf(sensor_nr, "%d", i); +
- lcd_gfx_write_string(sensor_nr); +
- }  +
- else  +
-+
- // CRC error (Connection is down) +
- error++; +
-+
-  +
-+
- else  +
-+
- // Taking the measurements failed, maybe in data bus has a short circuit  +
- error++; +
-+
-+
-  +
-  +
- // Displaying the error messege+
- if error )  +
-+
- lcd_gfx_goto_char_xy(13); +
- lcd_gfx_write_string("ERROR!"); +
- error = 0; +
-+
- else +
-+
- lcd_gfx_goto_char_xy(13); +
- lcd_gfx_write_string("        "); +
- }+
  
- sw_delay_ms(500);+     // Displaying the temprerature 
 +     lcd_gfx_goto_char_xy(7, 1);  
 +     lcd_gfx_write_string(s); 
 + 
 +     // Displaying sensors queue number. 
 +     // Firstly it's converted to a string. 
 +     lcd_gfx_goto_char_xy(0, 1); 
 +     sprintf(sensor_nr, "%d", i); 
 +     lcd_gfx_write_string(sensor_nr); 
 +   }  
 +   else  
 +   { 
 +     // CRC error (Connection is down) 
 +     error++; 
 +   }
  }  }
 + else 
 + {
 +   error++;
 + }
 +    }
 +    // Displaying the error messege.
 +    if ( error ) 
 +    {
 + lcd_gfx_goto_char_xy(1, 3);
 + lcd_gfx_write_string("ERROR!");
 + error = 0;
 +    }
 +    else
 +    {
 + lcd_gfx_goto_char_xy(1, 3);
 + lcd_gfx_write_string("        ");
 +    }
 +    sw_delay_ms(500);
 +  }
 } }
- 
 </code> </code>
en/examples/sensor/1-wire.1337010273.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0