X9241A - Digital Potentiometer

From Combustory
Revision as of 00:03, 26 September 2008 by Combustor (Talk | contribs)
Jump to: navigation, search

Welcome to Combustory


Any questions or comments:

  • Send them to - combustor@combustory.com

Contents

Summary

This code shows how to communicate with the X9241A Digital Potentiometer. I plan on updating this info eventually to explain this chip, but for now you will probably want to get the data sheet to help with understanding what was done here. The bottom line is that I have created this program to accept commands via serial commands to set the storage registers to certain values and to modify the pot registers to change based on serial commands. I have not figured out how to read the registers, but that is not so important to my application.

Method

I2C

There are basically only two functions that I am after right now which are both three byte instructions:

  • Read WCR(Wiper Control Register) for the four potentiometers 0-3 (So far this is failing miserably)
    • Send 1st byte - Address (my circuit - 01011010 or 0x2D)(binary - 0101 A3 A2 A1 A0) A values are actual pins set to high or low for an address.
    • Send 2nd byte - Instruction (binary - 1001 (1/0)(1/0) 00) - The two bits are used to choose the potentiometer
    • Read WCR requested Byte
  • Write WCR for the four potentiometers 0-3 (This works)
    • Send 1st byte - Address (my circuit - 01011010 or 0x2D)(binary - 0101 A3 A2 A1 A0) A values are actual pins set to high or low for an address.
    • Send 2nd byte - Instruction (binary - 1010 (1/0)(1/0) 00) - The two bits are used to choose the potentiometer
    • Send WCR Byte - Potentiometer value (binary - 00 D5 D4 D3 D2 D1 D0) - D values are the 0-63 potentiometer positions


x9241A_Digital_Potentiometer_v.01 code

/*
 * x9241A Digital Potentiometer v.01
 * by <http://www.combustory.com> John Vaughters
 *
 * This Program illustrates the use of I2C to set up the x9241A Digital Potentiometer by setting and transferring the registers.
 * I attempted to read the register data through I2C, but was unable to get it to work.
 * Since the reading part was not so important to me I stopped trying.  
 * To set the registers up and transfer them to the Wiper registers met my goals and that is where I stopped.
 *
 * The x9142A has four Digital Pots and each pot has four static registers and one dynamic register the Wiper Control Register (WCR). 
 * Therefore, there are 16 total bytes of storage and four WCR's.
 * This Program has the following commands:
 * S - Set the 16 registers.  Each pot has four registers and this command will set each register group with the same value. So register 00 for each pot will
 *     be the same value for each pot, and the same for register 01 through 11.
 * G(0-3) - Global Transfer of Registers to the WCR's.  This command will bulk load the values stored in registers into the WCR's.  
 *          For example, a G0 will load the 00 register for each pot into the WCR in one command. A G1 loads the 01 register for each pot, etc.
 * (0-3)(0-3) - This set of commands transfers a single register into a single WCR. The digits are arranged like this (WCR)(Reg). 
 *              For example, a 00 command will load register 00 into WCR 00 or a 13 command will load register 11 into WCR 01, etc.
 * W(0-3) - This command writes directly to the WCR.  I have created four values that are just random, but the command shows how to write directly to the WCR for each pot.
 *          For example, a W0 command will write a value of decimal 16.  These commands are just for example purposes.
 *
 * Command Note: If you enter a command that is not recognized, then it is ignored and nothing happens and nothing is printed.
 */
 

#include "Wire.h"
#define X9241_I2C_ADDRESS 0x2D

// Global Variables
long command;
  

void setup() {
  Wire.begin();
  Serial.begin(57600);
  
}

