Thursday, April 30, 2015

Arduino SPI and Proteus ISIS simulation

 

The SPI stands for Serial Peripheral Interface which is a serial communication and it is used for short distance communications. It common uses are in sensor, LCDs and Secure digital cards.

It uses four (4) cables where 3 of them are the same for  each device (MOSI, MISO, SCK) and the fourth one is for selecting the device you want to talk to. There is a fifth cable that it is important in every communication and that is GND.

Probably you are wondering what do MOSI, MISO and SCK mean, right? Well those are the name for the pins on SPI communication and their names came from:

MOSI: Master Out Slave In

MISO: Master In Slave Out

SCLK: Signal Clock

SS: Slave Select

Let´s say you want to have two (2) devices on the SPI communication, so you’ll need six cables, three are common for the two devices and two separated SS for each device and finally GND.

 

Devices

 

These are the components I used to do the schematics:

1 x Arduino board

1 x AD5206 SPI Potentiometer

1 x SPI debugger

This is how it looks the schematic and how should everything be connected:

 

Schematics

 

After everything was connected I wrote a code to change each potentiometer on the AD5206 to output a different value given by the microcontroller. Here you see that I wrote the code on the “Source code” window.

 

code

The code I used was this:

/*
////////////////////////////////////////////////////////////////////////////////////////////////////
AUTOR: JUAN BIONDI FECHA: FEBRERO/2014
PROGRAMA: Potentiometer SPI VERSION: 1.0
DISPOSITIVO: ATMEL 328 COMPILADOR: AVR
ENTORNO: PROTEUS SIMULADOR: VSM
TARJETA DE PROGRAMACION: DEBUGGER:
////////////////////////////////////////////////////////////////////////////////////////////////////

Control an Analog Devices AD5206 digital potentiometer.
The AD5206 has 6 potentiometer channels. Each channel's pins are labeled
A - connect this to voltage
W - this is the pot's wiper, which changes when you set it
B - connect this to ground.

The AD5206 is SPI-compatible,and to command it, you send two bytes,
one with the channel number (0 - 5) and one with the resistance value for the
channel (0 - 255).

The circuit:
* All A pins of AD5206 connected to +5V
* All B pins of AD5206 connected to ground
* An LED and a 220-ohm resisor in series connected from each W pin to ground
* CS - to digital pin 10 (SS pin)
* SDI - to digital pin 11 (MOSI pin)
* CLK - to digital pin 13 (SCK pin)



////////////////////////////////////////////////////////////////////////////////////////////////////
*/


////////////////////////////////////////////////////////////////////////////////////////////////////
// LIBRERIAS //
////////////////////////////////////////////////////////////////////////////////////////////////////

#include <SPI.h>

////////////////////////////////////////////////////////////////////////////////////////////////////
// VARIABLES GLOBALES //
////////////////////////////////////////////////////////////////////////////////////////////////////

// set pin 10 as the slave select for the digital pot:
const int slaveSelectPin = 10;

////////////////////////////////////////////////////////////////////////////////////////////////////
// FUNCIONES //
////////////////////////////////////////////////////////////////////////////////////////////////////

void digitalPotWrite(int address, int value)
{
// take the SS pin low to select the chip:
digitalWrite(slaveSelectPin,LOW);


// send in the address and value via SPI:
SPI.transfer(address);
SPI.transfer(value);
digitalWrite(slaveSelectPin,HIGH); // take the SS pin high to de-select the chip:
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// CONFIGURACION //
////////////////////////////////////////////////////////////////////////////////////////////////////

void setup()
{
pinMode (slaveSelectPin, OUTPUT); // set the slaveSelectPin as an output:
SPI.begin(); // initialize SPI:

for (int canal = 0; canal < 6; canal++) // Initialize all potentiometers to 0
{
digitalPotWrite(canal, 0);
delay(10);
}

}


////////////////////////////////////////////////////////////////////////////////////////////////////
// PRINCIPAL //
// //
////////////////////////////////////////////////////////////////////////////////////////////////////
void loop()
{
int valor = random(0,255); // Select a random number from 0 to 255
int canal = random (0,5); // Select a random number from 0 to 5
digitalPotWrite(canal,valor); // Send the data to the potentiometer
delay(1500); //Delay to see the changes

}

As you can see in the code every section is described and it is pretty simple to make changes


Once the code and the schematic are done, you can click on the play button and it should appear the SPI debugger like in the next picture:


 


Code_running


 


You can download the file on the following link:


https://drive.google.com/folderview?id=0B7dtMeeMPK5rfnJCSVlRVTkxRzdSeHBMWDJ6THVPWmdHdm1kZ3NwNTYzSEJ5b0ZmdlFoMG8&usp=sharing


File name: SPI.zip


Version of Proteus 8.1

No comments:

Post a Comment