Xmega Application Note


twi_example.c

Go to the documentation of this file.
00001 /* This file has been prepared for Doxygen automatic documentation generation.*/
00054 #include "avr_compiler.h"
00055 #include "twi_master_driver.h"
00056 #include "twi_slave_driver.h"
00057 
00059 #define SLAVE_ADDRESS    0x55
00060 
00062 #define NUM_BYTES        8
00063 
00065 #define CPU_SPEED       2000000
00066 #define BAUDRATE        100000
00067 #define TWI_BAUDSETTING TWI_BAUD(CPU_SPEED, BAUDRATE)
00068 
00069 
00070 /* Global variables */
00071 TWI_Master_t twiMaster;    
00072 TWI_Slave_t twiSlave;      
00076 uint8_t sendBuffer[NUM_BYTES] = {0x55, 0xAA, 0xF0, 0x0F, 0xB0, 0x0B, 0xDE, 0xAD};
00077 
00078 
00082 void TWIC_SlaveProcessData(void)
00083 {
00084         uint8_t bufIndex = twiSlave.bytesReceived;
00085         twiSlave.sendData[bufIndex] = (~twiSlave.receivedData[bufIndex]);
00086 }
00087 
00088 
00095 int main(void)
00096 {
00097         /* Initialize PORTE for output and PORTD for inverted input. */
00098         PORTE.DIRSET = 0xFF;
00099         PORTD.DIRCLR = 0xFF;
00100         PORTCFG.MPCMASK = 0xFF;
00101         PORTD.PIN0CTRL |= PORT_INVEN_bm;
00102 //      PORTCFG.MPCMASK = 0xFF;
00103 //      PORTD.PIN0CTRL = (PORTD.PIN0CTRL & ~PORT_OPC_gm) | PORT_OPC_PULLUP_gc;
00104         
00105         // Enable internal pull-up on PC0, PC1.. Uncomment if you don't have external pullups
00106 //      PORTCFG.MPCMASK = 0x03; // Configure several PINxCTRL registers at the same time
00107 //      PORTC.PIN0CTRL = (PORTC.PIN0CTRL & ~PORT_OPC_gm) | PORT_OPC_PULLUP_gc; //Enable pull-up to get a defined level on the switches
00108 
00109         
00110 
00111         /* Initialize TWI master. */
00112         TWI_MasterInit(&twiMaster,
00113                        &TWIC,
00114                        TWI_MASTER_INTLVL_LO_gc,
00115                        TWI_BAUDSETTING);
00116 
00117         /* Initialize TWI slave. */
00118         TWI_SlaveInitializeDriver(&twiSlave, &TWIC, TWIC_SlaveProcessData);
00119         TWI_SlaveInitializeModule(&twiSlave,
00120                                   SLAVE_ADDRESS,
00121                                   TWI_SLAVE_INTLVL_LO_gc);
00122 
00123         /* Enable LO interrupt level. */
00124         PMIC.CTRL |= PMIC_LOLVLEN_bm;
00125         sei();
00126 
00127         uint8_t BufPos = 0;
00128         while (1) {
00129                 while(!PORTD.IN); /* Wait for user to press button */      
00130           
00131                 switch(PORTD.IN){
00132                         case (PIN0_bm):  BufPos = 0; break;
00133                         case (PIN1_bm):  BufPos = 1; break;
00134                         case (PIN2_bm):  BufPos = 2; break;
00135                         case (PIN3_bm):  BufPos = 3; break;
00136                         case (PIN4_bm):  BufPos = 4; break;
00137                         case (PIN5_bm):  BufPos = 5; break;
00138                         case (PIN6_bm):  BufPos = 6; break;
00139                         case (PIN7_bm):  BufPos = 7; break;
00140                         default:    break;
00141                 }
00142 
00143                 /* Show the byte to send while holding down the key. */
00144                 while(PORTD.IN != 0x00){
00145                         PORTE.OUT = sendBuffer[BufPos];
00146                 }
00147 
00148                 TWI_MasterWriteRead(&twiMaster,
00149                                     SLAVE_ADDRESS,
00150                                     &sendBuffer[BufPos],
00151                                     1,
00152                                     1);
00153 
00154 
00155                 while (twiMaster.status != TWIM_STATUS_READY) {
00156                         /* Wait until transaction is complete. */
00157                 }
00158 
00159                 /* Show the sent byte received and processed on LEDs. */
00160                 PORTE.OUT = (twiMaster.readData[0]);
00161                 
00162                 while(PORTD.IN); /* Wait for user to release button */
00163         }
00164 }
00165 
00167 ISR(TWIC_TWIM_vect)
00168 {
00169         TWI_MasterInterruptHandler(&twiMaster);
00170 }
00171 
00173 ISR(TWIC_TWIS_vect)
00174 {
00175         TWI_SlaveInterruptHandler(&twiSlave);
00176 }
@DOC_TITLE@
Generated on Tue Aug 11 12:42:12 2009 for AVR1308 Using the XMEGA TWI by doxygen 1.5.9