User Tools

Site Tools


arduino:gammon-spi-controller

From SPI - Serial Peripheral Interface - for Arduino by Nick Gammon. Modified to use the newish and preferred SPI.beginTransaction() and SPI.endTransaction().

gammon-spi-controller.ino
// Written by Nick Gammon
// February 2011
// With Mithat Mods -- July 2023
 
#include <SPI.h>
 
void setup (void)
{
 
  digitalWrite(SS, HIGH);  // ensure SS stays high for now
 
  // Put SCK, MOSI, SS pins into output mode
  // also put SCK, MOSI into LOW state, and SS into HIGH state.
  // Then put SPI hardware into Master mode and turn SPI on
  SPI.begin ();
 
  // Slow down the master a bit
//  SPI.setClockDivider(SPI_CLOCK_DIV8);  // mithat: let's do this the preferred way
 
}  // end of setup
 
 
void loop (void)
{
 
  char c;
 
  SPI.beginTransaction(SPISettings(400000, MSBFIRST, SPI_MODE0));  // mithat: this is the preferred way
 
  // enable Slave Select
  digitalWrite(SS, LOW);    // SS is pin 10
 
  // send test string
  for (const char * p = "Hello, world!\n" ; (c = *p); p++)
  {
    SPI.transfer (c);
    delayMicroseconds(5);    // mithat: added small delay to make things work at given speed
  }
 
  // disable Slave Select
  digitalWrite(SS, HIGH);
  SPI.endTransaction();      // mithat: finish preferred way
 
  delay (1000);  // 1 seconds delay 
}  // end of loop
arduino/gammon-spi-controller.txt · Last modified: 2023/07/24 18:43 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki