Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

about the MCP4261 pot0 and pot1 output different

Status
Not open for further replies.

laoadam

Member level 2
Joined
Feb 22, 2019
Messages
52
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
324
Hi,
I am testing a MCP4261 by the code below, got the result measured by a multi-meter with different readings, why?
The pot0 output at: 0 - 2.24 loop;
The pot1 output at: 2.52 - 4.80 loop.
why?
Thanks for help.
adam


Code:
#include <SPI.h>

const byte csPin           = 2;       // MCP42100 chip select pin
const int  maxPositions    = 256;     // wiper can move from 0 to 255 = 256 positions
const long rAB             = 100000;  // 100k pot resistance between terminals A and B
const byte rWiper          = 125;     // 125 ohms pot wiper resistance
const byte pot0            = 0x11;    // pot0 addr
const byte pot1            = 0x12;    // pot1 addr
const byte potBoth         = 0x13;    // pot0 and pot1 simultaneous addr
const byte pot0Shutdown    = 0x21;    // pot0 shutdown
const byte pot1Shutdown    = 0x22;    // pot1 shutdown
const byte potBothShutdown = 0x23;    // pot0 and pot1 simultaneous shutdown


byte addressPot0 =     0b00010001;      //PIN 9 // To define potentiometer use last two BITS 01= POT 0
byte addressPot1 =     0b00010010;      //PIN 6 // To define potentiometer use last two BITS 10= POT 1
byte addressPot0and1 = 0b00010011;  //To define potentiometer use last two BITS 10= POT 0 and 1
byte CS = 53;   // MCP4261_1, Chip control goes to pin 10
byte SHDN = 49;  //MCP4261_12, Chip SHUTDOWN - PIN 9
byte RS = 48;    // MCP4261_11, Chip RESET - PIN 8

void setup()
{
  pinMode (CS, OUTPUT); //CS - When High, sets chip to read the data.
  pinMode (SHDN, OUTPUT); //CS - When High, sets chip to read the data.
  pinMode (RS, OUTPUT); //CS - When High, sets chip to read the data.

  digitalWrite(SHDN, HIGH); //Power ON (HIGH)
  digitalWrite(RS, HIGH); //Power NO RESET (LOW)
  SPI.begin();
}

void loop()
{
   Test0_loop();
 
}


void Test0_loop()
{
  PotHighAndLow_mt(addressPot0);      //Change POT values on Pot0
  PotHighAndLow_mt(addressPot1);     //Change POT values on Pot1
  PotHighAndLow_mt(addressPot0and1); //Change POT values on both


  digitalPotWrite(245,  addressPot1);
  digitalWrite(SHDN, LOW); //Power OFF (LOW)
  delay(5000); //delay for 5 seconds to test current consumption of potentiometer
  digitalWrite(SHDN, HIGH); //Power ON (HIGH)
  digitalPotWrite(245,  addressPot1);

  digitalWrite(RS, LOW); //Power NO RESET (LOW)
  delay(100); //delay 100 mls
  digitalWrite(RS, HIGH); //Power RESET (HIGH)
  delay(10000); //delay 10 sec

}

void PotHighAndLow_mt(byte address)
{
  /* We have limit from 130 - 255 just for LED test, but for other projects it can be 0-255 */
      for (int i = 130; i <= 255; i++)
    {
      digitalPotWrite(i,address);
      delay(50);
    }
   // delay(500);
    for (int i = 255; i >= 130; i--)
    {
      digitalPotWrite(i,address);
      delay(100);
    }
}



int digitalPotWrite(byte value, byte address)
{
  digitalWrite(CS, LOW); //Set Chip Active
  SPI.transfer(address);
  SPI.transfer(value);
  digitalWrite(CS, HIGH); //Set Chip Inactive
}
 

Hi

First you should provide full information:
* your schematic
* exact device type
Without it we can not know which pin is which...

Next:
You need to read the datasheet.

Then..
Please use the same variable names as in datasheet. You use "address" while the datasheet says "command".
This does not only help us to understand, but also yourself when you need to read the code after years.

Then you use 0b00010000 for wiper 0
0b00010001 for wiper1
And 0b00010011 for both.
I can't find any information in the datasheet to update both wipers at the same time with the same data at all.
Within the command there is an address (upper 4 bits) which specify where the data is written to.
0b0000xxxx stands for wiper0
0b0001xxxx stands for wiper1
This is different to your command.
0bxxxx00xx is for write
0bxxxxxxx1 stands for most UP wiper position = 256

--> Please check datasheet.

Klaus
 

Hi

First you should provide full information:
* your schematic
* exact device type
Without it we can not know which pin is which...

Next:
You need to read the datasheet.

Then..
Please use the same variable names as in datasheet. You use "address" while the datasheet says "command".
This does not only help us to understand, but also yourself when you need to read the code after years.

Then you use 0b00010000 for wiper 0
0b00010001 for wiper1
And 0b00010011 for both.
I can't find any information in the datasheet to update both wipers at the same time with the same data at all.
Within the command there is an address (upper 4 bits) which specify where the data is written to.
0b0000xxxx stands for wiper0
0b0001xxxx stands for wiper1
This is different to your command.
0bxxxx00xx is for write
0bxxxxxxx1 stands for most UP wiper position = 256

--> Please check datasheet.

Klaus
Thank you, I'll check what you said.

I used MEGA2560+MCP4261.

PP1P1.PNG
 


Hi,

You may call me ignorant, but I rather rely on the manufacturer´s datasheet.

