Hardware SPI

Working libraries, libraries being ported and related hardware
stevestrong
Posts: 894
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany

Re: Hardware SPI

Postby stevestrong » Mon Feb 06, 2017 10:45 am

BennehBoy wrote:Regarding the libmaple spi.h should I worry that I can't find it?

You mean this file? It seems that it exists though :)
BennehBoy wrote:Is that code already compiled to object form so the linker works just fine?

The compiler/linker knows where it should take this file from.
Last edited by stevestrong on Mon Feb 06, 2017 12:33 pm, edited 1 time in total.

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

Re: Hardware SPI

Postby BennehBoy » Mon Feb 06, 2017 10:53 am

Thanks, just me looking in the wrong location. I have a lot to learn.
-------------------------------------
https://github.com/BennehBoy

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

Re: Hardware SPI

Postby BennehBoy » Tue Feb 07, 2017 2:24 pm

Roger, I don't suppose you have your version of this code to hand still? -> http://forum.arduino.cc/index.php?topic ... msg2945098

I might give it a try with begintransaction, still not had any luck with the adafruit library, I don't need all the bloat that library brings to the table so I'm quite happy to go with a minimalist approach.
-------------------------------------
https://github.com/BennehBoy

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

Re: Hardware SPI

Postby BennehBoy » Tue Feb 07, 2017 7:39 pm

Never mind,

I hacked this together and it works... swapped it to use beginTransation also.

Some things to note... the adafruit library incorrectly uses SPI_MODE0, the device expects SPI_MODE3 - correcting the library did not result in success.

Double checking wiring always helps, I had SDO & SDI swapped! Correcting this did not result in success.

Learning is great fun, even when it includes public humiliation :lol: :lol:

Just need to hack this into my project and make sure it coexists nicely with the OLED's now.

Code: Select all

#include "SPI.h"

#define MAX_CS      29

#define NumRegisters 10
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() {
  // put your setup code here, to run once:
  Serial.begin(250000);

  pinMode(MAX_CS, OUTPUT);  //init CS
  digitalWrite(MAX_CS, HIGH);

  InitializeChannel(MAX_CS);

  delay(500);
}

void InitializeChannel(int Pin) {
  Serial.print("Initializing channel on pin ");
  Serial.println(Pin);
  for (int i = 0; i < NumRegisters; i++) {
    WriteRegister(Pin, i, RegisterValues[i]);
  }
}

void loop() {
  Serial.println("=================================================");
  double TC_temp = ReadTemperature(MAX_CS);
  Serial.print("\tThermocouple temp is ");
  Serial.println(TC_temp);
  delay(1000);
}

byte ReadSingleRegister(int Pin, byte Register) {
  SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3));
  digitalWrite(Pin, LOW);
  delayMicroseconds(1);
  SPI.transfer(Register & 0x7F); //set bit 7 to 0 to ensure a read command
  delayMicroseconds(1);
  byte data = SPI.transfer(0);
  digitalWrite(Pin, HIGH);
  SPI.endTransaction();
  return data;
}

unsigned long ReadMultipleRegisters(int Pin, byte StartRegister, int count) {
  //reads up to 4 sequential registers
  SPI.beginTransaction(SPISettings(4000000, 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 WriteRegister(int Pin, byte Register, byte Value) {
  byte Address = Register | 0x80; //Set bit 7 high for a write command
  SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE3));
  digitalWrite(Pin, LOW);
  delayMicroseconds(1);
  SPI.transfer(Address);
  delayMicroseconds(1);
  SPI.transfer(Value);
  digitalWrite(Pin, HIGH);
  SPI.endTransaction();
}

double ReadTemperature(int Pin) {
  double temperature;
  long data;
  data = ReadMultipleRegisters(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
BennehBoy
Posts: 414
Joined: Thu Jan 05, 2017 8:21 pm
Location: Yorkshire
Contact:

Re: Hardware SPI

Postby BennehBoy » Tue Feb 07, 2017 8:51 pm

OK, have merged the code into my LRDuino project and all is working well. YAY :D :D

I added some extra code in which checks the state of the status register also, so my fault checking still works.

Job jobbed. Code will be on github later.
-------------------------------------
https://github.com/BennehBoy

User avatar
ahull
Posts: 1423
Joined: Mon Apr 27, 2015 11:04 pm
Location: Sunny Scotland
Contact:

Re: Hardware SPI

Postby ahull » Tue Feb 07, 2017 9:35 pm

BennehBoy wrote:OK, have merged the code into my LRDuino project and all is working well. YAY :D :D

I added some extra code in which checks the state of the status register also, so my fault checking still works.

Job jobbed. Code will be on github later.


Pictures.. we want pictures. :D
- Andy Hull -

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

Re: Hardware SPI

Postby BennehBoy » Tue Feb 07, 2017 9:56 pm

As if by magic...

Image

The all important part is the top third from left screen, reading just over ambient (well within %err for the k-type).
-------------------------------------
https://github.com/BennehBoy


Return to “Libraries & Hardware”

Who is online

Users browsing this forum: No registered users and 1 guest