I'm working on a test program for an application using a mega128, and I just wanted to test the I2C (TWI) interface. I did the following frequency definitions in a header file:
#define Xtal 16000000 // uC speed = 16Mhz #define TWI_FREQUENCY 400000 // 400 kHz Prescaler = 0x00 #define TWI_BIT_RATE (Xtal/(2*TWI_FREQUENCY)-8) // Bit Rate if prescaler = 0x00
and the following in the TWI initialization:
TWBR = TWI_BIT_RATE;
Everything works fine.
The problem appears when I want to try 20kHz with these definitions:
#define Xtal 16000000 // uC speed = 16Mhz #define TWI_FREQUENCY 20000 // 20 kHz Prescaler = 0x01 #define TWI_BIT_RATE (Xtal/(8*TWI_FREQUENCY)-2) // Bit Rate if prescaler = 0x01
Then I get the following warning when trying to write the TWI_BIT_RATE to the TWBR register:
C:\Prosjekter\Sintef\SDF\twi.c (93) : Warning[Pe061]: integer operation result is out of range
I've tried several combinations, and it seems it is the value of TWI_FREQUENCY that is the problem. If it is less than 33kHz I get the "out of range"-error regardless of which TWI_BIT_RATE-definition I use.
Anybody have any suggestions to what might be the problem?