Pages: [1]   Go Down
Author Topic: Using the DS1803 digital pot.  (Read 2958 times)
0 Members and 1 Guest are viewing this topic.
Montréal, Qc
Offline Offline
Full Member
***
Karma: 1
Posts: 185
Practice safe hex!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello, I'm trying to interface a DS1803 I2C digital pot without much luck :/
It looks like I'm just writing/reading back the buffer when I communicate with it.
I don't understand why, the address is properly set. And the I2C bus works, I have another device (PCF8574) on it.
If you guys have any idea....

These are the read/write function I'm using. As usual the read address is write+1.
As far as I can see from the datasheet the registers are none looping.
When I write a value and read back, the first is the register address instead of the first byte of data.

Code:
Void DS1803::setValue(uint8_t value)
{
      Wire.beginTransmission(DPOT_ADDRESS_W);
      Wire.send(DPOT_WIPERS); // Send the wiper address
      
      Wire.send(value);
      
      Wire.endTransmission();
}

uint8_t DS1803::getValue()
{
      Wire.requestFrom(DPOT_ADDRESS_R, 2);
      
      while (Wire.available()) {
            Serial.println((uint8_t)Wire.receive(), DEC);
      }
      
      return 0;
}
Logged

"Pilots believe in a clean living... they never drink wisky from a dirty glass."

Montréal, Qc
Offline Offline
Full Member
***
Karma: 1
Posts: 185
Practice safe hex!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Got it! Ok so apparently the datasheet gave the address as a 7bit address and I was reading it as an 8bit address. I should have been using 0x28 instead of 0x50... but the fun part is how I figured it out. As someone on IRC suggested I tapped into the twi library and wrote an I2C scanner. Here's the code:

Code:
void scanTWI(void)
{
      Wire.begin();
      
      int ii;
      uint8_t *txBuffer;
      
      for (ii=1;ii<128;ii++)
      {
            twi_writeTo(ii, txBuffer, 0, 1);
            
            if (twi_isFound())
            {
                  Serial.println(ii, HEX);
            }
      }
      Serial.println();
}

I also had to apply the following modification to the twi.c file:
Code:
312a313,324
> int foundDevice = 0;
> int twi_isFound(void)
> {
>       if (foundDevice)
>       {
>               foundDevice = 0;
>               return 1;
>       }
>
>       return 0;
> }
>
325a338,340
>       {
>               foundDevice = 1;
>       }

It's probably terrible code, but as a patch, it works. The question is why isn't something like this in the library? It would be very useful! IMHO at least.

Hope this helps others
Logged

"Pilots believe in a clean living... they never drink wisky from a dirty glass."

Montréal, Qc
Offline Offline
Full Member
***
Karma: 1
Posts: 185
Practice safe hex!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can also do the following:

Add this to the public functions in Wire.h
Code:
bool checkAddress(uint8_t);

And this in Wire.cpp
Code:
bool TwoWire::checkAddress(uint8_t twiAddress)
{
      uint8_t *txBuffer;
      twi_writeTo(twiAddress, txBuffer, 0, 1);
      twi_isFound();
}

You can now easily check an address by doing Wire.checkAddress(address). w00t  smiley-grin
« Last Edit: October 12, 2007, 08:20:41 pm by xSmurf » Logged

"Pilots believe in a clean living... they never drink wisky from a dirty glass."

Montréal, Qc
Offline Offline
Full Member
***
Karma: 1
Posts: 185
Practice safe hex!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I also added this to the Wire.h so I can detect the mod:

Code:
#define WIRE_LIB_SCAN_MOD
Logged

"Pilots believe in a clean living... they never drink wisky from a dirty glass."

Pages: [1]   Go Up
Jump to: