// Copyright 2006 Yamane Akira //ICC-AVR application builder : 2005/07/23 23:05 // Target : M8 // Crystal: 4.0000Mhz // TWI MASTER Test Program #include #include unsigned char s_addr, status_b; void port_init(void) { PORTB = 0x00; DDRB = 0x00; PORTC = 0x00; DDRC = 0x00; PORTD = 0xFF; DDRD = 0xFF; } //TWI initialize // bit rate:12 void twi_init(void) { TWCR= 0X00; //disable twi TWBR= 0x0C; //set bit rate 12 TWSR= 0x00; //set prescale 1x TWAR= 0x00; //set slave address TWCR= 0x04; //enable twi } void ERROR (unsigned char edata) { PORTD = ~edata; //display error# while(1); } void MasterTransmit(unsigned char tdata) { TWCR = (1 << TWINT)|(1 << TWSTA)|(1 << TWEN); // send START condition while (!(TWCR & (1 << TWINT))) // Confirm START has been transmitted ; if ((TWSR & 0xF8) != 0x08) // Check TWI Status register ; TWDR = (s_addr <<1) | 0; // Load SLA_W into TWDR TWCR = (1 << TWINT) | (1<