Esempio n. 1
0
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
}
// start a measurement for all sensors on the bus:
void start_temp_meas(void){
        gTemp_measurementstatus=0;
        if ( DS18X20_start_meas(NULL) != DS18X20_OK) 
		  {
                gTemp_measurementstatus=1;
        }
}
Esempio n. 3
0
void async_start_read_sensors(void) {
  logs_P( "\r\nReading temperature for all sensors - " ); 	
  if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ) == DS18X20_OK) {
	logs_P("started measurement.\r\n");	
  } else 
	logs_P("Start meas. failed (short circuit?)");
}
Esempio n. 4
0
void loopTempSensors(void) {
  error = 0;

  if ( nSensors == 0 ) {
    error++;
  }

  if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ) 
      == DS18X20_OK) {

    for ( i = 0; i < nSensors; i++ ) {
      if ( DS18X20_read_maxres( &gSensorIDs[i][0], &temp_eminus4 )
          == DS18X20_OK ) {
        ;; // we're cool - read a value.
        //uart_put_temp_maxres( temp_eminus4 );
      }
      else {
        error++;
      }
    }
  } else {
    error++;
  }

  if ( error ) {
    nSensors = search_sensors();
    //uart_put_int( (int) nSensors );
    error = 0;
  }
}
Esempio n. 5
0
int main(void)
{
	DDRB |= (1<<PB0);
	PORTB |= (1<<PB0);

	DDRD &= ~(1<<PD6);
	PORTD &= ~(1<<PD6);

	czujniki_cnt = search_sensors();

	DS18X20_start_meas(DS18X20_POWER_EXTERN, NULL);
	_delay_ms(750);
	if(DS18X20_OK == DS18X20_read_meas_single(0x28, &subzero, &cel, &cel_frac_bits)) display_temp();
	else {
		lcd_puts_P("err");
	}

     lcd_clrscr();   /* clear display home cursor */

 for(;;){


 }


}
Esempio n. 6
0
void set1WirePin(struct IO_pin *vpin, uint8_t power_mode) {
    uint8_t port_num = 0;
    uint8_t pin_num = 0;
    uint8_t eeprom_virt_data_addr = 0;

#ifndef OW_ONE_BUS
    //set current pin to current used 1 wire bus
    ow_set_bus(vpin->PPIN, vpin->PPORT, vpin->PDDR, vpin->pin);
#endif


    for (uint8_t vpin_num = 0; vpin_num < COUNT_IO_PINS; vpin_num++) {
        port_num = vpin_num/8;
        pin_num = vpin_num%8;
        eeprom_virt_data_addr = VIRTUAL_DATA_START + (port_num * (VIRTUAL_PORT_PINCOUNT * VIRTUAL_DATA_LENGTH)) + (pin_num * VIRTUAL_DATA_LENGTH);


        if(&io_pins[port_num].pins[pin_num] == vpin) {
            I2C_MAIN_DEBUG("Found OW PIN[0x%x;0x%x]\r\n", port_num, pin_num);
            //reset data
            txbuffer[eeprom_virt_data_addr] = 0;
            txbuffer[eeprom_virt_data_addr + 1] = 0xFF;
            txbuffer[eeprom_virt_data_addr + 2] = 0;
            txbuffer[eeprom_virt_data_addr + 3] = 0;

            break;
        }
    }

    DS18X20_start_meas( power_mode, NULL );
}
Esempio n. 7
0
void read1WirePin(struct IO_pin *vpin, uint8_t power_mode) {
    uint8_t port_num = 0;
    uint8_t pin_num = 0;
    uint8_t ram_virt_data_addr = 0;


    uint16_t result;
    uint8_t subzero, cel, cel_frac_bits;
    uint8_t *tempID = 0;



    for (uint8_t vpin_num = 0; vpin_num < COUNT_IO_PINS; vpin_num++) {
        port_num = vpin_num/8;
        pin_num = vpin_num%8;
        ram_virt_data_addr = VIRTUAL_DATA_START + (port_num * (VIRTUAL_PORT_PINCOUNT * VIRTUAL_DATA_LENGTH)) + (pin_num * VIRTUAL_DATA_LENGTH);


        if(&io_pins[port_num].pins[pin_num] == vpin) { //try to find virtual pin by given pointer
            I2C_MAIN_DEBUG("Found OW PIN[0x%x;0x%x]\r\n", port_num, pin_num);

            tempID = &rxbuffer[ram_virt_data_addr];	//get 1 wire rom code pointer


#ifndef OW_ONE_BUS
            //set current pin to current used 1 wire bus
            ow_set_bus(vpin->PPIN, vpin->PPORT, vpin->PDDR, vpin->pin);
#endif

            if ( ss%10 == 5 ) {
                I2C_MAIN_DEBUG("st OW\r\n");
                DS18X20_start_meas( power_mode, NULL );
            }



            if (ss % 10 == 8) {

                I2C_MAIN_DEBUG("rd OW\r\n");
                txbuffer[ram_virt_data_addr]++;	//raise seqnr

                if (DS18X20_read_meas(tempID, &subzero, &cel, &cel_frac_bits) == DS18X20_OK) {

                    result = DS18X20_temp_to_decicel(subzero, cel, cel_frac_bits);

                    // Minuswerte:
                    if (subzero)
                        result *= (-1);

                    I2C_MAIN_DEBUG("T:%i\r\n", result);
                    txbuffer[ram_virt_data_addr + 1] = 0;	//set data ok

                    txbuffer[ram_virt_data_addr + 2] = result >> 8;		//high byte
                    txbuffer[ram_virt_data_addr + 3] = result & 0xFF;	//low byte


                } else {
Esempio n. 8
0
void readTemp()
{
  PORTC &= 0b11111000;; //turns off display because interrupts are disabled during DS18b20 measurements
  volatile int16_t decicelsius;
  DS18X20_start_meas( DS18X20_POWER_EXTERN, NULL );
  _delay_ms( DS18B20_TCONV_12BIT );
  DS18X20_read_decicelsius_single( gSensorIDs[0][0], &decicelsius );
  display[0] = decicelsius / 100; 
  display[1] = (decicelsius - (display[0] * 100))/ 10;
  display[2] = decicelsius % 10;
}
Esempio n. 9
0
uint8_t start_OW(void) {
#if OW_EXTERN_POWERED
	if ( DS18X20_start_meas( DS18X20_POWER_EXTERN, 0) == DS18X20_OK ) {
	        // wait 750ms (?) for conversion
		return (DS18B20_TCONV_12BIT / TIMERBASE);
	}

//	MES_DEBUG("\r\nOW short?");
	return 0;
#else
	return 1;  // wait min time(?) (well, conversion is not started!)
#endif
}
Esempio n. 10
0
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;
}
Esempio n. 11
0
void hw_read_tick(Context * ctx)
{

	/* DS */
	if (DS18X20_conversion_in_progress() == DS18X20_CONVERSION_DONE) {
		if (DS18X20_read_decicelsius_single(DS18B20_FAMILY_CODE, &ctx->t1) == DS18X20_OK) {
			ctx->temp_status = DS_OK;
		} else {
			ctx->temp_status = DS_READ_ERROR;
		}
		if (DS18X20_start_meas(DS18X20_POWER_EXTERN, NULL) != DS18X20_OK) {
			ctx->temp_status = DS_MEAS_ERROR;
		}
	}
}
Esempio n. 12
0
void read_temp() {
	uint8_t gSensorIDs[OW_ROMCODE_SIZE];

	uint16_t decicelsius;
	uint8_t diff, i, subzero, cel, cel_frac_bits;

	OW_set_bus(&PORTC,5);
	//	DS18X20_find_sensor(&diff, &gSensorIDs[0]);


	DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL );
	delayms(DS18B20_TCONV_12BIT);
	DS18X20_read_meas_single(0x10, &subzero, &cel, &cel_frac_bits);
	decicelsius = DS18X20_temp_to_decicel(subzero, cel, cel_frac_bits);

}
Esempio n. 13
0
static void request_temperature_measurement(void)
{
    uint8_t bus;

    for( bus = 0; bus < N_1W_BUS; bus++ )
    {
#ifndef OW_ONE_BUS
        ow_set_bus(&PINB,&PORTB,&DDRB,PB0+bus);
#endif
        // if( DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ) != DS18X20_OK)
        if( DS18X20_start_meas( DS18X20_POWER_EXTERN, NULL ) != DS18X20_OK)
        {
            // Error starting temp mesaure.
            REPORT_ERROR(ERR_FLAG_1WIRE_START_FAULT);
            //ow_error_cnt++;
            //ow_bus_error_cnt[bus];
            count_1w_bus_error( bus );
            //led1_timed( 200 );
        }
    }
}
Esempio n. 14
0
void measure_temp(uint8_t nSensors, int32_t* temp_eminus4)
{
    uint8_t i = 0;
    uint8_t result = 0;

    if (DS18X20_start_meas( DS18X20_POWER_EXTERN, NULL) == DS18X20_OK)
    {
        _delay_ms(DS18B20_TCONV_12BIT);

        for (i = 0; i < nSensors; i++)
        {

            vTaskSuspendAll();
            result = DS18X20_read_maxres(&gSensorIDs[i][0], &temp_eminus4[i]);
            xTaskResumeAll();

        }

    }


}
Esempio n. 15
0
void Temp_handler(void)
{
      static Temp_ReadState_t state = TEMPERATURE_MEASURE_REQUEST;
      unsigned char counter = 0;

      switch (state) 
      {
   		case TEMPERATURE_MEASURE_REQUEST:
         {
            //		i = gSensorIDs[0]; // family-code for conversion-routine
            DS18X20_start_meas( DS18X20_POWER_EXTERN, NULL );
            counter = 0;
            state  = TEMPERATURE_WAITING_FOR_RESPONSE;
            break;
         }
      
         case TEMPERATURE_WAITING_FOR_RESPONSE:
         {
            counter++;
            if(counter > TEMP_GET_COUNTS_MS(DS18B20_TCONV_12BIT))
            {
               state  = TEMPERATURE_UPDATE;
            }
            break;      
         }
         
         case TEMPERATURE_UPDATE:
         {
            DS18X20_read_meas_single(0x28, &subzero, &cel, &cel_frac_bits);
		      Temp_decicelsius = DS18X20_temp_to_decicel(subzero, cel, cel_frac_bits);
            Temp_decicelsiusAvg = MovingAvg(Temp_decicelsius, 6u);
            state  = TEMPERATURE_MEASURE_REQUEST;
            break;
         }
      }
}
Esempio n. 16
0
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)
}
Esempio n. 17
0
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);
	}
}
int main(void) {

  uint8_t numSensors = 0, i;
  // unsigned long nextflash = 0;

  // Configure all pins as inputs with pullups initially
  DDRA = 0x00;
  PORTA = 0xff;
  DDRB = 0x00;
  PORTB = 0xff;

  // Serial output line
  DDRB |= _BV(PINB0);
  PORTB |= _BV(PINB0);

  // LED
  // DDRA |= _BV(PINA0);

  // Radio power is PA1
  PORTA &= ~_BV(PINA1);
  DDRA |= _BV(PINA1);

  // Onewire power is PA2
  PORTA &= ~_BV(PINA2);
  DDRA |= _BV(PINA2);

  myPutStr("Hello world\r\n");

  // Various power-saving things

  // Disable BOD while sleeping. I hope.
  MCUCR |= (_BV(BODS) | _BV(BODSE));
  MCUCR &= ~_BV(BODSE);
  MCUCR |= (_BV(BODS));
 
  // Disable the ADC
  ADCSRA &= ~_BV(ADEN);

  // Disable the Analog Comparator
  ACSR |= _BV(ACD);

  // Disable clocking of timer1 and ADC
  PRR |= (_BV(PRTIM1)|_BV(PRADC));

  timerInit();
  wdtInit();
  initInterrupts();


  // Power up the Onewire bus
  PORTA |= _BV(PINA2);
  while(numSensors == 0) {
    myPutStr("Scanning for sensors\r\n");
    numSensors = search_sensors();
    myPutStr("Found ");
    myPutUint8(numSensors);
    myPutStr(" sensors\r\n");

    for (i=0;i<numSensors;i++) {
      uint8_t j;
      myPutStr("Sensor ");
      myPutUint8(i);
      myPutStr(" address ");
      for (j=0;j<OW_ROMCODE_SIZE;j++) {
	myPutUint8(gSensorIDs[i][j]);
      }
      if (gSensorIDs[i][0] == DS18S20_FAMILY_CODE ) {
	myPutStr(" DS18S20/DS1820");
      } else if ( gSensorIDs[i][0] == DS1822_FAMILY_CODE ) {
	myPutStr(" DS1822");
      } else {
	myPutStr(" DS18B20");
      }
      if ( DS18X20_get_power_status( &gSensorIDs[i][0] ) == DS18X20_POWER_PARASITE ) {
	myPutStr(" parasite\r\n");
      } else {
	myPutStr(" external\r\n");
      }

      // Enable 12 bit mode (won't do anything on DS18S20)
      DS18X20_write_scratchpad(&gSensorIDs[i][0], 0, 0, DS18B20_12_BIT);
      DS18X20_scratchpad_to_eeprom(DS18X20_get_power_status( &gSensorIDs[i][0] ),&gSensorIDs[i][0]);
	
    }
      

  }
  while(1) {
    unsigned long wakepoint;
    myRadioBuf_t radiobuf;
    //char debugbuf[10];

    // if ((signed long)now - (signed long)nextflash >= 0) {
    //   debugLedToggle(0);
    //   nextflash = now + 1000;
    // }

    // Power up the Onewire bus
    PORTA |= _BV(PINA2);
    // Let is stabilize for a few ms
    wakepoint = getMillis() + 15;
    numSensors = search_sensors();
    while (! ((signed long)getMillis() - (signed long)wakepoint >= 0)) {
      set_sleep_mode(SLEEP_MODE_IDLE);
      sleep_mode();
    }
    if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ) == DS18X20_OK) {
      wakepoint = getMillis() + DS18B20_TCONV_12BIT;
      while (! ((signed long)getMillis() - (signed long)wakepoint >= 0)) {
	set_sleep_mode(SLEEP_MODE_IDLE);
	sleep_mode();
      }
      // Power the radio up
      PORTA |= _BV(PINA1);
      // Wakepoint set to now +100ms to allow radio to wake
      wakepoint = getMillis() + 100;

      for ( i = 0; i < numSensors; i++ ) {
        radiobuf.tenthousandths = -9999999L;
	if (DS18X20_read_maxres( &gSensorIDs[i][0], &(radiobuf.tenthousandths) ) != DS18X20_OK) {
	  radiobuf.tenthousandths = -9999999L;
	}

	//myPutStr("Sensor ");
	uint8_t j;
	for (j=0;j<OW_ROMCODE_SIZE;j++) {
            radiobuf.sensid[j] = gSensorIDs[i][j];
	    //myPutUint8(gSensorIDs[i][j]);
        }
	//myPutStr(" = ");
	//sprintf(debugbuf, "%d.%d\r\n", (int)(radiobuf.tenthousandths/10000), (int)(radiobuf.tenthousandths % 10000));
	//myPutStr(debugbuf);
        if (0 == i) {
	  // First time around, radio not initialized
	  while (! ((signed long)getMillis() - (signed long)wakepoint >= 0)) {
            set_sleep_mode(SLEEP_MODE_IDLE);
            sleep_mode();
          }
	  radioInit();
	  // No auto ack
	  radioSetAutoAck(0);
	  radioOpenWritingPipe(pipe);
        }
	radiobuf.tstamp = getMillis();
	//myPutStr("about to radioWrite, i=");
	//myPutUint8(i);
	//myPutStr("...");
	radioWrite(&radiobuf,sizeof(radiobuf));
	//myPutStr("Done\r\n");
      }
      // Power the radio down
      PORTA &= ~_BV(PINA1);
      // Plus the CE and CSN pins
      PORTA &= ~_BV(PINA7);
      PORTB &= ~_BV(PINB2);
      // And the onewire bus
      PORTA &= ~_BV(PINA2);
    } else {
      myPutStr("Error measuring sensors\n");
    }
    //tmp = getMillis();
    //radioWrite(&tmp , sizeof(unsigned long) );
    // Sleep hard until WDT fires
    set_sleep_mode(SLEEP_MODE_PWR_DOWN);
    sleep_mode();
  }
}
Esempio n. 19
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);
    }
}
Esempio n. 20
0
void updateOWSensors()
{
#ifdef OW_DEBUG
	printf_P(PSTR("Update onewire \r\n"));
#endif

	uint8_t subzero, cel, cel_frac_bits;
	uint16_t maalt;

	if (!DS18B20Conv) {
		DS18B20Conv = true;

		for (uint8_t active_OW_channel = 1; active_OW_channel <= 3;
		     active_OW_channel++) {
			OW_selectPort(active_OW_channel);
			DS18X20_start_meas(DS18X20_POWER_PARASITE, NULL);
		}

	} else {
		DS18B20Conv = false;

		for (uint8_t active_OW_channel = 1; active_OW_channel <= 3;
		     active_OW_channel++) {
			OW_selectPort(active_OW_channel);

#ifdef OW_DEBUG
			printf_P(PSTR("Scanner for onewire sensore paa %d\r\n"), active_OW_channel);
#endif

			int nSensors = search_sensors(MAXSENSORS);  //Finder alle sensore (op til max)

#ifdef OW_DEBUG
			printf("Found %d sensors \r\n", nSensors);
#endif

			for (int i = 0; i < nSensors; i++) {
				if (sensorScan[i * OW_ROMCODE_SIZE + 0] == 0x10
				    || sensorScan[i * OW_ROMCODE_SIZE + 0] == 0x28) {
					uint8_t sensorID[OW_ROMCODE_SIZE];
					for (uint8_t o = 0; o < OW_ROMCODE_SIZE; o++)
						sensorID[o] = sensorScan[i * OW_ROMCODE_SIZE + o];

					if (DS18X20_read_meas(sensorID, &subzero, &cel, &cel_frac_bits,
					                      &maalt) == DS18X20_OK) {
#ifdef OW_DEBUG
						int frac =
						    cel_frac_bits *
						    DS18X20_FRACCONV; //Ganger de sidste par bits, med det step DS18B20 bruger
#endif
						char sign = (subzero) ? '-' : '+';

						uint16_t pos = findSensor(
						                   sensorScan[i * OW_ROMCODE_SIZE + FAMILY],
						                   sensorScan[i * OW_ROMCODE_SIZE + ID1],
						                   sensorScan[i * OW_ROMCODE_SIZE + ID2],
						                   sensorScan[i * OW_ROMCODE_SIZE + ID3],
						                   sensorScan[i * OW_ROMCODE_SIZE + ID4],
						                   sensorScan[i * OW_ROMCODE_SIZE + ID5],
						                   sensorScan[i * OW_ROMCODE_SIZE + ID6],
						                   sensorScan[i * OW_ROMCODE_SIZE + CRC]
						               );

						sensorValues[(pos * SENSORSIZE) + VALUE1]     = cel;
						sensorValues[(pos * SENSORSIZE) + VALUE2]     = cel_frac_bits;
						sensorValues[(pos * SENSORSIZE) + SIGN]       = sign;

#ifdef OW_DEBUG
						printf_P(PSTR("Sensor# %d (%02X%02X%02X%02X%02X%02X%02X%02X) =  : %c%d.%04d\r\n"),
						         i + 1,
						         sensorValues[(pos * SENSORSIZE) + FAMILY],
						         sensorValues[(pos * SENSORSIZE) + ID1],
						         sensorValues[(pos * SENSORSIZE) + ID2],
						         sensorValues[(pos * SENSORSIZE) + ID3],
						         sensorValues[(pos * SENSORSIZE) + ID4],
						         sensorValues[(pos * SENSORSIZE) + ID5],
						         sensorValues[(pos * SENSORSIZE) + ID6],
						         sensorValues[(pos * SENSORSIZE) + CRC],
						         sensorValues[(pos * SENSORSIZE) + SIGN],
						         sensorValues[(pos * SENSORSIZE) + VALUE1],
						         frac
						        );
#endif
					} else
						printf_P(PSTR("CRC Error (lost connection?)"));
				}
			}
		}
	}
}
Esempio n. 21
0
/*-----------------------------------------------------------------------------
 * Main Program
 *---------------------------------------------------------------------------*/
