I have a question regarding eeproms and an Arduino (I am using Arduino Uno). I have the following eeprom: 93LC46B. From the datasheet I understand that in order to write the integers 1
and 2
to the first address I need to send the following bits:
1
: starting bit01
: write opcode000000
: first address00110001
:1
00110010
:2
The result is 1010000000011000100110010
I have the following code but I can't seem to find out how to send the bits, can anyone help?
#include "SPI.h"
int ss=10;
void setup()
{
pinMode(ss, OUTPUT);
digitalWrite(ss, LOW);
SPI.begin();
SPI.setBitOrder(MSBFIRST);
digitalWrite(ss, HIGH);
//what to send?
digitalWrite(ss, LOW);
}
So I updated my code but I still can't get a desired output. This is the code:
#define DATAOUT 11//MOSI
#define DATAIN 12//MISO
#define SPICLOCK 13//sck
#define SLAVESELECT 10//ss
//opcodes
#define READ 0x80
#define WRITE 0x40
byte eeprom_output_data;
byte clr;
void setup()
{
Serial.begin(9600);
pinMode(DATAOUT, OUTPUT);
pinMode(DATAIN, INPUT);
pinMode(SPICLOCK, OUTPUT);
pinMode(SLAVESELECT, OUTPUT);
digitalWrite(SLAVESELECT, LOW); //disable device
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);
clr=SPSR;
clr=SPDR;
delay(10);
digitalWrite(SLAVESELECT, HIGH);
digitalWrite(DATAOUT, HIGH);
digitalWrite(SPICLOCK, HIGH);
delay(1);
digitalWrite(SPICLOCK, LOW);
spi_transfer(WRITE); //01000000
spi_transfer(0x10);
spi_transfer(0x11);
digitalWrite(SLAVESELECT,LOW); //release chip
delay(3000);
eeprom_output_data = read_eeprom();
Serial.print(eeprom_output_data,DEC);
Serial.print('\n');
delay(500); //pause for readability
}
char spi_transfer(volatile char data)
{
SPDR = data; // Start the transmission
while (!(SPSR & (1<<SPIF))) // Wait the end of the transmission
{
};
return SPDR; // return the received byte
}
byte read_eeprom()
{
//READ EEPROM
int data;
digitalWrite(SLAVESELECT, HIGH);
digitalWrite(DATAOUT, HIGH);
digitalWrite(SPICLOCK, HIGH);
delay(1);
digitalWrite(SPICLOCK, LOW);
spi_transfer(READ); //transmit read opcode
data = spi_transfer(0xFF); //get data byte
digitalWrite(SLAVESELECT, LOW); //release chip, signal end transfer
return data;
}
void loop()
{
}