March Madness - I2C Arduino to Arduino Test

We had the make-up Arduino class today, there were 10 students. It went very well. I thought (maybe hoped is a better term) that I would get my March Madness done during the class. NOT...

In February I designed an LCD serial shield that can communicate via serial or I2C. I rememberd (thanks Keith) that I never tested the I2C even though I had written the code to receive I2C. (Note I2C is also known as TWI or just "Wire").

This program uses scans the I2C bus for debugging purposes and then just sends a string and counter to the I2C interface. Everything worked and I did not have to make any changes on the LCD serial board.

//************************************************************************
//*	I2C transmission test
//*	this tests sending data from one Arduino to anohter via I2C
//************************************************************************

#include	<stdlib.h>

#include	"WProgram.h"
#include	"HardwareSerial.h"

#include "Wire.h"
extern "C" { 
	#include "utility/twi.h"	// from Wire library, so we can do bus scanning
	//#include "twi.h"			// from Wire library, so we can do bus scanning
}

#define	kLCDserial_I2C	77

void ScanI2CBus(byte from_addr, byte to_addr) ;

int	i2cTestCounter;

//************************************************************************
void setup()
{
	Serial.begin(9600);           // set up Serial library at 9600 bps
	Serial.println("Test I2C (TWI) transmission");

	//*	join the I2C bus as the master
	Wire.begin();

	i2cTestCounter	=	0;
	ScanI2CBus(0, 100);

}



//************************************************************************
void loop()
{
char	tempString[32];

	Serial.println("loop");

	Wire.beginTransmission(kLCDserial_I2C);
	Wire.send("I2C test ");

	sprintf(tempString, "%d\r", i2cTestCounter);

	Wire.send(tempString);

	Wire.endTransmission();
	
	
	i2cTestCounter++;
	
	delay(1000);

}





//****************************************************************************
//*	Scan the I2C bus between addresses from_addr and to_addr.
//*	On each address, call the callback function with the address and result.
//*	If result==0, address was found, otherwise, address wasn't found
//*	(can use result to potentially get other status on the I2C bus, see twi.c)
//*	Assumes Wire.begin() has already been called
//*	2009, Tod E. Kurt, http://todbot.com/blog/
//****************************************************************************
void ScanI2CBus(byte from_addr, byte to_addr) 
{
byte addr;
byte rc;
byte data; // not used, just an address to feed to twi_writeTo()
int	foundCount;

	Serial.print("starting scanning of I2C bus from ");
	Serial.print(from_addr, DEC);
	Serial.print(" to ");
	Serial.print(to_addr, DEC);
	Serial.println("...");

	data		=	0;
	foundCount	=	0;
	for(addr = from_addr; addr <= to_addr; addr++ )
	{
		Serial.print("addr:");
		Serial.print(addr,DEC);
		rc	=	twi_writeTo(addr, &data, 0, 1);
		if (rc == 0)
		{
			foundCount++;
		}
		Serial.print( (rc==0) ? " found!":"       ");
		Serial.print( (addr%4) ? "\t":"\r\n");
	}
	Serial.println();

	Serial.print("I2C device count = ");
	Serial.println(foundCount);
}


// this is the code for the receiving end. Note, once the receive data event
// is registerd, no polling is required.

//************************************************************************
static void	I2CrecevieDataEvent(int howMany)
{
byte	rcvData;

	while (Wire.available())
	{
		rcvData	=	Wire.receive();    // receive byte as an integer
		LCD_ProcessChar(rcvData);
		CheckScreenSaver(kScreenShouldBe_ON);
	}
}

//************************************************************************
static void StartI2C(void)
{
	Wire.begin(gLCD_I2Caddress);			//*	join i2c bus as slave
	Wire.onReceive(I2CrecevieDataEvent);	//*	register receive data event
}