interfacing pic16f877a to spi eeprom problem

Discussion in 'Embedded Systems and Microcontrollers' started by walid el masry, Jul 23, 2010.

Thread Status:
Not open for further replies.
  1. walid el masry

    Thread Starter Active Member

    Mar 31, 2009
    132
    0
    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

    Code ( (Unknown Language)):
    1.  
    2. /*
    3. buffer : spi buffer
    4. data_in : data to be read from spi
    5. data_out : data to be written to spi
    6. address : eeprom(25AA010A) address pointer
    7. status_reg : eeprom(25AA010A) status register
    8. */
    9. unsigned short buffer,data_in,status_reg;
    10. //------------------------------------------------------------------------------
    11. //Read data from memory array beginning at selected address
    12. void READ(unsigned short address){
    13.   PORTB.f7=0;
    14.   Spi_Write(0b00000011);//'0000 x011' READ Instruction
    15.   Spi_Write(address);
    16.   data_in=Spi_Read(buffer);
    17.   PORTB.f7=1;
    18. }
    19. //------------------------------------------------------------------------------
    20. //Reset the write enable latch (disable write operations)
    21. void WRDI(){
    22.   Spi_Write(0b00000100);//'0000 x100' WRDI Instruction
    23. }
    24. //------------------------------------------------------------------------------
    25. //Set the write enable latch (enable write operations)
    26. void WRENL(){
    27.   Spi_Write(0b00000110);//'0000 x110' WREN Instruction
    28.   PORTB.f7=1;
    29.   PORTB.f7=0;
    30. }
    31. //------------------------------------------------------------------------------
    32. //Write data to memory array beginning at selected address
    33. void WRITE(unsigned short address, unsigned short data_out){
    34.   PORTB.f7=0;
    35.   WRENL();
    36.   Spi_Write(0b00000010);//'0000 x010' WRITE Instruction
    37.   Spi_Write(address);
    38.   Spi_Write(data_out);
    39.   WRDI();
    40.   PORTB.f7=1;
    41. }
    42. //------------------------------------------------------------------------------
    43. //Read STATUS register
    44. void RDSR(){
    45.  
    46. }
    47. //------------------------------------------------------------------------------
    48. //Write STATUS register
    49. void WRSR(){
    50.  
    51. }
    52. //------------------------------------------------------------------------------
    53. void inti(){
    54.   TRISB = 0x03;
    55.   PORTB = 0x80;
    56.   TRISD = 0x00;
    57.   PORTD = 0x00;
    58.   Spi_Init_Advanced(MASTER_OSC_DIV4,DATA_SAMPLE_MIDDLE,CLK_IDLE_LOW,LOW_2_HIGH);
    59. }
    60. //------------------------------------------------------------------------------
    61. void main(){
    62.   inti();
    63.   while(1){
    64.     if(PORTB.f0=1){
    65.       while(PORTB.f0=1){}
    66.       PORTD=0x00;
    67.       WRITE(0x00,0x04);
    68.     }
    69.     if(PORTB.f1=1){
    70.       while(PORTB.f1=1){}
    71.       READ(0x00);
    72.       PORTD=data_in;
    73.     }
    74.   }
    75. }
    76. //------------------------------------------------------------------------------
    77.  
    and the source and design here

    and finally the circuit

    [​IMG]
     
  2. tom66

    Senior Member

    May 9, 2009
    2,613
    214
    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...
     
  3. walid el masry

    Thread Starter Active Member

    Mar 31, 2009
    132
    0
    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
     
  4. walid el masry

    Thread Starter Active Member

    Mar 31, 2009
    132
    0
    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
     
  5. walid el masry

    Thread Starter Active Member

    Mar 31, 2009
    132
    0
    [​IMG][​IMG][​IMG]

    analysis of the 2nd button is from 3rd ss active till the end and the rest is for the 1st switch
     
  6. tom66

    Senior Member

    May 9, 2009
    2,613
    214
    If you preprogram the chip with an EEPROM programmer (the PICkit 2/3, again, has this function) can you read data off it?
     
  7. walid el masry

    Thread Starter Active Member

    Mar 31, 2009
    132
    0
    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
     
  8. mrinalini

    New Member

    Aug 28, 2012
    6
    0
    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 ???
     
  9. dalexdisuja

    New Member

    Jan 5, 2014
    3
    0
  10. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
  11. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    necroposting
     
  12. bertus

    Administrator

    Apr 5, 2008
    15,648
    2,347
    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.
     
Loading...
Thread Status:
Not open for further replies.