int main(void) {
#if defined(USE_DS18S20)
    uint8_t nSensors, i;
    uint8_t subzero, cel, cel_frac_bits;
    uint32_t timeStamp_DS = 0, timeStamp_DS_del = 0;
	/* This is to identify and give the sensors correct ID 
	 * TODO read ds18s20 serial id and that way give the "can id" */
	uint16_t sensor_ds_id[] = {TEMPSENSORID_1, TEMPSENSORID_2, TEMPSENSORID_3, TEMPSENSORID_4};
#endif
#if defined(USE_TC1047)
    uint32_t tcTemperature = 0, timeStamp_TC = 0;
    adcTemperatureInit();
#endif
#if defined(USE_LDR)
	uint32_t timeStamp_LDR = 0;
	uint8_t ldr;
	LDR_SensorInit();
#endif
    sei();

	Timebase_Init();

    Can_Message_t txMsg;
	txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID);
	txMsg.DataLength = 4;
	txMsg.RemoteFlag = 0;
	txMsg.ExtendedFlag = 1;
	txMsg.Data.words[0] = APP_TYPE;
	txMsg.Data.words[1] = APP_VERSION;
	BIOS_CanSend(&txMsg);

#if defined(USE_DS18S20)
    /* Make sure there is no more then 4 DS-sensors. */
    nSensors = search_sensors();
