Converting code from Pro Mini to pro Micro

Hello,

I have a simple program written by a friend of mine that monitors some input pins and sends a command via SPI. it works just fine on a Pro Mini, but I want to convert it to work on a Pro micro so i can get rid of the FTDI cable i need for the Mini, the pro mico of course having USB built in.

The boards are the same size, same number of pins. but the Micro has a couple of pins numbered differently. I THOUGHT all i had to do is to change the pin number in the code. but the code does not work.

So I tried an Uno R3 as i figured the pin numbers are the same. still doesn't work. I am missing something. I am not very good with writing code. can I entice anyone to help?

Welcome to the forum

Start by posting your sketch, using code tags when you do

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

1 Like
#include <Arduino.h>
#include <SPI.h>
#include "binary.h"

#define PIN_I2S_OVER_USB_DSD_PCM 5
#define PIN_I2S_OVER_USB_A0 6
#define PIN_I2S_OVER_USB_A1 7
#define PIN_I2S_OVER_USB_A2 8
#define PIN_I2S_OVER_USB_A3 9
//#define PIN_I2S_OVER_USB_A0_INV 4 // unused
#define PIN_AD1955_RESET 4    // unused
#define PIN_AD1955_CLATCH 10  // SS
#define PIN_AD1955_CDATA 11   // MOSI
#define PIN_AD1955_CCLK 13    // SCLK

void setup() {
  Serial.begin(9600);
  SPI.begin();
  Serial.println("Starting ...");
  pinMode(PIN_I2S_OVER_USB_DSD_PCM, INPUT_PULLUP);
  pinMode(PIN_I2S_OVER_USB_A0, INPUT_PULLUP);
  pinMode(PIN_I2S_OVER_USB_A1, INPUT_PULLUP);
  pinMode(PIN_I2S_OVER_USB_A2, INPUT_PULLUP);
  pinMode(PIN_I2S_OVER_USB_A3, INPUT_PULLUP);
  //pinMode(PIN_I2S_OVER_USB_A0_INV, INPUT_PULLUP);
  // pinMode(PIN_I2S_OVER_USB_RESET, OUTPUT);
  pinMode(PIN_AD1955_RESET, OUTPUT);
  digitalWrite(PIN_AD1955_RESET, 0);
  delay(1);
  digitalWrite(PIN_AD1955_RESET, 1);
}

void loop() {
  // put your main code here, to run repeatedly:
  double sampling_rate = GetSampleRate();

  uint16_t mclk_mode = 0;
  if (sampling_rate <= 48) {
    mclk_mode = 0;
  } else if (sampling_rate <= 96) {
    mclk_mode = 0b00;
  } else if (sampling_rate <= 192) {
    mclk_mode = 0b00;
  }
  uint16_t reg_val = 0;
  reg_val &= ~(0b11 << 9);  // Clear MCLK Mode
  reg_val &= ~(0b11 << 0);  // Clear SPI Register Address

  // Set the new values
  reg_val |= (mclk_mode << 9);  // Set MCLK Mode
  reg_val |= (1 << 0);          // Write regster 1
  writeRegister(reg_val);

  reg_val = 0;
  uint16_t pcm_sample_rate = 0;
  if (sampling_rate <= 48) {
    pcm_sample_rate = 0;
  } else if (sampling_rate <= 96) {
    pcm_sample_rate = 0b01;
  } else if (sampling_rate <= 192) {
    pcm_sample_rate = 0b10;
  }
  reg_val |= (pcm_sample_rate << 8);  // Set MCLK Mode
  writeRegister(reg_val);
}


void writeRegister(uint16_t value) {
  // take the chip select low to select the device:
  digitalWrite(PIN_AD1955_CLATCH, LOW);

  SPI.transfer16(value);  //Send register location

  // take the chip select high to de-select:
  digitalWrite(PIN_AD1955_CLATCH, HIGH);
}

