Jump to content
43oh

G2553 and i2c slave


Recommended Posts

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 ?
 

Link to post
Share on other sites

@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.

Link to post
Share on other sites

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().

Link to post
Share on other sites
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).

Link to post
Share on other sites

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?

Link to post
Share on other sites

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

Link to post
Share on other sites

@@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!

Link to post
Share on other sites

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.
Link to post
Share on other sites

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);
}
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...