#endif

    txMsg.DataLength = 2;

    /* main loop */
    while (1) {
#if defined(USE_DS18S20)
        /* check temperature and send on CAN */
		if( Timebase_PassedTimeMillis(timeStamp_DS) >= DS_SEND_PERIOD ){
			timeStamp_DS = Timebase_CurrentTime();

            if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ) == DS18X20_OK) {
                delay_ms(DS18B20_TCONV_12BIT);
                for ( i=0; i<nSensors; i++ ) {
                    if ( DS18X20_read_meas( &gSensorIDs[i][0], &subzero, &cel, &cel_frac_bits) == DS18X20_OK ) {

                        if (subzero) {
                            txMsg.Data.bytes[0] = -cel-1;
                            txMsg.Data.bytes[1] = ~(cel_frac_bits<<4);
                        }else{
                            txMsg.Data.bytes[0] = cel;
                            txMsg.Data.bytes[1] = (cel_frac_bits<<4);
                        }
                    }
					/* Delay */
					timeStamp_DS_del = Timebase_CurrentTime();
					while(Timebase_PassedTimeMillis(timeStamp_DS_del) < DS_SEND_DELAY){}

					/* send txMsg */
					txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_TEMPERATURE << CAN_SHIFT_SNS_TYPE) | (NODE_ID << CAN_SHIFT_SNS_SID));
					txMsg.Id |= ( sensor_ds_id[i] << CAN_SHIFT_SNS_ID); /* Set sensor id */
					BIOS_CanSend(&txMsg);
                }
            }
        }
