Back port to Arduino

Anything not related to STM32
Post Reply
User avatar
BennehBoy
Posts: 420
Joined: Thu Jan 05, 2017 8:21 pm
Location: Yorkshire
Contact:

Back port to Arduino

Post by BennehBoy » Sat Mar 18, 2017 2:12 pm

When I started to use STM32 for my multigauge project, I had problems with the max31856 library that I was using so I ditched it for some lean code not in library form.

I'm now making a cut down version of the gauge system for a friend, since he needs far less features I'm using an Arduino nano.

Unfortunately the max code that I found for stm32 does not appear to work, I suspect there may be something it's doing that my inexperienced eye is not catching.

The code just returns 0.

Circuit wise there is another difference, the 31856 is 3.3v so I've introduced a 3.3v to 5v bidirectional level shifter between all the GPIO of the arduino and it. I've checked with a logic analyser and the shifter is correctly passing the signals.

So here's a chop out of all the code which works on STM32, please could the more experienced cast their eyes over it?

Code: Select all

//initial definitions

#define MAX_CS      10
#define NumRegisters 10

#include <SPI.h>

byte RegisterValues[] =    {0x90,  0x03,   0xFC,   0x7F,   0xC0,   0x07,     0xFF,     0x80,     0x00,     0x00 };
String RegisterNames[] =   {"CR0", "CR1", "MASK", "CJHF", "CHLF", "LTHFTH", "LTHFTL", "LTLFTH", "LTLFTL", "CJTO"};
byte RegisterAddresses[] = {0x00,  0x01,   0x02,   0x03,   0x04,   0x04,     0x06,     0x07,     0x08,     0x09 };

void setup()   {

  Serial.begin(9600);

  pinMode (MAX_CS, OUTPUT);
  digitalWrite(MAX_CS, HIGH);
  
  MAXInitializeChannel(MAX_CS); // Init the MAX31856 
}

void loop() {
  int t = int(MAXReadTemperature(MAX_CS));
  Serial.println(t);
}  

void MAXInitializeChannel(int Pin) {
  for (int i = 0; i < NumRegisters; i++) {
    MAXWriteRegister(Pin, i, RegisterValues[i]);
  }
}

unsigned long MAXReadMultipleRegisters(int Pin, byte StartRegister, int count) {
  //reads up to 4 sequential registers
  SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE3));
  digitalWrite(Pin, LOW);
  unsigned long data = 0;
  SPI.transfer(StartRegister & 0x7F); //force bit 7 to 0 to ensure a read command
  delayMicroseconds(1);

  for (int i = 0; i < count; i++) {
    data = (data << 8) | SPI.transfer(0); //bitshift left 8 bits, then add the next register
  }
  digitalWrite(Pin, HIGH);
  SPI.endTransaction();
  return data;
}

void MAXWriteRegister(int Pin, byte Register, byte Value) {
  byte Address = Register | 0x80; //Set bit 7 high for a write command
  SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE3));
  digitalWrite(Pin, LOW);
  delayMicroseconds(1);
  SPI.transfer(Address);
  delayMicroseconds(1);
  SPI.transfer(Value);
  digitalWrite(Pin, HIGH);
  SPI.endTransaction();
}

double MAXReadTemperature(int Pin) {
  double temperature;
  long data;
  data = MAXReadMultipleRegisters(Pin, 0x0C, 4);
  // Strip the unused bits and the Fault Status Register
  data = data >> 13;
  // Negative temperatures have been automagically handled by the shift above :-)
  // Convert to Celsius
  temperature = (double) data * 0.0078125;
  // Return the temperature
  return (temperature);
}
-------------------------------------
https://github.com/BennehBoy

User avatar
RogerClark
Posts: 6911
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Back port to Arduino

Post by RogerClark » Sat Mar 18, 2017 7:50 pm

Why are you using a nano for the other version. There is virtually no cost saving.

I now only use AVR arduinos if I need 5V signals or if some library is far to hard to port to STM32.

Edit.

Porting to AVR is probably harder than porting from AVR

User avatar
BennehBoy
Posts: 420
Joined: Thu Jan 05, 2017 8:21 pm
Location: Yorkshire
Contact:

Re: Back port to Arduino

Post by BennehBoy » Sat Mar 18, 2017 8:20 pm

RogerClark wrote:Why are you using a nano for the other version. There is virtually no cost saving.

I now only use AVR arduinos if I need 5V signals or if some library is far to hard to port to STM32.
1 reason only, I already have it here.
-------------------------------------
https://github.com/BennehBoy

User avatar
BennehBoy
Posts: 420
Joined: Thu Jan 05, 2017 8:21 pm
Location: Yorkshire
Contact:

Re: Back port to Arduino

Post by BennehBoy » Sat Mar 18, 2017 10:11 pm

Here's where I found the code, it originated on AVR so I had hope it would work - > http://forum.arduino.cc/index.php?topic=390824.0
-------------------------------------
https://github.com/BennehBoy

User avatar
BennehBoy
Posts: 420
Joined: Thu Jan 05, 2017 8:21 pm
Location: Yorkshire
Contact:

Re: Back port to Arduino

Post by BennehBoy » Sat Mar 18, 2017 10:18 pm

Well, as per that thread it works fine with spi.begin but not with spi.beginTransaction :(

I suppose I'd better order up some more maple minis
-------------------------------------
https://github.com/BennehBoy

Post Reply