I have a smart card here and i can only read and write one "sector" like only the first "slot" in the i2c memory card and i can fill it with stuff like AB but that is it.
How can i get this software to allow to me write to all sectors?
#include <Wire.h>
void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data ) {
int rdata = data;
Wire.beginTransmission(deviceaddress);
Wire.send((int)(eeaddress >> 8)); // MSB
Wire.send((int)(eeaddress & 0xFF)); // LSB
Wire.send(rdata);
Wire.endTransmission();
}
// WARNING: address is a page address, 6-bit end will wrap around
// also, data can be maximum of about 30 bytes, because the Wire library has a buffer of 32 bytes
void i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length ) {
Wire.beginTransmission(deviceaddress);
Wire.send((int)(eeaddresspage >> 8)); // MSB
Wire.send((int)(eeaddresspage & 0xFF)); // LSB
byte c;
for ( c = 0; c < length; c++)
Wire.send(data[c]);
Wire.endTransmission();
}
byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress ) {
byte rdata = 0xFF;
Wire.beginTransmission(deviceaddress);
Wire.send((int)(eeaddress >> 8)); // MSB
Wire.send((int)(eeaddress & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(deviceaddress,1);
if (Wire.available()) rdata = Wire.receive();
return rdata;
}
// maybe let's not read more than 30 or 32 bytes at a time!
void i2c_eeprom_read_buffer( int deviceaddress, unsigned int eeaddress, byte *buffer, int length ) {
Wire.beginTransmission(deviceaddress);
Wire.send((int)(eeaddress >> 8)); // MSB
Wire.send((int)(eeaddress & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(deviceaddress,length);
int c = 0;
for ( c = 0; c < length; c++ )
if (Wire.available()) buffer[c] = Wire.receive();
}
void setup()
{
Wire.begin();
Serial.begin(9600);
}
void loop()
{
i2c_eeprom_write_byte(0x50, 1, 0xaa);
Serial.println("waiting");
delay(1000);
byte temp = i2c_eeprom_read_byte(0x50, 1);
Serial.println(temp);
delay(2000);
}
Ive tried change the : i2c_eeprom_write_byte(0x50, 1, 0xaa); to : i2c_eeprom_write_byte(0x50, 2, 0xaa);
but it wont write to the second bit!! It writes well to the first but now the second or third. How can i go about to make it right beyond the 1st bit. Also was does the 0x50 represent?
Please anyone? I have it as a smart card, so i dont have access to A0 A1 A2 so i just put them as 0. Can anyone please help me write to this card in a way that i can track?
This code should work. Note that the adressing starts at 0, so when you use address 1 it already has accessed the 2nd byte; there is no reason it should not access the third one.
Problems with the wiring? Pullups?
0x50 is the (fixed) I2C address of the EEPROM.
Given your code above I would start to get bytes and ints sorted out correctly. "deviceaddress" should be a byte (it is not critical, but nevertheless). MSB and LSB are both bytes. You need to rewrite as follows:
Thanks ben, i have edited my code now. I have the out below. I am really stuck. Can you please have a look? Ive tried everything basically. Powering the card on and off between reading and writing, not power cycling etc...
Here is writing code:
#include <Wire.h>
void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data ) {
int rdata = data;
Wire.beginTransmission(deviceaddress);
Wire.send((byte)(eeaddress >> 8)); // MSB
Wire.send((byte)eeaddress); // LSB
Wire.send(rdata);
Wire.endTransmission();
}
// WARNING: address is a page address, 6-bit end will wrap around
// also, data can be maximum of about 30 bytes, because the Wire library has a buffer of 32 bytes
void i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length ) {
Wire.beginTransmission(deviceaddress);
Wire.send((byte)(eeaddresspage >> 8)); // MSB
Wire.send((byte)eeaddresspage); // LSB
byte c;
for ( c = 0; c < length; c++)
Wire.send(data[c]);
Wire.endTransmission();
}
byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress ) {
byte rdata = 0xFF;
Wire.beginTransmission(deviceaddress);
Wire.send((byte)(eeaddress >> 8)); // MSB
Wire.send((byte)eeaddress); // LSB
Wire.endTransmission();
Wire.requestFrom(deviceaddress,1);
if (Wire.available()) rdata = Wire.receive();
return rdata;
}
// maybe let's not read more than 30 or 32 bytes at a time!
void i2c_eeprom_read_buffer( int deviceaddress, unsigned int eeaddress, byte *buffer, int length ) {
Wire.beginTransmission(deviceaddress);
Wire.send((byte)(eeaddress >> 8)); // MSB
Wire.send((byte)eeaddress); // LSB
Wire.endTransmission();
Wire.requestFrom(deviceaddress,length);
int c = 0;
for ( c = 0; c < length; c++ )
if (Wire.available()) buffer[c] = Wire.receive();
}
void setup()
{
Wire.begin();
Serial.begin(9600);
}
int n = 0;
void loop()
{
for(int x = 0;x<256;x++){
i2c_eeprom_write_byte(0x50,x,0x2);
delay(50);
Serial.print("wrote: ");
Serial.println(x);
}
Serial.println("done1");
delay(10000);
}
Here is reading code:
#include <Wire.h>
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
}
void loop()
{
Wire.requestFrom(0x50,255);
int x = Wire.available();
Serial.println(Wire.available(),HEX);
delay(50);
Serial.println("Entering Loop for 2048 Bytes");
for(int i = 0; i<2048;i++){
byte rdata;
if (Wire.available()){
rdata = Wire.receive();
}else{
Serial.print("Breaking after : ");
Serial.print(a);
Serial.println(" Bytes");
break;
}
Serial.print("Byte(");
Serial.print(a);
Serial.print(") ");
Serial.print(i);
Serial.print(" : ");
Serial.println(rdata,HEX);
a++;
}
}
#include <Wire.h>
void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data ) {
int rdata = data;
Wire.beginTransmission(deviceaddress);
Wire.send((byte)(eeaddress >> 8)); // MSB
Wire.send((byte)eeaddress&255);
Wire.send(rdata);
Wire.endTransmission();
}
// WARNING: address is a page address, 6-bit end will wrap around
// also, data can be maximum of about 30 bytes, because the Wire library has a buffer of 32 bytes
void i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length ) {
Wire.beginTransmission(deviceaddress);
Wire.send((byte)(eeaddresspage >> 8)); // MSB
Wire.send((byte)eeaddresspage&255);
byte c;
for ( c = 0; c < length; c++)
Wire.send(data[c]);
Wire.endTransmission();
}
byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress ) {
byte rdata = 0xFF;
Wire.beginTransmission(deviceaddress);
Wire.send((byte)(eeaddress >> 8)); // MSB
Wire.send((byte)eeaddress&255); // LSB
Wire.endTransmission();
Wire.requestFrom(deviceaddress,1);
if (Wire.available()) rdata = Wire.receive();
return rdata;
}
// maybe let's not read more than 30 or 32 bytes at a time!
void i2c_eeprom_read_buffer( int deviceaddress, unsigned int eeaddress, byte *buffer, int length ) {
Wire.beginTransmission(deviceaddress);
Wire.send((byte)(eeaddress >> 8)); // MSB
Wire.send((byte)eeaddress&255); // LSB
Wire.endTransmission();
Wire.requestFrom(deviceaddress,length);
int c = 0;
for ( c = 0; c < length; c++ )
if (Wire.available()) buffer[c] = Wire.receive();
}
void setup()
{
Wire.begin();
Serial.begin(9600);
}
int n = 0;
void loop()
{
for(int x = 0;x<256;x++){
i2c_eeprom_write_byte(0x50,x,0x2);
delay(50);
Serial.print("wrote: ");
Serial.println(x);
}
Serial.println("done1");
delay(10000);
}