In this exercise I am going to explain how use the protocol SPI to communicate two Arduinos.
This exercise consist in write an EEPROM memory with the first Arduino, and then read the same memory with the other Arduino and finally we can see in one LCD screen that is written into memory.
This communication between two Arduinos will be done with the SPI interface.
The EEPROM memory chosen is the 25AA020A of Microchip, with 2Kbit and SPI Bus Serial Interface.
At first, we have to design the scheme in Proteus:
Once we have the scheme, is time to program:
We have to program first the master Arduino which write in the memory:
///////////////////////////////////////////////////
#include <SPI.h>
#define MOSI 11
#define MISO 12
#define SCK 13
#define SS 10
#define PULSADOR 8
#define INT 9
char DATO[] = "JOSEMA";
int DIR_H = 0x00;
int DIR_L = 0x00;
int letra;
int estado_pulsador = LOW;
void escribir_eeprom()
{
digitalWrite(SS, HIGH); //disable device
delay(50);
digitalWrite(SS, LOW);
SPI.transfer(0x06); //write enable
digitalWrite(SS, HIGH);
delay(50);
digitalWrite(SS, LOW);
SPI.transfer(0x02); //write instruction
SPI.transfer(DIR_H); //send MSByte address first
SPI.transfer(DIR_L); //send LSByte address
for(letra>=0; letra<=6;letra++)
{
SPI.transfer(DATO[letra]); //write data byte
}
digitalWrite(SS, HIGH); //release chip
//wait for eeprom to finish writing
delay(500);
}
void setup()
{
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
pinMode(MISO, OUTPUT);
pinMode(MOSI, INPUT);
pinMode(SCK, OUTPUT);
pinMode(SS, OUTPUT);
pinMode(PULSADOR, INPUT);
pinMode(INT, OUTPUT);
digitalWrite(SS,HIGH);
}
void loop()
{
do
{
estado_pulsador = digitalRead(PULSADOR);
}
while(estado_pulsador == LOW);
escribir_eeprom();
digitalWrite(INT, LOW);
delay(50);
digitalWrite(INT, HIGH);
for(;;){}
}
///////////////////////////////////////////////////
And now we have to program the second Arduino that will read the memory and write the data into the LCD:
///////////////////////////////////////////////////
#include <SPI.h>
#include <LiquidCrystal.h>
#define DATAOUT 11//MOSI
#define DATAIN 12//MISO
#define SPICLOCK 13//sck
#define SS 10//slave select
#define RESET 9//RESET del ARD1
LiquidCrystal lcd(8, 7, 6, 5, 4, 3);
char DATO;
int letra = 0;
byte DIR_H = 0x00;
byte DIR_L = 0x01;
volatile byte READ = 0x03;
void read_eeprom()
{
digitalWrite(SS,LOW); //slave selection
SPI.transfer(READ); //transmit read opcode
}
void setup()
{
pinMode(RESET, OUTPUT);
pinMode(DATAOUT, OUTPUT);
pinMode(DATAIN, INPUT);
pinMode(SPICLOCK,OUTPUT);
pinMode(SS,OUTPUT);
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
SPI.setClockDivider(SPI_CLOCK_DIV4);
lcd.begin(16, 2);
lcd.print(" LECTURA:");
digitalWrite(RESET,HIGH);
digitalWrite(RESET,LOW); //resetea el ARD1
digitalWrite(SS,HIGH); //slave deselection
read_eeprom();
for(letra>0; letra<8; letra++)
{
DATO = SPI.transfer((DIR_L)&&(DIR_H));
lcd.setCursor(letra, 1);
lcd.print(DATO);
DIR_L++;
if(DIR_L = 0xFF)
{
DIR_H++;
DIR_L = 0x00;
}
if (letra<2)
{
lcd.scrollDisplayLeft();
}
}
digitalWrite(SS,HIGH); //slave deselection
SPI.end(); //disable SPI communication
}
void loop()
{
}
///////////////////////////////////////////////////
When we have all of this, is time to verify the programs and take the .elf code which we will use to simulate
in Proteus.
We have the .elf code and and now we only have to double click in the Arduino and copy.
When we loaded the two programs, now we have to run the simulation and see the results:
First run the simulation and see the LCD is ON but nothing happened...
When we push the button, the first Arduino write in the EEPROM memory.
And next, the second Arduino read it and write the data in the LCD screen.
No hay comentarios:
Publicar un comentario