sukhavsa
Newbie level 6
Hi All
I am trying to interface an serial SRAM 23k256 to PIC18f8722. I am using the MSSP2 module for this.
the output is weird. it only works if the input data is 0xff if i give any other data to be written it reads random values.for example id data_in = 0xf0, data_out was E3. Similarly other when i tried with different data the values read did not match.
I could not figure out what is going wrong. Any help is greatly appreciated.
Here is the code
#include "p18f8722.h"
#include "spi.h"
#include "delays.h"
unsigned char data_in;
unsigned char data_out;
#define SRAM_CS LATDbits.LATD3
/************* SPI commands for SRAM******************/
#define Read 0x03
#define Write 0x02
#define ReadSR 0x05
#define WriteSR 0x01
/********** DIFFERENT MODES OF SRAM *******************/
#define byte_mode 0x01
#define seq_mode 0x41 //LSB = 1 to diable HOLD
/*********Function prototypes *******************/
void main (void)
{
unsigned char mode_read,add_low, add_high;
//shifting the frequency to 32MHz
OSCCON = 0x70
while(!OSCCONbits.IOFS);
OSCTUNEbits.PLLEN = 1;
TRISDbits.TRISD3 = 0; // CS
TRISDbits.TRISD4 = 0; //SD02
TRISDbits.TRISD5 = 1; //SDI2
TRISDbits.TRISD6 = 0; //SCK2
SRAM_CS = 1; //ensuring that the SRAM is disabled
OpenSPI2(SPI_FOSC_4, MODE_00, SMPMID);
data_in = 0xff;
add_high = 0x50;
add_low = 0x20;
Write_SR(0x01);
mode_read = Read_SR();
data_out = 0;
WriteByte(add_high,add_low, data_in);
Delay1TCY();
data_out = ReadByte (add_high, add_low);
}
CloseSPI2();
}
void Write_SR(unsigned char mode)
{
SRAM_CS = 0; //enable SRAM
Delay1TCY();
var = putcSPI2(WriteSR); //send write status register command
var = putcSPI2(mode); // write the mode into status register
SRAM_CS = 1; //diable SRAM
}
unsigned char Read_SR (void)
{
SRAM_CS = 0; //enable SRAM
Delay1TCY();
var = putcSPI2(ReadSR);
// while(!PIR3bits.SSP2IF); //wait until the data is received - buffer is full
var = getcSPI2();
SRAM_CS = 1; //Disable SRAM
return(var);
}
void WriteByte (unsigned char addH, unsigned char addL, unsigned char data)
{
SRAM_CS = 0; //enable SRAM
Delay1TCY();
var = putcSPI2(Write); //sending write command
var = putcSPI2(addH); //sending high byte of address
var = putcSPI2(addL); //sending low byte of address
var = putcSPI2(data); //sending data byte
SRAM_CS = 1; //Disable SRAM
}
unsigned char ReadByte (unsigned char addH, unsigned char addL)
{
SRAM_CS = 0; //enable SRAM
Delay1TCY();
var = putcSPI2(Read); //sending write command
var = putcSPI2(addH); //sending high byte of address
var = putcSPI2(addL); //sending low byte of address
var = getcSPI2(); //reading data byte
SRAM_CS = 1; //Disable SRAM
return(var);
}
thanks
I am trying to interface an serial SRAM 23k256 to PIC18f8722. I am using the MSSP2 module for this.
the output is weird. it only works if the input data is 0xff if i give any other data to be written it reads random values.for example id data_in = 0xf0, data_out was E3. Similarly other when i tried with different data the values read did not match.
I could not figure out what is going wrong. Any help is greatly appreciated.
Here is the code
#include "p18f8722.h"
#include "spi.h"
#include "delays.h"
unsigned char data_in;
unsigned char data_out;
#define SRAM_CS LATDbits.LATD3
/************* SPI commands for SRAM******************/
#define Read 0x03
#define Write 0x02
#define ReadSR 0x05
#define WriteSR 0x01
/********** DIFFERENT MODES OF SRAM *******************/
#define byte_mode 0x01
#define seq_mode 0x41 //LSB = 1 to diable HOLD
/*********Function prototypes *******************/
void main (void)
{
unsigned char mode_read,add_low, add_high;
//shifting the frequency to 32MHz
OSCCON = 0x70
while(!OSCCONbits.IOFS);
OSCTUNEbits.PLLEN = 1;
TRISDbits.TRISD3 = 0; // CS
TRISDbits.TRISD4 = 0; //SD02
TRISDbits.TRISD5 = 1; //SDI2
TRISDbits.TRISD6 = 0; //SCK2
SRAM_CS = 1; //ensuring that the SRAM is disabled
OpenSPI2(SPI_FOSC_4, MODE_00, SMPMID);
data_in = 0xff;
add_high = 0x50;
add_low = 0x20;
Write_SR(0x01);
mode_read = Read_SR();
data_out = 0;
WriteByte(add_high,add_low, data_in);
Delay1TCY();
data_out = ReadByte (add_high, add_low);
}
CloseSPI2();
}
void Write_SR(unsigned char mode)
{
SRAM_CS = 0; //enable SRAM
Delay1TCY();
var = putcSPI2(WriteSR); //send write status register command
var = putcSPI2(mode); // write the mode into status register
SRAM_CS = 1; //diable SRAM
}
unsigned char Read_SR (void)
{
SRAM_CS = 0; //enable SRAM
Delay1TCY();
var = putcSPI2(ReadSR);
// while(!PIR3bits.SSP2IF); //wait until the data is received - buffer is full
var = getcSPI2();
SRAM_CS = 1; //Disable SRAM
return(var);
}
void WriteByte (unsigned char addH, unsigned char addL, unsigned char data)
{
SRAM_CS = 0; //enable SRAM
Delay1TCY();
var = putcSPI2(Write); //sending write command
var = putcSPI2(addH); //sending high byte of address
var = putcSPI2(addL); //sending low byte of address
var = putcSPI2(data); //sending data byte
SRAM_CS = 1; //Disable SRAM
}
unsigned char ReadByte (unsigned char addH, unsigned char addL)
{
SRAM_CS = 0; //enable SRAM
Delay1TCY();
var = putcSPI2(Read); //sending write command
var = putcSPI2(addH); //sending high byte of address
var = putcSPI2(addL); //sending low byte of address
var = getcSPI2(); //reading data byte
SRAM_CS = 1; //Disable SRAM
return(var);
}
thanks