00001
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
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
00098 PORTE.DIRSET = 0xFF;
00099 PORTD.DIRCLR = 0xFF;
00100 PORTCFG.MPCMASK = 0xFF;
00101 PORTD.PIN0CTRL |= PORT_INVEN_bm;
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112 TWI_MasterInit(&twiMaster,
00113 &TWIC,
00114 TWI_MASTER_INTLVL_LO_gc,
00115 TWI_BAUDSETTING);
00116
00117
00118 TWI_SlaveInitializeDriver(&twiSlave, &TWIC, TWIC_SlaveProcessData);
00119 TWI_SlaveInitializeModule(&twiSlave,
00120 SLAVE_ADDRESS,
00121 TWI_SLAVE_INTLVL_LO_gc);
00122
00123
00124 PMIC.CTRL |= PMIC_LOLVLEN_bm;
00125 sei();
00126
00127 uint8_t BufPos = 0;
00128 while (1) {
00129 while(!PORTD.IN);
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
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
00157 }
00158
00159
00160 PORTE.OUT = (twiMaster.readData[0]);
00161
00162 while(PORTD.IN);
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 }