1
\$\begingroup\$

I am desperately trying to get to work an arduino program to control the 10-bit digital potentiometer MAX5483 with SPI, but something is not working. I get some resistance values, but the protocol is wrongly implemented, I fear.. Why is my arduino code below not working?

Here is the arduino code:

#include <SPI.h> 

const int csPin = 3;
const int selPin = 2;


void setup() {

 SPI.begin();
 SPI.setBitOrder(MSBFIRST); 

 pinMode(csPin,OUTPUT); 
 digitalWrite(csPin, LOW);

 pinMode(selPin,OUTPUT);
 digitalWrite(selPin, HIGH); 


}


void loop()
{     

    digitalPotWrite(250); 


}

// When working 
// 512 = 5070 Ohm
// 0 = 70 Ohm
// 1 = 80
// 1023 = 10070


void digitalPotWrite(int value) {
digitalWrite(csPin, LOW);
byte command=0x0;
SPI.transfer(command);
byte byte0 = (value >> int(value/255));
byte byte1 = ((value & B11) << 6);
SPI.transfer(byte0);
SPI.transfer(byte1);
digitalWrite(csPin, HIGH);
}

Here is the data sheet. Here is a code for a PIC, which should work:

#include <main.h>

int16 count = 0;
int16 tmp;

void write_pot(int16 data)
{
   output_high(PIN_C2);     //chip select
   output_high(PIN_C3);      //clock
   output_low(PIN_C4);      //Data
   delay_us(1);

   output_low(PIN_C2);     //chip select
   delay_us(1);

   for(tmp=0;tmp<24;tmp++)
   {
      if(tmp<8)  
         output_low(PIN_C4);   //Data
      else if (tmp<18)
      {
         if(data&(0x0001<<(9-(tmp-8))))
            output_high(PIN_C4);   //Data
         else
            output_low(PIN_C4);   //Data
      }

      output_low(PIN_C3);      //clock
      delay_us(1);
      output_high(PIN_C3);      //clock

   }
   delay_us(1);
   output_high(PIN_C2);

   output_toggle(PIN_C1);

}

void main()
{
  // spispi_write( )
   while(TRUE)
   {
      if(count<200)  
         count++;
      else
         count = 50;


   //   printf("\n\r=%ld",count);

      write_pot(count);
      if(count==50)
         delay_ms(100);
      else
         delay_us(10);

   }

}
\$\endgroup\$
  • \$\begingroup\$ What is B11 ? II don't see a reference to it. Unless B11 is binary 3 ? Is that valid arduino/wiring syntax ? \$\endgroup\$ – efox29 Jul 2 '13 at 21:43
1
\$\begingroup\$

Try this. All I need was just rewrite the byte0 and byte1, added a delay after CS goes low and before CS goes high. Also I changed the order of the SPI.transfer() so that its COMMAND, BYTE 1, and then BYTE 0.

void digitalPotWrite(int value) {
digitalWrite(csPin, LOW);
//some delay here delay(1) ?
byte command=0x0;
byte byte0 = (value & 0x03) << 6;
byte byte1 = (value & 0x3FC) >> 2;
SPI.transfer(command);
SPI.transfer(byte1);
SPI.transfer(byte0);
//some delay here delay(1) ?
digitalWrite(csPin, HIGH);
}

If this doesnt work, then add this to your setup.

SPI.setDataMode(SPI_MODE2);

Looking at the page 15, it seems that the clock base rests at '1'. And the data is captured on the clocks rising edge. I'm not sure what the default setting is for SPI.begin() but it could be that its set for SPI_MODE1 (just a guess).

Have a look at SPI for the different modes and what they mean.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.