23K256 SRAM interfacing problem

General discussion on mikroC.
Post Reply
Author
Message
boletus
Posts: 11
Joined: 14 Apr 2010 09:26

23K256 SRAM interfacing problem

#1 Post by boletus » 28 Apr 2010 16:07

Hi to all,

I would like to interface a 18F252 with a 32K SRAM modul 23K256 via SPI. For level shifting on SCK, SI, CS I use the following voltage dividers:

PIC pin----3K3----to 23K256 pin---5K2----GND

For SO, I connected directly on the PIC's SDI pin. The VCC of the SRAM module is 3.3V and I connected the HOLD pin to VCC as the datasheet suggests.

I created a simple code skeleton to check this circuit. It writes #4 into the ram module, read from it and blink a LED x times depending on what has been read (4x in my case). It's not working, it blinks 200+ times, obviously wrong. I double checked the circuit, the code, the datasheets with no luck. I would be very happy if someone could help.

Here is the code:

Code: Select all

/*******************************************************************************
*******************************PIC18F252***************************************/


#define OFF 0
#define ON 1

#define CS0 PORTB.F6  //CS for 23K256


#define LEDR2 PORTB.F5 //LEDs
#define LEDG2 PORTB.F4

//23K256 commands

#define MEMORY_READ 0b00000011
#define MEMORY_WRITE 0b00000010
#define READ_STATUS 0b00000101
#define WRITE_STATUS 0b00000001



//------------------------------------------------------------------------------
void InitVariables();
void InitPorts ();
void ExecuteCommand (char, char, char);
//------------------------------------------------------------------------------

//   Variables
char pc[3],k;
int  i,j;
unsigned short Q, buffer;

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

void main () {

     InitVariables();
     InitPorts();
     Usart_Init(56000);

     Spi_Init_Advanced(_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
     
     CS0=OFF;
     Spi_Write(WRITE_STATUS);
     Spi_Write(0b01000001);  //sequential mode+HOLD bit 1
     
     Spi_Write(MEMORY_WRITE);
     Spi_Write(0);
     Spi_Write(0);

     Spi_Write(4);
     CS0=ON;
     
     //Stop for a while
     Delay_ms(1000);
     //Continue


     CS0=OFF;
     
     Spi_Write(MEMORY_READ);
     Spi_Write(0);
     Spi_Write(0);
     
     Q=Spi_Read(buffer);

     for (j=0;j<Q;j++)
     {
         LEDR2=ON;
         Delay_ms(200);
         LEDR2=OFF;
         Delay_ms(200);
     }

     CS0=ON;
     
     //Finished
     LEDG2=ON;
     Delay_ms(3000);
     LEDG2=OFF;

    
     for(;;){

              if (Usart_Data_Ready()){

                  if (k<3) {

                            pc[k]=Usart_Read();
                            k++;
                            }
                  if (k==3) {

                            k=0;
                            ExecuteCommand(pc[0],pc[1],pc[2]);
                            }
                  }//if


          }//for
}//main

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

void InitPorts (){

     TRISC=0;
     TRISB=0;

     LEDR2=OFF; //red
     LEDG2=OFF; //green

}//InitPorts

void InitVariables() {

     k=0;
     for (i=0;i<3;i++) pc[i]=0;

     Q=0;
     buffer=0;
    
}//InitVariables

void ExecuteCommand (char command, char byteH, char byteL)
{


     switch (command){


            case START_DAQ_STREAM:


                 //TODO

                 break;

            case STOP_DAQ_STREAM:

                 //TODO

                 break;

            case PUSH_BTN1:

                 break;

             case SET_VALUE0:


                  //TODO

                  break;

            default:
                 break;
    }//switch


}//ExecuteCommand

nikosxan
Posts: 6
Joined: 24 Nov 2007 14:20

Re: 23K256 SRAM interfacing problem

#2 Post by nikosxan » 02 Jun 2010 22:01

Hello

I've had similar problem with same device and 18F2525 (@ 32MHZ)

Solved it just 5 min before..............

I'm using routines below for write / read data and default SPI init procedure. Should work OK !!

void Write_Data(unsigned int address, unsigned short dat){
SramCS = 0; // Select SerialRAM
Spi_Write(2); // Write instruction
Spi_Write(address >> 8); // Sending 16 bits address
Spi_Write(address);
Spi_Write(dat); // Writing one byte of data
SramCS = 1; // Deselect SerialRAM
}


unsigned short Read_Data(unsigned int address){
char c;
SramCS = 0; // Select Serial RAM
Spi_Write(3); // Read instruction
Spi_Write(address >> 8); // Sending 16 bits address
Spi_Write(address);
c = Spi_Read(0); // Read one byte of data
SramCS = 1; // Deselect SerialRAM
return c;
}
NX[b][color=#0000FF][/color][/b]

nikosxan
Posts: 6
Joined: 24 Nov 2007 14:20

Re: 23K256 SRAM interfacing problem

#3 Post by nikosxan » 02 Jun 2010 22:06

For testing operation i used a simple loop :

char b,c;

for (b=1; b<20; b++) {
Write_Data(10+b, b);
c=Read_Data(10+b);
portb=c; // show data read
Delay_ms(300);
}

My advice : try to make a simple program JUST to test the above as main + Write / Read roytines ..... If this works OK copy paste code to your bigger program...

Luck

Nick
NX[b][color=#0000FF][/color][/b]

nikosxan
Posts: 6
Joined: 24 Nov 2007 14:20

Re: 23K256 SRAM interfacing problem

#4 Post by nikosxan » 02 Jun 2010 22:12

For H/W connections I wired HOLD pin to 3.3V......., CS to PIC pin for selection....

CS should be High and pulled low just before send an SPI command.....

Check microe code and pdfs for H/W setup from
http://www.mikroe.com/eng/products/view ... ram-board/
NX[b][color=#0000FF][/color][/b]

boletus
Posts: 11
Joined: 14 Apr 2010 09:26

Re: 23K256 SRAM interfacing problem

#5 Post by boletus » 10 Jun 2010 15:00

Hi nikosxan!

I left that project but now I'll try your solution. Much appreciated!

nikosxan
Posts: 6
Joined: 24 Nov 2007 14:20

Re: 23K256 SRAM interfacing problem

#6 Post by nikosxan » 10 Jun 2010 15:50

It works OK. I've used it succesfully in a project that stores sound samples in it and it works Flawlessly ........
NX[b][color=#0000FF][/color][/b]

Post Reply

Return to “mikroC General”