void convert_show(void) { uint8_t i; int16_t decicelsius; uint8_t error; PORTB |= _BV(OW_PWR_PIN); // turn on power supply for DS18B20 for ( i = nSensors; i > 0; i-- ) { if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, &gSensorIDs[i-1][0] ) == DS18X20_OK ) { _delay_ms( DS18B20_TCONV_12BIT ); //uart_puts_P( "Sensor# " ); if ( DS18X20_read_decicelsius( &gSensorIDs[i-1][0], &decicelsius) == DS18X20_OK ) { //uart_put_temp( decicelsius ); deci2string(decicelsius, temperature_string); } else { //uart_puts_P( "CRC Error (lost connection?)" ); error++; } //uart_puts_P( NEWLINESTR ); } else { //uart_puts_P( "Start meas. failed (short circuit?)" ); error++; } } morse_emit(temperature_string[0] - 0x30); morse_emit(temperature_string[1] - 0x30); PORTB &= ~ _BV(OW_PWR_PIN); // turn off power supply for DS18B20 }
int main(int argc, char *argv[]) { if (argc != 2) { puts("Path to COM port required.\n"); return 1; } if (ow_init(argv[1])) { puts("Bus INIT failed. Check COM port.\n"); return 1; } uint8_t c = 0, diff = OW_SEARCH_FIRST; int16_t temp_dc; while (diff != OW_LAST_DEVICE) { DS18X20_find_sensor(&diff, id); if (diff == OW_ERR_PRESENCE) { puts("All sensors are offline now.\n"); ow_finit(); return 1; } if (diff == OW_ERR_DATA) { puts("Bus error.\n"); ow_finit(); return 1; } fprintf(stdout, "Bus %s Device %03u Type 0x%02hx (%s) ID %02hx%02hx%02hx%02hx%02hx%02hx CRC 0x%02hx ", \ argv[1], c, id[0], get_type_by_id(id[0]), id[6], id[5], id[4], id[3], id[2], id[1], id[7]); fflush(stdout); c ++; if (DS18X20_start_meas(DS18X20_POWER_EXTERN, NULL) == DS18X20_OK) { while (DS18X20_conversion_in_progress() == DS18X20_CONVERTING) { delay_ms(100); /* It will take a while */ } if (DS18X20_read_decicelsius(id, &temp_dc) == DS18X20_OK) { /* Copied from my MCU code, so no float point */ fprintf(stdout, "TEMP %3d.%01d C\n", temp_dc / 10, temp_dc > 0 ? temp_dc % 10 : -temp_dc % 10); continue; } } puts("MEASURE FAILED!\n"); } puts("Sensors listed.\n"); ow_finit(); return 0; }
int main( void ) { uint8_t nSensors, i; int16_t decicelsius; uint8_t error; uart_init((UART_BAUD_SELECT((BAUD),F_CPU))); #ifndef OW_ONE_BUS ow_set_bus(&PINA,&PORTA,&DDRA,PA6); #endif sei(); uart_puts_P( NEWLINESTR "DS18X20 1-Wire-Reader Demo by Martin Thomas" NEWLINESTR ); uart_puts_P( "-------------------------------------------" ); nSensors = search_sensors(); uart_put_int( (int)nSensors ); uart_puts_P( " DS18X20 Sensor(s) available:" NEWLINESTR ); #if DS18X20_VERBOSE for (i = 0; i < nSensors; i++ ) { uart_puts_P("# in Bus :"); uart_put_int( (int)i + 1); uart_puts_P(" : "); DS18X20_show_id_uart( &gSensorIDs[i][0], OW_ROMCODE_SIZE ); uart_puts_P( NEWLINESTR ); } #endif for ( i = 0; i < nSensors; i++ ) { uart_puts_P( "Sensor# " ); uart_put_int( (int)i+1 ); uart_puts_P( " is a " ); if ( gSensorIDs[i][0] == DS18S20_FAMILY_CODE ) { uart_puts_P( "DS18S20/DS1820" ); } else if ( gSensorIDs[i][0] == DS1822_FAMILY_CODE ) { uart_puts_P( "DS1822" ); } else { uart_puts_P( "DS18B20" ); } uart_puts_P( " which is " ); if ( DS18X20_get_power_status( &gSensorIDs[i][0] ) == DS18X20_POWER_PARASITE ) { uart_puts_P( "parasite" ); } else { uart_puts_P( "externally" ); } uart_puts_P( " powered" NEWLINESTR ); } #if DS18X20_EEPROMSUPPORT if ( nSensors > 0 ) { eeprom_test(); } #endif if ( nSensors == 1 ) { uart_puts_P( NEWLINESTR "There is only one sensor " "-> Demo of \"DS18X20_read_decicelsius_single\":" NEWLINESTR ); i = gSensorIDs[0][0]; // family-code for conversion-routine DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ); _delay_ms( DS18B20_TCONV_12BIT ); DS18X20_read_decicelsius_single( i, &decicelsius ); uart_put_temp( decicelsius ); uart_puts_P( NEWLINESTR ); } for(;;) { // main loop error = 0; if ( nSensors == 0 ) { error++; } uart_puts_P( NEWLINESTR "Convert_T and Read Sensor by Sensor (reverse order)" NEWLINESTR ); for ( i = nSensors; i > 0; i-- ) { if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, &gSensorIDs[i-1][0] ) == DS18X20_OK ) { _delay_ms( DS18B20_TCONV_12BIT ); uart_puts_P( "Sensor# " ); uart_put_int( (int) i ); uart_puts_P(" = "); if ( DS18X20_read_decicelsius( &gSensorIDs[i-1][0], &decicelsius) == DS18X20_OK ) { uart_put_temp( decicelsius ); } else { uart_puts_P( "CRC Error (lost connection?)" ); error++; } uart_puts_P( NEWLINESTR ); } else { uart_puts_P( "Start meas. failed (short circuit?)" ); error++; } } uart_puts_P( NEWLINESTR "Convert_T for all Sensors and Read Sensor by Sensor" NEWLINESTR ); if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ) == DS18X20_OK) { _delay_ms( DS18B20_TCONV_12BIT ); for ( i = 0; i < nSensors; i++ ) { uart_puts_P( "Sensor# " ); uart_put_int( (int)i + 1 ); uart_puts_P(" = "); if ( DS18X20_read_decicelsius( &gSensorIDs[i][0], &decicelsius ) == DS18X20_OK ) { uart_put_temp( decicelsius ); } else { uart_puts_P( "CRC Error (lost connection?)" ); error++; } uart_puts_P( NEWLINESTR ); } #if DS18X20_MAX_RESOLUTION int32_t temp_eminus4; for ( i = 0; i < nSensors; i++ ) { uart_puts_P( "Sensor# " ); uart_put_int( i+1 ); uart_puts_P(" = "); if ( DS18X20_read_maxres( &gSensorIDs[i][0], &temp_eminus4 ) == DS18X20_OK ) { uart_put_temp_maxres( temp_eminus4 ); } else { uart_puts_P( "CRC Error (lost connection?)" ); error++; } uart_puts_P( NEWLINESTR ); } #endif } else { uart_puts_P( "Start meas. failed (short circuit?)" ); error++; } #if DS18X20_VERBOSE // all devices: uart_puts_P( NEWLINESTR "Verbose output" NEWLINESTR ); DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ); _delay_ms( DS18B20_TCONV_12BIT ); DS18X20_read_meas_all_verbose(); #endif if ( error ) { uart_puts_P( "*** problems - rescanning bus ..." ); nSensors = search_sensors(); uart_put_int( (int) nSensors ); uart_puts_P( " DS18X20 Sensor(s) available" NEWLINESTR ); error = 0; } _delay_ms(3000); } }
int main (void) { // (2) ledidx_t i; DDRB = 0xFF; // Port B: 1 = output PORTB = 0x01; //bootup 1 //_delay_ms(1000); // Initialize LCD Display DDRC |= (1<<PC1) | (1<<PC3); //PC1 = R/W, PC3 = Backlight control PORTC &= ~(1<<PC1); //Switch Backlight on: PORTC |= (1<<PC3); _delay_ms(10); lcd_init(); PORTB = 0x02; //bootup 2 _delay_ms(100); lcd_string_P(PSTR("blinkylight 0.3 ")); lcd_setcursor(0,2); lcd_string_P(PSTR("Booting ... ")); //PORTB = 0x03; //bootup 3 //_delay_ms(1000); uart_init(); uart_putc('p'); uart_putc('w'); uart_putc('r'); uart_putc('O'); uart_putc('N'); uart_putc('\n'); //PORTB = 0x04; //bootup 4 //PORTB = 0x05; //bootup 5 // Enable Interrupts sei(); PORTB = 0x06; //bootup 6 // muss vor ws2801_init stehen, da dieser PA1 und PA2 als output schaltet DDRA = 0x00; // Port A: 0 = input PORTA = 0x00; // 0 = pull-ups off //PORTB = 0x0a; //bootup a PORTB = 0x00; //bootup d lcd_setcursor(0,2); lcd_string_P(PSTR("Boot complete ")); _delay_ms(10); //Switch Backlight off: PORTC &= ~(1<<PC3); // Enter main loop uint8_t dezisek = 0; #define DEZISEKTHRES 4 while(1) { // (5) /* "leere" Schleife*/ // (6) _delay_ms(25); //pb_scroll <<= 1; //if (pb_scroll == 0b00010000) pb_scroll = 0b00000001; //PORTB &= 0b11110000; //PORTB |= pb_scroll; PORTB ^= (1<<PB2); if (dezisek > DEZISEKTHRES) { if (relay_timer > 0) { relay_timer --; if (relay_timer == 0) relay_reset = 1; else { PORTB ^= ( 1 << PB5 )|(1<<PB6)|(1<<PB7); } } } dezisek++; if (disp_set) { lcd_clear(); lcd_home(); for(i=0;i<16;i++)lcd_data(disp_buf[i]); lcd_setcursor(0,2); for(;i<32;i++)lcd_data(disp_buf[i]); disp_set = 0; _delay_ms(250); } if (relay_set) { PORTB |= (1<<PB4); PORTB |= (1<<PB5)|(1<<PB6)|(1<<PB7); relay_set = 0; } if (relay_reset) { PORTB &= ~(1<<PB4); PORTB &= ~((1<<PB5)|(1<<PB6)|(1<<PB7)); relay_reset = 0; relay_timer = 0; } if (PINA & (1<<PA7)) { uart_putc('5'); } if (PINA & (1<<PA6)) { uart_putc('4'); } if (PINA & (1<<PA5)) { uart_putc('3'); } if (PINA & (1<<PA4)) { uart_putc('2'); } if (measure_temp == 1) { //PORTC ^= (1<<PC3); uint8_t sensor_id[OW_ROMCODE_SIZE]; uint8_t diff = OW_SEARCH_FIRST; ow_reset(); DS18X20_find_sensor(&diff, &sensor_id[0]); if (diff == OW_PRESENCE_ERR) strcpy_P(&disp_tmp_buf[0], PSTR("Err:Presence ")); else if (diff == OW_DATA_ERR) strcpy_P(&disp_tmp_buf[0], PSTR("Err:Data ")); else { if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ) == DS18X20_OK) { _delay_ms( DS18B20_TCONV_12BIT ); int16_t decicelsius; if ( DS18X20_read_decicelsius( &sensor_id[0], &decicelsius) == DS18X20_OK ) { disp_tmp_buf[0]='T'; disp_tmp_buf[1]='e'; disp_tmp_buf[2]='m'; disp_tmp_buf[3]='p'; disp_tmp_buf[4]=':'; disp_tmp_buf[5]=' '; DS18X20_format_from_decicelsius( decicelsius, &disp_tmp_buf[6], 8 ); } else { strcpy_P(&disp_tmp_buf[0], PSTR("Err: Read ")); } } else { strcpy_P(&disp_tmp_buf[0], PSTR("Err: StartMeasure")); } } sprintf(&disp_tmp_buf[16], "%d bytes recv.", recv_len); //disp_show_buf(&disp_tmp_buf[0]); for(i=0;i<20;i++) uart_putc(disp_tmp_buf[i]); measure_temp=0; } if (PINA & (1<<PA3)) { relay_set = 1; relay_timer = 10; uart_putc('1'); } //uart_putc('+'); // uart_putc('\n'); } // (7) /* wird nie erreicht */ return 0; // (8) }
int main( void ) { uint8_t i=0; int16_t decicelsius; uint8_t error; uint8_t delayCounter=0; hw_init(); uart_init((UART_BAUD_SELECT((BAUD),F_CPU))); memset(&sensor_fw, 0, sizeof(_sensor_data)); /* init 485 write */ DDRB |= 0b0000001; //1 = output, 0 = input //PORTB |= 0b00000001; //Enable pin 5 internal pullup PORTB &= 0b11111110; //Enable pin 5 internal pullup read //PORTB |= 0b00000001; //Enable pin 5 internal pullup 485 write led_g_on(); _delay_ms(1000); led_y_on(); _delay_ms(1000); led_r_on(); _delay_ms(1000); led_r_off(); _delay_ms(1000); led_y_off(); _delay_ms(1000); led_g_off(); #ifndef OW_ONE_BUS ow_set_bus(&PIND,&PORTD,&DDRD,PD6); #endif led_g_on(); search_bus(); led_g_off(); sei(); sensor_fw.fw_state = FW_STATE_SENSOR_START_MEAS; for(;;) { // main loop switch (sensor_fw.fw_state) { case FW_STATE_SENSOR_SEARCH: led_g_on(); search_bus(); led_g_off(); uart_puts_P("FW_STATE_SENSOR_SEARCH? =0\n"); sensor_fw.fw_state = FW_STATE_SENSOR_START_MEAS; break; case FW_STATE_SENSOR_START_MEAS: if ( sensor_fw.sensor_num == 0 ) { sensor_fw.fw_state = FW_STATE_SENSOR_SEARCH; uart_puts_P("error sensor num =0\n"); break; } if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ) == DS18X20_OK) { sensor_fw.fw_state = FW_STATE_SENSOR_DELAY_750ms; //reset_timeout(); } else { sensor_fw.fw_state = FW_STATE_SENSOR_SEARCH; uart_puts_P("error start mes faul =0\n"); } break; case FW_STATE_SENSOR_DELAY_750ms: //if (TCNT1 > 5400) //750 ms _delay_ms( DS18B20_TCONV_12BIT ); { sensor_fw.fw_state = FW_STATE_SENSOR_READ_I; i = 0; } break; case FW_STATE_SENSOR_READ_I: if ( DS18X20_read_decicelsius(&sensor_fw.sensors[i].id[0], &decicelsius ) == DS18X20_OK ) { sensor_fw.sensors[i].temp = decicelsius; uart_puts_P( "Sensor# " ); uart_put_int( (int)i + 1 ); uart_puts_P(" = "); uart_put_temp( decicelsius ); uart_puts_P( NEWLINESTR ); } else { //uart_puts_P( "CRC Error (lost connection?)" ); sensor_fw.fw_state = FW_STATE_SENSOR_SEARCH; uart_puts_P("error lost connection? =0\n"); break; } i++; if (i >= sensor_fw.sensor_num) { sensor_fw.fw_state = FW_STATE_SENSOR_DELAY_5s; delayCounter = 0; //reset_timeout(); } //uart_puts_P( NEWLINESTR ); break; case FW_STATE_SENSOR_DELAY_5s: //if (TCNT1 > (5 * TICKS_PER_SEC)) /*5s*/ _delay_ms( 1000 ); { if(delayCounter > 5) sensor_fw.fw_state = FW_STATE_SENSOR_START_MEAS; delayCounter++; } break; case FW_STATE_READ_COMM: { //uart_puts_P( "Communication cmd rx\n" ); switch(sensor_fw.comm.rxbuff[2]) { case 0x01: sensor_fw.comm.txbuff[0] = 0x7E; sensor_fw.comm.txbuff[1] = 0x01; // dev_id sensor_fw.comm.txbuff[2] = 0x02; // len sensor_fw.comm.txbuff[3] = 0x00; // len sensor_fw.comm.txbuff[4] = 0xBB; // data sensor_fw.comm.txbuff[5] = 0xBB; // data sensor_fw.comm.txbuff[6] = 0x00; // crc sensor_fw.comm.txbuff[7] = 0x00; // crc uart_putData(sensor_fw.comm.txbuff,8); break; } sensor_fw.comm.rxlen = 0; sensor_fw.comm.valid_cmd = 0; sensor_fw.fw_state = FW_STATE_SENSOR_START_MEAS; } break; } if(sensor_fw.comm.valid_cmd) { sensor_fw.fw_state = FW_STATE_READ_COMM; } //_delay_ms(3000); } }