Go Down

Topic: TinyWireS.h on the attiny85 stange behaviour (Read 1 time) previous topic - next topic

crchisholm

May 01, 2012, 01:31 am Last Edit: May 01, 2012, 01:46 am by crchisholm Reason: 1
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: [Select]
#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);
   
 }
Charlie R Chisholm

Nick Gammon

To save me hunting it down, where did you get this TinyWire library from?
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

crchisholm

Hi Nick

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

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

thanks
Charlie R Chisholm

Nick Gammon

Do you have pull-up resistors (say, 4.7K) on both SDA and SCL?
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

crchisholm

#4
May 01, 2012, 02:21 am Last Edit: May 01, 2012, 02:49 am by crchisholm Reason: 1
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)  



Charlie R Chisholm

Nick Gammon

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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

crchisholm

#6
May 01, 2012, 02:56 am Last Edit: May 01, 2012, 03:03 am by crchisholm Reason: 1
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.

Charlie R Chisholm

Nick Gammon

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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

crchisholm

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?
Charlie R Chisholm

Nick Gammon

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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

crchisholm

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.
Charlie R Chisholm

crchisholm

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
Charlie R Chisholm

Go Up