Pages: [1]   Go Down
Author Topic: TinyWireS.h on the attiny85 stange behaviour  (Read 1517 times)
0 Members and 1 Guest are viewing this topic.
St Petersburg, FL
Offline Offline
Jr. Member
**
Karma: 0
Posts: 70
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The code below works perfectly AS LONG AS I have the "TinyWireS.begin(I2C_SLAVE_ADDR)" commented out.   If I uncomment that line, it seems that only the LED attached to pin 1 works.  LEDs on pin 0 and 2 do not come on.  Comment the TinyWireS.begin... line again and all is well.

What might I be doing wrong to cause this behaviour?


The I2C_SLAVE_ADDR value was stollen from some example code, so I don't know if that is an issue or not.  
Any direction or questions would probably be helpful.

Once I get this figured out, I hope to send a value 0 - 5 from the Arduino Pro Mini using I2C.  I have not coded the master yet because I wanted to make sure I could get this to work even if there was nothing to read yet.  Is that a bad plan?

Thanks

Charlie

Code:
#include "TinyWireS.h"

 #define I2C_SLAVE_ADDR  0x26            // i2c slave address (38)

int leftPin=2;
int backupPin=0;
int rightPin=1;
int lightMode=0;
int flash=0;
byte byteRcvd = 0;
int cnt=0;

void setup() {
  pinMode(backupPin,OUTPUT);
  pinMode(leftPin,OUTPUT);
  pinMode(rightPin,OUTPUT);
//  TinyWireS.begin(I2C_SLAVE_ADDR);      // init I2C Slave lightMode
  }
  
void loop() {

  flash ^= 1;

  // these lines will be used to retrieve a byte (0 - 5) once I can get this to work
  
    //if (TinyWireS.available()) {byteRcvd=TinyWireS.receive();}
    //else {byteRcvd=2;};
  
  /*

   lightModes
     0=lignts off
     1=brakes
     2=backup
     3=left
     4=right
     5=hazard

  */

  switch(lightMode) {
    case 0:
      lights_off();
      break;
    case 1:
      digitalWrite(backupPin,LOW);
      digitalWrite(rightPin,HIGH);
      digitalWrite(leftPin,HIGH);
      break;
    case 2:
      digitalWrite(backupPin,HIGH);
      digitalWrite(rightPin,LOW);
      digitalWrite(leftPin,LOW);
      break;
    case 3:
      digitalWrite(backupPin,LOW);
      digitalWrite(rightPin,LOW);
      digitalWrite(leftPin,flash);
      break;
    case 4:
      digitalWrite(backupPin,LOW);
      digitalWrite(leftPin,LOW);
      digitalWrite(rightPin,flash);
      break;
    case 5:
      digitalWrite(leftPin,flash);
      digitalWrite(backupPin,LOW);
      digitalWrite(rightPin,flash);
      break;
    default:
      digitalWrite(leftPin,flash);
      digitalWrite(backupPin,flash);
      digitalWrite(rightPin,flash);
      break;      
    }
  
  delay(200);

  // these lines are for testing only.  This will cycle through all five modes
  // for a few seconds each
  
  if (cnt > 25) {
    if (lightMode < 5)
      {lightMode += 1;}
    else
      {lightMode=0;}
    cnt=0;
    }
  cnt += 1;

  }
  
 void lights_off()
  {
     digitalWrite(rightPin,LOW);
     digitalWrite(backupPin,LOW);
     digitalWrite(leftPin,LOW);
    
  }
« Last Edit: April 30, 2012, 06:46:22 pm by crchisholm » Logged

Charlie R Chisholm

Global Moderator
Online Online
Brattain Member
*****
Karma: 485
Posts: 18810
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

To save me hunting it down, where did you get this TinyWire library from?
Logged


St Petersburg, FL
Offline Offline
Jr. Member
**
Karma: 0
Posts: 70
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Nick

I got it from the arduino playground at ....

http://arduino.cc/playground/Code/USIi2c

thanks
Logged

Charlie R Chisholm

Global Moderator
Online Online
Brattain Member
*****
Karma: 485
Posts: 18810
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Do you have pull-up resistors (say, 4.7K) on both SDA and SCL?
Logged


St Petersburg, FL
Offline Offline
Jr. Member
**
Karma: 0
Posts: 70
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No.    might be an opps.  

What I am calling these pins may be miss-leading also .... PB2,  (a85 pin 7), PB1 (a85 pin 6) and PB0 (a85 pin 5)  all have a 220 ohm resister  ->  LED ->  ground.  At this point, I have n/c on a85 pins 1,2 or 3 (PB5, PB3, PB4 respectively)  



« Last Edit: April 30, 2012, 07:49:10 pm by crchisholm » Logged

Charlie R Chisholm

Global Moderator
Online Online
Brattain Member
*****
Karma: 485
Posts: 18810
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Well as I read the datasheet SCL is pin 7 and SDA is pin 5. They should have pull-ups. I'm not sure the LED will help.
Logged


St Petersburg, FL
Offline Offline
Jr. Member
**
Karma: 0
Posts: 70
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The whole point is to light the LEDs based on the value of lightMode.   Should I not be using those particular pins?  I may be more confused than I thought I was.

« Last Edit: April 30, 2012, 08:03:46 pm by crchisholm » Logged

Charlie R Chisholm

Global Moderator
Online Online
Brattain Member
*****
Karma: 485
Posts: 18810
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I am a bit confused now about your expectations. I gather this chip is lighting some LEDs in response to messages via I2C from another chip? Is that the plan?

Well when you initialize I2C it seizes control of the output pins, so I would not be expecting you to light LEDs, and communicate via I2C, on the same pins.
Logged


St Petersburg, FL
Offline Offline
Jr. Member
**
Karma: 0
Posts: 70
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your description of "the plan" is correct and what you say about I2C using the pins makes sence.     I guess I need to figure out what pins the I2C needs to communicate.    Do you think I could use the PB5 (reset) or would I have to make changes to the fuses to do that?  How about the CLK1, or CLK0. 

Do you know what pins I2C requires.  Also, my limited understanding is that the attiny does not directly support I2C so the TinyWireS library creates a wrapper around USI in order to communicate.  Does that mean that I am more concerned with what pins USI uses than I2C?
Logged

Charlie R Chisholm

Global Moderator
Online Online
Brattain Member
*****
Karma: 485
Posts: 18810
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Do you know what pins I2C requires.

I presume, the two I mentioned, plus Gnd.

Quote
SCL is pin 7 and SDA is pin 5.

Quote
Do you think I could use the PB5 (reset) or would I have to make changes to the fuses to do that?

Yes you would.

Quote
How about the CLK1, or CLK0.

Why not? I think they are just for an external clock, if you happen to set the fuses to use that. Otherwise you can use them as digital pins.
Logged


St Petersburg, FL
Offline Offline
Jr. Member
**
Karma: 0
Posts: 70
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Therein lies my salvation......maybe.   I will swap  SDA and SCL for CLK0 and CLK1.   I will also use a 4k7 pull-up on SCL and SDA.   Does that sound right?

I will also post the results in case  I am not the only knucklehead that runs into this issue. 

Thanks.
Logged

Charlie R Chisholm

St Petersburg, FL
Offline Offline
Jr. Member
**
Karma: 0
Posts: 70
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yep, that did it!

Thank you very much, Nick.   I learned a good bit.   

Now on to coding the master.  Hopefully with more understanding.

Charlie
Logged

Charlie R Chisholm

Pages: [1]   Go Up
Jump to: