Author Topic: Weird behavior on Arduino envioroment  (Read 2041 times)

0 Members and 1 Guest are viewing this topic.

Offline dan3460Topic starter

  • Frequent Contributor
  • **
  • Posts: 326
  • Country: us
Weird behavior on Arduino envioroment
« on: May 27, 2017, 01:55:52 pm »
I working on a project that uses a DAC and I need to change the address for the I2C to the chip, the datasheet is very simple, you write to the original address, send a command, set one pin on the chip low, write a new address, confirm the address and put the pin high. I normally work on Atmel Studio, but for this very simple process I decided to use Arduino, so I made the following program:
Code: [Select]
#include <Wire.h>


void setup() {
  // put your setup code here, to run once:
  Wire.begin();
  Serial.begin(9600);
  pinMode(12,OUTPUT);
  digitalWrite(12,HIGH);
  Serial.println("Setting up Address");

  Wire.beginTransmission(0x60);
  Wire.write(0x61);
 
  digitalWrite(12,LOW);
  Wire.write(0x66);

  Wire.write(0x67);
  Wire.endTransmission();
  digitalWrite(12,HIGH);
}

void loop() {
  // put your main code here, to run repeatedly:

}
It cannot be simpler, but didn't work, the chip still have the old address. I stared at the program for quite a while could not see any problem. I pulled my digital analyzer and saw what is in picture i2c-1. The pin is pulled down before the start of the I2C communication and the chip doesn't AKC the request. This is weird as I clearly have the pulled down after the command 0x61.
I did the following test, I comment out the digital low for the pin and I got was is in picture i2c-2. Now the chip ACK the command to change address, but NAK the change request because the pin is not low.
It appears to me that the pre-compiler is arranging some commands, is this possible? or am I missing something on my code? Ideas?
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12860
Re: Weird behavior on Arduino envioroment
« Reply #1 on: May 27, 2017, 02:33:59 pm »
You are missing something.  The Arduino Wire library queues the bytes to be sent and defers sending them till  Wire.endTransmission();

See https://www.arduino.cc/en/Reference/WireEndTransmission

Worse: there isn't a flush command and WireEndTransmission *ALWAYS* either sends a stop or a restart.

Wire.cpp contains
Code: [Select]
void TwoWire::flush(void)
{
  // XXX: to be implemented.
}
:wtf: |O :wtf:
 

Offline dan3460Topic starter

  • Frequent Contributor
  • **
  • Posts: 326
  • Country: us
Re: Weird behavior on Arduino envioroment
« Reply #2 on: May 27, 2017, 02:38:33 pm »
So, if I understand correctly, there is no way to do what I need to do using Arduino. Is that correct?
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12860
Re: Weird behavior on Arduino envioroment
« Reply #3 on: May 27, 2017, 03:11:13 pm »
Don't know - will your chip tolerate a restart in the address set sequence?
If not you could try the Software I2C library: https://playground.arduino.cc/Main/SoftwareI2CLibrary
 

Offline dan3460Topic starter

  • Frequent Contributor
  • **
  • Posts: 326
  • Country: us
Re: Weird behavior on Arduino envioroment
« Reply #4 on: May 27, 2017, 06:16:06 pm »
It does not for changing the address. I just dumped Arduino and got back using Atmel Studio.  Thanks for the help.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf