View previous topic :: View next topic |
Author |
Message |
beaker404
Joined: 24 Jul 2012 Posts: 163
|
23LC1024 interface. |
Posted: Sun Apr 21, 2019 8:46 am |
|
|
Burned a good portion of my Easter weekend on this one.
Trying to evaluate the 23LC1024T for a larger project.
datasheet: http://ww1.microchip.com/downloads/en/DeviceDoc/20005142C.pdf
Found a thread showing basic byte write and read functions for the 23LC1024T.
here is my code as I am trying to run it.
Code: | #include <18F87K22.h>
#include <stdlib.h>
#FUSES HSH // UP TO 25MHZ.
#FUSES NOEBTRB
#FUSES NOEBTR
#FUSES NOWRTD
#FUSES NOWRTB
#FUSES NOPROTECT
#FUSES NOCPB
#FUSES NOCPD
#FUSES NOWRT
#FUSES NODEBUG
#FUSES NOSTVREN
#FUSES NOMCLR
#FUSES NOWAIT
#FUSES NOWDT
#FUSES NOIESO
#FUSES NOPUT
#FUSES NOBROWNOUT
#FUSES NOFCMEN
#FUSES NOPLLEN
#USE delay(clock=20000000)
#USE rs232(UART1,baud=9600, xmit=PIN_C6,rcv=PIN_C7,STREAM=SERIAL,errors)
#USE SPI(MASTER, MODE=0, BITS=8, SPI1, STREAM=SPI, FORCE_HW)
/*
*********************************************************************************************
* DEFINES
*********************************************************************************************
*/
#DEFINE SRAM_CS1 PIN_J2
#DEFINE SRAM_CS2 PIN_J3
#DEFINE SRAM_CS3 PIN_J6
#DEFINE EEPROM_CS PIN_B1
#DEFINE MODE_CMD 0x01
#DEFINE WRITE_CMD 0x02
#DEFINE READ_CMD 0x03
#DEFINE LED_BLINK PIN_J5
/*
*********************************************************************************************
* GLOBAL VARIABLES
*********************************************************************************************
*/
void init_hardware() {
// Initializes all hardware.
output_high(SRAM_CS1);
output_high(SRAM_CS2);
output_high(SRAM_CS3);
output_high(EEPROM_CS);
} // end init_hardware()
void SRAM_write_byte(unsigned int32 address, byte val)
{
// Write one byte to the 23LC1024 SRAM selected
int8 dummy;
output_low(SRAM_CS1); // test with chip 1
spi_xfer(SPI, WRITE_CMD, 8); // send write command
spi_xfer(SPI, address, 24); // send the address
dummy = spi_xfer(SPI, val, 8); // send byte and wait for it to send.
output_high(SRAM_CS1);
} // end write_byte()
byte SRAM_read_byte(unsigned int32 address)
{
// Read one byte from the 23LC1024 SRAM selected
int8 rval;
output_low(SRAM_CS1);
spi_xfer(SPI, READ_CMD, 8); // send read command
spi_xfer(SPI, address, 24); // send address
rval=(spi_xfer(SPI, 0, 8)); // clock dummy to get reply
output_high(SRAM_CS1);
return rval;
} // end read_byte()
void main() {
// one SRAM holds 1Mb of data.
int i;
byte rval = 0;
long RAM_location=0;
int LED_toggle=0;
output_low(LED_BLINK);
RAM_location = 0L;
SRAM_write_byte(RAM_location, 0x11);
if(LED_toggle == 0) {
output_high(LED_BLINK);
LED_toggle = 1;
}
else {
output_low(LED_BLINK);
LED_toggle = 0;
}
delay_ms(1000); // one second delay between write and read cycle
RAM_location = 0L;
rval = SRAM_read_byte(RAM_location);
fprintf(SERIAL,"** VALUE= %d#\n\r",rval);
if(LED_toggle == 0) {
output_high(LED_BLINK);
LED_toggle = 1;
}
else {
output_low(LED_BLINK);
LED_toggle = 0;
}
} // end main |
using a 18F87K22 at 20 MHz CCS version is 5.064 running on windows 10
when I run it, I get zeros back on the serial port.
Goal here is to get a test program running to check SPI speed of the SRAM to verify speed for a larger project. Having problems walking before I try to run. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19592
|
|
Posted: Sun Apr 21, 2019 9:03 am |
|
|
You are limiting the driver to 8bit mode in the setup, then sending 24bits...
Leave the SPI setup at the default 32bit setting.
Then your use of 'long' for RAM_location, is wrong. You need to explicitly
use an int32. By default on a PIC18, 'long' is an int16.
You need to send a write to the MODE register to select sequential mode.
Have you got the /HOLD pin pulled high?. |
|
|
beaker404
Joined: 24 Jul 2012 Posts: 163
|
|
Posted: Sun Apr 21, 2019 9:34 am |
|
|
took the 8 bit config out of the SPI setup.
changed RAM_location to INT32
My hold pin was floating. tied high (thought this was being done)
As for sequential mode. the chip wakes up in sequential mode. do you have to do anything explicitly to make sequential mode for sure happen?
still reading zeros |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Apr 21, 2019 9:55 am |
|
|
Read the Mode register. See what you get.
Also, the CCS driver for 23K256.c, puts the SRAM into byte mode first.
Try that. |
|
|
beaker404
Joined: 24 Jul 2012 Posts: 163
|
|
Posted: Sun Apr 21, 2019 10:17 am |
|
|
still reading zeros.
RDMR gives a zero as well,
quick code to read mode register is:
Code: | output_low(SRAM_CS1); // test with chip 1
spi_xfer(SPI, RDMR, 8); // send read mode register.
rval = spi_xfer(SPI, 0, 8);
output_high(SRAM_CS1);
fprintf(SERIAL,"** VALUE= %d#\n\r",rval); |
i have checked power supplies, ground and SPI wiring to the chip. CS is correct as well.
RDMR is defined as 0x05 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19592
|
|
Posted: Sun Apr 21, 2019 10:27 am |
|
|
I'd be double checking my connections at this point. Though you say you
have done this, 'directions'. SI on the chip has to go to SO on the PIC, and
SO on the chip to SI on the PIC. It's input, is the PIC's output, and vice versa. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Apr 21, 2019 10:28 am |
|
|
Check the connections between the PIC and the SRAM.
Post the pin number to pin number connections.
(Not the pin name, but the pin numbers on the PIC and the SRAM). |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19592
|
|
Posted: Sun Apr 21, 2019 10:41 am |
|
|
Also, as a minor comment, enable the PUT. This should always be used
with a crystal. There is a very real risk that a crystal will take several
mSec to stabilise, and without this your clock rate could be very wrong
at the start of the code.
Generally develop "don't assume anything" as a motto. Never assume a
pin is pulled up unless you do it, and don't trust registers to be initialised
as expected, till you read them to be sure.... |
|
|
beaker404
Joined: 24 Jul 2012 Posts: 163
|
|
Posted: Mon Apr 22, 2019 7:53 am |
|
|
Sorry for the delay in responding. Big problem caused by the fact that there was a small bridge between the DI and DO pins for the SPI. It works now. I will post a complete working code for the 18F87K22 and 23LC1024 partnership in the code section when I get the block write and read code going. Thanks for the help and the additional input on this topic. |
|
|
|