Beispiel #1
0
// functions
void i2cInit(void)
{
	// set pull-up resistors on I2C bus pins
	// TODO: should #ifdef these
	sbi(PORTC, 0);	// i2c SCL on ATmega163,323,16,32,etc
	sbi(PORTC, 1);	// i2c SDA on ATmega163,323,16,32,etc
	sbi(PORTD, 0);	// i2c SCL on ATmega128,64
	sbi(PORTD, 1);	// i2c SDA on ATmega128,64

	// clear SlaveReceive and SlaveTransmit handler to null
	i2cSlaveReceive = 0;
	i2cSlaveTransmit = 0;
	// set i2c bit rate to 100KHz
	i2cSetBitrate(100);
	// enable TWI (two-wire interface)
	sbi(TWCR, TWEN);
	// set state
	I2cState = I2C_IDLE;
	// enable TWI interrupt and slave address ACK
	sbi(TWCR, TWIE);
	sbi(TWCR, TWEA);
	//outb(TWCR, (inb(TWCR)&TWCR_CMD_MASK)|BV(TWINT)|BV(TWEA));
	// enable interrupts
	sei();
}
Beispiel #2
0
void i2cInit(void)
{
	// set i2c bit rate to 100KHz
	i2cSetBitrate(100);
	// enable TWI (two-wire interface)
	sbi(TWCR, TWEN);	// Enable TWI
}
Beispiel #3
0
// functions
void i2cInit(void)
{
    GenCal_Flag = 1;
    // set pull-up resistors on I2C bus pins
    // this seems unnecessary, commented out by MBR
    /*
    sbi(PORTC, 0);	// i2c SCL on ATmega163,323,16,32,etc
    sbi(PORTC, 1);	// i2c SDA on ATmega163,323,16,32,etc
    sbi(PORTD, 0);	// i2c SCL on ATmega128,64
    sbi(PORTD, 1);	// i2c SDA on ATmega128,64
*/
    // clear SlaveReceive and SlaveTransmit handler to null
    i2cSlaveReceive = 0;
    i2cSlaveTransmit = 0;
    // set i2c bit rate to 100KHz
    i2cSetBitrate(100);
    // enable TWI (two-wire interface)
    sbi(TWCR, TWEN);
    // set state
    I2cState = I2C_IDLE;
    // enable TWI interrupt and slave address ACK
    sbi(TWCR, TWIE);
    sbi(TWCR, TWEA);
    // outb(TWCR, (inb(TWCR)&TWCR_CMD_MASK)|BV(TWINT)|BV(TWEA));
    // enable interrupts
    sei();
}
void i2cInit(void)
{
	// set i2c bit rate to 40KHz
	i2cSetBitrate(100);
	// enable TWI (two-wire interface)
	sbi(TWCR, TWEN);
	
	//initialize
	i2cSendStart();
	i2cWaitForComplete();
	i2cSendByte(0xA6);    //write to ADXL
	i2cWaitForComplete();
	i2cSendByte(0x2D);    //power register
	i2cWaitForComplete();
	i2cSendByte(0x08);    //measurement mode
	i2cWaitForComplete();
	i2cSendStop();
	
	i2cSendStart();
	i2cWaitForComplete();
	i2cSendByte(0xA6);    //write to ADXL
	i2cWaitForComplete();
	i2cSendByte(0x31);    //data format
	i2cWaitForComplete();
	i2cSendByte(0x08);    //full resolution
	i2cWaitForComplete();
	i2cSendStop();
}
Beispiel #5
0
void spyglassInit(void)
{
	i2cInit();
	i2cSetBitrate(100);
	PcfCtrlData = (SPYGLASS_LED0 | SPYGLASS_LED1 | SPYGLASS_BEEPER);
	spyglassSetLeds(0);
	spyglassSetBeeper(0);
}
Beispiel #6
0
// functions
void edpInit(void)
{
	// initialize i2c interface and function library
	i2cInit();
	// set i2c bit rate to 30KHz
	i2cSetBitrate(30);
	// set the Slave Receive Handler function
	// (this function will run whenever a master somewhere else on the bus
	//  writes data to us as a slave)
	i2cSetSlaveReceiveHandler( edpSlaveReceiveService );
	// set the Slave Transmit Handler function
	// (this function will run whenever a master somewhere else on the bus
	//  attempts to read data from us as a slave)
	i2cSetSlaveTransmitHandler( edpSlaveTransmitService );
}
Beispiel #7
0
// functions
void i2cInit(void)
{
    // clear SlaveReceive and SlaveTransmit handler to null
    i2cSlaveReceive = 0;
    i2cSlaveTransmit = 0;
    // set i2c bit rate to 100KHz
    i2cSetBitrate(100);
    // set state
    I2cState = I2C_IDLE;
    // enable TWI (two-wire interface)
    // enable TWI interrupt and slave address ACK
    TWCR |= BV(TWEN) | BV(TWIE) | BV(TWEA);
    //outb(TWCR, (inb(TWCR)&TWCR_CMD_MASK)|BV(TWINT)|BV(TWEA));
    // enable interrupts
}
Beispiel #8
0
int main(void) {
    uchar   i;

    DDRC &= (~0x30);

    _delay_ms(20);

    DDRC |= (1U << 3);
    PORTC |= (1U << 3);

    i2cInit();
    i2cSetBitrate(10);

    i2cSetLocalDeviceAddr(0x80, TRUE);

    i2cSetSlaveReceiveHandler(i2c_write);

    _delay_ms(20);

    PORTC ^= (1U << 3);

    wdt_enable(WDTO_1S);
    /* Even if you don't use the watchdog, turn it off here. On newer devices,
     * the status of the watchdog (on/off, period) is PRESERVED OVER RESET!
     */
    wdt_disable();

    /* RESET status: all port bits are inputs without pull-up.
     * That's the way we need D+ and D-. Therefore we don't need any
     * additional hardware initialization.
     */
    usbInit();
    usbDeviceDisconnect();  /* enforce re-enumeration, do this while interrupts are disabled! */
    i = 0;
    while(--i){             /* fake USB disconnect for > 250 ms */
        wdt_reset();
        _delay_ms(1);
    }
    usbDeviceConnect();
    sei();
    for(;;) {                /* main event loop */
        wdt_reset();
        usbPoll();
    }
    return 0;
}
Beispiel #9
0
void twi_init(void)
{
#if TWILIB == AVR315 || TWILIB == AVR315_SYNC || TWILIB == AVR315_QUEUED

    TWI_Master_Initialise();

#elif TWILIB == BUFFTW

    i2cInit();
    i2cSetBitrate(400);

#else

    twi_init();

#endif
}
Beispiel #10
0
// functions
void i2cInit(void)
{
	// set pull-up resistors on I2C bus pins
	// TODO: should #ifdef these
	//sbi(PORTC, 4);	// i2c SCL on ATmega163,323,16,32,etc
	//sbi(PORTC, 5);	// i2c SDA on ATmega163,323,16,32,etc

	// set i2c bit rate to 100KHz
	i2cSetBitrate(100);
	// enable TWI (two-wire interface)
	sbi(TWCR, TWEN);
	// set state
	I2cState = I2C_IDLE;
	// enable TWI interrupt and slave address ACK
	sbi(TWCR, TWIE);
	sbi(TWCR, TWEA);
	//outb(TWCR, (inb(TWCR)&TWCR_CMD_MASK)|BV(TWINT)|BV(TWEA));
}
Beispiel #11
0
void ads7828test(void)
{
	u08 i;
	u16 conv=0;

	// initialize i2c function library
	i2cInit();
	i2cSetBitrate(30);

	// initialize
	if(ads7828Init(ADS7828_I2C_ADDR))
	{
		rprintf("ADS7828 detected and initialized!\r\n");
	}
	else
	{
		rprintf("Cannot detect ADS7828!\r\n");
		return;
	}
	
	// use the externally applied voltage on REF pin
	ads7828SetReference(0);
	// or use the internal 2.5V voltage reference
	//ads7828SetReference(1);

	while(1)
	{
		// position cursor
		vt100SetCursorPos(4,1);

		for(i=0; i<8; i++)
		{
			// convert
			conv = ads7828Convert(ADS7828_I2C_ADDR, i);
			// print results
			rprintf("CH#%d: Conversion result is: %d    \r\n", i, conv-2048);
		}
		// pause between readings
		timerPause(100);
	}
}
Beispiel #12
0
// functions
void i2cInit(void)
{
	// set pull-up resistors on I2C bus pins
	sbi(I2C_SCL_PORT, I2C_SCL_PIN);
	sbi(I2C_SDA_PORT, I2C_SDA_PIN);

	// clear SlaveReceive and SlaveTrasmit handler to null
	i2cSlaveReceive = 0;
	i2cSlaveTransmit = 0;
	// set i2c bit rate to 100KHz
	i2cSetBitrate(100);
	// enable TWI (two-wire interface)
	sbi(TWCR, TWEN);
	// set state
	I2cState = I2C_IDLE;
	// enable TWI interrupt and slave address ACK
	sbi(TWCR, TWIE);
	sbi(TWCR, TWEA);
	// enable interrupts
	sei();
}
// functions
void i2cInit(void) {

#ifdef I2C_USE_INT_PULLUP_RESISTORS
	// set pull-up resistors on I2C bus pins
#if (defined (__AVR_ATmega64C1__) || defined (__AVR_ATmega64M1__) ||\
	defined (__AVR_ATmega128__) || defined (__AVR_ATmega1280__) ||\
	defined (__AVR_ATmega1281__) || defined (__AVR_ATmega1284P__) ||\
	defined (__AVR_ATmega128RFA1__) || defined(__AVR_ATmega2560__))

	sbi(PORTD, 0);	// i2c SCL on ATmega128,64
	sbi(PORTD, 1);	// i2c SDA on ATmega128,64
