Archivo de la categoría: ARDUINO

SPI EXERCISE WITH ARDUINO

In this exercise I’m going to explain how to write an EEPROM memory  with an Arduino and then reading the memory with another Arduino which will be also connected to a LCD screen where we could see the content of the memory previusly written. This will happen when I press a button.

Seguir leyendo SPI EXERCISE WITH ARDUINO

Anuncios

Writting and reading an EEPROM memory via I2C bus with Arduino

In this exercise I will show how to write and read an EEPROM memory by I2C interface with an Arduino Uno and simulating with Proteus.

First I had to download the Arduino Uno library for Proteus from the Arduino’s web site (arduino.cc). Now you can start simulating Arduino with Proteus.

In Proteus I’ve built the next scheme with the 24LC256 EEPROM memory of Microchip, the Arduino Uno and a LCD display to see the data stored in the memory:

sq

 

Here is the source code I’ve compiled with the Arduino software to load in the microcontroller:

#include <LiquidCrystal.h>
#include <Wire.h>

LiquidCrystal lcd(6, 7, 8, 9, 10, 11);

void i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte *data, byte length )

{
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddresspage >> 8)); // MSB
Wire.write((int)(eeaddresspage & 0xFF)); // LSB
byte c;
for ( c = 0; c < length; c++)
Wire.write(data[c]);
Wire.endTransmission();
}

byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress )
{
byte rdata = 0xFF;
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(deviceaddress,1);
if (Wire.available()) rdata = Wire.read();
return rdata;
}

void setup()
{
char somedata[] = “Hola CIFPN1”; //cadena a escribir
Wire.begin(); //es obligatorio inicializar la conexion
Serial.begin(9600);
i2c_eeprom_write_page(0x50, 0, (byte *)somedata, sizeof(somedata)); //escribir la cadena al principio de la EEPROM; comentar esta linea para probar que la memoria es no volatil
delay(10); //pequeña pausa despues de escribir en la memoria
lcd.begin(16, 2);
lcd.print(somedata);
}
void loop()
{
int addr=0; //direccion a leer
byte b = i2c_eeprom_read_byte(0x50, 0); //acceso a la primera posicion de memoria
while (b!=0)
{

Serial.print((char)b); //enviar al ordenador
addr++; //siguiente direccion
b = i2c_eeprom_read_byte(0x50, addr); //acceso a posicion de memoria
}
Serial.println();
delay(500);
}

And there are some pics of the result of the simulation with Proteus.

First I show you the empty memory:

sim1

And then you can see the memory have loaded the data and those data are shown in the LCD and in the virtual terminal.

sim2

 

You can download all the files of this exercise clicking here:

https://mega.co.nz/#!zsICyC7Y!HekaH682jvVyeagJRkTSqHLSveKhMdabPSfDrVePfjo