So, ich habe mal mit diesen Programmen getestet:
RP6 Base (Slave):
Code:
/*
* ****************************************************************************
* RP6 ROBOT SYSTEM - ROBOT BASE TESTS
* ****************************************************************************
* Example: I2C Slave - I2C Receive Test
* Author(s): D. Ottensmeyer
* ****************************************************************************
* Description:
*
* This Example receives data via I2C as slave.
*
*
* ############################################################################
* The Robot does NOT move in this example! You can simply put it on a table
* next to your PC and you should connect it to the PC via the USB Interface!
* ############################################################################
* ****************************************************************************
*/
/*****************************************************************************/
// Includes:
#include "RP6RobotBaseLib.h" // The RP6 Robot Base Library.
// Always needs to be included!
#include "RP6I2CTWI.h" // Include the new I2C-Bus Library
/*****************************************************************************/
// Defines:
// The Slave Address on the I2C Bus can be specified here:
#define RP6BASE_I2C_SLAVE_ADR 10
#define CMD_SHOW_DATA 99 // Command to display the data
#define TEXT_OUTPUT // Show text
/*****************************************************************************/
// Variables:
uint16_t cnt = 0;
uint16_t error_cnt = 0;
uint8_t controlbyte;
uint8_t highbyte = 0;
uint8_t lowbyte = 0;
/*****************************************************************************/
// Main function - The program starts here:
int main(void)
{
initRobotBase();
setLEDs(0b111111);
mSleep(500);
setLEDs(0b000000);
I2CTWI_initSlave(RP6BASE_I2C_SLAVE_ADR);
while (true)
{
if (I2CTWI_writeRegisters[0] && !I2CTWI_writeBusy) {
controlbyte = I2CTWI_writeRegisters[0]; // Read control byte
I2CTWI_writeRegisters[0] = 0; // and reset it (!!!)
lowbyte = I2CTWI_writeRegisters[1]; // Read lowbyte
highbyte = I2CTWI_writeRegisters[2]; // Read highbyte
if (controlbyte == CMD_SHOW_DATA) {
cnt = ((highbyte << 8) | lowbyte); // Restore 16-bit value
#ifdef TEXT_OUTPUT
writeString_P("Received: "); // and show it
writeInteger(cnt, DEC);
#endif
if (!cnt) error_cnt = 0; // Reset error counter
else error_cnt++; // Error counter + 1
if (cnt != error_cnt) {
writeString_P(" ERROR!\n");
error_cnt = cnt;
}
#ifdef TEXT_OUTPUT
else writeChar('\n');
#endif
controlbyte = 0;
}
}
}
return 0;
}
/******************************************************************************
* Additional info
* ****************************************************************************
* Changelog:
* - v. 1.0 (initial release) 19.02.2011 by D. Ottensmeyer
*
* ****************************************************************************
*/
/*****************************************************************************/
RP6 M32 (Master):
Code:
/*
* ****************************************************************************
* RP6 ROBOT SYSTEM - RP6 CONTROL M32 EXAMPLES
* ****************************************************************************
* Example: I2C Master - I2C Send Test
* Author(s): D. Ottensmeyer
* ****************************************************************************
* Description:
*
* This Example sends data via I2C as master.
*
*
* ############################################################################
* The Robot does NOT move in this example! You can simply put it on a table
* next to your PC and you should connect it to the PC via the USB Interface!
* ############################################################################
* ****************************************************************************
*/
/*****************************************************************************/
// Includes:
#include "RP6ControlLib.h" // The RP6 Control Library.
// Always needs to be included!
#include "RP6I2CTWI.h" // Include the new I2C-Bus Library
/*****************************************************************************/
// Defines:
// The Slave Address on the I2C Bus can be specified here:
#define RP6BASE_I2C_SLAVE_ADR 10
#define CMD_SHOW_DATA 99 // Command to display the data
#define TEXT_OUTPUT // Show text
/*****************************************************************************/
// I2C Event handlers:
/**
* This function gets called automatically if there was an I2C Error like
* the slave sent a "not acknowledge" (NACK, error codes e.g. 0x20 or 0x30).
* The most common mistakes are:
* - using the wrong address for the slave
* - slave not active or not connected to the I2C-Bus
* - too fast requests for a slower slave
* Be sure to check this if you get I2C errors!
*/
void I2C_transmissionError(uint8_t errorState)
{
writeString_P("\nI2C ERROR --> TWI STATE IS: 0x");
writeInteger(errorState, HEX);
writeChar('\n');
}
/*****************************************************************************/
// Variables:
uint16_t cnt = 0;
uint8_t buffer[4];
/*****************************************************************************/
// Main - The program starts here:
int main(void)
{
initRP6Control();
initLCD();
I2CTWI_initMaster(100); // Initialize the TWI Module for Master operation
// with 100kHz SCL Frequency
// Register the event handlers:
I2CTWI_setTransmissionErrorHandler(I2C_transmissionError);
setLEDs(0b1111);
mSleep(500);
setLEDs(0b0000);
while(true)
{
if (!I2CTWI_isBusy()) {
cnt++; // TEST: COUNTER!!!
if (cnt > 32767) cnt = 0;
buffer[0] = 0;
buffer[1] = CMD_SHOW_DATA;
buffer[2] = cnt;
buffer[3] = (cnt >> 8);
I2CTWI_transmitBytes(RP6BASE_I2C_SLAVE_ADR, buffer, 4);
#ifdef TEXT_OUTPUT
setCursorPosLCD(0, 0);
writeStringLCD_P("I2C has sent:"); // What was sent?
setCursorPosLCD(1, 0);
writeIntegerLCD(cnt, DEC);
#endif
}
}
return 0;
}
/******************************************************************************
* Additional info
* ****************************************************************************
* Changelog:
* - v. 1.0 (initial release) 19.02.2011 by D. Ottensmeyer
*
* ****************************************************************************
*/
/*****************************************************************************/
Ergebnis:
Der Zähler zählt immer 3 Stellen hoch, danach wird eine Stelle ausgelassen. Da pro Zähler 4 Bytes übertragen werden, flutschen wohl immer ca. 12 Bytes, bevor eins verloren geht.
Gruß Dirk
... ich bleibe dran ...
Lesezeichen