コード例 #1
0
ファイル: twi_master.c プロジェクト: valerii76/electronics
/****************************************************************************
Call this function to resend the last message. The driver will reuse the data previously put in the transceiver buffers.
The function will hold execution (loop) until the TWI_ISR has completed with the previous operation,
then initialize the next operation and return.
****************************************************************************/
void twi_start_transceiver( void )
{
    while ( twi_transceiver_busy() )
        ;             // Wait until TWI is ready for next transmission.
    TWI_statusReg.all = 0;
    TWI_state         = TWI_NO_STATE ;
    TWCR = (1<<TWEN)|                             // TWI Interface enabled.
           (1<<TWIE)|(1<<TWINT)|                  // Enable TWI Interupt and clear the flag.
           (0<<TWEA)|(1<<TWSTA)|(0<<TWSTO)|       // Initiate a START condition.
           (0<<TWWC);                             //
}
コード例 #2
0
ファイル: twi_master.c プロジェクト: InSoonPark/asf
/**
 * @brief  To Transmit data over TWI
 * @ingroup QDebug-Target
 */
void twi_send_message( void )
{
  TWI_msgSize = ((TX_Buffer[1]<<8)+TX_Buffer[2]);// Number of data to transmit.
  
  TWI_addr  =(TWISLAVE_ADDR<<TWI_ADR_BITS) | (FALSE<<TWI_READ_BIT); // Store slave address with R/W setting.
  
  TWI_statusReg.all = 0;      

  TWCR = (1<<TWEN)|                             // TWI Interface enabled.
         (1<<TWIE)|(1<<TWINT)|                  // Enable TWI Interupt and clear the flag.
         (0<<TWEA)|(1<<TWSTA)|(0<<TWSTO)|       // Initiate a START condition.
         (0<<TWWC);                             //

  while ( twi_transceiver_busy() );             // Wait until TWI is ready for next transmission.
}
コード例 #3
0
ファイル: twi_master.c プロジェクト: valerii76/electronics
/****************************************************************************
Call this function to read out the requested data from the TWI transceiver buffer. I.e. first call
TWI_Start_Transceiver to send a request for data to the slave. Then Run this function to collect the
data when they have arrived. Include a pointer to where to place the data and the number of bytes
requested (including the address field) in the function call. The function will hold execution (loop)
until the TWI_ISR has completed with the previous operation, before reading out the data and returning.
If there was an error in the previous transmission the function will return the TWI error code.
****************************************************************************/
unsigned char twi_get_data_from_transceiver( unsigned char *msg, unsigned char msgSize )
{
    unsigned char i;

    while ( twi_transceiver_busy() )
        ;             // Wait until TWI is ready for next transmission.

    if( TWI_statusReg.lastTransOK )               // Last transmission competed successfully.              
    {
        for ( i = 0; i < msgSize; i++ )                 // Copy data from Transceiver buffer.
        {
            msg[ i ] = TWI_buf[ i ];
        }
    }
    return( TWI_statusReg.lastTransOK );
}
コード例 #4
0
ファイル: twi_master.c プロジェクト: InSoonPark/asf
/**
 * @brief  To Receive data over TWI
 * @ingroup QDebug-Target
 */
void twi_retrieve_message( void )
{
  	unsigned int i;
  
  	TWI_addr  = (TWISLAVE_ADDR<<TWI_ADR_BITS) | (TRUE<<TWI_READ_BIT); 
	
  	TWI_statusReg.all = 0;      

  	TWCR = 	(1<<TWEN)|                             // TWI Interface enabled.
         	(1<<TWIE)|(1<<TWINT)|                  // Enable TWI Interupt and clear the flag.
         	(0<<TWEA)|(1<<TWSTA)|(0<<TWSTO)|       // Initiate a START condition.
         	(0<<TWWC);                             //

  	while ( twi_transceiver_busy() );             	// Wait until TWI is ready for next transmission.  
	
	if( ! TWI_statusReg.lastTransOK )               // Last transmission completed successfully.              
		for ( i=0; i<=TWI_msgSize; i++ )           	// Copy data from Transceiver buffer.
		  	RX_Buffer[i] = 0;						// Clear data if transmission not successful
}
コード例 #5
0
ファイル: twi_master.c プロジェクト: valerii76/electronics
/****************************************************************************
Call this function to send a prepared message. The first byte must contain the slave address and the
read/write bit. Consecutive bytes contain the data to be sent, or empty locations for data to be read
from the slave. Also include how many bytes that should be sent/read including the address byte.
The function will hold execution (loop) until the TWI_ISR has completed with the previous operation,
then initialize the next operation and return.
****************************************************************************/
void twi_start_transceiver_with_data( unsigned char *msg, unsigned char msgSize )
{
    unsigned char temp;

    /*DBG_FLASH (DDRB, PORTB, 2, 3);*/
    /*_delay_ms (5000);*/
    while ( twi_transceiver_busy() )
        ;             // Wait until TWI is ready for next transmission.

    TWI_msgSize = msgSize;                        // Number of data to transmit.
    TWI_buf[0]  = msg[0];                         // Store slave address with R/W setting.
    if (!( msg[0] & (TRUE<<TWI_READ_BIT) ))       // If it is a write operation, then also copy data.
    {
        for ( temp = 1; temp < msgSize; temp++ )
            TWI_buf[ temp ] = msg[ temp ];
    }
    TWI_statusReg.all = 0;
    TWI_state         = TWI_NO_STATE ;
    TWCR = (1<<TWEN)|                             // TWI Interface enabled.
           (1<<TWIE)|(1<<TWINT)|                  // Enable TWI Interupt and clear the flag.
           (0<<TWEA)|(1<<TWSTA)|(0<<TWSTO)|       // Initiate a START condition.
           (0<<TWWC);                             //
}
コード例 #6
0
ファイル: twi_master.c プロジェクト: valerii76/electronics
/****************************************************************************
Call this function to fetch the state information of the previous operation. The function will hold execution (loop)
until the TWI_ISR has completed with the previous operation. If there was an error, then the function 
will return the TWI State code. 
****************************************************************************/
unsigned char twi_get_state_info( void )
{
    while ( twi_transceiver_busy() )
        ;             // Wait until TWI has completed the transmission.
    return ( TWI_state );                         // Return error state.
}