For sure there is a chance that I misunderstood the datasheet.

***
just recognized that in your schematic P0A, P0B, P1A, P1B are unconnected....
* if this really is the case then you need to use the correct TCAP setup
* if this is not the case, then please update your schematic.

***
I still miss the complete partnumber. Without knowing which package you use we are not able to verify pinning.
Different packages have different pin_number for the same signal.

Klaus
 

Hi,

You may call me ignorant, but I rather rely on the manufacturer´s datasheet.

For sure there is a chance that I misunderstood the datasheet.

***
just recognized that in your schematic P0A, P0B, P1A, P1B are unconnected....
* if this really is the case then you need to use the correct TCAP setup
* if this is not the case, then please update your schematic.

***
I still miss the complete partnumber. Without knowing which package you use we are not able to verify pinning.
Different packages have different pin_number for the same signal.

Klaus
Klaus, thanks.
The all A to 5v, and all B to GND.
The part number is: MCP4261-103E/P.
 

Hi,

does it work now?

Klaus
Klaus thanks.
I used Matthew's code works well. that means the wiring and components OK.
I don't see many people works on MCP. I wonder if you can take a look of the code below, that works well, but as long as the line 128 "// mcpDrive();" added, no work at all. why?

Code:
#include <SPI.h>

//MCP cinfigrition
const int slaveSelectPin = 53; //WAS 10 on Arduino uno.
const int shutdownPin = 49; // was 7 on uno.

const int wiper0writeAddr = B00000000;  //these addresses can be found in your chip's datasheet
const int wiper1writeAddr = B00010000;

uint8_t receivedValue_x = 0;
uint8_t receivedValue_y = 0;

#define enA 8
#define in1 4
#define in2 5

#define in3 6
#define in4 7
#define enB 9

int xAxis = 140, yAxis = 140;

int motorSpeedA = 0;
int motorSpeedB = 0;

void setup() {
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

  pinMode (slaveSelectPin, OUTPUT);
  pinMode (shutdownPin, OUTPUT);
 
  Serial.begin(9600);
  Serial1.begin(9600); // Default communication rate of the Bluetooth module
  delay(500);
}

void loop() {
  // Default value - no movement when the Joystick stays in the center
  //xAxis = 140;
  //yAxis = 140;

  // Read the incoming data from the Smartphone Android App
  while (Serial1.available() >= 2) {
    xAxis = Serial1.read();
    delay(10);
    yAxis = Serial1.read();
    Serial.print(xAxis);
    Serial.print(",");
    Serial.println(yAxis);
  }
  delay(10);

  // Makes sure we receive corrent values

  if (xAxis > 130 && xAxis < 150 && yAxis > 130 && yAxis < 150)
  {
    Stop();
  }

  if (yAxis > 130 && yAxis < 150) {

    if (xAxis < 130) {
      turnRight();
      motorSpeedA = map(xAxis, 130, 60, 0, 255);
      motorSpeedB = map(xAxis, 130, 60, 0, 255);
    }

    if (xAxis > 150) {
      turnLeft();
      motorSpeedA = map(xAxis, 150, 220, 0, 255);
      motorSpeedB = map(xAxis, 150, 220, 0, 255);
    }

  } else {

    if (xAxis > 130 && xAxis < 150) {

      if (yAxis < 130) {
        forword();
      }
      if (yAxis > 150) {
        backword();
      }

      if (yAxis < 130) {
        motorSpeedA = map(yAxis, 130, 60, 0, 255);
        motorSpeedB = map(yAxis, 130, 60, 0, 255);
      }

      if (yAxis > 150) {
        motorSpeedA = map(yAxis, 150, 220, 0, 255);
        motorSpeedB = map(yAxis, 150, 220, 0, 255);
      }

    } else {

      if (yAxis < 130) {
        forword();
      }
      if (yAxis > 150) {
        backword();
      }

      if (xAxis < 130) {
        motorSpeedA = map(xAxis, 130, 60, 255, 50);
        motorSpeedB = 255;
      }

      if (xAxis > 150) {
        motorSpeedA = 255;
        motorSpeedB = map(xAxis, 150, 220, 255, 50);
      }

    }
  }

  //Serial.print(motorSpeedA);
  //Serial.print(",");
  //Serial.println(motorSpeedA);

  analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
  analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
  // mcpDrive();
  Serial.print("motorSpeedA122=");
  Serial.print(motorSpeedA);

}

void mcpDrive ()
{
  digitalPotWrite(wiper0writeAddr, motorSpeedA); // Send PWM signal to motor A
  digitalPotWrite(wiper1writeAddr, motorSpeedB); // Send PWM signal to motor B
}

void digitalPotWrite(int address, int value) {

  digitalWrite(slaveSelectPin, LOW);
  digitalWrite(shutdownPin, HIGH);
  delay(2);
  /// digitalWrite(shutdownPin, HIGH); //Turn off shutdown THIS MAKE WRITEABLE OR DISABLE THIS CAN'T WRITE
  //  send in the address and value via SPI:
  SPI.transfer(address);
  SPI.transfer(value);
  digitalWrite(slaveSelectPin, HIGH);
  delay(2);
}

void forword() {
  Serial.println("forword");
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
}

void backword() {
  Serial.println("backword");
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
}

void turnRight() {
  Serial.println("turnRight");
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
}

void turnLeft() {
  Serial.println("turnLeft");
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
}

void Stop() {
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  Serial.println("stop");
}
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top