This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:examples:sensor:1-wire [2012/05/17 09:52] – raivo.sell | en:examples:sensor:1-wire [2020/07/20 09:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | <note important> | + | ====== 1-wire temperature sensor ====== |
| - | ====== 1-wire temperature sensor | ||
| //The necessary knowledge: [HW] [[en: | //The necessary knowledge: [HW] [[en: | ||
| [HW] [[en: | [HW] [[en: | ||
| Line 7: | Line 6: | ||
| ===== Theory ===== | ===== Theory ===== | ||
| - | ==== 1-wire protocol ==== | + | [{{ : |
| - | [{{ : | + | |
| 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' | 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' | ||
| 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 " | 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 " | ||
| - | 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. | + | [{{ : |
| - | {{:et: | + | [{{ :en: |
| - | {{: | + | 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 read-time is initiated by master device pulling the 1-wire bus low for a | + | 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. |
| - | 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. | + | |
| - | When transmitting a 0, slave device | + | When transmitting a 0, slave device |
| ===== Practice ===== | ===== Practice ===== | ||
| - | The DS18S20 digital thermometer provides 9–bit centigrade temperature measurements and has an | + | 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. |
| - | 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' | Sensors' | ||
| Line 47: | Line 43: | ||
| Example code enabling to read the temperature with 1-wire protocol is shown below. It is important to include | Example code enabling to read the temperature with 1-wire protocol is shown below. It is important to include | ||
| - | ~~PB~~ | + | < |
| <code c> | <code c> | ||
| Line 53: | Line 49: | ||
| #include < | #include < | ||
| #include < | #include < | ||
| - | |||
| - | |||
| #include " | #include " | ||
| #include " | #include " | ||
| - | |||
| #include < | #include < | ||
| #include < | #include < | ||
| #include < | #include < | ||
| - | // | ||
| // Sensor queue number and sensor' | // Sensor queue number and sensor' | ||
| - | // | ||
| 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; | + | |
| - | 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' | + | |
| - | pin multiplexer_pin = PIN(G, 0); | + | pin multiplexer_pin = PIN(G, 0); |
| - | // LCD display initialization | + | |
| - | lcd_gfx_init(); | + | lcd_gfx_init(); |
| - | // LCD display clear | + | |
| - | lcd_gfx_clear(); | + | lcd_gfx_clear(); |
| - | // Switching back light on | + | |
| - | lcd_gfx_backlight(true); | + | lcd_gfx_backlight(true); |
| - | // Multiplexer' | + | |
| - | // 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(& | + | ow_set_bus(& |
| - | // 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) | + | |
| - | { | + | { |
| - | + | error = 0; | |
| - | error = 0; | + | |
| - | // 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. | + | |
| - | 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, | + | if (DS18X20_start_meas(DS18X20_POWER_PARASITE,& |
| - | { | + | { |
| - | sw_delay_ms( 750 ); | + | |
| - | + | ||
| - | // Measurements are saved in decicelsius. In case of error, the error flag is set. | + | |
| - | if ( DS18X20_read_decicelsius( & | + | // the error flag is set. |
| - | { | + | |
| - | + | | |
| - | // Displaying the word " | + | |
| - | lcd_gfx_goto_char_xy(2, | + | |
| - | lcd_gfx_write_string(" | + | |
| - | // Displaying degree sign | + | |
| - | lcd_gfx_goto_char_xy(13, | + | |
| - | lcd_gfx_write_string(" | + | |
| - | + | ||
| - | // Making the readings to strings and adding +/-. | + | |
| - | DS18X20_format_from_decicelsius( decicelsius, | + | |
| - | // If is needed maximum accuracy use this function: | + | |
| - | // DS18X20_format_from_maxres( tval, s, 10 ); | + | DS18X20_format_from_decicelsius( decicelsius, s, 10 ); |
| - | + | ||
| - | // Displaying | + | |
| - | lcd_gfx_goto_char_xy(7, | + | |
| - | lcd_gfx_write_string(s); | + | |
| - | + | ||
| - | // Displaying sensors queue number. Firstly it's converted | + | |
| - | lcd_gfx_goto_char_xy(0, | + | |
| - | sprintf(sensor_nr, | + | |
| - | 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(1, 3); | + | |
| - | lcd_gfx_write_string(" | + | |
| - | error = 0; | + | |
| - | } | + | |
| - | else | + | |
| - | { | + | |
| - | lcd_gfx_goto_char_xy(1, 3); | + | |
| - | 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, | ||
| + | sprintf(sensor_nr, | ||
| + | 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, | ||
| + | lcd_gfx_write_string(" | ||
| + | error = 0; | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | lcd_gfx_goto_char_xy(1, | ||
| + | lcd_gfx_write_string(" | ||
| + | } | ||
| + | sw_delay_ms(500); | ||
| + | } | ||
| } | } | ||
| - | |||
| </ | </ | ||