interfacing pic16f877a to spi eeprom problem

Status
Not open for further replies.

Thread Starter

walid el masry

Joined Mar 31, 2009
133
hi
i'm trying to interface PIC16F877A to SPI eeprom "25AA010A" from microchip and btw it's my 1st time to interface SPI and i'm using Mikroc in programming, i guess i understand the datasheet and the Mikroc SPI library help well but it seems that iam missing something
i wrote a code to write value in the address 0x00 when switch on RB0 pushed and released and read the address 0x00 then display it's content on LEDs on PORTD when i do the same for the switch on RB1 but the problem is that the value is not correct

here is the code i wrote

Rich (BB code):
/*
buffer : spi buffer
data_in : data to be read from spi
data_out : data to be written to spi
address : eeprom(25AA010A) address pointer
status_reg : eeprom(25AA010A) status register
*/
unsigned short buffer,data_in,status_reg;
//------------------------------------------------------------------------------
//Read data from memory array beginning at selected address
void READ(unsigned short address){
  PORTB.f7=0;
  Spi_Write(0b00000011);//'0000 x011' READ Instruction
  Spi_Write(address);
  data_in=Spi_Read(buffer);
  PORTB.f7=1;
}
//------------------------------------------------------------------------------
//Reset the write enable latch (disable write operations)
void WRDI(){
  Spi_Write(0b00000100);//'0000 x100' WRDI Instruction
}
//------------------------------------------------------------------------------
//Set the write enable latch (enable write operations)
void WRENL(){
  Spi_Write(0b00000110);//'0000 x110' WREN Instruction
  PORTB.f7=1;
  PORTB.f7=0;
}
//------------------------------------------------------------------------------
//Write data to memory array beginning at selected address
void WRITE(unsigned short address, unsigned short data_out){
  PORTB.f7=0;
  WRENL();
  Spi_Write(0b00000010);//'0000 x010' WRITE Instruction
  Spi_Write(address);
  Spi_Write(data_out);
  WRDI();
  PORTB.f7=1;
}
//------------------------------------------------------------------------------
//Read STATUS register
void RDSR(){

}
//------------------------------------------------------------------------------
//Write STATUS register
void WRSR(){

}
//------------------------------------------------------------------------------
void inti(){
  TRISB = 0x03;
  PORTB = 0x80;
  TRISD = 0x00;
  PORTD = 0x00;
  Spi_Init_Advanced(MASTER_OSC_DIV4,DATA_SAMPLE_MIDDLE,CLK_IDLE_LOW,LOW_2_HIGH);
}
//------------------------------------------------------------------------------
void main(){
  inti();
  while(1){
    if(PORTB.f0=1){
      while(PORTB.f0=1){}
      PORTD=0x00;
      WRITE(0x00,0x04);
    }
    if(PORTB.f1=1){
      while(PORTB.f1=1){}
      READ(0x00);
      PORTD=data_in;
    }
  }
}
//------------------------------------------------------------------------------
and the source and design here

and finally the circuit

 

tom66

Joined May 9, 2009
2,595
I'm not sure if you've made sure of this, but chip select (CS) is active low. For a test, disconnect the chip select from your PIC and ground it. (It must be grounded; do not leave it floating.)

If you have an oscilloscope, probe SDA/SCL. This is where dual channel oscilloscopes become useful. If you happen to use a PICkit 2 (and possible PICkit 3), it has a built in 3-channel logic analyser, which could be very helpful.

If you don't have an oscilloscope a logic probe is a cheaper alternative...
 

Thread Starter

walid el masry

Joined Mar 31, 2009
133
about the CS here is what i get fro the eeprom datasheet
- Read Sequence
1- CS low (select chip)
2- send READ instruction
3- send Address
4- read the data
5- CS high (terminate READ operation)

- Write Sequence
1- CS low (select chip)
2- send WREN instruction
3- CS high
4- CS low
5- send WRITE instruction
6- send Address
7- send Data
8- send WRDI instruction (terminate WRITE operation)
9- CS high (deselect the chip)

so CS must be connected to the PIC so that imitate and terminate the operations
about the oscilloscope i don't have one but i can use Proteus
 

Thread Starter

walid el masry

Joined Mar 31, 2009
133
i tried the SPI debugger in Proteus and it shows the data out from the pic correctly but the incoming from eeprom it captures nothing just and it shows "??" for the data
 

tom66

Joined May 9, 2009
2,595
If you preprogram the chip with an EEPROM programmer (the PICkit 2/3, again, has this function) can you read data off it?
 

Thread Starter

walid el masry

Joined Mar 31, 2009
133
i never reprogram the eeprom and note that iam working on the simulation not in real and Proteus have an option to give initial data for the eeprom which require data in binary file, about PICkit i never have any development kit for pic microcontroller i wrote he program and simulate it then build the cirrcuit, about the spi debugger in Proteus it shows thas the pic act exactly like i wrote in the code but the eeprom is not responding
 

mrinalini

Joined Aug 28, 2012
6
hi ,
i had done interfacing with ur help but i have a little doubt please help me with that what are buffer,data_in ,data_out,adress in your program ...explain me little bit...i knew
data_in : data to be read from spi
data_out : data to be written to spi
address : eeprom(25AA010A) address pointer
what is buffer ???
 

bertus

Joined Apr 5, 2008
22,277
Hello,

The OP's last activity was in dec 2012.
I will close this thread now.

Bertus

PS if the OP wants the tread opened again, just PM a moderator.
 
Status
Not open for further replies.
Top