// This file has been prepared for Doxygen automatic documentation generation. /*! \file ******************************************************************** * * Atmel Corporation * * File : USI_TWI_Slave.c * Compiler : IAR EWAAVR 3.20c * Revision : $Revision: 1.14 $ * Date : $Date: Friday, December 09, 2005 17:25:38 UTC $ * Updated by : $Author: jtyssoe $ * * Support mail : avr@atmel.com * * Supported devices : All device with USI module can be used. * * AppNote : AVR312 - Using the USI module as a I2C slave * * Description : Example showing how to use the USI_TWI drivers; * Loops back received data. * * ****************************************************************************/ #include #include #include #include "usiTwiSlave.h" #define nsamples 100 // number of samples to accumulate // Sample TWI transmission commands #define TWI_CMD_MASTER_WRITE 0x10 #define TWI_CMD_MASTER_READ 0x20 #define TWI_CMD_MASTER_WRITE_ALOT 0x30 /* \Brief The main function. * The program entry point. Initates TWI and enters eternal loop, waiting for data. */ int main( void ) { unsigned char TWI_slaveAddress, temp = 0; //Setting up for read static uint16_t count; static uint32_t accum; ADMUX = (0 << REFS2) | (0 << REFS1) | (0 << REFS0) // Vcc ref | (0 << ADLAR) // right adjust | (0 << MUX3) | (0 << MUX2) | (1 << MUX1) | (0 << MUX0); // ADC4 ADCSRA = (1 << ADEN) // enable | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // prescaler /128 // LED feedback port - connect port B to the STK500 LEDS PORTB = 0x55; // Startup pattern // Own TWI slave address TWI_slaveAddress = 0x10; usiTwiSlaveInit( TWI_slaveAddress ); sei(); // This loop runs forever. If the TWI Transceiver is busy the execution will just continue doing other operations. for(;;) { usiTwiTransmitByte(temp); // // accumulate samples // accum = 0; for (count = 0; count < nsamples; ++count) { // // initiate conversion // ADCSRA |= (1 << ADSC); // // wait for completion // while (ADCSRA & (1 << ADSC)) ; // // add result // accum += ADC; } temp = accum; } }