Xmega Application Note


twi_example.c

Go to the documentation of this file.
00001 
00046 //_____ I N C L U D E S ____________________________________________________
00047 
00048 #include "avr_compiler.h"
00049 #include "twi_slave_driver.h"
00050 
00051 //_____ M A C R O S ________________________________________________________
00052 
00053 //_____ D E F I N I T I O N ________________________________________________
00054 
00055 extern void TWI_process(TWI_Slave_t* twi);
00056 
00057 uint8_t twic_status_reg;
00058 uint8_t twic_ctrla_reg;
00059 uint8_t twic_ctrlb_reg;
00060 uint8_t twic_data_reg;
00061 
00064 #define SLAVE_ADDRESS    0x51
00065 
00067 #define NUM_BYTES        8
00068 
00069 /* Global variables */
00070 TWI_Slave_t twiSlave;      
00077 void TWIC_SlaveProcessData(void)
00078 {
00079    PORTF.OUT=twiSlave.receivedData[twiSlave.bytesReceived];
00080    uint8_t recvData = twiSlave.receivedData[twiSlave.bytesReceived];
00081    twiSlave.sendData[twiSlave.bytesReceived] = recvData;
00082 
00083    //init first byte to write
00084    twic_data_reg =  twiSlave.sendData[0];
00085 }
00086 
00095 int main(void)
00096 {
00097    /* Initialize PORTF for debug on STK600 */
00098    PORTF.DIRSET = 0xFF;
00099         
00100    // Configure LEDs for STK600
00101    PORTCFG.MPCMASK = 0xFF; // Configure several PINxCTRL registers at the same time
00102    PORTF.PIN0CTRL = PORT_INVEN_bm; // Invert input to turn the leds on when port output value is 1
00103    PORTF.OUT = 0x00;
00104    
00105    /* Set up interrupt mask on SCL pin */
00106    PORTC.INT0MASK = PIN1_bm;
00107         
00108    /* Initialize TWI slave. */
00109    TWI_SlaveInitializeDriver(&twiSlave, &TWIC, TWIC_SlaveProcessData);
00110    TWI_SlaveInitializeModule(&twiSlave,
00111                              SLAVE_ADDRESS,
00112                              TWI_SLAVE_INTLVL_HI_gc);   
00113    TWIC.SLAVE.CTRLA |= TWI_SLAVE_SMEN_bm;
00114         
00115    /* interrupt level. */
00116    PMIC.CTRL |= PMIC_HILVLEN_bm | PMIC_MEDLVLEN_bm;
00117    sei();
00118         
00119    do{
00120       //User application        
00121    }while (1);  
00122 }
00123 
00129 ISR(PORTC_INT0_vect)
00130 {
00131    PORTC.INTFLAGS |= PORT_INT0IF_bm;
00132    PORTC.INTCTRL = PORT_INT0LVL_OFF_gc;
00133 
00134    TWI_process(&twiSlave);
00135 }
@DOC_TITLE@
Generated on Tue Jan 19 18:41:58 2010 for AVR1320: True 400kHz operation for TWI slave by doxygen 1.5.8