Esercitazioni su interfaccia SPI di Arduino

Esempio di I/O con registri SIPO e PISO

10 SS
11 MOSI
12 MISO
13 SCK

Software

#define SelectPin 10
#include <SPI.h>

void setup() {
  pinMode(SelectPin, OUTPUT);
  digitalWrite(SelectPin, LOW);
  SPI.begin();
  Serial.begin(9600);
}

void printbin(byte v)
{
  int n;
  for (n=0;n<8;n++)
  {
    Serial.print((char)('0'+((v & 0x80)>0)));
    v<<=1;
  }
  Serial.println();
}  


void TestOut()
{
  SPI.beginTransaction(SPISettings(250000, MSBFIRST, SPI_MODE0));  
  digitalWrite(SelectPin, LOW);
  SPI.transfer(0b10000001);
  digitalWrite(SelectPin, HIGH); 
  SPI.endTransaction();
}

void TestIn()
{
  int v;
  SPI.beginTransaction(SPISettings(250000, MSBFIRST, SPI_MODE0));  
  digitalWrite(SelectPin, LOW);
  v=SPI.transfer(0);
  printbin(v);
  digitalWrite(SelectPin, HIGH); 
  SPI.endTransaction();
}

void TestInOut()
{
  int n,v;
  for (n=0;n<256;n++)
  {     
     SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));  
     digitalWrite(SelectPin, LOW);
     v=SPI.transfer(n);
     printbin(v);
     digitalWrite(SelectPin, HIGH); 
     SPI.endTransaction();
     delay(100);
  }  
}

void loop()
{
  TestOut();
  //TestIn();
  //TestInOut();
}
Analizzare tramite oscilloscopio i segnali MISO, MOSI, SCK durante l'esecuzione di   TestOut() o TestIn() o TestInOut();
 

Pilotaggio con SPI di ADC TLC1543

http://www.ti.com/lit/ds/symlink/tlc1543.pdf

Software


//http://www.ti.com/lit/ds/symlink/tlc1543.pdf
// Arduino TLC 1543
//MOSI(11) ADDRESS(17)
//MISO(12) DATA_OUT(16)
//SCK(13) I/O_CLOCK(18)
//SS(10) CS(15)
// include the SPI library:
#include <SPI.h>
// set pin 10 as the slave select
const int slaveSelectPin = 10;
void setup()
{
  // set the slaveSelectPin as an output:
  pinMode (slaveSelectPin, OUTPUT);
  // initialize SPI:  
  SPI.begin();
  Serial.begin(9600);
}

int AdcRead(int address)
{
  byte vh,vl;
  digitalWrite(slaveSelectPin, LOW);
  vh=SPI.transfer(address<<4);
  vl=SPI.transfer(0);
  digitalWrite(slaveSelectPin, HIGH);
  return(vh<<2)+(vl>>6);
}

void ReadAll()
{
  int n;
  float v;
  AdcRead(0);
  for (n=1;n<=11;n++)  
  {
    v=AdcRead(n)*5.0/1023;
    Serial.print(v);
    Serial.print("\t");
  }
  Serial.println(); 
}

void loop() 
{
  //delayMicroseconds(50);
  SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0));    
  ReadAll();
  //Serial.println(AdcRead(0));
  SPI.endTransaction();
}

Pilotaggio con SPI di DAC MCP4911

http://ww1.microchip.com/downloads/en/DeviceDoc/22248a.pdf

Software

Liberia DAC https://github.com/exscape/electronics

#include <SPI.h>
#include <DAC_MCP49xx.h>

#define SS_PIN 10
#define LDAC_PIN 7
DAC_MCP49xx dac(DAC_MCP49xx::MCP4911, SS_PIN,LDAC_PIN);

void setup(void) 
{
  dac.setSPIDivider(SPI_CLOCK_DIV16);
  dac.setPortWrite(true);  
  Serial.begin(9600);
  SPI.begin();
}

void loop() 
{
  int n;
  for (n=0;n<360;n++)
  {
    dac.output(512+511*sin(n*3.14/180));
    dac.latch();
  }
}

Driver Display LCD + 8 ingressi per eventuale tastierino

Sofware LCD SPI Arduino


Pilotaggio con SPI dell' IOExpander MCP23S17SP 

https://cdn-shop.adafruit.com/datasheets/mcp23017.pdf

Libreria arduino per MCP23S17SP https://github.com/n0mjs710/MCP23S17

Effettuare test con programmi esempio forniti dalla libreria


Misure su oscilloscopio

TestOut()

TestIn()