@Rolf:
Hier 'mal meine Testprogramme, die (erwartungsgemäß) nicht funktionieren. Das "erwartungsgemäß" hat damit zu tun, dass ich nicht wirklich verstanden hatte (SORRY!), wie auf beiden Systemen Master- und Slavefunktionen zu initialisieren sind.
Ich bin sicher, du kannst da rasch weiter helfen.
Meine Idee war, in einer Richtung einen Aufwärtszähler laufen zu lassen, in die andere Richtung einen Abwärtszähler. Abwechselnd würden dann beide Systeme den Master machen. Auf dem LCD der M32 wäre die eine Zeile für die (eigene) Masterfunktion, die andere für die Gegenrichtung, für die Base würden die Textausgaben über UART mit "M-" für die eine und "S-" für die andere Richtung ausgegeben. Vielleicht habe ich mir das Ganze auch zu naiv so vorgestellt.
In der twi_target.h habe ich beide Systeme als TWISLAVE definiert (richtig?).
Als TWI-Adresse für die M32 nehme ich 12 dezimal (NICHT hex!). An der Base habe ich kein I2C-LCD.
Testprogramm 3 (RP6Control M32):
Code:/* * **************************************************************************** * RP6 ROBOT SYSTEM - RP6 CONTROL M32 EXAMPLES * **************************************************************************** * Example: I2C Master/Slave - I2C Send/Receive Test 3 * Author(s): Dirk * **************************************************************************** * Description: * * This Example sends/receives data via I2C. * * * ############################################################################ * 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 "twi_target.h" // Include the new I2C-Bus Library #include "RP6ControlLib.h" // The RP6 Control Library. // Always needs to be included! /*****************************************************************************/ // Defines: // The Slave Address of the RP6Base 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: uint8_t buffer[4]; uint16_t error_cnt = 0; uint8_t controlbyte; uint8_t highbyte = 0; uint8_t lowbyte = 0; /*****************************************************************************/ // Functions: void task_Slave(void) { static uint16_t cnt; if I2CTWI_needAction() { 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 I2CTWI_readableRegisters[0] = controlbyte; // copy controlbyte I2CTWI_readableRegisters[1] = lowbyte; // copy lowbyte I2CTWI_readableRegisters[2] = highbyte; // copy highbyte cnt = ((highbyte << 8) | lowbyte); // Restore 16-bit value #ifdef TEXT_OUTPUT setCursorPosLCD(1, 0); writeStringLCD_P("S-Rcved "); // What was received? writeIntegerLCD(cnt, DEC); writeStringLCD_P(" "); #endif if (!cnt) error_cnt = 0; // Reset error counter else error_cnt++; // Error counter + 1 if (cnt != error_cnt) { setCursorPosLCD(1, 0); writeStringLCD_P("S-ERROR!"); error_cnt = cnt; } else { setCursorPosLCD(1, 0); writeStringLCD_P(" "); } controlbyte = 0; I2CTWI_doneAction(); } } void task_Master(void) { static uint16_t cnt = 32768; if (!I2CTWI_isBusy()) { cnt--; // TEST: COUNTER!!! if (cnt > 32767) cnt = 32767; buffer[0] = 0; buffer[1] = CMD_SHOW_DATA; buffer[2] = cnt; buffer[3] = (cnt >> 8); I2CTWI_transmitBytes(RP6BASE_I2C_SLAVE_ADR, buffer, 4); buffer[0] = 0; // reset buffer buffer[1] = 0; buffer[2] = 0; buffer[3] = 0; I2CTWI_readRegisters(RP6BASE_I2C_SLAVE_ADR, 0, buffer, 4); if (cnt == ((buffer[2] << 8) | buffer[1])) { #ifdef TEXT_OUTPUT setCursorPosLCD(0, 0); writeStringLCD_P("M-Moved "); // What was sent? writeIntegerLCD(cnt, DEC); writeStringLCD_P(" "); #endif } else { setCursorPosLCD(0, 0); writeStringLCD_P("M-ERROR! "); // What was sent? } // if (cnt==3) while(true); // countertrap } } /*****************************************************************************/ // Main - The program starts here: int main(void) { initRP6Control(); initLCD(); // I2CTWI_init(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) { task_Slave(); task_Master(); if (RP6LIB_VERSION == 15) { // Only for task_RP6M32System(); // Lib V1.3beta!!! } else mSleep(5); // Other Libs: 5ms } return 0; } /****************************************************************************** * Additional info * **************************************************************************** * Changelog: * - v. 1.0 (initial release) 27.02.2011 by Dirk * * **************************************************************************** */ /*****************************************************************************/
Testprogramm 3 (RP6Base):
Vielleicht kannst du ja daraus was machen. Ich brauche auf jeden Fall noch ein paar Anregungen.Code:/* * **************************************************************************** * RP6 ROBOT SYSTEM - ROBOT BASE TESTS * **************************************************************************** * Example: I2C MaSTER/Slave - I2C Send/Receive Test 3 * Author(s): Dirk * **************************************************************************** * Description: * * This Example sends/receives data via I2C. * * * ############################################################################ * 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 "twi_target.h" // Include the new I2C-Bus Library //#include "PCF8583.h" //#include "RP6RobotBaseLib.h" // The RP6 Robot Base Library. // Always needs to be included! /*****************************************************************************/ // Defines: // The Slave Address of the Control M32 on the I2C Bus can be specified here: #define RP6CONTROL_I2C_SLAVE_ADR 12 #define CMD_SHOW_DATA 99 // Command to display the data #define TEXT_OUTPUT // Show text /*****************************************************************************/ // Variables: uint8_t buffer[4]; uint16_t error_cnt = 0; uint8_t controlbyte; uint8_t highbyte = 0; uint8_t lowbyte = 0; /*****************************************************************************/ // Functions: void task_Slave(void) { static uint16_t cnt; if I2CTWI_needAction() { 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 I2CTWI_readableRegisters[0] = controlbyte; // copy controlbyte I2CTWI_readableRegisters[1] = lowbyte; // copy lowbyte I2CTWI_readableRegisters[2] = highbyte; // copy highbyte cnt = ((highbyte << 8) | lowbyte); // Restore 16-bit value #ifdef TEXT_OUTPUT writeString_P("S-Received: "); // and show it writeInteger(cnt, DEC); writeChar('\n'); #endif if (!cnt) error_cnt = 0; // Reset error counter else error_cnt--; // Error counter - 1 if (cnt != error_cnt) { writeString_P("S-ERROR!\n"); error_cnt = cnt; } controlbyte = 0; I2CTWI_doneAction(); } } void task_Master(void) { static uint16_t cnt = 0; 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(RP6CONTROL_I2C_SLAVE_ADR, buffer, 4); buffer[0] = 0; // reset buffer buffer[1] = 0; buffer[2] = 0; buffer[3] = 0; I2CTWI_readRegisters(RP6CONTROL_I2C_SLAVE_ADR, 0, buffer, 4); if (cnt == ((buffer[2] << 8) | buffer[1])) { #ifdef TEXT_OUTPUT writeString_P("M-Moved: "); writeInteger(cnt, DEC); writeChar('\n'); #endif } else { writeString_P("M-ERROR!\n"); } // if (cnt==3) while(true); // countertrap } } /*****************************************************************************/ // Main function - The program starts here: int main(void) { extIntOFF(); initRobotBase(); // initLCD(); setLEDs(0b111111); mSleep(500); setLEDs(0b000000); // I2CTWI_init(RP6BASE_I2C_SLAVE_ADR); // clearI2CLCD(); // setCursorPosLCD(0,0); // writeStringLCD("I2C Init "); writeIntegerLCD(1,DEC); //--------------------------------------- /* writeString("START ----------------------------------\n"); uint8_t results[15]; I2CTWI_transmitByte(PCF8583_ADDR_WRITE, 0); I2CTWI_readBytes(PCF8583_ADDR_READ, results, 7); */ //--------------------------------------- //while (true); while (true) { task_Slave(); task_Master(); task_RP6System(); } return 0; } /****************************************************************************** * Additional info * **************************************************************************** * Changelog: * - v. 1.0 (initial release) 27.02.2011 by Dirk * * **************************************************************************** */ /*****************************************************************************/
Gruß Dirk
P.S.: Oh, ja, ich habe auch noch eine 6502 und eine 6510 liegen. Ich gehe mal in den Keller,- suchen ...







Zitieren

Lesezeichen