93c66 MICROWIRE EEPROM Secuential read

This code does not use Arduino libraries, and connections are made by normal pin input / output I / O. The main function focuses on reading the 512 memory locations containing the model 93C66 EEPROM without autoincrement address. Therefore, whenever you increase the address to read, we made a start condition. In some chips with 93c66A, 93c66B, termination etc., have the potential to address Autoincrement so no need to put the CS pin to zero for a new condition inico every time we read the chip. In my case, this program is for chip 93C66 without words at the end, but can be applied to any chip series.
The initial position is the 0 and the maximum reading is 511 (512 bits in 8-bit mode). This chip has made the GND pin 6 so that it is configured to make the data of 8 bits.
EWEN not send EWDS necessary nor by what is after the start condition (startBit ()) waiting to introduce the code reading is 10 (2 bits) I put in a variable called OPcode_Lectura = B00000010;
Job description:
Bit void start () does not receive data nor return anything. This puts CS 0 then 1 Minimum timed and performs the combination of states of the CLK and DI pins for the three are one or high. This condition is called the SB (start bit).
void write (int data, byte count): This function receives an integer data and 8-bit data to use as a counter. When the data is the code for the operating mode of the chip (reading, writing, etc.) followed by the counter number which is the number of bits that we will send. In the present case, the data corresponds to code reading (February 1) and therefore must send 2 bits forming the code.
Internally memory sends the data starting with the MSB in this case is the bit that is in position 1 of the record “data”, given that the 0 position is the position of the LSB. Once this function, the (93C66) chip is waiting for the read address consisting of 9 bits. Thus, the data should now be 9 address bits and the counter must also be 9 bits.
void lectura (): This function read the available bit in the DO pin and writes the MSB of any variable. In my case I called Buffer variable. Screen presents the result of the reading when it is full.
lecturaSecuencial void (int scope): This function performs automatic address reading increasing Variable up to 512 bytes. Is programmable in the sense that the entire value needed as a parameter, is used to tell the function how many bytes you want to read with the maximum 512 bytes. If your meroria has fewer bytes, specify here will go.
General operation
The program starts basically sent the start condition, followed by the code reading mode. Here in this mode has passed the contents of the variable OPcode_Lectura is 2 and the number of bits sent will be 2 also.
The program continues with a slight delay ded 1 ms (not necessary but I put on stability), and then send the address to your accountant to 9 bits for shipping. At this point we have the list memory to read in the initial position that determines the variable “direction”. I put it first, I mean 0.
The last is secuential proccess. Is easy…

const int CS=5;
const int DI=3;
const int DO=2;
const int CLK=4;
const byte OPcode_Lectura=B00000010;
byte Buffer;
int direccion=10;

void setup()
{
  pinMode(CS, OUTPUT);//Pin CS como salida
  pinMode(DI, OUTPUT);
  pinMode(CLK, OUTPUT);
  pinMode(DO, INPUT);// Pin DO como entrada
  Serial.begin(9600);//Velocidad de conexion terminal
  //condicion de inicio
  digitalWrite(CS, LOW);//Desconectamos la memoria
  digitalWrite(DI, LOW);//Ponemos este pin a cero
  digitalWrite(CLK, LOW);// Reloj a cero
  delayMicroseconds(1);
   }

void startBit()
{
  digitalWrite(CS, LOW);//condicion de inicio
    delayMicroseconds(1);
    digitalWrite(CLK, LOW);
    digitalWrite(DI, LOW);
    digitalWrite(CS, HIGH);
    delayMicroseconds(1);
    digitalWrite(DI, HIGH);
    delayMicroseconds(1);
    digitalWrite(CLK, HIGH);//Flanco de subida de reloj
    delayMicroseconds(1);//Se ha escrito el bit SB
    digitalWrite(CLK,LOW);
    delayMicroseconds(1);
    
}
void escribe(int dato,byte contador)
  {
    //Serial.print("Dato Recibido: ");
    //Serial.println(dato);
    while(contador>0)//repite el ciclo tantas veces
    {
    delayMicroseconds(1);
    digitalWrite(DI, bitRead(dato,(contador-1)));
    //Posicionamos el bit a leer de dato y lo copiamos a DI
    delayMicroseconds(1);
    digitalWrite(CLK, HIGH);
    delayMicroseconds(1);
    digitalWrite(CLK,LOW);
    delayMicroseconds(1);
    contador--;//necesario para el ciclo se repita
    }
   
  }

void leeMemoria()//solo para 8 bits de datos
{
    Buffer=0;//limpia el Buffer
    byte n=8;//contador para escribir bit a bit en Buffer.8 bits
    while(n>0)
    {
    digitalWrite(CLK, HIGH);//un impulso completo
    delayMicroseconds(1);
    digitalWrite(CLK, LOW);
    delayMicroseconds(1);
    bitWrite(Buffer,n-1,digitalRead(DO));//copia el bit leido a Buffer
    delayMicroseconds(1);
    n--;
    }
    Serial.print("Dato ledio en Buffer completo: ");
    Serial.println(Buffer);
    delayMicroseconds(1);
    
}

void lecturaSecuencial(int alcance)
{
  
  while(alcance>0 && direccion<512)
  {
  
  digitalWrite(CLK, LOW);
  digitalWrite(DI, LOW);
  //Serial.println("Primer paso envio del op");
  startBit();
  escribe(OPcode_Lectura,2);//mandamos el codigo op que es de 2 bits
  delay(1);//1ms de retraso
  //Serial.println("Segundo paso: envio de direccion");
  escribe(direccion,9);//9 bits de direccion
  direccion++;
  //Serial.println("Tercer paso: comprobando si DO es cero");
  while(digitalRead(DO)!=0)//comprueba si DO es 0.
  {
    Serial.println("Esperando dato sea cero");
   delay(1);
   } 
  leeMemoria();
  Serial.print("Posicion leida ");
  Serial.println(direccion);
  alcance--;
  }
  Serial.println("Fin de la lectura secuancial");
  direccion=0;
  }

void loop()
{
  digitalWrite(CLK, LOW);
  digitalWrite(DI, LOW);
  //Serial.println("Primer paso envio del op");
  startBit();
  escribe(OPcode_Lectura,2);//mandamos el codigo op que es de 2 bits
  delay(1);//1ms de retraso
  //Serial.println("Segundo paso: envio de direccion");
  escribe(direccion,9);//9 bits de direccion
  //Serial.println("Tercer paso: comprobando si DO es cero");
  while(digitalRead(DO)!=0)//comprueba si DO es 0.
  {
    Serial.println("Esperando dato sea cero");
   delayMicroseconds(1);
   } 
  //Serial.println("Cuarto paso: Leemos la memoria.");
  leeMemoria();
  digitalWrite(CS, LOW);//desactiva el chip
  delayMicroseconds(1);
  digitalWrite(CS, HIGH);//activa el chip
  delayMicroseconds(1);
  Serial.println("Ahora hacemos una lectura secuncial n bytes");
  lecturaSecuencial(512);//leemos 511 posiciones de memoria consecutivas
  digitalWrite(CS, LOW);
  delayMicroseconds(1); 
}