#endif
#if defined(USE_TC1047)
#error tc1047 id is still not correct // FIXME
            /* check temperature and send on CAN */
		if( bios->timebase_get() - timeStamp_TC >= TC_SEND_PERIOD ){
			timeStamp_TC = bios->timebase_get();

            tcTemperature = getTC1047temperature();
            txMsg.Data.bytes[0] = tcTemperature & 0x00FF;
            txMsg.Data.bytes[1] = (tcTemperature & 0xFF00)>>8;
            txMsg.DataLength = 2;

			txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_TEMPERATURE << CAN_SHIFT_SNS_TYPE) | (NODE_ID << CAN_SHIFT_SNS_SID));
			txMsg.Id |= (TEMPSENSORID_5 << CAN_SHIFT_SNS_ID); // sätt korrekt sensorid

            BIOS_CanSend(&txMsg);
        }
#endif
#if defined(USE_LDR)
            /* check light and send on CAN */
		if( Timebase_PassedTimeMillis(timeStamp_LDR) >= LDR_SEND_PERIOD ){
			timeStamp_LDR = Timebase_CurrentTime();

            ldr = (uint8_t)LDR_GetData(0);
            txMsg.Data.bytes[0] = ldr;
            txMsg.DataLength = 1;

			txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_LIGHT << CAN_SHIFT_SNS_TYPE) | (LIGHTSENSORID_1 << CAN_SHIFT_SNS_ID) | (NODE_ID << CAN_SHIFT_SNS_SID));

			BIOS_CanSend(&txMsg);
        }
#endif
    }
Esempio n. 22
0
/**
 * \ingroup usartcmdline
 * \b OWREAD-Befehl DS18x20 auf Bus suchen und anzeigen
 */
int16_t command_OWlookup(char *outbuffer)
{
	if (outbuffer)					// nur bei USART
		return cmd_502(outbuffer);

	#if USE_OW
	uint8_t i;
	uint8_t diff, nSens;
	uint16_t TWert;
	uint8_t subzero, cel, cel_frac_bits;
	uint8_t gSensorIDs[MAXLOOKUP][OW_ROMCODE_SIZE];
	
	usart_write("\r\nScanning Bus for DS18X20");
	
	nSens = 0;
	
	for( diff = OW_SEARCH_FIRST; 
		diff != OW_LAST_DEVICE && nSens < MAXLOOKUP ;  )
	{
		DS18X20_find_sensor( &diff, &gSensorIDs[nSens][0] );
		
		if( diff == OW_PRESENCE_ERR ) {
			usart_write("\r\nNo Sensor found");
			break;
		}
		
		if( diff == OW_DATA_ERR ) {
			usart_write("\r\nBus Error");
			break;
		}
		
		nSens++;
	}
	usart_write("\n\r%i 1-Wire Sensoren gefunden.\r\n", nSens);

//	for (i=0; i<nSens; i++) {
//		// set 10-bit Resolution - Alarm-low-T 0 - Alarm-high-T 85
//		DS18X20_write_scratchpad( &gSensorIDs[i][0] , 0, 85, DS18B20_12_BIT);
//	}

	for (i=0; i<nSens; i++) {
		usart_write("\r\n#%i ist ein ",(int) i+1);
		if ( gSensorIDs[i][0] == DS18S20_ID)
			usart_write("DS18S20/DS1820");
		else usart_write("DS18B20");

		usart_write(" mit ");
		if ( DS18X20_get_power_status( &gSensorIDs[i][0] ) ==
			DS18X20_POWER_PARASITE ) 
			usart_write( "parasitaerer" );
		else usart_write( "externer" ); 
		usart_write( " Spannungsversorgung. " );

		// T messen
		if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, &gSensorIDs[i][0] ) == DS18X20_OK ) {
				_delay_ms(DS18B20_TCONV_12BIT);
				if ( DS18X20_read_meas( &gSensorIDs[i][0], &subzero,
						&cel, &cel_frac_bits) == DS18X20_OK ) {
					DS18X20_show_id_uart( &gSensorIDs[i][0], OW_ROMCODE_SIZE );
					TWert = DS18X20_temp_to_decicel(subzero, cel, cel_frac_bits);
					usart_write(" %i %i.%4i C %i",subzero, cel, cel_frac_bits,TWert);
				}
				else usart_write(" CRC Error (lost connection?)");
			}
			else usart_write(" *** Messung fehlgeschlagen. (Kurzschluss?) ***");
	}
	
	#endif
	return 0;
}
Esempio n. 23
0
/*-----------------------------------------------------------------------------
 * Main Program
 *---------------------------------------------------------------------------*/