void loop() {
   if (Serial.available()) {      // Look for char in serial que and process if found
      command = Serial.read();
      if (command == 83) {      //If command = "S" Set Register Data
      // This sets the four registers of each Digital Pot
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xc0);  // This is the set register command c and the 16 registers are 0-f
          Wire.send(0x0a);  // This line is the value to set the register: 0-63 is a valid setting
          Wire.endTransmission(); 
          delay(100);
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xc1);
          Wire.send(0x14);
          Wire.endTransmission();
          delay(100);
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xc2);
          Wire.send(0x20);
          Wire.endTransmission();
          delay(100);
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xc3);
          Wire.send(0x3c);
          Wire.endTransmission();
          delay(100);
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xc4);
          Wire.send(0x0a);
          Wire.endTransmission(); 
          delay(100);
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xc5);
          Wire.send(0x14);
          Wire.endTransmission();
          delay(100);
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xc6);
          Wire.send(0x20);
          Wire.endTransmission();
          delay(100);
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xc7);
          Wire.send(0x3c);
          Wire.endTransmission();
          delay(100);          
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xc8);
          Wire.send(0x0a);
          Wire.endTransmission(); 
          delay(100);
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xc9);
          Wire.send(0x14);
          Wire.endTransmission();
          delay(100);
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xca);
          Wire.send(0x20);
          Wire.endTransmission();
          delay(100);
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xcb);
          Wire.send(0x3c);
          Wire.endTransmission();
          delay(100);
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xcc);
          Wire.send(0x0a);
          Wire.endTransmission(); 
          delay(100);
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xcd);
          Wire.send(0x14);
          Wire.endTransmission();
          delay(100);
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xce);
          Wire.send(0x20);
          Wire.endTransmission();
          delay(100);
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xcf);
          Wire.send(0x3c);
          Serial.println("cmd: S");
       }   
       else if (command == 71) {      //If command = "G" Transfer Global Register Data   
        delay(100);
        if (Serial.available()) {
         command = Serial.read(); 
         if (command == 48) {        // If command = "0" 
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0x10); // Global Load WCR's with Reg-00
          Wire.endTransmission();
          Serial.println("cmd: G0");
         }
         else if (command == 49) {        // If command = "1" 
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0x11); // Global Load WCR's with Reg-01
          Wire.endTransmission();
          Serial.println("cmd: G1");
         }
         else if (command == 50) {        // If command = "2" 
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0x12); // Global Load WCR's with Reg-10
          Wire.endTransmission();
          Serial.println("cmd: G2");
         }
         else if (command == 51) {        // If command = "3" 
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0x13); // Global Load WCR's with Reg-11
          Wire.endTransmission();
          Serial.println("cmd: G3");
         }
        }
       }
       
       else if (command == 48) {      //If command = "0" Transfer Register-00 Data to WCR 0-3
         delay(100);
         if (Serial.available()) {
         command = Serial.read(); 
         if (command == 48) {              // If command = "0" 
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xd0); // Load WCR-00 with Reg-00
          Wire.endTransmission();
          Serial.println("cmd: 00");
         }
         else if (command == 49) {        // If command = "1" 
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xd1); // Load WCR-00 with Reg-01
          Wire.endTransmission();
          Serial.println("cmd: 01");
         }
         else if (command == 50) {        // If command = "2" 
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xd2); // Load WCR-00 with Reg-10
          Wire.endTransmission();
          Serial.println("cmd: 02");
         }
         else if (command == 51) {        // If command = "3" 
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xd3); // Load WCR-00 with Reg-11
          Wire.endTransmission();
          Serial.println("cmd: 03");
         }
        }
       }
        else if (command == 49) {      //If command = "1" Transfer Register-01 Data to WCR 0-3
         delay(100);
         if (Serial.available()) {
         command = Serial.read(); 
          if (command == 48) {              // If command = "0" 
           Wire.beginTransmission(X9241_I2C_ADDRESS);
           Wire.send(0xd4); // Load WCR-01 with Reg-00
           Wire.endTransmission();
           Serial.println("cmd: 10");
          }
          else if (command == 49) {        // If command = "1" 
           Wire.beginTransmission(X9241_I2C_ADDRESS);
           Wire.send(0xd5); // Load WCR-01 with Reg-01
           Wire.endTransmission();
           Serial.println("cmd: 11");
          }
          else if (command == 50) {        // If command = "2" 
           Wire.beginTransmission(X9241_I2C_ADDRESS);
           Wire.send(0xd6); // Load WCR-01 with Reg-10
           Wire.endTransmission();
           Serial.println("cmd: 12");
          }
          else if (command == 51) {        // If command = "3" 
           Wire.beginTransmission(X9241_I2C_ADDRESS);
           Wire.send(0xd7); // Load WCR-01 with Reg-11
           Wire.endTransmission();
           Serial.println("cmd: 13");
          }
         }
        }
        else if (command == 50) {      //If command = "2" Transfer Register-10 Data to WCR 0-3
         if (Serial.available()) {
          command = Serial.read(); 
          if (command == 48) {              // If command = "0" 
           Wire.beginTransmission(X9241_I2C_ADDRESS);
           Wire.send(0xd8); // Load WCR-10 with Reg-00
           Wire.endTransmission();
           Serial.println("cmd: 20");
          }
         else if (command == 49) {        // If command = "1" 
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xd9); // Load WCR-10 with Reg-01
          Wire.endTransmission();
          Serial.println("cmd: 21");
         }
         else if (command == 50) {        // If command = "2" 
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xda); // Load WCR-10 with Reg-10
          Wire.endTransmission();
          Serial.println("cmd: 22");
         }
         else if (command == 51) {        // If command = "3" 
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xdb); // Load WCR-10 with Reg-11
          Wire.endTransmission();
          Serial.println("cmd: 23");
         }
        }
       }
       else if (command == 51) {      //If command = "3" Transfer Register-11 Data to WCR 0-3
        delay(100);
        if (Serial.available()) {
         command = Serial.read(); 
        if (command == 48) {              // If command = "0" 
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xdc); // Load WCR-11 with Reg-00
          Wire.endTransmission();
          Serial.println("cmd: 30");
         }
         else if (command == 49) {        // If command = "1" 
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xdd); // Load WCR-11 with Reg-01
          Wire.endTransmission();
          Serial.println("cmd: 31");
         }
         else if (command == 50) {        // If command = "2" 
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xde); // Load WCR-11 with Reg-10
          Wire.endTransmission();
          Serial.println("cmd: 32");
         }
         else if (command == 51) {        // If command = "3" 
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xdf); // Load WCR-11 with Reg-11
          Wire.endTransmission();
          Serial.println("cmd: 33");
         }
        }
       }
       else if (command == 87) {      //If command = "W" write directly to WCR 0-3
        delay(100);
        if (Serial.available()) {
         command = Serial.read(); 
        if (command == 48) {              // If command = "0" 
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xa0); //Instruction to write WCR-00
          Wire.send(0x10); //Send a D value of 16
          Wire.endTransmission();
          Serial.println("cmd: W0");
         }
         else if (command == 49) {        // If command = "1" 
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xa4); //Instruction to write WCR-01
          Wire.send(0x20); //Send a D value of 32
          Wire.endTransmission();
          Serial.println("cmd: W1");
         }
         else if (command == 50) {        // If command = "2" 
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xa8); //Instruction to write WCR-10
          Wire.send(0x2a); //Send a D value of 26
          Wire.endTransmission();
          Serial.println("cmd: W2");
         }
         else if (command == 51) {        // If command = "3" 
          Wire.beginTransmission(X9241_I2C_ADDRESS);
          Wire.send(0xac); //Instruction to write WCR-11
          Wire.send(0x30); //Send a D value of 48
          Wire.endTransmission();
          Serial.println("cmd: W3");
         }
        }
       }
    }
}
//*****************************************************The End***********************

x9241A_Digital_Potentiometer_v.01 User Guide

Commands

Personal tools
Sponsers
Your Ad Here
Your Ad Here