コード例 #1
0
ファイル: twi.c プロジェクト: nishantP-10/WSNS15IRFence
static int8_t twi_tx(uint8_t *buf, uint8_t len)
{
    uint8_t i;
    uint8_t waits = 0;
    const uint8_t max_waits =
        TIME_TO_MS(twi_tx_timeout) / TIME_TO_MS(twi_tx_poll_interval);

    LOG("TWI write: ");
    for (i = 0; i < len; ++i)
        LOGP("0x%x ", msg_buf[i]);
    LOGA("\r\n");

    TWI_Start_Transceiver_With_Data(msg_buf, len);

    /* NOTE: can't use nrk_time_get because before nrk_start() */
    while (TWI_Transceiver_Busy() && waits++ < max_waits)
        nrk_spin_wait_us(twi_tx_poll_interval.nano_secs / 1000);

    if (waits >= max_waits) {
        LOG("WARN: TWI write timed out\r\n");
        return NRK_ERROR;
    }

    if (!TWI_statusReg.lastTransOK)
    {
        LOG("WARN: TWI write failed\r\n");
        handle_error(TWI_Get_State_Info());
        return NRK_ERROR;
    }

    return NRK_OK;
}
コード例 #2
0
ファイル: twi_example.c プロジェクト: oddballbutler/litbike
void main(void)
{
	unsigned char messageBuf[4];
	unsigned char TWI_targetSlaveAddress, temp, TWI_operation = 0,
			pressedButton, myCounter = 0;

	//LED feedback port - connect port B to the STK500 LEDS
	DDRB = 0xFF;
	PORTB = myCounter;

	//Switch port - connect portD to the STK500 switches
	DDRD = 0x00;

	TWI_Master_Initialize();
	__enable_interrupt();

	TWI_targetSlaveAddress = 0x10;

	// This example is made to work together with the AVR311 TWI Slave application note and stk500.
	// In adition to connecting the TWI pins, also connect PORTB to the LEDS and PORTD to the switches.
	// The code reads the pins to trigger the action you request. There is an example sending a general call,
	// address call with Master Read and Master Write. The first byte in the transmission is used to send
	// commands to the TWI slave.

	// This is a stk500 demo example. The buttons on PORTD are used to control different TWI operations.
	for (;;)
	{
		pressedButton = ~PIND;
		if (pressedButton)       // Check if any button is pressed
		{
			do
			{
				temp = ~PIND;
			}      // Wait until key released
			while (temp);

			switch (pressedButton)
			{
			// Send a Generall Call
			case (1 << PD0):
				messageBuf[0] = TWI_GEN_CALL; // The first byte must always consit of General Call code or the TWI slave address.
				messageBuf[1] = 0xAA; // The command or data to be included in the general call.
				TWI_Start_Transceiver_With_Data(messageBuf, 2);
				break;

				// Send a Address Call, sending a command and data to the Slave
			case (1 << PD1):
				messageBuf[0] = (TWI_targetSlaveAddress << TWI_ADR_BITS)
						| (FALSE << TWI_READ_BIT); // The first byte must always consit of General Call code or the TWI slave address.
				messageBuf[1] = TWI_CMD_MASTER_WRITE; // The first byte is used for commands.
				messageBuf[2] = myCounter; // The second byte is used for the data.
				TWI_Start_Transceiver_With_Data(messageBuf, 3);
				break;

				// Send a Address Call, sending a request, followed by a resceive
			case (1 << PD2):
				// Send the request-for-data command to the Slave
				messageBuf[0] = (TWI_targetSlaveAddress << TWI_ADR_BITS)
						| (FALSE << TWI_READ_BIT); // The first byte must always consit of General Call code or the TWI slave address.
				messageBuf[1] = TWI_CMD_MASTER_READ; // The first byte is used for commands.
				TWI_Start_Transceiver_With_Data(messageBuf, 2);

				TWI_operation = REQUEST_DATA; // To release resources to other operations while waiting for the TWI to complete,
											  // we set a operation mode and continue this command sequence in a "followup"
											  // section further down in the code.

											  // Get status from Transceiver and put it on PORTB
			case (1 << PD5):
				PORTB = TWI_Get_State_Info();
				break;

				// Increment myCounter and put it on PORTB
			case (1 << PD6):
				PORTB = ++myCounter;
				break;

				// Reset myCounter and put it on PORTB
			case (1 << PD7):
				PORTB = myCounter = 0;
				break;
			}
		}

		if (!TWI_Transceiver_Busy())
		{
			// Check if the last operation was successful
			if (TWI_statusReg.lastTransOK)
			{
				if (TWI_operation) // Section for follow-up operations.
				{
					// Determine what action to take now
					if (TWI_operation == REQUEST_DATA)
					{ // Request/collect the data from the Slave
						messageBuf[0] = (TWI_targetSlaveAddress << TWI_ADR_BITS)
								| (TRUE << TWI_READ_BIT); // The first byte must always consit of General Call code or the TWI slave address.
						TWI_Start_Transceiver_With_Data(messageBuf, 2);
						TWI_operation = READ_DATA_FROM_BUFFER; // Set next operation
					}
					else if (TWI_operation == READ_DATA_FROM_BUFFER)
					{ // Get the received data from the transceiver buffer
						TWI_Get_Data_From_Transceiver(messageBuf, 2);
						PORTB = messageBuf[1];        // Store data on PORTB.
						TWI_operation = FALSE;     // Set next operation
					}
				}
			}
			else // Got an error during the last transmission
			{
				// Use TWI status information to detemine cause of failure and take appropriate actions.
				TWI_Act_On_Failure_In_Last_Transmission(TWI_Get_State_Info());
			}
		}

		// Do something else while waiting for TWI operation to complete and/or a switch to be pressed
		asm volatile ("nop");
		// Put own code here.

	}
}
コード例 #3
0
ファイル: main.c プロジェクト: vehar/Interface_adapter
void main( void )
{
  unsigned char messageBuf[TWI_BUFFER_SIZE];
  unsigned char TWI_slaveAddress;
  
  // LED feedback port - connect port B to the STK500 LEDS
  DDRB  = 0xFF; // Set to ouput
  PORTB = 0x55; // Startup pattern
  
  // Own TWI slave address
  TWI_slaveAddress = 0x10;

  // Initialise TWI module for slave operation. Include address and/or enable General Call.
  TWI_Slave_Initialise( (unsigned char)((TWI_slaveAddress<<TWI_ADR_BITS) | (TRUE<<TWI_GEN_BIT) )); 
                       
  __enable_interrupt();

  // Start the TWI transceiver to enable reseption of the first command from the TWI Master.
  TWI_Start_Transceiver();

  // This example is made to work together with the AVR315 TWI Master application note. In adition to connecting the TWI
  // pins, also connect PORTB to the LEDS. The code reads a message as a TWI slave and acts according to if it is a 
  // general call, or an address call. If it is an address call, then the first byte is considered a command byte and
  // it then responds differently according to the commands.

  // This loop runs forever. If the TWI is busy the execution will just continue doing other operations.
  for(;;)
  { 
    #ifdef POWER_MANAGEMENT_ENABLED
      // Sleep while waiting for TWI transceiver to complete or waiting for new commands.
      // If we have data in the buffer, we can't enter sleep because we have to take care
      // of it first.
      // If the transceiver is busy, we enter idle mode because it will wake up by all TWI
      // interrupts.
      // If the transceiver not is busy, we can enter power-down mode because next receive
      // should be a TWI address match and it wakes the device up from all sleep modes.
      if( ! TWI_statusReg.RxDataInBuf ) {
        if(TWI_Transceiver_Busy()) {
          MCUCR = (1<<SE)|(0<<SM2)|(0<<SM1)|(0<<SM0); // Enable sleep with idle mode
        } else {
          MCUCR = (1<<SE)|(0<<SM2)|(1<<SM1)|(0<<SM0); // Enable sleep with power-down mode
        }
        __sleep();
      } else {
        __no_operation(); // There is data in the buffer, code below takes care of it.
      }
    #else // No power management
      // Here you can add your own code that should be run while waiting for the TWI to finish    
      __no_operation(); // Put own code here.
    #endif
      
    
    // Check if the TWI Transceiver has completed an operation.
    if ( ! TWI_Transceiver_Busy() )                              
    {
      // Check if the last operation was successful
      if ( TWI_statusReg.lastTransOK )
      {
        // Check if the last operation was a reception
        if ( TWI_statusReg.RxDataInBuf )
        {
          TWI_Get_Data_From_Transceiver(messageBuf, 2);         
          // Check if the last operation was a reception as General Call        
          if ( TWI_statusReg.genAddressCall )
          {
            // Put data received out to PORTB as an example.        
            PORTB = messageBuf[0];
          }               
          else // Ends up here if the last operation was a reception as Slave Address Match   
          {
            // Example of how to interpret a command and respond.
            
            // TWI_CMD_MASTER_WRITE stores the data to PORTB
            if (messageBuf[0] == TWI_CMD_MASTER_WRITE)
            {
              PORTB = messageBuf[1];                            
            }
            // TWI_CMD_MASTER_READ prepares the data from PINB in the transceiver buffer for the TWI master to fetch.
            if (messageBuf[0] == TWI_CMD_MASTER_READ)
            {
              messageBuf[0] = PINB;
              TWI_Start_Transceiver_With_Data( messageBuf, 1 );
            }
          }
        }                
        else // Ends up here if the last operation was a transmission  
        {
            __no_operation(); // Put own code here.
        }
        // Check if the TWI Transceiver has already been started.
        // If not then restart it to prepare it for new receptions.             
        if ( ! TWI_Transceiver_Busy() )
        {
          TWI_Start_Transceiver();
        }
      }
      else // Ends up here if the last operation completed unsuccessfully
      {
        TWI_Act_On_Failure_In_Last_Transmission( TWI_Get_State_Info() );
      }
    }
  }
}
コード例 #4
0
ファイル: main_single.c プロジェクト: bppinho/Hiperion-Copter
void main( void )
{
  unsigned char messageBuf[TWI_BUFFER_SIZE];
  unsigned char TWI_slaveAddress;
  
  // Feedback (opcional)
  /* DDRB  = 0xFF; // Set to output
     PORTB = 0x55; // Startup pattern */
  // Le os pinos 0-2 para endereçamento
  DDRC  = ~((1 << 0) | (1 << 1) | (1 << 2)); //Bits 0-2 como entrada
  
  // Own TWI slave address
  TWI_slaveAddress = 0x10 + 0b00000111 & PORTD;

  // Initialise TWI module for slave operation. Include address and/or enable General Call.
  TWI_Slave_Initialise( (unsigned char)((TWI_slaveAddress<<TWI_ADR_BITS) | (TRUE<<TWI_GEN_BIT) )); 
                       
  __enable_interrupt();

  // Start the TWI transceiver to enable reseption of the first command from the TWI Master.
  TWI_Start_Transceiver();

  // This example is made to work together with the AVR315 TWI Master application note. In adition to connecting the TWI
  // pins, also connect PORTB to the LEDS. The code reads a message as a TWI slave and acts according to if it is a 
  // general call, or an address call. If it is an address call, then the first byte is considered a command byte and
  // it then responds differently according to the commands.

  // This loop runs forever. If the TWI is busy the execution will just continue doing other operations.
  for(;;)
  {    
    
    // Check if the TWI Transceiver has completed an operation.
    if ( ! TWI_Transceiver_Busy() )                              
    {
      // Check if the last operation was successful
      if ( TWI_statusReg.lastTransOK )
      {
        // Confere se ha algo no buffer
        if ( TWI_statusReg.RxDataInBuf )
        {
          TWI_Get_Data_From_Transceiver(messageBuf, 2);         
          // Confere se o ultimo pedido foi um General Call
          if ( TWI_statusReg.genAddressCall )
          {
            // Trata o "broadcast"
            PORTB = messageBuf[0];
          }               
          else // Ends up here if the last operation was a reception as Slave Address Match   
          {
            // Example of how to interpret a command and respond.
            
            // TWI_CMD_MASTER_WRITE stores the data to PORTB
            if (messageBuf[0] == TWI_CMD_MASTER_WRITE)
            {
              PORTB = messageBuf[1];                            
            }
            // TWI_CMD_MASTER_READ prepares the data from PINB in the transceiver buffer for the TWI master to fetch.
            if (messageBuf[0] == TWI_CMD_MASTER_READ)
            {
              messageBuf[0] = PINB;
              TWI_Start_Transceiver_With_Data( messageBuf, 1 );
            }
          }
        }                
        // Apos a operacao ele reinicia o receptor
        if ( ! TWI_Transceiver_Busy() )
        {
          TWI_Start_Transceiver();
        }
      }
      else // Trata erro de transmissao
      {
        TrataErroTransI2C( TWI_Get_State_Info() );
      }
    }
  }
}
コード例 #5
0
ファイル: main.c プロジェクト: mattdarcy/KAUSat-5-Sensor-Sim
void main( void )
{
  unsigned char messageBuf[4];
  unsigned char TWI_slaveAddress, TWI_slaveAddress2, TWI_slaveAddressMask, temp;

  // LED feedback port - connect port B to the STK500 LEDS
  DDRB  = 0xFF; // Set to ouput
  PORTB = 0x55; // Startup pattern

  // Own TWI slave address
  TWI_slaveAddress     = (0x10<<TWI_ADR_BITS);
  TWI_slaveAddress2    = (0x11<<TWI_ADR_BITS);                  // Alternativ slave address to respond to.
  TWI_slaveAddressMask = TWI_slaveAddress ^ TWI_slaveAddress2;  // XOR the addresses to get the address mask.

  // Initialise TWI module for slave operation. Include address and/or enable General Call.
  TWI_Slave_Initialise( TWI_slaveAddress | (TRUE<<TWI_GEN_BIT), TWI_slaveAddressMask); 
                                                                                  
  __enable_interrupt();

  // Start the TWI transceiver to enable reseption of the first command from the TWI Master.
  TWI_Start_Transceiver();

  // This example is made to work together with the AVR315 TWI Master application note. In adition to connecting the TWI
  // pins, also connect PORTB to the LEDS. The code reads a message as a TWI slave and acts according to if it is a 
  // general call, or an address call. If it is an address call, then the first byte is considered a command byte and
  // it then responds differently according to the commands.

  // This loop runs forever. If the TWI is busy the execution will just continue doing other operations.
  for(;;)
  {    
    // Check if the TWI Transceiver has completed an operation.
    if ( ! TWI_Transceiver_Busy() )                              
    {
    // Check if the last operation was successful
      if ( TWI_statusReg.lastTransOK )
      {
    // Check if the last operation was a reception
        if ( TWI_statusReg.RxDataInBuf )
        {
          TWI_Get_Data_From_Transceiver(messageBuf, 3);         
    // Check if the last operation was a reception as General Call        
          if ( TWI_statusReg.genAddressCall )
          {
          // Put data received out to PORTB as an example.        
            PORTB = messageBuf[1];                              
          }
    // Ends up here if the last operation was a reception as Slave Address Match                  
          else
          {
    // Take action dependant on what slave address that was used in the message
            if (messageBuf[0] == TWI_slaveAddress)
            {
    // Example of how to interpret a command and respond.            
            // TWI_CMD_MASTER_WRITE stores the data to PORTB
              if (messageBuf[1] == TWI_CMD_MASTER_WRITE)
                PORTB = messageBuf[2];                            
            // TWI_CMD_MASTER_READ prepares the data from PINB in the transceiver buffer for the TWI master to fetch.
              if (messageBuf[1] == TWI_CMD_MASTER_READ)
              {
                messageBuf[0] = PINB;                             
                TWI_Start_Transceiver_With_Data( messageBuf, 1 );           
              }
            }
            else
            {
              PORTB = messageBuf[1];  // Put TWI address data on PORTB
            }
          }
        }
    // Ends up here if the last operation was a transmission                  
        else
        {
            __no_operation(); // Put own code here.
        }
    // Check if the TWI Transceiver has already been started.
    // If not then restart it to prepare it for new receptions.             
        if ( ! TWI_Transceiver_Busy() )
        {
          TWI_Start_Transceiver();
        }      
      }
    // Ends up here if the last operation completed unsuccessfully
      else
      {
        TWI_Act_On_Failure_In_Last_Transmission( TWI_Get_State_Info() );
      }
      
    }
    // Do something else while waiting for the TWI transceiver to complete.    
    __no_operation(); // Put own code here.
  }
}
コード例 #6
0
ファイル: main.c プロジェクト: skeezix/shadowcar
int main( void )
{
  uint32_t val = 0;
  char textbuf [ (2*16) + 1 ]; // lcd

  /* setup
   */

  // LCD
  lcd_init();

  // Timer: enable a timer so we can measure passage of time
  //
  // Given: 20MHz clock
  // --> if we want resolution of ms (1/1000th second) .. actually, we want us (1/10th of a ms) so we can measure partial ms
  // --> and we have 1/20000000 clock resolution
  // -----> 2000 ticks will get us there (20,000 will get us ms)
  //
  // Goal: Use CTC interupt mode (CTC -> Clear on Timer Compare)
  // So a compare matches, it clears to zero and triggers interupt
  TCCR1B |= (1 << WGM12); // Configure timer 1 for CTC mode 
  OCR1A = 2000; // number to compare against
  TIMSK1 |= (1 << OCIE1A); // Enable CTC interrupt 
  TCCR1B |= (1 << CS10); // Set up timer , with no prescaler (works at full MHz of clock)

  // Receiver setup - set up pin change interupt
#if 1
  EICRA &= ~ ( (1 << ISC01) | (1 << ISC01) ); // clear ISC01+ISC00
  EICRA |= ( (1 << ISC00) ); // 00 set and 01 unset means any edge will make event
  PCMSK0 |= ( (1 << PCINT0) | (1 << PCINT1) ); // Pins to monitor: PA0 and PA1
  PCICR |= (1 << PCIE0); // PA is monitored
#endif

  // Serial - setup (for motor controller)
  mc_setup();

  // TWI - set up
  unsigned char twibuf [ TWI_BUFFER_SIZE ];
  unsigned char TWI_slaveAddress; 
  TWI_slaveAddress = 0x10; // our TWI address
  TWI_Slave_Initialise( (unsigned char)((TWI_slaveAddress<<TWI_ADR_BITS) | (TRUE<<TWI_GEN_BIT) )); // Initialise TWI module as slave; include addr+general
  unsigned char twi_heartbeat_counter = 0;

  // setup done - kick up interupts
  sei();

  // lets burn the first couple of seconds, so the receiver can get some signal
  // before we start blasting stuff into the motor controllers
  {
    unsigned int start_sec = g_time_s;
    while ( g_time_s - start_sec < 3 ) {
      nop();
    }
  }

  // Start the TWI transceiver to enable reseption of the first command from the TWI Master.
  TWI_Start_Transceiver();

#if 1 // timer test .. show per-second counter update on lcd
  if ( 1 ) {
    unsigned int last_sec = g_time_s;
    unsigned int last_us = _g_time_us_tick;

    unsigned char sent_l = 0, sent_r = 0;
    char message [ 17 ];

    while(1) {
      unsigned int ch1 = g_ch1_duration;
      unsigned int ch2 = g_ch2_duration;

      // 100ms has past?
      if ( _g_time_us_tick - last_us > 1000 ) {

        if ( g_control_twi ) {
          // we're on TWI control, but if nothing comes in.. revert back to RC
          if ( g_time_s - g_control_last_s > 2 ) {
            g_control_twi = 0;
          }
        } else {
          mc_set_by_receiver ( ch1, ch2, &sent_l, &sent_r, message );
        }

        last_us = _g_time_us_tick;
      } // .1sec tick

      // one second has past? update lcd
      if ( g_time_s != last_sec ) {
        //sprintf ( textbuf, "recv %2d %2d     ", g_ch1_duration, g_ch2_duration );
        sprintf ( textbuf, "m %2d %2d th %2d %2d #", sent_l, sent_r, ch1, ch2 );
        lcd_xy ( 0, 0 );
        lcd_puts( textbuf ); // display number right adjusted

        //sprintf ( textbuf, "t%2d #", g_time_s );
        sprintf ( textbuf, "t%2d # %s", g_time_s, message );
        lcd_xy ( 0, 1 );
        lcd_puts( textbuf ); // display number right adjusted

        last_sec = g_time_s;
      } // 1 sec tick

      // TWI/I2C stuff, talk to r-pi
      //

      // Check if the TWI Transceiver has completed an operation.
      if ( ! TWI_Transceiver_Busy() ) {

        // Check if the last operation was successful
        if ( TWI_statusReg.lastTransOK ) {

          // Check if the last operation was a reception
          if ( TWI_statusReg.RxDataInBuf ) {
            TWI_Get_Data_From_Transceiver ( twibuf, 3 );

            // Check if the last operation was a reception as General Call        
            if ( TWI_statusReg.genAddressCall ) {
              // don't care

            } else { // Ends up here if the last operation was a reception as Slave Address Match   
              // Example of how to interpret a command and respond.

#if 0            
              // TWI_CMD_MASTER_WRITE stores the data to PORTB
              if (twibuf[0] == TWI_CMD_MASTER_WRITE) {
                PORTB = twibuf[1];                            
              }
#endif

              // TWI_CMD_MASTER_READ prepares the data from PINB in the transceiver buffer for the TWI master to fetch.
              if ( twibuf[0] == tc_heartbeat ) {
                twibuf [ 0 ] = 1;
                twibuf [ 1 ] = twi_heartbeat_counter++;
                TWI_Start_Transceiver_With_Data ( twibuf, TWI_BUFFER_SIZE );

              } else if ( twibuf[0] == tc_gethello ) {
                sprintf ( twibuf + 1, "hello" );
                twibuf [ 0 ] = strlen ( twibuf + 1 ); // len
                TWI_Start_Transceiver_With_Data ( twibuf, TWI_BUFFER_SIZE );

              } else if ( twibuf[0] == tc_setmotors ) {
                g_control_last_s = g_time_s;
                mc_speed ( mcm_left, twibuf [ 1 ] );
                mc_speed ( mcm_right, twibuf [ 2 ] );

              } else if ( twibuf[0] == tc_takeover ) {
                g_control_last_s = g_time_s;
                g_control_twi = 1;

              } else if ( twibuf[0] == tc_release ) {
                g_control_twi = 0;

              } else {
                twibuf [ 0 ] = 1;
                twibuf [ 1 ] = 0xde;
                twibuf [ 2 ] = 0xad;
                twibuf [ 3 ] = 0xbe;
                twibuf [ 4 ] = 0xef;
                TWI_Start_Transceiver_With_Data ( twibuf, TWI_BUFFER_SIZE );
              }

            }

          } else { // Ends up here if the last operation was a transmission  
            // don't care
          }

          // Check if the TWI Transceiver has already been started.
          // If not then restart it to prepare it for new receptions.             
          if ( ! TWI_Transceiver_Busy() ) {
            TWI_Start_Transceiver();
          }

        } else { // Ends up here if the last operation completed unsuccessfully
          TWI_Act_On_Failure_In_Last_Transmission ( TWI_Get_State_Info() );
        } // success/fail

      } // TWI busy?

      // spin
      _delay_ms ( 20 );

    } // while forever

  } // if 1
#endif

  /* churn forever
   */
  while(1);

  return ( 0 );
}
コード例 #7
0
ファイル: main.c プロジェクト: dgt0011/motor_controller
int main(void)
{
  unsigned char messageBuf[TWI_BUFFER_SIZE];  
  unsigned char responseBuf[1];
  unsigned char TWI_slaveAddress;
  
  CMotor *m = NULL;
  int speed = 0;
  int direction = 0;
  
  // Own TWI slave address
  TWI_slaveAddress = 0x10;

  // Initialise TWI module for slave operation. Include address and/or enable General Call.
  TWI_Slave_Initialise( (unsigned char)((TWI_slaveAddress<<TWI_ADR_BITS) | (TRUE<<TWI_GEN_BIT) )); 
                       
   sei();

  // Start the TWI transceiver to enable reception of the first command from the TWI Master.
  TWI_Start_Transceiver(); 
  
  //start out withe the last tx being OK - to make sure we don't end up blocking on the first request
  TWI_statusReg.lastTransOK = TRUE;  
	
  m = motor_new(0);
  
  // This loop runs forever. If the TWI is busy the execution will just continue doing other operations.
  for(;;)
	{ 
	  
    // Check if the TWI Transceiver has completed an operation.
    if ( ! TWI_Transceiver_Busy() )                              
    {			  				
      // Check if the last operation was successful
      if ( TWI_statusReg.lastTransOK )
      {		  
        // Check if the last operation was a reception	
        if ( TWI_statusReg.RxDataInBuf )
        {					
          TWI_Get_Data_From_Transceiver(messageBuf, 4);       
                  
            if (messageBuf[1] == TWI_CMD_MOTOR_HALT)
            {
				motor_halt(m);
				responseBuf[0] = TWI_CMD_MOTOR_HALT;
				TWI_Start_Transceiver_With_Data( responseBuf, 1 );            
            } 
            else if (messageBuf[1] == TWI_CMD_MOTOR_SETSPEED)
            {				
				speed = messageBuf[2];
				direction = messageBuf[3];
				
				motor_setspeed_and_direction(m,speed,direction);
				responseBuf[0] = TWI_CMD_MOTOR_SETSPEED;
				TWI_Start_Transceiver_With_Data( responseBuf, 1 );
            }
            else
            {
				//log an error?  UNknown CMD
			}
        }                
        else // Ends up here if the last operation was a transmission  
        {
            //__no_operation(); // Put own code here.
 		
            responseBuf[0] = TWI_CMD_CONFIRM;
			TWI_Start_Transceiver_With_Data( responseBuf, 1 );
        }
        // Check if the TWI Transceiver has already been started.
        // If not then restart it to prepare it for new receptions.             
        if ( ! TWI_Transceiver_Busy() )
        {
          TWI_Start_Transceiver();
        }
      }
      else // Ends up here if the last operation completed unsuccessfully
      {	 		 
			//fail safe  - halt the motor
			motor_halt(m);
			TWI_Act_On_Failure_In_Last_Transmission( TWI_Get_State_Info() );
      }
    }
  }
}
コード例 #8
0
ファイル: piezo.c プロジェクト: miboome/miboo_hw
int main(){
  unsigned char messageBuf[TWI_BUFFER_SIZE];
  unsigned char TWI_slaveAddress;
  


  
  // Own TWI slave address
  TWI_slaveAddress = 0x20;


	InitIO();
	defset();
	InitTimer();
	OSCCAL=0xA8;
//	wdt_reset();
	/* Write logical one to WDCE and WDE */
//	WDTCR = (1<<WDCE) | (1<<WDE) | (2<<WDP0);
//	WDTCR = (1<<WDE) | (2<<WDP0);
	TIMING=eeprom_read_word(&EETIMING);
	TWI_Slave_Initialise( (unsigned char)((TWI_slaveAddress<<TWI_ADR_BITS) | (FALSE<<TWI_GEN_BIT) ));
	sei();
  TWI_Start_Transceiver();
	while (1){
   // Check if the TWI Transceiver has completed an operation.
    if ( ! TWI_Transceiver_Busy() )                              
    {
      // Check if the last operation was successful
      if ( TWI_statusReg.lastTransOK )
      {
        // Check if the last operation was a reception
        if ( TWI_statusReg.RxDataInBuf )
        {
		PORTB^=1<<PB3;
          TWI_Get_Data_From_Transceiver(messageBuf, 2);         
          // Check if the last operation was a reception as General Call        
          if ( TWI_statusReg.genAddressCall )
          {
            // Put data received out to PORTB as an example.        
           // PORTB = messageBuf[0];
		   
          }               
          else // Ends up here if the last operation was a reception as Slave Address Match   
          {
            // Example of how to interpret a command and respond.
			
			switch (messageBuf[0]){
			case xval:
				OCR1A=255-messageBuf[1];                            
				break;
			case yval:
              OCR1B=255-messageBuf[1];                            
			  break;
//              TWI_Start_Transceiver_With_Data( messageBuf, 1 );
			}
          }
        }                
        else // Ends up here if the last operation was a transmission  
        {
          //  __no_operation(); // Put own code here.

        }
        // Check if the TWI Transceiver has already been started.
        // If not then restart it to prepare it for new receptions.             
        if ( ! TWI_Transceiver_Busy() )
        {
          TWI_Start_Transceiver();
        }
      }
      else // Ends up here if the last operation completed unsuccessfully
      {
        TWI_Act_On_Failure_In_Last_Transmission( TWI_Get_State_Info() );
      }
    }

	}

}