Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Revision 0:486c7ab9114b, committed 2017-06-05
- Comitter:
- Roietronics
- Date:
- Mon Jun 05 21:11:32 2017 +0000
- Commit message:
- Initial commit of one wire example for MBED 2
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DS18X20.h Mon Jun 05 21:11:32 2017 +0000 @@ -0,0 +1,60 @@ +/* DS18X20 specific values (see datasheet www.maxim-ic.com/datasheet/index.mvp/id/2812) */ +#ifndef _DS18X20_ +#define _DS18X20_ +#define DS18S20_ID 0x10 +#define DS18B20_ID 0x28 + +#define DS18X20_POWER_PARASITE 0x00 +#define DS18X20_POWER_EXTERN 0x01 +#define DS18X20_CONVERT_T 0x44 +#define DS18X20_READ 0xBE +#define DS18X20_WRITE 0x4E +#define DS18X20_EE_WRITE 0x48 +#define DS18X20_EE_RECALL 0xB8 +#define DS18X20_READ_POWER_SUPPLY 0xB4 + +#define DS18X20_OK 0x00 +#define DS18X20_ERROR 0x01 +#define DS18X20_START_FAIL 0x02 +#define DS18X20_ERROR_CRC 0x03 + +#define DS18B20_CONF_REG 4 +#define DS18B20_9_BIT 0 +#define DS18B20_10_BIT (1<<5) +#define DS18B20_11_BIT (1<<6) +#define DS18B20_12_BIT ((1<<6)|(1<<5)) + +// indefined bits in LSB if 18B20 != 12bit +#define DS18B20_9_BIT_UNDF ((1<<0)|(1<<1)|(1<<2)) +#define DS18B20_10_BIT_UNDF ((1<<0)|(1<<1)) +#define DS18B20_11_BIT_UNDF ((1<<0)) +#define DS18B20_12_BIT_UNDF 0 + +// conversion times in ms +#define DS18B20_TCONV_12BIT 750 +#define DS18B20_TCONV_11BIT DS18B20_TCONV_12_BIT/2 +#define DS18B20_TCONV_10BIT DS18B20_TCONV_12_BIT/4 +#define DS18B20_TCONV_9BIT DS18B20_TCONV_12_BIT/8 +#define DS18S20_TCONV DS18B20_TCONV_12_BIT + +// constant to convert the fraction bits to cel*(10^-4) +#define DS18X20_FRACCONV 625 + +#define DS18X20_SP_SIZE 9 + +// DS18X20 EEPROM-Support +#define DS18X20_WRITE_SCRATCHPAD 0x4E +#define DS18X20_COPY_SCRATCHPAD 0x48 +#define DS18X20_RECALL_E2 0xB8 +#define DS18X20_COPYSP_DELAY 10 /* ms */ +#define DS18X20_TH_REG 2 +#define DS18X20_TL_REG 3 + +uint8_t DS18X20_get_power_status(uint8_t id[]); +uint8_t DS18X20_start_meas( uint8_t with_external, uint8_t id[]); +uint8_t DS18X20_read_meas(uint8_t id[], uint8_t *subzero, uint8_t *cel, uint8_t *cel_frac_bits); +uint8_t DS18X20_read_meas_single(uint8_t familycode, uint8_t *subzero, uint8_t *cel, uint8_t *cel_frac_bits); +uint8_t DS18X20_meas_to_cel( uint8_t fc, uint8_t *sp, uint8_t* subzero, uint8_t* cel, uint8_t* cel_frac_bits); +uint16_t DS18X20_temp_to_decicel(uint8_t subzero, uint8_t cel, uint8_t cel_frac_bits); +int8_t DS18X20_temp_cmp(uint8_t subzero1, uint16_t cel1, uint8_t subzero2, uint16_t cel2); +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DS2450.h Mon Jun 05 21:11:32 2017 +0000 @@ -0,0 +1,41 @@ +/* DS2450 specific values (see datasheet www.maxim-ic.com/datasheet/index.mvp/id/2921) */ +#ifndef _DS2450_ +#define _DS2450_ +#define DS2450_ID 0x20 +#define DS2450_READ_MEMORY 0xAA +#define DS2450_WRITE_MEMORY 0x55 +#define DS2450_CONVERT 0x3C + +#define DS2450_SP_SIZE 13 +#define DS2450_PAGE0 0x00 +#define DS2450_PAGE1 0x08 +#define DS2450_PAGE2 0x10 +#define DS2450_PAGE3 0x18 + +#define DS2450_ADCA 0x00 //channel A +#define DS2450_ADCB 0x02 //channel B +#define DS2450_ADCC 0x04 //channel C +#define DS2450_ADCD 0x06 //channel D + +#define DS2450_IR_2V5 0x00 //input voltage range 2.55V +#define DS2450_IR_5V1 0x01 //input voltage range 5.1V +#define DS2450_AFH 0x20 //flag alarm value higher +#define DS2450_AFL 0x10 //flag alarm value lower +#define DS2450_AEH_ENABLE 0x08 //alarm value higher enable +#define DS2450_AEL_ENABLE 0x04 //alarm value lower enable + +#define DS2450_DISABLE_OUT 0x00 //disable ouput +#define DS2450_ENABLE_OUT 0x80//enable ouput + +#define DS2450_16_BIT 0x00 //ADC 16bits enable ouput +#define DS2450_15_BIT 0x0F //ADC 15bits enable ouput +#define DS2450_12_BIT 0x0C //ADC 12bits enable ouput +#define DS2450_8_BIT 0x08 //ADC 8bits enable ouput +#define DS2450_1_BIT 0x01 //ADC 1bits enable ouput +uint8_t DS2450_read_page(uint8_t id[], uint8_t adresse, uint8_t *val); +uint8_t DS2450_convert(uint8_t id[], uint8_t input_select_mask,uint8_t read_out_control); +uint8_t DS2450_read_ADC(uint8_t id[], uint16_t adc[]); +uint8_t DS2450_start_and_read_ADC(uint8_t id[], uint16_t adc[]); +uint8_t DS2450_configure_channel_ADC(uint8_t id[],uint8_t channel,uint8_t conflsb,uint8_t confmsb); +uint8_t DS2450_configure_page(uint8_t id[], uint8_t adresse,uint8_t configpage[]); +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/crc8.cpp Mon Jun 05 21:11:32 2017 +0000
@@ -0,0 +1,39 @@
+#include <inttypes.h>
+
+#define CRC8INIT 0x00
+#define CRC8POLY 0x18 //0X18 = X^8+X^5+X^4+X^0
+
+uint8_t crc8 ( uint8_t *data_in, uint16_t number_of_bytes_to_read )
+{
+ uint8_t crc;
+ uint16_t loop_count;
+ uint8_t bit_counter;
+ uint8_t data;
+ uint8_t feedback_bit;
+
+ crc = CRC8INIT;
+
+ for (loop_count = 0; loop_count != number_of_bytes_to_read; loop_count++)
+ {
+ data = data_in[loop_count];
+
+ bit_counter = 8;
+ do {
+ feedback_bit = (crc ^ data) & 0x01;
+
+ if ( feedback_bit == 0x01 ) {
+ crc = crc ^ CRC8POLY;
+ }
+ crc = (crc >> 1) & 0x7F;
+ if ( feedback_bit == 0x01 ) {
+ crc = crc | 0x80;
+ }
+
+ data = data >> 1;
+ bit_counter--;
+
+ } while (bit_counter > 0);
+ }
+
+ return crc;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/crc8.h Mon Jun 05 21:11:32 2017 +0000 @@ -0,0 +1,4 @@ +#ifndef _CRC8_ +#define _CRC8_ +uint8_t crc8 (uint8_t* data_in, uint16_t number_of_bytes_to_read); +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Mon Jun 05 21:11:32 2017 +0000
@@ -0,0 +1,237 @@
+/*****************************************************************************
+ * *
+ * BOSCH RainGauge LoraWan Interface *
+ * *
+ * (c) 2016 Global Quality Corp *
+ * mailto:sudhir@gqc.com *
+ * Global Quality Corp *
+ * *
+ * Pal Alto, CA. 94040 USA *
+ * *
+ * PERMISSION GRANTED TO USE IN NON-COMMERCIAL AND EDUCATIONAL PROJECTS *
+ * NO RESPONSIBILITY ASSUMED FOR DAMAGE OR LOSS OF ANY HARDWARE OR SOFTWARE *
+ * *
+ * version 0.2 *
+ * last change 7, Dec 2016 *
+ * *
+ ****************************************************************************/
+#include "mbed.h"
+#include "onewire.h"
+#include "DS18X20.h"
+#include "DS2450.h"
+
+uint8_t gSensorIDs[MAXSENSORS][OW_ROMCODE_SIZE];
+uint8_t nSensors;
+uint32_t nErreur;
+uint32_t nErreur_DS18X20;
+uint32_t nErreur_DS2450;
+uint32_t nErreur_CRC;
+uint32_t nErreur_BUS;
+uint32_t nErreur_DS2450_START;
+uint32_t nErreur_BUSY;
+uint32_t nErreur_RESET;
+uint32_t nMesure;
+uint32_t micro_seconds;
+Serial pc(USBTX, USBRX); // tx, rx
+BusOut bugleds(LED1, LED2, LED3, LED4);
+
+/**
+* @brief DS18X20_show_temp
+* @param [in] id[] = rom_code
+* @param [in] n number of id[n]
+* @param [out] text temp in degre celsius
+
+* @date 20/06/2011
+*/
+/*
+void DS18X20_show_temp(uint8_t subzero, uint8_t cel, uint8_t cel_frac_bits,char *text) {
+ uint16_t decicelsius;
+ char s[10];
+ float temperature;
+ sprintf(text,"");
+ sprintf(s,"%s", (subzero)?"-":"+");
+ strcat(text,s);
+ decicelsius = DS18X20_temp_to_decicel(subzero, cel, cel_frac_bits);
+ temperature = decicelsius;
+ temperature = temperature/10;
+ sprintf(s,"%4.1f", temperature);
+ strcat(text,s);
+
+}
+*/
+
+int main(void) {
+ uint8_t sp[25];
+ uint8_t num_sensor;
+ uint8_t subzero, cel, cel_frac_bits;
+ Timer timer;
+
+ pc.printf("version 2017-06-2\n");
+ pc.printf("Qantum 1 wire interface test!\n");
+
+
+
+ ow_PullUp();
+ if (search_sensors(&nSensors,&gSensorIDs[0][0])) {
+ pc.printf("no Sensors found\n");
+ return -1;
+ } else {
+ pc.printf("Sensors found : %d\n", nSensors);
+
+ for (num_sensor=0; num_sensor<nSensors; ++num_sensor) {
+ char text_id[25];
+ ow_show_id( &gSensorIDs[num_sensor][0], OW_ROMCODE_SIZE,text_id );
+ pc.printf("%s Sensor # %d is a ",text_id, num_sensor+1);
+ //DS18X20
+ if (( gSensorIDs[num_sensor][0] == DS18S20_ID) || ( gSensorIDs[num_sensor][0] == DS18B20_ID)) {
+ if ( gSensorIDs[num_sensor][0] == DS18S20_ID)
+ pc.printf("DS18S20/DS1820 ");
+ if ( gSensorIDs[num_sensor][0] == DS18B20_ID)
+ pc.printf("DS18B20 ");
+/*
+ if ( DS18X20_get_power_status( &gSensorIDs[num_sensor][0] ) ==
+ DS18X20_POWER_PARASITE )
+ pc.printf( "parasite" );
+ else pc.printf( "externally" );
+ pc.printf( " powered\n" );
+8/
+ }
+ //DS2450
+ /*
+ if ( gSensorIDs[num_sensor][0] == DS2450_ID) {
+ pc.printf("DS2450 ");
+ for (uint8_t i=0 ; i< DS2450_SP_SIZE; i++ ) // DS2450 POWER extern
+ sp[i]=0;
+ sp[4]=0x40;
+ if (DS2450_configure_page(&gSensorIDs[num_sensor][0], DS2450_PAGE3,&sp[0]))
+ pc.printf("CRC Error conf page \n");
+ if (DS2450_read_page(&gSensorIDs[num_sensor][0], DS2450_PAGE3,&sp[0]))
+ ;
+ else
+ printf( "\n" );
+ for ( char i=0 ; i< DS2450_SP_SIZE; i++ )
+ printf(":%2.2X",sp[i]);
+ printf( "\n" );
+ if (DS2450_configure_channel_ADC(&gSensorIDs[num_sensor][0],DS2450_ADCA,DS2450_12_BIT ,DS2450_IR_5V1))
+ pc.printf("CRC Error conf ADC\n");
+ if (DS2450_configure_channel_ADC(&gSensorIDs[num_sensor][0],DS2450_ADCB,DS2450_1_BIT ,DS2450_IR_5V1))
+ pc.printf("CRC Error conf ADC\n");
+ if (DS2450_configure_channel_ADC(&gSensorIDs[num_sensor][0],DS2450_ADCC,DS2450_1_BIT,DS2450_IR_5V1))
+ pc.printf("CRC Error conf ADC\n");
+ if (DS2450_configure_channel_ADC(&gSensorIDs[num_sensor][0],DS2450_ADCD,DS2450_1_BIT,DS2450_IR_5V1))
+ pc.printf("CRC Error conf ADC\n");
+ uint16_t adc[4];
+ if (DS2450_start_and_read_ADC(&gSensorIDs[num_sensor][0], &adc[0]))
+ pc.printf("CRC Error read ADC\n");
+ for (uint8_t i=0; i<4; ++i )
+ pc.printf(" adc%d:%X %f |",i,adc[i],adc[i]*5.1/65535);
+ pc.printf("\n");
+ }
+*/
+ }
+ nErreur=0;
+ nErreur_CRC=0;
+ nErreur_BUS=0;
+ nErreur_DS18X20=0;
+ nErreur_DS2450=0;
+ nErreur_DS2450_START=0;
+ nErreur_BUSY=0;
+ nErreur_RESET=0;
+ nMesure=0;
+ timer.start();
+/*
+ while (1) {
+ wait(1); // temps attente
+ nMesure++;
+ pc.printf("Mesure:%d erreur%d\n",nMesure,nErreur);
+ bugleds=nErreur;
+ switch (DS18X20_start_meas(DS18X20_POWER_EXTERN, 0 )) { // start measure ALL DS18X20
+ case DS18X20_OK :
+ wait_ms(DS18B20_TCONV_12BIT);
+ break;
+ case DS18X20_START_FAIL:
+ nErreur++;
+ nErreur_DS18X20++;
+ pc.printf("sensor DS18X20 : Start meas. failed \n");
+ break;
+ }
+
+ for (num_sensor=0; num_sensor<nSensors; ++num_sensor) {
+ //DS2450
+ if (gSensorIDs[num_sensor][0] == DS2450_ID) {
+ micro_seconds=timer.read_us();
+ uint16_t adc[4];
+ if (DS2450_start_and_read_ADC(&gSensorIDs[num_sensor][0], &adc[0])) // 3 essais avant erreur
+ if (DS2450_start_and_read_ADC(&gSensorIDs[num_sensor][0], &adc[0]))
+ switch (DS2450_start_and_read_ADC(&gSensorIDs[num_sensor][0], &adc[0])) {
+ case OW_OK:
+
+ break;
+ case OW_BUSY:
+ nErreur++;
+ nErreur_DS2450++;
+ nErreur_BUSY++;
+
+ break;
+ case OW_ERROR:
+ nErreur++;
+ nErreur_DS2450++;
+ nErreur_BUS++;
+
+ break;
+ case OW_ERROR_CRC:
+ nErreur++;
+ nErreur_DS2450++;
+ nErreur_CRC++;
+ pc.printf("Error:%d\n",nErreur);
+ pc.printf("ErrorDS2450_busy:%d\n",nErreur_BUSY);
+ pc.printf("ErrorDS2450_start:%d\n",nErreur_DS2450_START);
+ pc.printf("ErrorDS2450:%d\n",nErreur_DS2450);
+ pc.printf("ErrorDS18X20:%d\n",nErreur_DS18X20);
+ pc.printf("ErrorCRC:%d\n",nErreur_CRC);
+ pc.printf("ErrorBUS:%d\n",nErreur_BUS);
+ pc.printf("ErrorRESET:%d\n",nErreur_RESET);
+ pc.printf("Mesure:%d\n",nMesure);
+ break;
+ case OW_SHORT_CIRCUIT:
+ nErreur++;
+ nErreur_DS2450++;
+ nErreur_RESET++;
+ break;
+ case OW_ERROR_BAD_ID:
+
+ break;
+ }
+ micro_seconds = timer.read_us()- micro_seconds; // duree lecture DS2450
+ pc.printf("sensor %d : time:%umicros\n",num_sensor+1,micro_seconds);
+ for (uint8_t i=0; i<4; ++i )
+ pc.printf(" adc%d:%X %f |",i,adc[i],adc[i]*5.1/65535);
+ pc.printf("\n");
+ }
+
+ //TEMP DS1820
+ if ( (gSensorIDs[num_sensor][0] == DS18S20_ID) ||( gSensorIDs[num_sensor][0] == DS18B20_ID))
+ switch (DS18X20_read_meas( &gSensorIDs[num_sensor][0], &subzero, &cel, &cel_frac_bits)) {
+ case DS18X20_OK :
+ char text[25];
+ DS18X20_show_temp(subzero, cel, cel_frac_bits,text);
+ pc.printf("sensor %d : %s\n",num_sensor+1,text);
+ break;
+ case OW_ERROR:
+ nErreur++;
+ nErreur_BUS++;
+ nErreur_DS18X20++;
+ pc.printf("sensor %d : BUS Error\n",num_sensor+1);
+ break;
+ case DS18X20_ERROR_CRC:
+ nErreur++;
+ nErreur_DS18X20++;
+ nErreur_CRC++;
+ pc.printf("sensor %d : CRC Error\n",num_sensor+1);
+ break;
+ }
+ }
+*/
+ }
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.h Mon Jun 05 21:11:32 2017 +0000 @@ -0,0 +1,19 @@ +/***************************************************************************** + * * + * Dallas Semiconductorn (Maximum) One Wire Device Enumerator * + * * + * (c) 2016 Global Quality Corp * + * mailto:sudhir@gqc.com * + * Global Quality Corp * + * * + * Pal Alto, CA. 94040 USA * + * * + * PERMISSION GRANTED TO USE IN NON-COMMERCIAL AND EDUCATIONAL PROJECTS * + * NO RESPONSIBILITY ASSUMED FOR DAMAGE OR LOSS OF ANY HARDWARE OR SOFTWARE * + * * + * version 0.1 * + * last change 2, June 2017 * + * * + ****************************************************************************/ + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon Jun 05 21:11:32 2017 +0000 @@ -0,0 +1,1 @@ +https://mbed.org/users/mbed_official/code/mbed/builds/86740a56073b \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/onewire.cpp Mon Jun 05 21:11:32 2017 +0000
@@ -0,0 +1,318 @@
+/**
+* @file onewire.c
+* @brief library 1-Wire(www.maxim-ic.com)
+* @author Maciej Rajtar (Published 10 May 2010 www.mbed.org)
+* @author Frederic BLANC
+*/
+#include "mbed.h"
+#include "onewire.h"
+#include "DS2450.h"
+#include "DS18X20.h"
+#include "crc8.h"
+
+DigitalInOut ow_pin(ONEWIRE_PIN);
+
+/**
+* @brief PUL-UP bus OW
+* @return OW_OK
+* @date 20/06/2011
+*/
+uint8_t ow_PullUp(void)
+{
+ ow_pin.mode(PullUp); //PULL-UP bus OW
+return OW_OK;
+}
+/**
+* @brief search_sensors
+* @param [out] nSensors number of device onewire
+* @param [out] gSensorIDs[][] array of id romcode
+* @return OW_OK or OW_PRESENCE_ERR or OW_DATA_ERR
+* @date 20/06/2011
+*/
+uint8_t search_sensors(uint8_t *nSensors,uint8_t *gSensorIDs ) {
+ uint8_t i;
+ uint8_t id[OW_ROMCODE_SIZE];
+ uint8_t diff;
+ printf( "Scanning Bus\n" );
+ diff = OW_SEARCH_FIRST;
+ for (*nSensors = 0 ; (diff != OW_LAST_DEVICE) && (*nSensors < MAXSENSORS) ;++(*nSensors) ) {
+ ow_find_sensor( &diff, &id[0] );
+ if ( diff == OW_PRESENCE_ERR ) {
+ printf( "No Sensor found\n" );
+ return diff;
+ }
+ if ( diff == OW_DATA_ERR ) {
+ printf( "Bus Error\n" );
+ return diff;
+ }
+ for (i=0;i<OW_ROMCODE_SIZE;i++)
+ gSensorIDs[(*nSensors)*OW_ROMCODE_SIZE+i]=id[i];
+
+ }
+ return OW_OK;
+}
+/**
+* @brief show_id
+* @param [in] id[] = rom_code
+* @param [in] n number of id[n]
+* @param [out] text id
+* @return OW_OK or OW_ERROR_CRC
+* @date 20/06/2011
+*/
+uint8_t ow_show_id( uint8_t id[], size_t n ,char *text) {
+ size_t i;
+ char hex[4];
+ sprintf(text,"");
+
+ for ( i = 0; i < n; i++ ) {
+ if ( i == 0 ) strcat(text, " FC: " );
+ else if ( i == n-1 ) strcat(text, "CRC: " );
+ if ( i == 1 ) strcat(text, " SN: " );
+ sprintf(hex,"%2.2X ",id[i]);
+ strcat(text,hex);
+ if ( i == 0 ) {
+ if ( id[0] == DS18S20_ID ) strcat(text,"(18S)");
+ else if ( id[0] == DS18B20_ID ) strcat(text,"(18B)");
+ else if ( id[0] == DS2450_ID ) strcat(text,"(ADC)");
+ else strcat(text,"( ? )");
+ }
+ }
+ if ( crc8( id, OW_ROMCODE_SIZE) )
+ return OW_ERROR_CRC;
+ return OW_OK;
+}
+
+/**
+* @brief test pin onewire bus
+* @return etat pin ow
+* @date 20/06/2011
+*/
+uint8_t ow_test_pin (void){
+ if (ow_pin)
+ return 1;
+return 0;
+}
+/**
+* @brief onewire reset bus
+* @return pin ow or OW_SHORT_CIRCUIT
+* @date 20/06/2011
+*/
+uint8_t ow_reset(void) { // reset. Should improve to act as a presence pulse
+ uint8_t err;
+
+ ow_pin.output();
+ ow_pin = 0; // bring low for 500 us
+ wait_us(500);
+ ow_pin.input();
+ wait_us(60);
+ err = ow_pin;
+ wait_us(240);
+ if ( ow_pin == 0 ) { // short circuit
+ err = OW_SHORT_CIRCUIT;
+ }
+ return err;
+}
+
+/**
+* @brief read write onewire
+* @param [in/out] b data
+* @return data
+* @date 20/06/2011
+*/
+uint8_t ow_bit_io( uint8_t b ) {
+
+ ow_pin.output(); // drive bus low
+ ow_pin = 0;
+ wait_us(1); // Recovery-Time wuffwuff was 1
+
+ if ( b )
+ ow_pin.input(); // if bit is 1 set bus high (by ext. pull-up)
+ // delay was 15uS-1 see comment above
+ wait_us(15-1);
+ if ( ow_pin == 0 ) b = 0; // sample at end of read-timeslot
+ wait_us(60-15);
+ ow_pin.input();
+ return b;
+}
+
+/**
+* @brief byte write on onewire
+* @param [in] b data
+* @return data
+* @date 20/06/2011
+*/
+uint8_t ow_byte_wr( uint8_t b ) {
+ uint8_t i = 8, j;
+
+ do {
+ j = ow_bit_io( b & 1 );
+ b >>= 1;
+ if ( j )
+ b |= 0x80;
+ } while ( --i );
+ return b;
+}
+
+/**
+* @brief byte read on onewire
+* @param [in] uint8_t b
+* @return
+* @date 20/06/2011
+*/
+uint8_t ow_byte_rd( void ) {
+ // read by sending 0xff (a dontcare?)
+ return ow_byte_wr( 0xFF );
+}
+
+
+/**
+* @brief search romcode
+* @param [in] uint8_t diff
+* @param [out] id romcode
+* @return next_diff or OW_LAST_DEVICE or OW_DATA_ERR or OW_PRESENCE_ERR
+* @date 20/06/2011
+*/
+uint8_t ow_rom_search( uint8_t diff, uint8_t id[] ) {
+ uint8_t i, j, next_diff;
+ uint8_t b;
+
+ if ( ow_reset() )
+ return OW_PRESENCE_ERR; // error, no device found
+ ow_byte_wr( OW_SEARCH_ROM ); // ROM search command
+ next_diff = OW_LAST_DEVICE; // unchanged on last device
+ i = OW_ROMCODE_SIZE * 8; // 8 bytes
+ do {
+ j = 8; // 8 bits
+ do {
+ b = ow_bit_io( 1 ); // read bit
+ if ( ow_bit_io( 1 ) ) { // read complement bit
+ if ( b ) // 11
+ return OW_DATA_ERR; // data error
+ } else {
+ if ( !b ) { // 00 = 2 devices
+ if ( diff > i || ((*id & 1) && diff != i) ) {
+ b = 1; // now 1
+ next_diff = i; // next pass 0
+ }
+ }
+ }
+ ow_bit_io( b ); // write bit
+ *id >>= 1;
+ if ( b )
+ *id |= 0x80; // store bit
+ --i;
+ } while ( --j );
+ id++; // next byte
+ } while ( i );
+ return next_diff; // to continue search
+}
+
+/**
+* @brief write command
+* @param [in] command
+* @param [in] id romcode
+* @date 20/06/2011
+*/
+uint8_t ow_command( uint8_t command, uint8_t id[] ) {
+ uint8_t i;
+
+ ow_reset();
+ if ( id ) {
+ ow_byte_wr( OW_MATCH_ROM ); // to a single device
+ i = OW_ROMCODE_SIZE;
+ do {
+ ow_byte_wr( *id );
+ ++id;
+ } while ( --i );
+ } else {
+ ow_byte_wr( OW_SKIP_ROM ); // to all devices
+ }
+ ow_byte_wr( command );
+ return 0;
+}
+/**
+* @brief parasite enable
+* @date 20/06/2011
+*/
+uint8_t ow_parasite_enable(void) {
+ ow_pin.output();
+ ow_pin = 1;
+ return 0;
+}
+/**
+* @brief parasite disable
+* @date 20/06/2011
+*/
+uint8_t ow_parasite_disable(void) {
+
+ ow_pin.input();
+ return 0;
+}
+
+
+/**
+* @brief find Sensors on 1-Wire-Bus
+* @param [in/out] diff is the result of the last rom-search
+* @param [out] is the rom-code of the sensor found
+* @return OW_OK or OW_ERROR
+*/
+uint8_t ow_find_sensor(uint8_t *diff, uint8_t id[]) {
+ for (;;)
+ {
+ *diff = ow_rom_search( *diff, &id[0] );
+ if ( *diff==OW_PRESENCE_ERR)
+ return OW_ERROR;
+ if ( *diff==OW_DATA_ERR )
+ return OW_ERROR;
+ if ( *diff == OW_LAST_DEVICE )
+ return OW_OK ;
+ if ( id[0] == DS18B20_ID || id[0] == DS18S20_ID )
+ return OW_OK ;
+ if ( id[0] == DS2450_ID )
+ return OW_OK ;
+ }
+}
+
+/**
+* @brief output byte d (least sig bit first)
+* @param [in] d output byte (least sig bit first)
+* @return OW_OK
+*/
+uint8_t OneWireOutByte(uint8_t d) { // output byte d (least sig bit first).
+ for (int n=8; n!=0; n--) {
+ if ((d & 0x01) == 1) { // test least sig bit
+ ow_pin.output();
+ ow_pin = 0;
+ wait_us(5);
+ ow_pin.input();
+ wait_us(80);
+ } else {
+ ow_pin.output();
+ ow_pin = 0;
+ wait_us(80);
+ ow_pin.input();
+ }
+ d=d>>1; // now the next bit is in the least sig bit position.
+ }
+ return OW_OK;
+}
+/**
+* @brief read byte, least sig byte first
+* @return byte, least sig byte first
+*/
+uint8_t OneWireInByte(void) { // read byte, least sig byte first
+ uint8_t d = 0, b;
+ for (int n=0; n<8; n++) {
+ ow_pin.output();
+ ow_pin = 0;
+ wait_us(5);
+ ow_pin.input();
+ wait_us(5);
+ b =ow_pin;
+ wait_us(50);
+ d = (d >> 1) | (b << 7); // shift d to right and insert b in most sig bit position
+ }
+ return d;
+}
+
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/onewire.h Mon Jun 05 21:11:32 2017 +0000 @@ -0,0 +1,56 @@ +/** +* @file onewire.h +* @brief library 1-Wire(www.maxim-ic.com) +* @author Maciej Rajtar (Published 10 May 2010 www.mbed.org) +* @author Frederic BLANC +*/ + +#ifndef _onewire_ +#define _onewire_ + +//#define DEBUG 1 +//#define DEBUG_L1 1 +#define ONEWIRE_PIN PB_0 + +#define MAXSENSORS 16 + +// rom-code size including CRC +#define OW_ROMCODE_SIZE 8 + +#define OW_OK 0x00 +#define OW_ERROR 0x01 +#define OW_START_FAIL 0x02 +#define OW_ERROR_CRC 0x03 +#define OW_ERROR_BAD_ID 0x04 +#define OW_BUSY 0x05 + +#define OW_MATCH_ROM 0x55 +#define OW_SKIP_ROM 0xCC +#define OW_SEARCH_ROM 0xF0 +#define OW_READ_ROM 0x33 +#define OW_CONDITIONAL_SEARCH 0xEC +#define OW_OVERDRIVE_SKIP_ROM 0x3C +#define OW_OVERDRIVE_MATCH_ROM 0x69 + +#define OW_SHORT_CIRCUIT 0xFF +#define OW_SEARCH_FIRST 0xFF // start new search +#define OW_PRESENCE_ERR 0x01 +#define OW_DATA_ERR 0xFE +#define OW_LAST_DEVICE 0x00 // last device found +// 0x01 ... 0x40: continue searching +uint8_t search_sensors(uint8_t *nSensors,uint8_t *gSensorIDs ); +uint8_t ow_PullUp(void); +uint8_t ow_show_id( uint8_t id[], size_t n ,char *text); +uint8_t ow_test_pin (void); +uint8_t ow_reset(void); +uint8_t ow_rom_search( uint8_t diff, uint8_t id[] ); +uint8_t ow_command( uint8_t command, uint8_t id[] ); +uint8_t ow_find_sensor(uint8_t *diff, uint8_t id[]); +uint8_t ow_parasite_enable(void); +uint8_t ow_parasite_disable(void); +uint8_t ow_bit_io( uint8_t b ); +uint8_t ow_byte_wr( uint8_t b ); +uint8_t ow_byte_rd( void ); + + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/utils.c Mon Jun 05 21:11:32 2017 +0000
@@ -0,0 +1,12 @@
+#include <inttypes.h>
+
+uint16_t uint8_to_uint16(uint8_t lsb, uint8_t msb)
+{
+ uint16_t data;
+ uint8_t *ptr;
+
+ ptr=(uint8_t*)&data;
+ *ptr=lsb;
+ *++ptr=msb;
+ return data;
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/utils.h Mon Jun 05 21:11:32 2017 +0000 @@ -0,0 +1,4 @@ +#ifndef _utils_ +#define _utils_ +uint16_t uint8_to_uint16(uint8_t lsb, uint8_t msb); +#endif \ No newline at end of file