int main(void) {
	uint8_t nSensors, i;
	uint8_t subzero, cel, cel_frac_bits;
	
	Mcu_Init();
	Timebase_Init();
	Serial_Init();
	
	sei();
	
	printf( "\nDS18X20 1-Wire-Reader\n" );
	printf( "-----------------------" );
	nSensors = search_sensors();
	printf( "%i DS18X20 Sensor(s) available:\n", (int) nSensors );
	
	for (i=0; i<nSensors; i++) {
		printf("Sensor# %i is a ", (int) i+1);
		if ( gSensorIDs[i][0] == DS18S20_ID)
			printf("DS18S20/DS1820");
		else printf("DS18B20");
		printf(" which is ");
		if ( DS18X20_get_power_status( &gSensorIDs[i][0] ) ==
			DS18X20_POWER_PARASITE ) 
			printf( "parasite" );
		else printf( "externally" ); 
		printf( " powered\n" );
	}
	
	printf("CanInit...\n");
	if (Can_Init() != CAN_OK) {
		printf("FAILED!\n");
	}
	else {
		printf("OK!\n");
	}
	
	uint32_t timeStamp = 0;
	
	Can_Message_t txMsg;
	Can_Message_t rxMsg;
	
	txMsg.DataLength = 2;
	txMsg.Id = 0;
	txMsg.RemoteFlag = 0;
	txMsg.ExtendedFlag = 1;
	
	/* main loop */
	while (1) {
		/* service the CAN routines */
		Can_Service();

		/* check if any messages have been received */
		while (Can_Receive(&rxMsg) == CAN_OK) {
			
		}

		/* check temperature and send on CAN once every other second */
		if (Timebase_PassedTimeMillis(timeStamp) >= 2000) {
			timeStamp = Timebase_CurrentTime();
			
			if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ) == DS18X20_OK) {
				printf("Measuring temperature... ");
				delay_ms(DS18B20_TCONV_12BIT);
				for ( i=0; i<nSensors; i++ ) {
					if ( DS18X20_read_meas( &gSensorIDs[i][0], &subzero,
							&cel, &cel_frac_bits) == DS18X20_OK ) {
						
						//txMsg.Data.bytes[0] = subzero;
						if (subzero) {
							txMsg.Data.bytes[i*2] = -cel;
							txMsg.Data.bytes[i*2+1] = ~(cel_frac_bits<<4);
						} else {
							txMsg.Data.bytes[i*2] = cel;
							txMsg.Data.bytes[i*2+1] = (cel_frac_bits<<4);
						}
						
					}
					else printf("CRC Error (lost connection?)\n");
				}
				
				txMsg.DataLength = nSensors*2;
				printf("sending...\n");
				/* send txMsg */
				Can_Send(&txMsg);
			}
			else printf("Start meas. failed (short circuit?)\n");

		}
	}
	
	return 0;
}
void M0(char *str)
{	
	static uint8_t state=0, term_state=0, i=0;
	static uint32_t cnt=0, offset_cnt=0;
	static char buf[41], buf_lcd[9];
	if(Flagi.flaga_lcd)
	{
		lcd_locate(0, 0);
		lcd_str(str);
		lcd_locate(1, 0);
		lcd_str(str);
		Flagi.flaga_lcd=0;
	}
	if (Flagi.flaga_term)
	{
		switch(term_state)
		{
			case 0:
					DS18X20_start_meas( DS18X20_POWER_EXTERN, NULL );
					term_state=1;
					break;
			case 1:
					if( DS18X20_OK == DS18X20_read_meas(gSensorIDs[0], &subzero, &cel, &cel_fract_bits) ) display_temp(0);
					else {
						lcd_locate(0,0);
						lcd_str("term. error ");	/* wyœwietlamy informacjê o b³êdzie jeœli np brak czujnika lub b³¹d odczytu */
					}
					term_state=0;
					break;
		}
		Flagi.flaga_term=0;

	}
	
	if(Flagi.flaga_rtc)
	{
		pobierz_czas(&czas);
		pobierz_date(&data);
		Flagi.flaga_rtc=0;
	}

	lcd_locate(1,0);
	if( czas.godziny < 10 ) lcd_char('0');
	lcd_int(czas.godziny);
	lcd_char(':');
	if( czas.minuty < 10 ) lcd_char('0');
	lcd_int(czas.minuty);
	lcd_char(':');
	if( czas.sekundy < 10 ) lcd_char('0');
	lcd_int(czas.sekundy);
	
	switch(state)
	{
		case 0:
				state=1;
				
				if(i>= 2*strlen(buf))
					i=0;
				
				lcd_wyswietl_date(&data, buf, 0);
				fifo_bufor_lcd(buf, i, strlen(buf));
				lcd_locate(1,8);
				strncpy(buf_lcd,buf,sizeof(buf_lcd)-1);
				lcd_str(buf_lcd);
				
				cli();
				cnt=25000;
				offset_cnt=licznik;
				sei();
				break;
		case 1:
				if(cnt<=licznik-offset_cnt)
				{
					i++;
					state=0;
				}
				break;
	}
}
int main(void)
{	
	
#if _EXT_EEPROM_EMPTY==1
	DDRB=0xFF;
	PORTB=0x00;
	
	i2cSetBitrate(100); //Ustaw prêdkoœæ i2c na 100kHz
	EXT_EEPROM_var *struktura_eeprom;
	struktura_eeprom=malloc(sizeof(EXT_EEPROM_var));
	strcpy(struktura_eeprom->empty,  "                ");
	strcpy(struktura_eeprom->temperatura, "Temperatura:    ");
	strcpy(struktura_eeprom->m1,  "Ustawienia      ");
	strcpy(struktura_eeprom->m10, "Data:           ");
	strcpy(struktura_eeprom->m11, "Godzina:        ");
	strcpy(struktura_eeprom->m12, "LCD:            ");
	strcpy(struktura_eeprom->eePCF_Sunday, "Niedziela");
	strcpy(struktura_eeprom->eePCF_Monday, "Poniedzialek");
	strcpy(struktura_eeprom->eePCF_Tuesday, "Wtorek");
	strcpy(struktura_eeprom->eePCF_Wednesday, "Sroda");
	strcpy(struktura_eeprom->eePCF_Thursday, "Czwartek");
	strcpy(struktura_eeprom->eePCF_Friday, "Piatek");
	strcpy(struktura_eeprom->eePCF_Saturday, "Sobota");
	strcpy(struktura_eeprom->eePCF_January, "Styczen");
	strcpy(struktura_eeprom->eePCF_February, "Luty");
	strcpy(struktura_eeprom->eePCF_March, "Marzec");
	strcpy(struktura_eeprom->eePCF_April, "Kwiecien");
	strcpy(struktura_eeprom->eePCF_May, "Maj");
	strcpy(struktura_eeprom->eePCF_June, "Czerwiec");
	strcpy(struktura_eeprom->eePCF_July, "Lipiec");
	strcpy(struktura_eeprom->eePCF_August, "Sierpien");
	strcpy(struktura_eeprom->eePCF_September, "Wrzesien");
	strcpy(struktura_eeprom->eePCF_October, "Pazdziernik");
	strcpy(struktura_eeprom->eePCF_November, "Listopad");
	strcpy(struktura_eeprom->eePCF_December, "Grudzien");
	PORTB=0x0F;
	
	EI2C_write_buf(ADDR_EEMEM_24C64, 0, sizeof(EXT_EEPROM_var), (uint8_t*)struktura_eeprom);
	free(struktura_eeprom);
	PORTB=0xFF;
#endif // _EXT_EEPROM_EMPTY

#if _EXT_EEPROM_EMPTY==0

	char bufor_lcd[41];	//Bufor przechowuj¹cy znaki z pamiêci EEPROM
	
	//Zmienne wykorzystywane w opóŸnieniach:
	uint8_t state=0;
	uint32_t cnt=0, offset_cnt=0;
	uint8_t state_light=0;
	uint32_t cnt_light=0, offset_cnt_light=0;
	
	Ustawienia ustawienia;	//Struktura s³u¿¹ca do odczytania ustawieñ z EEPROM
	ustawienia.ustawienia_poczatkowe=0;
	
	//Ustaw PORTB jak wyjœcie dla LED:
	DDRB=0xFF;
	PORTB=0x00;
	Flagi.flaga_klawiatura=1;
	
	//// ustawienia TIMER0, przerwanie co 10us
	OCR0 = 19;
	TCCR0 |= (1<<WGM01);		//Tryb ctc
	TCCR0 |= (1<<CS01);			// Preskaler 8
	TIMSK |= (1<<OCIE0);

	// Przerwanie INT0
	MCUCR |= (1<<ISC01);	// wyzwalanie zboczem opadaj¹cym
	GICR |= (1<<INT0);		// odblokowanie przerwania
	PORTD |= (1<<PD2);		// podci¹gniêcie pinu INT0 do VCC
	
	i2cSetBitrate(100); //Ustaw prêdkoœæ i2c na 100kHz
	lcd_init();
	pobierz_czas(&czas);
	pobierz_date(&data);

	/* sprawdzamy ile czujników DS18xxx widocznych jest na magistrali */
	czujniki_cnt = search_sensors();

	/* wysy³amy rozkaz wykonania pomiaru temperatury
	 * do wszystkich czujników na magistrali 1Wire
	 * zak³adaj¹c, ¿e zasilane s¹ w trybie NORMAL,
	 * gdyby by³ to tryb Parasite, nale¿a³oby u¿yæ
	 * jako pierwszego prarametru DS18X20_POWER_PARASITE */
	DS18X20_start_meas( DS18X20_POWER_EXTERN, NULL );
	
	/* czekamy 750ms na dokonanie konwersji przez pod³¹czone czujniki */
	_delay_ms(750);

	/* dokonujemy odczytu temperatury z pierwszego czujnika o ile zosta³ wykryty */
	/* wyœwietlamy temperaturê gdy czujnik wykryty */
	if( DS18X20_OK == DS18X20_read_meas(gSensorIDs[0], &subzero, &cel, &cel_fract_bits) ) display_temp(0);
	else {
		lcd_locate(0,0);
		lcd_str("term. error ");	/* wyœwietlamy informacjê o b³êdzie jeœli np brak czujnika lub b³¹d odczytu */
	}
	
#endif	 // _EXT_EEPROM_EMPTY
    while (1) 
    {	
#if _EXT_EEPROM_EMPTY==0
		eeprom_read_block(&ustawienia, &deafult_flags, sizeof(deafult_flags));
		if (ustawienia.ustawienia_poczatkowe)
		{
			eeprom_read_block(&data, &deafult_data, sizeof(data));
			eeprom_read_block(&czas, &deafult_czas, sizeof(czas));
			ustaw_date(&data);
			ustaw_czas(&czas);

			ustawienia.ustawienia_poczatkowe=0;
			eeprom_update_block(&ustawienia, &deafult_flags, sizeof(deafult_flags));
		}

		switch(state_light)
		{
			case 0:
				if (Flagi.light_lcd)
				{

					state_light=1;
					cli();
					cnt_light=200000;
					offset_cnt_light=licznik;
					sei();
					
				}
			break;
			case 1:
			if(cnt_light<=licznik-offset_cnt_light)
			{
				Flagi.light_lcd=0;
				state_light=0;
			}
			break;
		}
		lcd_light(Flagi.light_lcd);
		
		//OpóŸnienie dzia³añ po wciœniêciu przycisku
		switch(state)
		{
			case 0:
					key_code=debouncer(KlawiaturaSkan());
					if(key_code)
					{
						state=1;
						Flagi.light_lcd=1;	//W³¹cz podœwietlanie LCD
						cli();
						cnt=50000;
						offset_cnt=licznik;
						sei();
					}

					break;
			case 1:
					key_code=0;
					if(cnt<=licznik-offset_cnt)
					{
						state=0;
					}
					break;
		}
		
		if (Flagi.flaga_menu_func==1)
		{
			EI2C_read_buf(ADDR_EEMEM_24C64, menu_ptr->addr_ext_eeprom, menu_ptr->size_ext_eeprom, (uint8_t *)bufor_lcd);
			if(Flagi.flaga_lcd)
			{
				lcd_locate(0, 0);
			}

			(*menu_ptr->funkcja)(bufor_lcd);		//WskaŸnik na funkcje dla danej pozycji menu
		}

		if(Flagi.flaga_klawiatura==1)
		{	
			if (key_code == PRZYCISK_PRAWO)
			{
				if(menu_ptr->next!=NULL)
				{
					Flagi.flaga_lcd=1;
					menu_ptr=menu_ptr->next;
				}
			}
			else if (key_code == PRZYCISK_LEWO)
			{
				if(menu_ptr->prev!=NULL)
				{
					Flagi.flaga_lcd=1;
					menu_ptr=menu_ptr->prev;
				}
			}
			else if (key_code == PRZYCISK_DOL)
			{
				if(menu_ptr->down!=NULL)
				{
					Flagi.flaga_lcd=1;
					menu_ptr=menu_ptr->down;
				}
			}
			else if (key_code == PRZYCISK_GORA)
			{
				if(menu_ptr->up!=NULL)
				{
					Flagi.flaga_lcd=1;
					menu_ptr=menu_ptr->up;
				}
			}
		}
		
		if(menu_ptr->funkcja!=NULL)
			Flagi.flaga_menu_func=1;
		else
			Flagi.flaga_menu_func=0;

#endif  // _EXT_EEPROM_EMPTY
    }
}
Esempio n. 26
0
int main(void)
{  
	//Konfiguration der Ausgänge bzw. Eingänge
	//definition erfolgt in der config.h
	DDRA = OUTA;
	#if USE_SER_LCD
		DDRC = OUTC;
	#else
		DDRC = OUTC;
		#if PORTD_SCHALT
			DDRD = OUTD;
		#endif
	#endif
	// RoBue:
	// Pullups einschalten
	PORTA = (1 << PORTA0) | (1 << PORTA1) | (1 << PORTA2) | (1 << PORTA3) | (1 << PORTA4) | (1 << PORTA5) | (1 << PORTA6);
	
    unsigned long a;
	#if USE_SERVO
		servo_init ();
	#endif //USE_SERVO
	
    usart_init(BAUDRATE); // setup the UART
	
	#if USE_ADC
		ADC_Init();
	#endif
	
	usart_write("\n\rSystem Ready\n\r");
    usart_write("Compiliert am "__DATE__" um "__TIME__"\r\n");
    usart_write("Compiliert mit GCC Version "__VERSION__"\r\n");
	for(a=0;a<1000000;a++){asm("nop");};

	//Applikationen starten
	stack_init();
	httpd_init();
	telnetd_init();
	
	//Spielerrei mit einem LCD
	#if USE_SER_LCD
		udp_lcd_init();
		lcd_init();
		// RoBue:
		// LCD-Ausgaben:
		lcd_clear();
		lcd_print(0,0,"*AVR-NET-IO "Version"*");
		lcd_print(2,0,"Counter: ");
		lcd_print(3,0,"Zeit:");
	#endif

	//Ethernetcard Interrupt enable
	ETH_INT_ENABLE;
	
	#if USE_SER_LCD
		// RoBue:
		// IP auf LCD
		lcd_print(1,0,"%1i.%1i.%1i.%1i",myip[0],myip[1],myip[2],myip[3]);
	#endif
	
	//Globale Interrupts einschalten
	sei(); 
	
	#if USE_CAM
		#if USE_SER_LCD
			lcd_print(1,0,"CAMERA INIT");
		#endif //USE_SER_LCD
		for(a=0;a<2000000;a++){asm("nop");};
		cam_init();
		max_bytes = cam_picture_store(CAM_RESELUTION);
		#if USE_SER_LCD
			back_light = 0;
			lcd_print(1,0,"CAMERA READY");
		#endif //USE_SER_LCD
	#endif // -> USE_CAM
	
	#if USE_NTP
	        ntp_init();
	        ntp_request();
	#endif //USE_NTP
	
	#if USE_WOL
	        wol_init();
	#endif //USE_WOL
    
	#if USE_MAIL
	        mail_client_init();
	#endif //USE_MAIL  

	// Startwerte für ow_array setzen
	#if USE_OW
		uint8_t i = 0;
		for (i=0;i<MAXSENSORS;i++){
			ow_array[i]=OW_START;
		}
		for (i=MAXSENSORS;i<MAXSENSORS*3;i++){
			ow_array[i]=OW_MINMAX;
		}
		DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL );
		for(a=0;a<1000000;a++){asm("nop");};
		auslesen = 0;
		minmax = 1;
	#endif


