unknowsman 0 Posted April 24, 2019 Share Posted April 24, 2019 i try to use this example but nothing happened // Wire Slave Sender // by Nicholas Zambetti <http://www.zambetti.com> // Demonstrates use of the Wire library // Sends data as an I2C/TWI slave device // Refer to the "Wire Master Reader" example for use with this // Created 29 March 2006 // This example code is in the public domain. #include <Wire.h> void setup() { Wire.setModule(0); Wire.begin(2); // join i2c bus with address #2 Wire.onRequest(requestEvent); // register event } void loop() { delay(100); } // function that executes whenever data is requested by master // this function is registered as an event, see setup() void requestEvent() { Wire.write("hello "); // respond with message of 6 bytes // as expected by master } and this one // Wire Master Reader // by Nicholas Zambetti <http://www.zambetti.com> // Demonstrates use of the Wire library // Reads data from an I2C/TWI slave device // Refer to the "Wire Slave Sender" example for use with this // Created 29 March 2006 // This example code is in the public domain. #include <Wire.h> void setup() { Wire.setModule(0); Wire.begin(); // join i2c bus (address optional for master) Serial.begin(9600); // start serial for output } void loop() { Wire.requestFrom(2, 6); // request 6 bytes from slave device #2 while(Wire.available()) // slave may send less than requested { char c = Wire.read(); // receive a byte as character Serial.print(c); // print the character } delay(500); } with this pin thank you Quote Link to post Share on other sites
NurseBob 111 Posted April 24, 2019 Share Posted April 24, 2019 What i2c device are you connecting? What does is do? What's the device address? (per device specs) Is it really 2? Are you using pullups? I2c requires some type of pullup, and in general, the internal resistors on the '430 devices are not an appropriate value, so external are usually used. Note: by default, i2c comms are on LP pins 9 (SCL) and 10 (SDA) not 15 &&16 - P1.6 and P1.7, Energia defaults to spi on those pins. energia and unknowsman 1 1 Quote Link to post Share on other sites
energia 485 Posted April 24, 2019 Share Posted April 24, 2019 There is a bug for slave mode which has already been fixed in the git repo. You can work-around it with the following. If you have the latest Energia release installed (1.8.7E21), then edit the file: Linux: <Energia directory>hardware/energia/msp430/libraries/Wire/utility/twi.c Windows: <Energia directory>hardware\energia\msp430\libraries\Wire\utility\twi.c macOS: <Energia.app directory>/Contents/Java/hardware/energia/msp430/libraries/Wire/utility/twi.c Got to line 1183 and replace: 1183 } else if (twi_state == TWI_MRX) { // Master receive mode 1184 // copy data to output register 1185 UCBzTXBUF = twi_txBuffer[twi_txBufferIndex++]; by this: 1183 } else if (twi_state == TWI_STX) { // Slave transmit mode 1184 // copy data to output register 1185 UCBzTXBUF = twi_txBuffer[twi_txBufferIndex++]; unknowsman 1 Quote Link to post Share on other sites
energia 485 Posted April 24, 2019 Share Posted April 24, 2019 p.s. you should use the first Sketch you posted. Also make sure that the master is setup to talk to the right address you specify in the Wire.begin(addr) call. unknowsman 1 Quote Link to post Share on other sites
unknowsman 0 Posted April 25, 2019 Author Share Posted April 25, 2019 thank you so much Quote Link to post Share on other sites
unknowsman 0 Posted May 16, 2019 Author Share Posted May 16, 2019 @energia When I send data over i2c but slave(MSP430FR5969) cannot read all the data with this codeIts look like just only 12 characters can send . Pls give some advice. Master Code ( Arduino Mega2560): #include <Wire.h> uint8_t greeting[35] = "Hello World! and How are you today!"; void setup() { Serial.begin(250000); Wire.begin(); } void loop() { if (Serial.available() > 0) { byte inByte = Serial.read(); if (inByte == '1') { Wire.beginTransmission(0x23); Wire.write(greeting, 35); Wire.endTransmission(); } } } Slave Code(MSP430FR5969) : #include <Wire.h> void setup() { Wire.setModule(0); Wire.begin(0x23); // join i2c bus with address #0x23 Wire.onReceive(receiveEvent); // register event Serial.begin(115200); // start serial for output } void loop() { delay(100); } // function that executes whenever data is received from master // this function is registered as an event, see setup() void receiveEvent(int bytes) { if (Wire.available() != 0) { for (int i = 0; i < bytes; i++) { Serial.write(Wire.read()); } Serial.println(); } } thank you very much Quote Link to post Share on other sites
energia 485 Posted May 17, 2019 Share Posted May 17, 2019 The MSP430 Wire buffer length is set to 16. If you need a larger buffer, you can edit the file: Linux: <Energia directory>hardware/energia/msp430/libraries/Wire/Wire.h Windows: <Energia directory>hardware\energia\msp430\libraries\Wire\Wire.h macOS: <Energia.app directory>/Contents/Java/hardware/energia/msp430/libraries/Wire/Wire.h and in: Linux: <Energia directory>hardware/energia/msp430/libraries/Wire/utility/twi.h Windows: <Energia directory>hardware\energia\msp430\libraries\Wire\utility\twi.h macOS: <Energia.app directory>/Contents/Java/hardware/energia/msp430/libraries/Wire/utility/twi.h And set BUFFER_LENGTH / TWI_BUFFER_LENGTH to a larger number. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.