uint8_t last_config = 0;
double last_sampling_rate = 0.0;
double GetSampleRate() {
  uint8_t dsd_pcm, a0, a1, a2, a3, a0_inverted;
  dsd_pcm = digitalRead(PIN_I2S_OVER_USB_DSD_PCM);
  dsd_pcm = 0;
  a0 = digitalRead(PIN_I2S_OVER_USB_A0);
  a1 = digitalRead(PIN_I2S_OVER_USB_A1);
  a2 = digitalRead(PIN_I2S_OVER_USB_A2);
  a3 = digitalRead(PIN_I2S_OVER_USB_A3);
  //a0_inverted = digitalRead(PIN_I2S_OVER_USB_A0_INVERTED);
  uint8_t config = (dsd_pcm << 4) | (a3 << 3) | (a2 << 2) | (a1 << 1) | (a0);
  if (last_config != config) {
    Serial.print("New config: ");
    Serial.println(config);
    last_config = config;
  }
  double sampling_rate = 0.0;
  if ((config == 0)) {
    sampling_rate = 44.1;
  } else if (config == 0b1) {
    sampling_rate = 48;
  } else if (config == 0b10) {
    sampling_rate = 88.2;
  } else if (config == 0b11) {
    sampling_rate = 96;
  } else if (config == 0b100) {
    sampling_rate = 176.4;
  } else if (config == 0b101) {
    sampling_rate = 192;
  } else if (config == 0b110) {
    sampling_rate = 352.8;
  } else if (config == 0b111) {
    sampling_rate = 384;
  } else if (config == 0b01000) {
    sampling_rate = 705.6;
  } else if (config == 0b01001) {
    sampling_rate = 768;
  }
  if (sampling_rate != last_sampling_rate) {
    Serial.print("Sampling rate: ");
    Serial.println(sampling_rate, 1);
    last_sampling_rate = sampling_rate;
  }

  return sampling_rate;
}

why did you not use the default Pro Micro SPI pins - see Pro Micro & Fio V3 Hookup Guide , e.g. MISO pin 14 MOSI pin 16 SCK pin 15?
e.g. when I connected a RFM95 LoRa module to a Pro micro I used

// Pro Micro with RPi HAT RFM95W
// Pro Micro SCK  pin 15  to RFM95_pin SCK
// Pro Micro MISO pin 14  to RFM95_pin MISO
// Pro Micro MOSI pin 16  to RFM95_pin MOSI
// Pro Micro pin 8 to RFM95 SS    /
// Pro Micro pin 4 to RFM95 Reset
// Pro Micro pin 7 to RFM95 DIO0
// Pro Micro pin 6 to RFM95 DIO1

I suggest you take a step back and implement a separate program to just test your SPI target module working with the Pro Micro
similarly test any other modules you wish to connect to the Pro Micro in separate programs

when all modules work with the Pro Micro start building your complete project code

make sure you select the correct Pro Micro version using Tools>Board, e.g. 3.3V 8MHz or 5V 16MHz - selecting the incorrect version can cause problems

The code posted is for the pro mini and it works fine. I changed the spi pin numbers before uploading it to the pro micro. But it still did not work. Also, yes I have the correct 5V logic version.

And BTW I did get the uno to work. I had one wire in the wrong place. So the code posted works on the pro mini and an Uno R3, I just can't get it to work on the pro micro.

OK I GOT IT TO WORK!!!!

after a lot of googling it seems Pro Micro's and SPI do not work well together. SS is connected to PIN 17 which is not brought out to a pad. instead it is connected to the RX LED. by connecting a wire to the Resistor that feeds the LED (IE the connection straight from the Chip) and setting SS in my code to pin 17 it works!

Your Pro Micro is acting as an SPI master, correct? Because the only time you really need /SS (Slave Select) is when you're using the chip as an SPI slave. As a master, you can use whatever pin you like for your slave's /CS. It's an entirely different thing than /SS.

well, I know just enough about this to be dangerous. as I said, a friend of mine who is far more versed in code writing wrote the code for me. I know just enough to upload a! sketch and make a minor change or two like pin numbers etc. i am FAR better with hardware.

I did a lot of googling, "Pro Micro SPI" brought up many posts about the issue. 2 post's caught my attention.

this one
https://forums.adafruit.com/viewtopic.php?t=74383
talked about making Pin 17 the SS pin

and this one

which shows attaching a wire to the Board for the SS connection.

for whatever reason, it now works!!!

I am REALLY curious now as I also ordered a couple of Adafruit Itsy Bitsy boards and the schematic for that board shows that the SS pin isn't connected to anything. I might not be able to use those for SPI. eh whatever. I am just happy that I got it to work and I can put the lid on this thing now and be done with it!!!

As I said, you're not using slave mode. /SS is irrelevant. But have it your way, it makes no difference to me.

To help others. I have designed and created a Pro Micro Adapter board that serves 2 functions. #1 is a way to mount a pro micro in a project and #2 is to allow you to add a jumper to bring the SPI SS pin 17 out to a header for projects that need it!