Hallo shedepe,
sorry, hatte einfach noch keine Zeit.
Hier jetzt eine funktionierende Variante:
RP6Base (Master):
RP6Control (Slave):Code:/* * **************************************************************************** * RP6 ROBOT SYSTEM - ROBOT BASE EXAMPLES * **************************************************************************** * Example: I2C Master - I2C Send Test * Author(s): Dirk * **************************************************************************** * Description: * * This Example sends a 16-bit value 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 "RP6RobotBaseLib.h" // The RP6 Robot Base Library. // Always needs to be included! #include "RP6I2CmasterTWI.h" // Include the I2C-Bus Master Library /*****************************************************************************/ // Defines: // The Slave Address on the I2C Bus can be specified here: #define RP6CONTROL_I2C_SLAVE_ADR 10 #define CMD_SHOW_DATA 99 // Command to display the data /*****************************************************************************/ // 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 adc0; uint8_t buffer[4]; /*****************************************************************************/ // Functions: /*****************************************************************************/ // Main - The program starts here: int main(void) { initRobotBase(); I2CTWI_initMaster(100); // Initialize the TWI Module for Master operation // with 100kHz SCL Frequency // Register the event handlers: I2CTWI_setTransmissionErrorHandler(I2C_transmissionError); setLEDs(0b111111); mSleep(500); setLEDs(0b000000); powerON(); writeString_P("Hallo"); // UART Test writeChar('\n'); startStopwatch1(); while(true) { if (getStopwatch1() > 250) { // Jede 1/4 Sekunde: // adc0 = readADC(ADC_ADC0); // ADC auslesen adc0 += 10; // TEST: ZÄHLER!!! writeString_P("Zu sendender Wert: "); writeInteger(adc0, DEC); // Wert via UART senden writeChar('\n'); buffer[0] = 0; buffer[1] = CMD_SHOW_DATA; buffer[2] = adc0; buffer[3] = (adc0 >> 8); if (!I2CTWI_isBusy()) { I2CTWI_transmitBytes(RP6CONTROL_I2C_SLAVE_ADR, buffer, 4); writeString_P("I2C hat gesendet: "); // Kontrolle, dass etwas gesendet wurde writeInteger(adc0, DEC); writeChar('\n'); writeChar('\n'); } setStopwatch1(0); } } return 0; } /****************************************************************************** * Additional info * **************************************************************************** * Changelog: * - v. 1.0 (initial release) 13.01.2010 by Dirk * * **************************************************************************** */ /*****************************************************************************/
Gruß DirkCode:/* * **************************************************************************** * RP6 ROBOT SYSTEM - RP6 CONTROL M32 EXAMPLES * **************************************************************************** * Example: I2C Slave - I2C Receive Test * Author(s): Dirk * **************************************************************************** * Description: * * This Example receives a 16-bit value 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 "RP6ControlLib.h" // The RP6 Control Library. // Always needs to be included! #include "RP6I2CslaveTWI.h" // Include the I2C-Bus Slave Library /*****************************************************************************/ // Defines: // The Slave Address on the I2C Bus can be specified here: #define RP6CONTROL_I2C_SLAVE_ADR 10 #define CMD_SHOW_DATA 99 // Command to display the data /*****************************************************************************/ // Variables: uint16_t adcwert; uint8_t controlbyte; uint8_t highbyte = 0; uint8_t lowbyte = 0; /*****************************************************************************/ // Functions: /*****************************************************************************/ // Main function - The program starts here: int main(void) { initRP6Control(); // Always call this first! The Processor will not // work correctly otherwise. initLCD(); // Initialize the LC-Display (LCD) // Always call this before using the LCD! clearLCD(); // Clear the whole LCD Screen setLEDs(0b111111); mSleep(500); setLEDs(0b000000); I2CTWI_initSlave(RP6CONTROL_I2C_SLAVE_ADR); // Slave mit der Adresse 10 initialisieren showScreenLCD("I2C-Empfang", "ADC:"); // LC-Display vorbereiten while(true) { if (I2CTWI_writeRegisters[0] && !I2CTWI_writeBusy) { controlbyte = I2CTWI_writeRegisters[0]; // Kontrollbyte lesen I2CTWI_writeRegisters[0] = 0; // und zurücksetzen (!!!) lowbyte = I2CTWI_writeRegisters[1]; // lowbyte auslesen highbyte = I2CTWI_writeRegisters[2]; // highbyte auslesen if (controlbyte == CMD_SHOW_DATA) { adcwert = ((highbyte << 8) | lowbyte); // wieder zusammensetzen setCursorPosLCD(1, 5); writeIntegerLCD(adcwert, DEC); controlbyte = 0; } } } return 0; } /****************************************************************************** * Additional info * **************************************************************************** * Changelog: * - v. 1.0 (initial release) 13.01.2010 by Dirk * * **************************************************************************** */ /*****************************************************************************/







Zitieren
Lesezeichen