Serial function limitations?

Right now I'm developing some code and it appears as though they are crashing. Or maybe it's my Arduino? I am able to get a few decent outputs while looping on a reading. Then the connection just starts printing out random garbage. I'm curious what limitations there are to the serial function calls.

Hmm, can you provide a few more details? What's crashing? How can you tell? What are you reading the serial data with? Can you post your code (Arduino or otherwise)?

It konks out at 22 iterations and I’m not sure why. All I’m doing is cycling through the chip reading the memory addresses. I can even loop reading the same address and it still konks out. Here’s my code:

#include <Wire.h>

#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22

void setup( void )
{
Serial.begin(9600);

// Init I2C
PORTC |= 0x18; // Connect Pull-Up resistors
TWSR &= 0xF8; // Set bit rate prescalar (p239)
TWBR = 0x0a; // Set two wire bit rate
TWCR = (1 << TWEN); // Enable I2C
}

void loop( void )
{
int i = 0;

while(1)
{
Serial.print( "#: " );
Serial.print( i );

start(); // Send base register
TX( 0x3A );
TX( 0x0F + i );
start();
TX( 0x3B );
RX();
stop();

Serial.print( " D: " );
Serial.println( TWDR, HEX );

i++;
}
}

void start()
{
// ******* Send Start Sequence
/* TWINT - Clear Interrupt Flag
TWSTA - Transmit start
TWEN - Enable I2C Bus
TWEA - Enable Acknoledge */
TWCR = (1 << TWEN) | (1 << TWINT) | (1 << TWSTA) | (1 << TWEA);
while( !(TWCR & (1 << TWINT) ) );

#if defined DEBUG
Serial.print( “STA:” );
Serial.println( TWSR, HEX );
#endif
}

void TX( byte data )
{
// ******* Send register address
TWDR = data;
/* TWINT - Clear Interrupt Flag
TWEN - Enable I2C Bus
TWEA - Enable Acknoledge */
TWCR = (1 << TWINT) | (1 << TWEA) | (1 << TWEN);
while( !(TWCR & (1 << TWINT) ) );

#if defined DEBUG
Serial.print( "TX: " );
Serial.println( TWSR, HEX );
#endif
}

void RX()
{
TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA);
while( !(TWCR & (1 << TWINT) ) );

#if defined DEBUG
Serial.print( "RX: " );
Serial.println( TWSR, HEX );
#endif
}
void stop( void )
{
TWCR = (1 << TWEN) | (1 << TWEA) | (1 << TWINT) | (1 |TWSTO);
while( TWCR & (1 << TWSTO) );

#if defined DEBUG
Serial.print( “STO:” );
Serial.println( TWSR, HEX );
#endif
}

Here’s my output:

#: 0 D: 3A

#: 1 D: 92

#: 2 D: 8C

#: 3 D: 8A

#: 4 D: 10

#: 5 D: 20

#: 6 D: 40

#: 7 D: 17

#: 8 D: 9

#: 9 D: 36

#: 10 D: A3

#: 11 D: 5D

#: 12 D: A3

#: 13 D: 44

#: 14 D: 60

#: 15 D: 0

#: 16 D: 0

#: 17 D: 7

#: 18 D: 0

#: 19 D: 8

#: 20 D: 0

#: 21 D: 0

#: 22

Hmm, I'd try to figure out if it's reading the value of TWDR that's hanging or sending it over the Serial port. I'd guess the former. Maybe turn an LED on, then read the TWDR into a variable, then turn the LED off, and print the value of the variable?

After making the post I was able to determine that it was my I2C code. If I use the wire library it doesn't hang. However, I want to use my code instead of the wire library so I can write an ISR.

So, do you know much about the TWI on the Arduino? I haven't had much luck getting support for it on the forum.

The Arduino uses the I2C/TWI code from Wiring. I seem to recall there are instructions/examples on the Wiring site for polled and interrupt driven I2C clients.

-j