Author Topic: Anyone having problems with SPI?  (Read 2105 times)

Nevyn

  • Newbie
  • *
  • Posts: 25
Anyone having problems with SPI?
« on: April 23, 2016, 11:42:24 pm »
I've been working with the DS3234 real time clock with SPI interface for a few days now and have had no luck programming it, until this morning.

The breakout board from Sparkfun (https://www.sparkfun.com/products/10160) lists the board as being Mode 1 or Mode 3 compatible.

I have a small application that tries to set the time and then read all of the registers, dumping them to the serial port.

In Mode 1 or Mode 3, reading is fine but I could not set the registers.  After a while I decided to try Modes 0 and 2 just in case one of those worked.  No luck.

The spec for the chip states that the data to be written must be present on the rising edge of the clock and the data to be read will be available on the falling edge of the clock.

In desperation, I decided to bit bang the data out and this works fine.

Just wondered if anyone else had seen this.

Regards,
Mark

digi_guy

  • Jr. Member
  • **
  • Posts: 87
Re: Anyone having problems with SPI?
« Reply #1 on: April 27, 2016, 08:17:46 pm »
I just got an MCP3008 ACD chip working over SPI, unfortunately I have no idea how any of this works so I can't be of much more help, but I figured I'd post my working code here for anyone else to look at.

Code: [Select]
#include <SPI.h>


#define CS_PIN 5 // I used pin 5

int pinRead = 0;
char buf[64];

int adcRead(int channel) {
  if ((channel > 7) || (channel < 0)) {
    return -1;
  }

  digitalWrite(CS_PIN, LOW);

  SPI.transfer(1);
  uint8_t r1 = SPI.transfer((channel + 8) << 4);
  uint8_t r2 = SPI.transfer(0);

  digitalWrite(CS_PIN, HIGH);

  return ((r1 & 3) << 8) + r2;
}

void setup() {
  pinMode(CS_PIN, OUTPUT);
  SPI.begin();
  SPI.setBitOrder(MSBFIRST);
  SPI.setDataMode(SPI_MODE0);
  SPI.setFrequency(1000000); // 1 MHz -- remove line when running on Arduino UNO


  digitalWrite(CS_PIN, HIGH);
}

void loop() {
  pinRead = adcRead(0);
  sprintf(buf, "%i", pinRead);
  Particle.publish("Pin 0", buf);
  delay(1000);
}


PeterF

  • Hero Member
  • *****
  • Posts: 881
Re: Anyone having problems with SPI?
« Reply #2 on: April 28, 2016, 03:14:34 am »
Oh... nice one digi_guy! Just the thing to get around the limit analog support on the ESP8266/Oak!  8)