Hi,
is there some simple example for the TWI in Master Mode, no interrupt on the XMEGA ? I have the example from Atmel which seems to be a little too much blown up for me. I'm trying to interface an RTC (DS1340) with requires a 7 byte datagram. At the moment I'm only observing the PC0 and PC1 pins on the Xmega128A1 with an oscilloscope and nothing happens at all. I'd say at least the address should be sent ? Here is the code so far :
#define CPU_SPEED 32000000 #define BAUDRATE 400000 #define TWI_BAUD(F_SYS, F_TWI) ((F_SYS / (2 * F_TWI)) - 5) #define TWI_BAUDSETTING TWI_BAUD(CPU_SPEED, BAUDRATE) #define RTC_SLAVE_ADDRESS 0xD0; // already shifted !!!! /******************************************************************/ TWI_t * rtc = &TWIC; uint8_t twitest[7]={0x05, 0x06, 0x07, 0x08, 0x25, 0x45, 0x65}; // only some test bytes /****************************************************/ void twi_init(TWI_t * twiname){ twiname->MASTER.CTRLB = TWI_MASTER_SMEN_bm; twiname->MASTER.BAUD = TWI_BAUDSETTING; twiname->MASTER.CTRLA = TWI_MASTER_ENABLE_bm; twiname->MASTER.STATUS = TWI_MASTER_BUSSTATE_IDLE_gc; return; } /****************************************************/ void twi_write_rtc(TWI_t *twiname, uint8_t *writeData){ uint8_t i; twiname->MASTER.ADDR = RTC_SLAVE_ADDRESS; // write to RTC while(!(twiname->MASTER.STATUS&TWI_MASTER_WIF_bm)); twiname->MASTER.DATA = 0x00; // write word addr while(!(twiname->MASTER.STATUS&TWI_MASTER_WIF_bm)); for(i=0;i<7;i++){ // write date and time twiname->MASTER.DATA =writeData[i]; while(!(twiname->MASTER.STATUS&TWI_MASTER_WIF_bm)); } return; } /****************************************************/ void twi_read_rtc(TWI_t *twiname, uint8_t *readData){ // read from RTC uint8_t i; uint8_t address = RTC_SLAVE_ADDRESS; address |= 0x01; twiname->MASTER.ADDR = RTC_SLAVE_ADDRESS; while(!(twiname->MASTER.STATUS&TWI_MASTER_WIF_bm)); twiname->MASTER.DATA = 0x00; // write word addrpointer first twiname->MASTER.ADDR = address; // send read command for(i=0;i<7;i++){ // read date and time while(!(twiname->MASTER.STATUS&TWI_MASTER_RIF_bm)); readData[i] = twiname->MASTER.DATA; } return; } /****************************************************/ int main(void){ twi_init(rtc); while(1){ twi_write_rtc(rtc, twitest); } return 0; }
thanks for any help or comment
Mat
EDITED 03.05.2011