23K256 SRAM interfacing problem
Posted: 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:
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