B@tto 51 Posted May 23, 2016 Share Posted May 23, 2016 Hi, I'm trying to make a simple communication betwen two MSP430G2253, but it doesn't work. Master : #include <Wire.h> void setup() { Wire.setModule(0); Wire.begin(); } void loop() { static byte u=1; for(int i=0;i<3;i++) { Wire.beginTransmission(8); // transmit to device #8 Wire.write(i); Wire.write(u); // sends one byte Wire.endTransmission(); // stop transmitting delay(1000); } u=!u; } Slave : #include <Wire.h> #define LED_1 P1_2 #define LED_2 P1_3 #define LED_3 P1_4 void setup() { digitalWrite(LED_1,LOW); digitalWrite(LED_2,LOW); digitalWrite(LED_3,LOW); pinMode(LED_1,OUTPUT); pinMode(LED_2,OUTPUT); pinMode(LED_3,OUTPUT); Wire.setModule(0); Wire.begin(8); Wire.onReceive(receiveEvent); } void loop() { } void receiveEvent(int c) { digitalWrite(LED_1,HIGH); delay(500); digitalWrite(LED_1,LOW); byte led = Wire.read(); byte state = Wire.read(); switch(led) { case 0: digitalWrite(LED_1,state); break; case 1: digitalWrite(LED_2,state); break; case 2: digitalWrite(LED_3,state); break; default: break; } } But the slave doesn't seems to receive something. I looked with a logic analyzer, the slave well send ACK, and pinout seems ok as I tried to put it in master mode, and it well send commands. Another thing : master stay stuck after sending a certain quantity of bytes, I didn't count exactly but it's around 15-20, so I imagine because buffer is full. Logic analyzer shows slave doesn't release SCK. I tried to poll Wire.available() in loop but it does nothing more. Any idea ? Quote Link to post Share on other sites
spirilis 1,265 Posted May 23, 2016 Share Posted May 23, 2016 I advise getting delay() out of your ISR. Not 100% sure but it might be screwing up the protocol flow. Quote Link to post Share on other sites
dubnet 238 Posted May 23, 2016 Share Posted May 23, 2016 @spirilis Easy to do. Had a couple of troubleshooting lines in a serial transmit loop. Scratching my head until I hooked up the Saleae, saw the output and figured out what was happening. Was a 432 and it still messed up the timing. Quote Link to post Share on other sites
B@tto 51 Posted May 23, 2016 Author Share Posted May 23, 2016 @@spirilis : it does not change anything :s Quote Link to post Share on other sites
abecedarian 330 Posted May 23, 2016 Share Posted May 23, 2016 @@spirilis : it does not change anything :sPost the current code showing that? Quote Link to post Share on other sites
abecedarian 330 Posted May 23, 2016 Share Posted May 23, 2016 void loop() { static byte u=1; for(int i=0;i<3;i++) { Wire.beginTransmission(8); // transmit to device #8 Wire.write(i); Wire.write(u); // sends one byte Wire.endTransmission(); // stop transmitting delay(1000); } u=!u; }That won't work.... Okay, maybe it will, but: You define and assign "u" a value of 1 at the top of "loop()", then negate its value at the bottom of the loop()... but it gets re-defined and assigned "1" at the start of the loop()... which repeats over and over. I.e. "u" will always have the value of "1" within loop(). Quote Link to post Share on other sites
spirilis 1,265 Posted May 23, 2016 Share Posted May 23, 2016 void loop() { static byte u=1; for(int i=0;i<3;i++) { Wire.beginTransmission(8); // transmit to device #8 Wire.write(i); Wire.write(u); // sends one byte Wire.endTransmission(); // stop transmitting delay(1000); } u=!u; }That won't work.... Okay, maybe it will, but: You define and assign "u" a value of 1 at the top of "loop()", then negate its value at the bottom of the loop()... but it gets re-defined and assigned "1" at the start of the loop()... which repeats over and over. I.e. "u" will always have the value of "1" within loop(). That will work, actually. The "static" keyword when used inside a function tells the compiler to persist its value across subsequent executions of the function. The variable then actually gets allocated within the bss/global space, rather than the function's stack (where it would get re-initialized every execution of loop()). In this usage, the assignment is done only the first time the function runs (and/or might actually be done during the C init runtime phase, before main() runs, i.e. before the function ever does run). abecedarian and B@tto 2 Quote Link to post Share on other sites
abecedarian 330 Posted May 23, 2016 Share Posted May 23, 2016 That will work, actually. The "static" keyword when used inside a function tells the compiler to persist its value across subsequent executions of the function. The variable then actually gets allocated within the bss/global space, rather than the function's stack (where it would get re-initialized every execution of loop()). In this usage, the assignment is done only the first time the function runs (and/or might actually be done during the C init runtime part, i.e. before the function ever does run).But then... what about "u=!u;" at the end of loop()? That's what's confusing me. Does "u" get "not" u or is it "1" each time loop() executes? Quote Link to post Share on other sites
spirilis 1,265 Posted May 23, 2016 Share Posted May 23, 2016 But then... what about "u=!u;" at the end of loop()? That's what's confusing me. Does "u" get "not" u or is it "1" each time loop() executes? u gets not'ted (1 to 0, 0 to 1) every time it executes. First time around, u = 1. Then u gets notted. Second time around, u = 0. Then u gets notted. Third time around, u = 1. etc and so forth Quote Link to post Share on other sites
B@tto 51 Posted May 23, 2016 Author Share Posted May 23, 2016 u is simply inverted each loop(). If u=0 => becomes 1 if u=1 => becomes 0 Quote Link to post Share on other sites
abecedarian 330 Posted May 23, 2016 Share Posted May 23, 2016 @@B@tto @@spirilis But at the top of loop(): "static byte u=1;" So "u" is assigned the value of "1" at the start of each iteration, no? Quote Link to post Share on other sites
spirilis 1,265 Posted May 23, 2016 Share Posted May 23, 2016 @@B@tto @@spirilis But at the top of loop(): "static byte u=1;" So "u" is assigned the value of "1" at the start of each iteration, no? No. Because the "static" keyword is there, this only happens once, at the very beginning of program start. This is what makes the "static" keyword special in the context of a function (in addition to the fact that it informs the compiler to allocate the variable outside the function's ephemeral stack space). Note that in the global context, i.e. outside of a function, "static" means something totally different. Yay C! tripwire and abecedarian 2 Quote Link to post Share on other sites
abecedarian 330 Posted May 23, 2016 Share Posted May 23, 2016 Not what I would've expected but I see now it works. void setup() { // put your setup code here, to run once: pinMode(RED_LED, OUTPUT); } void loop() { // put your main code here, to run repeatedly: static int u = 1; digitalWrite(RED_LED, u); delay(500); u = !u; }Does, in fact, blink the LED. spirilis 1 Quote Link to post Share on other sites
spirilis 1,265 Posted May 23, 2016 Share Posted May 23, 2016 Not what I would've expected but I see now it works. void setup() { // put your setup code here, to run once: pinMode(RED_LED, OUTPUT); } void loop() { // put your main code here, to run repeatedly: static int u = 1; digitalWrite(RED_LED, u); delay(500); u = !u; }Does, in fact, blink the LED. This is all totally off topic from the OP but who cares... A common idiom I find myself doing to test serial access or whatever is: void loop() { static unsigned long i = 0; Serial.print("Number: "); Serial.println(i++); delay(250); } abecedarian 1 Quote Link to post Share on other sites
abecedarian 330 Posted May 23, 2016 Share Posted May 23, 2016 @spirilis- yes, this is off topic but does clarify something. I hope @@B@tto will forgive me for dragging us this way. 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.