Download avr-libc Reference Manual

Transcript
7.12 Example using the two-wire interface (TWI)
*/
#define TWI_SLA_24CXX
0xa0
/* E2 E1 E0 = 0 0 0 */
/*
* Maximal number of iterations to wait for a device to respond for a
* selection. Should be large enough to allow for a pending write to
* complete, but low enough to properly abort an infinite loop in case
* a slave is broken or not present at all. With 100 kHz TWI clock,
* transfering the start condition and SLA+R/W packet takes about 10
* s. The longest write period is supposed to not exceed ˜ 10 ms.
* Thus, normal operation should not require more than 100 iterations
* to get the device to respond to a selection.
*/
#define MAX_ITER
200
/*
* Number of bytes that can be written in a row, see comments for
* ee24xx_write_page() below. Some vendor’s devices would accept 16,
* but 8 seems to be the lowest common denominator.
*
* Note that the page size must be a power of two, this simplifies the
* page boundary calculations below.
*/
#define PAGE_SIZE 8
/*
* Saved TWI status register, for error messages only. We need to
* save it in a variable, since the datasheet only guarantees the TWSR
* register to have valid contents while the TWINT bit in TWCR is set.
*/
uint8_t twst;
/*
* Do all the startup-time peripheral initializations: UART (for our
* debug/test output), and TWI clock.
*/
void
ioinit(void)
{
UCSRB = _BV(TXEN);
/* tx enable */
UBRR = (SYSCLK / (16 * 9600UL)) - 1; /* 9600 Bd */
/* initialize TWI clock: 100 kHz clock, TWPS = 0 => prescaler = 1 */
#if defined(TWPS0)
/* has prescaler (mega128 & newer) */
TWSR = 0;
#endif
TWBR = (SYSCLK / 100000UL - 16) / 2;
}
/*
* Note [4]
* Send character c down the UART Tx, wait until tx holding register
* is empty.
Generated on Wed Nov 26 21:10:23 2003 for avr-libc by Doxygen
157