#elif (defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__))
    sbi(PORTC, 5); // i2c SCL on ATmega8
    sbi(PORTC, 4); // i2c SDA on ATmega8
#else
	sbi(PORTC, 0);	// i2c SCL on ATmega163,323,16,32,etc
	sbi(PORTC, 1);	// i2c SDA on ATmega163,323,16,32,etc
#endif
#endif

	// clear SlaveReceive and SlaveTransmit handler to null
	i2cSlaveReceive = 0;
	i2cSlaveTransmit = 0;
    i2cStopHandler = 0;
	// set i2c bit rate to 100KHz
	i2cSetBitrate(100);
	// enable TWI (two-wire interface)
	sbi(TWCR, TWEN);
	// set state
	I2cState = I2C_IDLE;
	// enable TWI interrupt and slave address ACK
	sbi(TWCR, TWIE);
	sbi(TWCR, TWEA);
	//outb(TWCR, (inb(TWCR)&TWCR_CMD_MASK)|BV(TWINT)|BV(TWEA));
	// enable interrupts
	sei();
}
Beispiel #14
0
int main(void) {
	DDRD = 0xff;
	DDRB = 0xff;
	DDRA = 0xff;
	clear();
	init();
	frame = 0;

	i2cInit();
	i2cSetBitrate(10);
	i2cSetLocalDeviceAddr(LOCAL_ADDR, TRUE);
	i2cSetSlaveReceiveHandler( i2cSlaveReceiveService );
	i2cSetSlaveTransmitHandler( i2cSlaveTransmitService );

	frameDelay = 100;

	// continuously loop
	while(1) {
//		draw();
//		updateOffsets();
//		frame++;
//		_delay_ms(frameDelay);
	}
}
Beispiel #15
0
int main (void)
{
	I2C_init();
	i2cSetBitrate(100);
	pca9685_init(PCA9685_WRITE);
	/*just for calibration purpose*/
	uint16_t pwm_data[] = {
			//0b0000000000101001, //decimal=41 -> 1% PWM
			0b0000000001010010,   //decimal=82 -> 2% PWM
			0b0000000001111011,   //decimal=123 -> 3% PWM
			0b0000000010100111,   //decimal=167 -> 4% PWM
			0b0000000011001101,   //decimal=205 -> 5% PWM
			0b0000000011110110,   //decimal=246 -> 6% PWM
			0b0000000100011111,   //decimal=287 -> 7% PWM
			0b0000000101001000,   //decimal=328 -> 8% PWM
			0b0000000101101100,   //decimal=365 -> 9% PWM
			0b0000000110011010,   //decimal=410 -> 10% PWM
			0b0000000111000011,   //decimal=451 -> 11% PWM
			0b0000000111101100,   //decimal=492 -> 12% PWM
			0b0000001000010100,   //decimal=532 -> 13% PWM
			0b0000001000111101,   //decimal=573 -> 14% PWM
			0b0000001001100110,   //decimal=614 -> 15% PWM
			//0b0000001010001111,   //decimal=655 -> 16% PWM
			//0b0000001010111000,   //decimal=696 -> 17% PWM
			//0b0000001011100001,   //decimal=737 -> 18% PWM
			//0b0000001100001010,   //decimal=778 -> 19% PWM
			//0b0000001100110011,   //decimal=819 -> 20% PWM
	};

	while(1)
	{

		/*servo_neutral(PCA9685_WRITE, 3);
		_delay_ms(500);
		servo_left(PCA9685_WRITE, 3);
		_delay_ms(500);
		servo_neutral(PCA9685_WRITE, 3);
		_delay_ms(500);
		servo_right(PCA9685_WRITE, 3);
		_delay_ms(500);*/

		I2C_start();
		I2C_write(PCA9685_WRITE);
		I2C_write(FIRST_CHANNEL);
		I2C_write(0x01);			//channel 0
		I2C_write(0x00);
		I2C_write(pwm_data[8]);
		I2C_write(pwm_data[8]>>8);
		I2C_write(0x01);			//channel 1
		I2C_write(0x00);
		I2C_write(pwm_data[11]);
		I2C_write(pwm_data[11]>>8);
		I2C_write(0x01);			//channel 2
		I2C_write(0x00);
		I2C_write(pwm_data[1]);
		I2C_write(pwm_data[1]>>8);
		I2C_stop();
		_delay_ms(2000);
		I2C_start();
		I2C_write(PCA9685_WRITE);
		I2C_write(FIRST_CHANNEL);
		I2C_write(0x01);			//channel 0
		I2C_write(0x00);
		I2C_write(pwm_data[1]);
		I2C_write(pwm_data[1]>>8);
		I2C_write(0x01);			//channel 1
		I2C_write(0x00);
		I2C_write(pwm_data[3]);
		I2C_write(pwm_data[3]>>8);
		I2C_write(0x01);			//channel 2
		I2C_write(0x00);
		I2C_write(pwm_data[1]);
		I2C_write(pwm_data[1]>>8);
		I2C_stop();
		_delay_ms(2000);

	}
}
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
    }
}
Beispiel #17
0
int main(void)
{	
	unsigned char tempSec;
	get_mcusr(); // Disable the watchdog timer
	uartInit(); // Init both UARTs
	uartSetBaudRate(0,BAUD);
	uartSetBaudRate(1,BAUD);
	stdout = stdin = &uart1_str; // Init the stdout and stdin (printf, scanf...) functions
	stderr = &uart0_str; // Init the stderror functions
	i2cInit();
	i2cSetBitrate(100);	
	ds1307_enable(DS1307_I2C_ADDR); // Enable the DS1307 RTC
	// Make a flash on LEDs connected to PORTB when starting / restarting
	DDRB=0xff; // PORTB output
	PORTB=0xff; // All bits on port B = 1 
	_delay_ms(1000); // Wait until XBee modem has started
	PORTB=0x00; // All bits on port B = 0
	// Empty the receive buffer of both UARTs
	uartFlushReceiveBuffer(EthUART);
	uartFlushReceiveBuffer(XBeeUART);
	wdt_enable(WDTO_2S); // Enable watchdog, reset after 2 seconds
	wdt_reset();
	while(1)
	{	
		wdt_reset(); // Reset the watchdog timer		
		tempSec=time.sec;
		if(ds1307_gettime(DS1307_I2C_ADDR, &time))
		{	//if(tempSec!=time.sec)	
			if((time.sec%2==0)&&(tempSec!=time.sec)) // True every 2nd second
			{
				while(!uartReceiveBufferIsEmpty(XBeeUART))
				{
					int i;
					i=0;
					signed char XBeePacket[12];
					// Read data from slave module 
					while (i<13)
					{
						while (uartReceiveBufferIsEmpty(XBeeUART))
							;;
						// Syncronization
						uartReceiveByte(XBeeUART, &XBeePacket[i]);
						if (XBeePacket[i]=='~') // Counter is set to 0 if the current character is '~'
							i=0;
						if (XBeePacket[0]=='~') //Increment if the first character in received data is '~'
							i++;
					}					
					sm[XBeePacket[7]-'0'].temp=XBeePacket[12]-7; // Compensate for heat from components in slave module
					// Check status of slave module and make status ready to send to webserver
					if (XBeePacket[10]=='f')
						sm[XBeePacket[7]-'0'].status=0;
					else if (XBeePacket[10]=='n')
						sm[XBeePacket[7]-'0'].status=1;
					XBeePacketCounter[XBeePacket[7]-'0']=1; // Set packet counter for this slave module to 1
				}
				// Check packet counter of all modules, if false: status=2 is sent to the webserver
				// Webserver will then now which modules that are unconnected
				for (int i=0; i<noOfModules; i++)
				{		
					if (!XBeePacketCounter[i])
						sm[i].status=2;
					XBeePacketCounter[i]=0;
				}
				// Receive data from webserver
				if (checkForEthPacket(ethPacket))
				{
					if (ethPacket[pHour]<24)// edit time 
					{
						time.yr=ethPacket[pYear];
						time.mon=ethPacket[pMonth];
						time.dat=ethPacket[pDate];
						time.hr=ethPacket[pHour];
						time.min=ethPacket[pMin];
						ds1307_settime(DS1307_I2C_ADDR,time);
					}
					int number=0;
					while (number<noOfModules)
					{
						sm_web[number].type=ethPacket[pType+number*pFieldsModules];
						sm_web[number].status=ethPacket[pStatus+number*pFieldsModules];
						sm_web[number].temp=ethPacket[pTemp+number*pFieldsModules];
						switch (sm_web[number].type)
						{
							case 0:
								if (sm_web[number].status==0) // Check if the current slave module is disabled on the website
									sendSlaveModule(number,0); // Send data to slave module: turn off relay
								else
								{
									if ((sm[number].temp)<(sm_web[number].temp-1)) // Measured temperature < wanted temperature - 1 ?
										sendSlaveModule(number,1); // Send data to slave module: turn on relay
									else if ((sm[number].temp)>(sm_web[number].temp+1)) // Measured temperature > wanted temperature + 1 ?
										sendSlaveModule(number,0); // Send data to slave module: turn off relay
									else 
										sendSlaveModule(number,2); // Send data to slave module
								}
								break;
						}
						number++; // next slave module
					}
				}				
				sendEthPacket(time, sm); // send packet with data to webserver
			}
		}
	}				
	return 0;	
}
Beispiel #18
0
//to initialize the i2c subroutine
void initI2c(void)
{
	i2cInit();
	i2cSetBitrate(100);
}
Beispiel #19
0
//#########################################
//#########################################
//#########################################
// S T A R T  M A I N  P R O G R A M 
int main(void)
{	

//-----------------------------------------
// Allocate memory
//-----------------------------------------	
	int32_t temperature = 0;
	int32_t pressure = 0;
	int16_t BMP085_calibration_int16_t[8];
	int16_t BMP085_calibration_uint16_t[3];
	uint8_t error_code=0;
	int32_t altitude=0;
//-----------------------------------------
// Initialize Stuff
//-----------------------------------------
	//uart_init();				//Initialize UART0
	volatile int16_t testVar;
	

	_delay_ms(11);
	i2cSetBitrate(1000);		//Initialize TWI 1000kHz	
	BMP085_Calibration(BMP085_calibration_int16_t, BMP085_calibration_uint16_t,&error_code);////Initialize BMP085
	lcd_init();
	lcd_set_font(FONT_FIXED_8,NORMAL);
//-----------------------------------------
// Do forever
//-----------------------------------------
		
		//printf("error code global init: %d \n",error_code);
    while(1)
    {
		/*	
		printf("------------------------\n");
		printf("------------------------\n");
		printf("Pressure raw %ld \n",bmp085ReadPressure(&error_code));
		printf("error code global: %d \n",error_code);
		printf("------------------------\n");
		printf("------------------------\n");
		printf("Temperature raw %ld \n",bmp085ReadTemp(&error_code));
		printf("error code global: %d \n",error_code);
		printf("------------------------\n");
		printf("------------------------\n");
		*/
		bmp085Convert(BMP085_calibration_int16_t, BMP085_calibration_uint16_t,&temperature, &pressure,&error_code);
		
		
		lcd_moveto_xy  (1, 0);		
		lcd_put_string_P(FONT_FIXED_8, NORMAL, PSTR("Temp: "));
		lcd_put_int((int16_t)temperature);

		lcd_moveto_xy  (2, 0);		
		lcd_put_string_P(FONT_FIXED_8, NORMAL, PSTR("Pressure: "));
		lcd_put_int((int16_t)pressure);
	
		altitude = bmp085CalcAltitude (pressure);
		
		lcd_moveto_xy  (3, 0);		
		lcd_put_string_P(FONT_FIXED_8, NORMAL, PSTR("Altitude: "));
		lcd_put_int((int16_t)altitude);


		/*
		printf("Temperature: %ld (in 0.1 deg C)\n", temperature);
		
		printf("Pressure: %ld Pa\n\n", pressure);	
		
		printf("Altitude: %ld dm\n", bmp085CalcAltitude(pressure));		
		
		
		printf("error code global: %d \n",error_code);
		printf("------------------------\n");
		printf("------------------------\n");
		*/
		_delay_ms(200);		
    }


    return 0;
}