//Hauptschlfeife
// *************
		
	while(1)
	{

	#if USE_ADC
		ANALOG_ON;
	#endif

	eth_get_data();
		
        //Terminalcommandos auswerten
	if (usart_status.usart_ready){
	usart_write("\r\n");
		if(extract_cmd(&usart_rx_buffer[0]))
		{
			usart_write("Ready\r\n\r\n");
		}
		else
		{
			usart_write("ERROR\r\n\r\n");
		}
		usart_status.usart_ready =0;
	
	}
	
	// RoBue:
	// Counter ausgeben
	#if USE_SER_LCD
		lcd_print(2,9,"%4i",var_array[MAX_VAR_ARRAY-1]);
	#endif

	// RoBue:
	// Uhrzeit bestimmen und auf LCD ausgeben
	hh = (time/3600)%24;
	mm = (time/60)%60;
	ss = time%60;

	#if USE_SER_LCD
		lcd_print(3,7,"%2i:%2i:%2i",hh,mm,ss);
	#endif

	#if USE_HIH4000
		var_array[VA_OUT_HIH4000] = ((var_array[VA_IN_HIH4000]-160)/6);
	#endif

	#if USE_OW
	// RoBue:
	// Zurücksetzen der Min/Max-Werte um 00:00 Uhr einschalten
	if (( hh == 00 )&&( mm == 00 )) {
		minmax = 1;
	}
	#endif

	// ******************************************************************
	// RoBue:
	// 1-Wire-Temperatursensoren (DS18B20) abfragen
	// ******************************************************************

		#if USE_OW

		uint8_t i = 0;
		uint8_t subzero, cel, cel_frac_bits;
		uint8_t tempID[OW_ROMCODE_SIZE];

		// Messen bei ss=5,15,25,35,45,55		
		if ( ss%10 == 5 ) {

		// Messen?
		if ( messen == 1 ) {

		// RoBue Anmerkung:
		// Hiermit werden ALLE Sensoren zum Messen aufgefordert.
		// Aufforderung nur bestimmter Sensoren:
		// "NULL" durch "tempID" ersetzen
		
		// RoBue Testausgabe UART:
		// usart_write("Starte Messvorgang ...\r\n");		 

		DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL );
		
		// Kein Messen mehr bis ss=5,15,25,35,45,55
		messen = 0;

		// Jetzt kann ausgelesen werden
		auslesen = 1;

		}	// -> if messen
		
		}	// -> if ss

		// Auslesen bei ss=8,18,28,38,48,58
		if ( ss%10 == 8 ) {

		// Auslesen?
		if ( auslesen == 1 ) {

		// (erste) ID ins RAM holen
		memcpy_P(tempID,DS18B20IDs[0],OW_ROMCODE_SIZE);	
				
		while ( tempID[0] != 0 ) {
		//while ( tempID[0] == 0x10 ) {
				
			// RoBue Anmerkung:
			// Hiermit wird jeweils ein einzelner Sensor ausgelesen
			// und die Temperatur in ow_array abgelegt.
			// Achtung:
			// Pro Sekunde können max. ca. 10 Sensoren ausgelesen werden!
			if ( DS18X20_read_meas( tempID, &subzero,&cel, &cel_frac_bits) == DS18X20_OK ) {

				ow_array[i] = DS18X20_temp_to_decicel(subzero, cel, cel_frac_bits);

				// Minuswerte:
					if ( subzero )
						ow_array[i] *= (-1);
				
				// min/max:
				if ( minmax == 1 ) {
					// Zurücksetzen der Min/Max_Werte 1x/Tag
					// auf die gerade aktuellen Temperaturen 
					ow_array[i+MAXSENSORS] = ow_array[i];
					ow_array[i+MAXSENSORS*2] = ow_array[i];
				}
				else {
					// Abgleich der Temp. mit den gespeicherten Min/Max-Werten
					if (ow_array[i]  < ow_array[i+MAXSENSORS])
        					ow_array[i+MAXSENSORS] = ow_array[i];
               				if (ow_array[i]  > ow_array[i+MAXSENSORS*2])
						ow_array[i+MAXSENSORS*2] = ow_array[i];
        			}

				//TWert = DS18X20_temp_to_decicel(subzero, cel, cel_frac_bits);
				//ow_array[i] = TWert;
				
				// RoBue:
				// Testausgabe UART:
				// usart_write("%2i:%2i:%2i: Temperatur: %3i Grad\r\n",hh,mm,ss,ow_array[i]/10);
				
			}	// -> if
			else {
				usart_write("\r\nCRC Error (lost connection?) ");
				DS18X20_show_id_uart( tempID, OW_ROMCODE_SIZE );


			}	// -> else
		
		// nächste ID ins RAM holen
		memcpy_P(tempID,DS18B20IDs[++i],OW_ROMCODE_SIZE);

		}	// -> while
		
		// RoBue:
		// Temperatur auf LCD ausgeben (IP von Startausgabe (s.o.) wird Überschrieben)
		#if USE_SER_LCD
			lcd_print(1,0,"Tmp:            ");
			lcd_print(1,5,"%i C",ow_array[0]/10);
			lcd_print(1,11,"%i C",ow_array[1]/10);
		#endif

		}	// -> if auslesen

		auslesen = 0;	// Auslesen vorläufig abschalten
		messen = 1;	// Messen wieder ermöglichen
		minmax = 0;	// Min/Max-Werte vergleichen

		}	// -> if ss
		
	#endif
	

	// **********************************************************
	// RoBue:
	// Schalten der Ports (PORTC) durch bestimmte Bedingungen
	// - Temperatur (1-Wire -> PORTA7) mit/ohne Lüftungsautomatik
	// - digital, analog (-> PORTA0-6)
	// - Zeit
	// **********************************************************
	
	// Automatik eingeschaltet?
	if ( var_array[9] == 1 ) {

	if ( ss%10 == 1 ) {
		schalten = 1;
	}

	// Abfrage bei ss =0,10,20,30,40,50
	if ( ss%10 == 0 ) {
	if ( schalten == 1 ) {
	
	// RoBue Testausgabe UART:
	// usart_write("%2i:%2i: Schaltfunktionen testen ...\r\n",hh,mm);	


	// PORTC0:
	// Über Temperatur: var_array[10] - Sensor0		
	if (( ow_array[0]/10 < var_array[10] ) || ( ow_array[0] < 0 )) {
		PORTC |= (1 << PC0); // ein
	//
	// Über Temperatur: var_array[10] - Sensor0 - PORTA0
	// if ((PINA&0b00000001) == 0 ) {		// PORTA0: Fenster geschlossen?
	//	if (( ow_array[0]/10 < var_array[10] ) || ( ow_array[0] < 0 )) {
	//		PORTC |= (1 << PC0); // ein
	//	}
	//  	else { 
	//     		PORTC &= ~(1 << PC0); // aus
	//	}
	}
	else {
	      	PORTC &= ~(1 << PC0); // aus
	}

	// PORTC1:
	// Über Temperatur: var_array[11] - Sensor1
	// if (( ow_array[1]/10 < var_array[11] ) || ( ow_array[1] < 0 )) {
	//	PORTC |= (1 << PC1); // ein
	//
	// Über Temperatur: var_array[11] - Sensor1 - PORTA1
	if ((PINA&0b00000010) == 0 ) {		// PORTA1: Fenster geschlossen?
		if (( ow_array[1]/10 < var_array[11] ) || ( ow_array[1] < 0 )) {
			PORTC |= (1 << PC1); // ein
		}
		else {
			PORTC &= ~(1 << PC1); // aus
		}
	}
	else {
		PORTC &= ~(1 << PC1); // aus
	}
		
	// PORTC2:
	// Über Temperatur: var_array[12] - Sensor2
	// if (( ow_array[2]/10 < var_array[12] ) || ( ow_array[2] < 0 )) {						
	//	PORTC |= (1 << PC2); // ein
	// Über Temperatur: var_array[12] - Sensor2 - PORTA2
	// if ((PINA&0b00000100) == 0 ) {		// PORTA2: Fenster geschlossen?
	//	if (( ow_array[2]/10 < var_array[12] ) || ( ow_array[2] < 0 )) {
	//		PORTC |= (1 << PC2); // ein
	//	}
	//	else {
	//		PORTC &= ~(1 << PC2); // aus
	//	}
	//}
	//else {
	//	PORTC &= ~(1 << PC2); // aus
	//}

	// PORTC4:
	// Über 2 Temperaturen (Differenz)
	// var_array[13] - Sensor3 Vorlauf, Sensor4 Ruecklauf
	//
	// z.B. Zirkulationspumpe für Warmwasser
	// Achtung: Temp. von Sensor3 MUSS höher/gleich sein als Temp. von Sensor4
	// Ansonsten laeuft die Pumpe immer
	//
		if ( (ow_array[3]/10 - ow_array[4]/10) >= var_array[14] ) {						
		PORTC |= (1 << PC4); // ein
	}
	else {
		PORTC &= ~(1 << PC4); // aus
	}

		// PORTC6:
	// Über Analogwert:
	if ( var_array[6] > var_array[18] ) {
		PORTC |= (1 << PC6); // ein
	}
	else {
		PORTC &= ~(1 << PC6); // aus
	}

	// PORTC7:
	// Über Zeit: ein/aus
	if ( hh == var_array[20] ) {
		if ( mm == var_array[21] ) {
			PORTC |= (1 << PC7); // ein
		}
	}
	if ( hh == var_array[22] ) {
		if ( mm == var_array[23] ) {
			PORTC &= ~(1 << PC7); // aus
		}
	}


	schalten = 0;	// Vorerst nicht mehr schalten

	} // -> schalten == 1
	} // -> if ss == ...
	} // -> if var_array == 1
	
	
        //Wetterdaten empfangen (Testphase)
        #if GET_WEATHER
        http_request ();
        #endif
        
        //Empfang von Zeitinformationen
	#if USE_NTP
		if(!ntp_timer){
		ntp_timer = NTP_REFRESH;
		ntp_request();
		}
	#endif //USE_NTP
		
        //Versand von E-Mails
        #if USE_MAIL
	        if (mail_enable == 1)
	        {
	            mail_enable = 0;
	            mail_send();
	        }
        #endif //USE_MAIL
        
        //Rechner im Netzwerk aufwecken
        #if USE_WOL
	        if (wol_enable == 1)
	        {
	            wol_enable = 0;
	            wol_request();
	        }
        #endif //USE_WOL
           
	//USART Daten für Telnetanwendung?
	telnetd_send_data();
    }

return(0);
}