uint8_t ds18x20_read_temp(int16_t *t, int8_t *e) { uint8_t i; if(ow_command(COMMAND_CONVERT_T,NULL) == RES_FAULT) return RES_FAULT; //ow_strong_pullup_line(); _delay_ms(200); // //ow_normal_line(); if(ow_command(COMMAND_READ_SP,NULL) == RES_FAULT) return RES_FAULT; for (i=0; i<9; i++) ds18x20_sp[i] = ow_byte_read(); if(crc8(ds18x20_sp,9) == CRC_OK) { *t = ((ds18x20_sp[1] << 8) | (ds18x20_sp[0])) / 16; *e = ((ds18x20_sp[0] & M00001111) * 10) / 16; return RES_OK; } else { //*t = 127; //*e = 0; return RES_FAULT; } }
static void send_selected_rom(void) { #if ONLY_1_DEVICE_ON_THE_BUS == 1 ow_read_rom(ds18x20_rom_code); ow_command(OW_SKIP_ROM); #elif ONLY_1_DEVICE_ON_THE_BUS == 0 unsigned char x=0; ow_command(OW_MATCH_ROM); for(x=0; x<8; x++) { ow_put_byte(ds18x20_rom_code[x]); } #endif return; }
/** This routine writes the PIO bits * * \param id The serial number of the switch that is to be turned on or off * \param state The PIO Status Bit Assignment. (2 LSBs) * \return true(1): If set is successful * false(0): If set is not successful */ uint8_t ow_ds2413_write(uint8_t id[], uint8_t state) { uint8_t buff[5]; uint8_t wrByte; wrByte = state | 0xFC; // PIO Access Write Command ow_command(0x5a, id); // Channel write information buff[1] = ow_byte_wr(wrByte); // Inverted write byte buff[2] = ow_byte_wr(~wrByte); // Used to read the Status Bits buff[3] = ow_byte_wr(0xFF); // Used to read the PIO State buff[4] = ow_byte_wr(0xFF); if (buff[3] != 0xAA) { return false; } return true; }
void ds18x20_init() { //only for DS18B20 (not for DS18S20) ow_command(COMMAND_WRITE_SP,NULL); // write ow_byte_write(0x00); //Th ow_byte_write(0x00); //Tl ow_byte_write(0x7F); //config }
/* get power status of DS18x20 input : id = rom_code returns: DS18X20_POWER_EXTERN or DS18X20_POWER_PARASITE */ uint8_t DS18X20_get_power_status(uint8_t id[]) { uint8_t pstat; ow_reset(); ow_command(DS18X20_READ_POWER_SUPPLY, id); pstat=ow_bit_io(1); // pstat 0=is parasite/ !=0 ext. powered ow_reset(); return (pstat) ? DS18X20_POWER_EXTERN:DS18X20_POWER_PARASITE; }
/** This routine reads the PIO bits * * \param id The serial number of the switch that is to be turned on or off * \return true(1): If set is successful * false(0): If set is not successful */ uint8_t ow_ds2413_read(uint8_t id[]) { // PIO Access Read Command ow_command(0xf5, id); // Used to read the PIO Status Bit Assignment return ow_byte_wr(0xFF); }
/* reads temperature (scratchpad) of a single sensor (uses skip-rom) output: subzero==1 if temp.<0, cel: full celsius, mcel: frac in millicelsius*0.1 i.e.: subzero=1, cel=18, millicel=5000 = -18,5000�C */ uint8_t DS18X20_read_meas_single(uint8_t familycode, uint8_t *subzero, uint8_t *cel, uint8_t *cel_frac_bits, uint16_t *maalt) { uint8_t i; uint8_t sp[DS18X20_SP_SIZE]; ow_command(DS18X20_READ, NULL); for (i = 0 ; i < DS18X20_SP_SIZE; i++) sp[i] = ow_byte_rd(); if (crc8(&sp[0], DS18X20_SP_SIZE)) return DS18X20_ERROR_CRC; DS18X20_meas_to_cel(familycode, sp, subzero, cel, cel_frac_bits, maalt); return DS18X20_OK; }
/* reads temperature (scratchpad) of sensor with rom-code id output: subzero==1 if temp.<0, cel: full celsius, mcel: frac in millicelsius*0.1 i.e.: subzero=1, cel=18, millicel=5000 = -18,5000°C */ uint8_t DS18X20_read_meas(uint8_t *id, uint8_t *subzero, uint8_t *cel, uint8_t *cel_frac_bits) { uint8_t i; uint8_t sp[DS18X20_SP_SIZE]; ow_reset(); //** ow_command(DS18X20_READ, id); for ( i=0 ; i< DS18X20_SP_SIZE; i++ ) sp[i]=ow_byte_rd(); if ( crc8( &sp[0], DS18X20_SP_SIZE ) ) return DS18X20_ERROR_CRC; DS18X20_meas_to_cel(id[0], sp, subzero, cel, cel_frac_bits); return DS18X20_OK; }
/* start measurement (CONVERT_T) for all sensors if input id==NULL or for single sensor. then id is the rom-code */ uint8_t DS18X20_start_meas( uint8_t with_power_extern, uint8_t id[]) { ow_reset(); //** if( ow_input_pin_state() ) { // only send if bus is "idle" = high ow_command( DS18X20_CONVERT_T, id ); if (with_power_extern != DS18X20_POWER_EXTERN) ow_parasite_enable(); return DS18X20_OK; } else { return DS18X20_START_FAIL; } }
/* start measurement (CONVERT_T) for all sensors if input id==NULL or for single sensor. then id is the rom-code */ uint8_t DS18X20_start_meas(uint8_t with_power_extern, uint8_t id[]) { ow_reset(); //** if (ow_input_pin_state()) { // only send if bus is "idle" = high ow_command(DS18X20_CONVERT_T, id); if (with_power_extern != DS18X20_POWER_EXTERN) ow_parasite_enable(); return DS18X20_OK; } else { #ifdef DS18X20_VERBOSE printf_P(PSTR("DS18X20_start_meas: Short Circuit !\r")); #endif return DS18X20_START_FAIL; } }
uint8_t DS18X20_read_scratchpad( uint8_t id[], uint8_t sp[] ) { uint8_t i; ow_reset(); //** if( ow_input_pin_state() ) { // only send if bus is "idle" = high ow_command( DS18X20_READ, id ); for ( i=0 ; i< DS18X20_SP_SIZE; i++ ) sp[i]=ow_byte_rd(); return DS18X20_OK; } else { return DS18X20_ERROR; } }
uint8_t DS18X20_recall_E2( uint8_t id[] ) { ow_reset(); //** if( ow_input_pin_state() ) { // only send if bus is "idle" = high ow_command( DS18X20_RECALL_E2, id ); // TODO: wait until status is "1" (then eeprom values // have been copied). here simple delay to avoid timeout // handling _delay_ms(10); return DS18X20_OK; } else { return DS18X20_ERROR; } }
uint8_t DS18X20_write_scratchpad( uint8_t id[], uint8_t th, uint8_t tl, uint8_t conf) { ow_reset(); //** if( ow_input_pin_state() ) { // only send if bus is "idle" = high ow_command( DS18X20_WRITE_SCRATCHPAD, id ); ow_byte_wr(th); ow_byte_wr(tl); if (id[0] == DS18B20_ID) ow_byte_wr(conf); // config avail. on B20 only return DS18X20_OK; } else { return DS18X20_ERROR; } }
uint8_t DS18X20_read_scratchpad(uint8_t id[], uint8_t sp[]) { uint8_t i; ow_reset(); //** if (ow_input_pin_state()) { // only send if bus is "idle" = high ow_command(DS18X20_READ, id); for (i = 0 ; i < DS18X20_SP_SIZE; i++) sp[i] = ow_byte_rd(); return DS18X20_OK; } else { #ifdef DS18X20_VERBOSE printf_P(PSTR("DS18X20_read_scratchpad: Short Circuit !\r")); #endif return DS18X20_ERROR; } }
uint8_t DS18X20_recall_E2(uint8_t id[]) { ow_reset(); //** if (ow_input_pin_state()) { // only send if bus is "idle" = high ow_command(DS18X20_RECALL_E2, id); // TODO: wait until status is "1" (then eeprom values // have been copied). here simple delay to avoid timeout // handling _delay_ms(DS18X20_COPYSP_DELAY); return DS18X20_OK; } else { #ifdef DS18X20_VERBOSE printf_P(PSTR("DS18X20_recall_E2: Short Circuit !\r")); #endif return DS18X20_ERROR; } }
uint8_t DS18X20_write_scratchpad(uint8_t id[], uint8_t th, uint8_t tl, uint8_t conf) { ow_reset(); //** if (ow_input_pin_state()) { // only send if bus is "idle" = high ow_command(DS18X20_WRITE_SCRATCHPAD, id); ow_byte_wr(th); ow_byte_wr(tl); if (id[0] == DS18B20_ID) ow_byte_wr(conf); // config avail. on B20 only return DS18X20_OK; } else { #ifdef DS18X20_VERBOSE printf_P(PSTR("DS18X20_write_scratchpad: Short Circuit !\r")); #endif return DS18X20_ERROR; } }
uint8_t DS18X20_copy_scratchpad( uint8_t with_power_extern, uint8_t id[] ) { ow_reset(); //** if( ow_input_pin_state() ) { // only send if bus is "idle" = high ow_command( DS18X20_COPY_SCRATCHPAD, id ); if (with_power_extern != DS18X20_POWER_EXTERN) ow_parasite_enable(); _delay_ms(10); // wait for 10 ms if (with_power_extern != DS18X20_POWER_EXTERN) ow_parasite_disable(); return DS18X20_OK; } else { return DS18X20_START_FAIL; } }
uint8_t DS18X20_copy_scratchpad(uint8_t with_power_extern, uint8_t id[]) { ow_reset(); //** if (ow_input_pin_state()) { // only send if bus is "idle" = high ow_command(DS18X20_COPY_SCRATCHPAD, id); if (with_power_extern != DS18X20_POWER_EXTERN) ow_parasite_enable(); _delay_ms(DS18X20_COPYSP_DELAY); // wait for 10 ms if (with_power_extern != DS18X20_POWER_EXTERN) ow_parasite_disable(); return DS18X20_OK; } else { #ifdef DS18X20_VERBOSE printf_P(PSTR("DS18X20_copy_scratchpad: Short Circuit !\r")); #endif return DS18X20_START_FAIL; } }
int main(void) { wdt_disable(); g_state = MEASURE_AND_TRANSMIT; INIT_RADIO_PORT(); INIT_PERIPHERIAL_ON_PORT(); ON_PERIPHERIA(); _delay_ms(500); ow_reset(); _delay_ms(500); ow_command( DS18X20_CONVERT_T, 0 ); char numOfSensors = search_sensors(); //g_cyclesToSleep = CYCLES_BEFORE_SLEEP; sei(); _delay_ms(780); while(1){ switch(g_state){ case MEAS_AND_DISP://Таймер для отображения цифр, порт для вывода цифр и количество показов цифр устанавливаются в INT0 case MEASURE_AND_TRANSMIT:{ cli(); DS18X20_read_temp(gSensorIDs[0],&g_temp); sei(); g_selectSensor = 0; SendTempDataToBase(); if (numOfSensors > 1)//Если есть еще датчики то читаем от него данные { if (g_cyclesToSleep == 0) { _delay_ms(3); }else _delay_ms(TIME_TO_DISP_SENSOR); cli(); DS18X20_read_temp(gSensorIDs[1],&g_temp); sei(); g_selectSensor = 1; SendTempDataToBase(); } }break; case GO_TO_SLEEP:{//засыпаем по флагу сна STOP_TIMER(); OFF_PERIPHERIA(); DEINIT_DIGIT_SELECT_PORT(); DEINIT_SEGMENT_PORT(); g_cyclesToSleep = 0; //Настраиваем вход прерывания 0 как вход и разрешаем прерывание ENABLE_INT0(); START_ON_TIMER(); //Спим не глубоко а просыпаемся на тикающий таймер для просыпания и отправки температуры по радиоканалу set_sleep_mode(SLEEP_MODE_IDLE); sleep_mode(); //ON_PERIPHERIA(); }break; case GO_TO_DEEP_SLEEP:{//уходим в глубокий сон по истечению таймера перед отправкой температуры при взведенной собаке set_sleep_mode(SLEEP_MODE_PWR_DOWN); sleep_mode(); }break; } g_timerToSleep++;//Если оттикали количество секунд то уходим в сон if ((g_timerToSleep < g_cyclesToSleep))//если не оттикали нужное количество циклов то перезапускаем измерение и ждем { cli(); ow_reset(); ow_command( DS18X20_CONVERT_T, 0 ); sei(); _delay_ms(TIME_TO_DISP_SENSOR); }else{ g_timerToSleep = 0; g_state = GO_TO_SLEEP; } } }