Skip to content

Get stuck in mcp.analogRead loop #4

@FelixWT

Description

@FelixWT

Hi,
Thank you for making this ADC library. It's one of the few library that support MCP3562.

But when I'm implement this code to my ESP32 S3 it seems to stuck in analogread infinite loop.
The differences are I make the MCP3562 instance aka mcp a global instance by adding extern MCP3562 mcp; in MCP3x6x.h and MCP3562 mcp=MCP3562(); in MCP3x6x.cpp .
Also dude to the layout error I swap the pin definition of MISO & MOSI in the class Constructor .

MCP3x6x::MCP3x6x(const uint16_t MCP3x6x_DEVICE_TYPE, const uint8_t pinCS, SPIClass *theSPI,
                 const uint8_t pinMOSI, const uint8_t pinMISO, const uint8_t pinCLK) {
  switch (MCP3x6x_DEVICE_TYPE) {
    case MCP3461_DEVICE_TYPE:
      _resolution_max = 16;
      _channels_max   = 2;
      break;
    case MCP3462_DEVICE_TYPE:
      _resolution_max = 16;
      _channels_max   = 4;
      break;
    case MCP3464_DEVICE_TYPE:
      _resolution_max = 16;
      _channels_max   = 8;
      break;
    case MCP3561_DEVICE_TYPE:
      _resolution_max = 24;
      _channels_max   = 2;
      break;
    case MCP3562_DEVICE_TYPE:
      _resolution_max = 24;
      _channels_max   = 4;
      break;
    case MCP3564_DEVICE_TYPE:
      _resolution_max = 24;
      _channels_max   = 8;
      break;
    default:
#warning "undefined MCP3x6x_DEVICE_TYPE"
      break;
  }

  //  settings.id = MCP3x6x_DEVICE_TYPE;
/* ========================================= */

  _spi        = theSPI;
  _pinMISO    = pinMOSI;//SWAP DUE TO LAYOUT ERROR
  _pinMOSI    = pinMISO;//SWAP DUE TO LAYOUT ERROR
  _pinCLK     = pinCLK;
  _pinCS      = pinCS;

  Serial.print("_pinMISO: ");
  Serial.println(_pinMISO);
  Serial.print("_pinMOSI: ");
  Serial.println(_pinMOSI);
  Serial.print("_pinCLK: ");
  Serial.println(_pinCLK);
  Serial.print("_pinCS: ");
  Serial.println(_pinCS);

/* ========================================= */

  _resolution = _resolution_max;
  _channel_mask |= 0xff << _channels_max;  // todo use this one
};

And this is my main task

#include <SPI.h>
#include "MCP3x6x.h"

void setup()
{  
  Serial.begin(115200);  
  while (!mcp.begin()) {
    Serial.println("failed to initialize MCP");
    delay(200);
  }
}
void loop()
{
  Serial.println("1");//stuck detect
  int32_t adcdata = mcp.analogRead(MCP_CH0);
  Serial.println("2");//stuck detect
  double voltage = adcdata * mcp.getReference() / mcp.getMaxValue();  
  Serial.print("voltage: ");
  Serial.println(voltage, 10);
  delay(1000);
}

And the Serial out put:
_pinMISO: 11
_pinMOSI: 13
_pinCLK: 12
_pinCS: 10
1
mux

Any advice is greatly appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions