|
|
View previous topic :: View next topic |
Author |
Message |
microWip
Joined: 19 May 2007 Posts: 2
|
FRAM FM25L256 read/write problem |
Posted: Sat May 19, 2007 4:03 pm |
|
|
Hello
I'm using a PIC 18F6722 to read and write to a FRAM chip through SPI.
I use this code:
Code: |
// OP codes
#define WREN 0x06
#define WRSR 0x01
#define READ 0x03
#define WRITE 0x02
//variabels
int8 temp = 0x02;
void init_fram() {
setup_spi(SPI_MASTER | SPI_XMIT_L_TO_H | SPI_CLK_DIV_4);
output_high(cs_fram);
delay_ms(11);
output_low(cs_fram);
spi_write(WREN);
output_high(cs_fram);
output_low(cs_fram);
spi_write(WRSR);
spi_write(0x80); // sets bank protect bits to 0, and enables external WP line
output_high(cs_fram);
}
void write_fram(int16 address, int8 data) {
output_low(cs_fram);
spi_write(WREN);
output_high(cs_fram);
output_low(cs_fram);
spi_write(WRITE);
spi_write(address>>8); //send the high int8 of the address
spi_write(address);
spi_write(0x33);
output_high(cs_fram);
delay_ms(11);
}
int8 read_fram(int16 address) {
int8 data;
output_low(cs_fram);
spi_write(READ);
spi_write(address>>8); //send the high int8 of the address
spi_write(address);
data=spi_read(0);
output_high(cs_fram);
return(data);
}
|
I write a byte to an address an try to read afterwards, but all I get is 0xFF. I thought that 0xFF is the default value of a memory entry so I presume my read code is correct.
Code: |
void main() {
init_fram();
while(true)
{
if(!input(knop1))
{
temp = read_fram(0x0001);
putC(temp);
delay_ms(500);
}
if(!input(knop2))
{
write_fram(0x0001, 0x22);
output_low(LED_ON);
delay_ms(500);
output_high(LED_ON);
}
}
}
|
Does anyone sees errors in my code? Please help...
Kinde regards |
|
|
Ttelmah Guest
|
|
Posted: Sun May 20, 2007 2:44 am |
|
|
First, I'd suggest using the SPI 'mode numbers', published here in the past. I think by Ckielstra?. These make it much simpler to setup the registers:
Code: |
#define SPI_MODE_0_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_0_1 (SPI_L_TO_H)
#define SPI_MODE_1_0 (SPI_H_TO_L)
#define SPI_MODE_1_1 (SPI_H_TO_L | SPI_XMIT_L_TO_H)
|
At the front of the data sheet, Ramtron say this chip supports the first, and last of these. However all the diagrams show it using the first. This should be what your current setup gives, but worth being sure.
Second comment. Are you running in standard IO mode, or fast IO mode?. If the latter, then I would suggest adding 1 clock cycle of delay on the strobe of the CS line (worth trying anyway). The PIC, samples the IO pin, at the start of the instruction, and updates it near the end of the instruction. Hence a pair of writes, one after the other, with no delay between can (depending on bus capacitance), result in a pin that doesn't update...
I presume WP, is wired high?. You make no mention of how it is wired?.
You don't need the delay after the write to the fram. Writes on these chips are basically instantaneous.
Is the 'hold' pin also wired high?.
Best Wishes |
|
|
microWip
Joined: 19 May 2007 Posts: 2
|
|
Posted: Sun May 20, 2007 3:16 am |
|
|
Thank you very much !!!
I followed al your suggestions and it worked:
I added in the inititalisation:
Code: | setup_spi(SPI_MASTER | SPI_MODE_1_1 | SPI_CLK_DIV_4);
output_high(hold_fram); //chip select
output_high(wp_fram); |
In the datasheet is mentioned dat the clocking of data in is on the falling clock edge, clocking data out on the rising clock edge, so I use SPI_MODE_1_1
Between chip select toggles:
Code: | output_high(cs_fram);
delay_cycles(1);
output_low(cs_fram); |
This changes made everything work
One last question: you talk about fast I/O mode...were can I see that I'm using that mode?
kind regards |
|
|
Ttelmah Guest
|
|
Posted: Sun May 20, 2007 4:21 am |
|
|
If you are not setting it, you are not using it.
The setup, would be:
#use fast_io(A)
and similar lines for the other ports, in the 'header' for your program, and then you would need to add a TRIS statement, setting the pins as inputs/outputs.
Basically, without this, an instruction like:
output_high(PIN_A0);
gets translated into setting the TRIS bit for this pin to 0, and then outputting the bit. Similarly, an input instruction, changes the tris to '1', and reads the bit. Hence every single I/O instruction, gets an extra delay added, while the TRIS is updated (even if it is not changing...).
Using fast_io, it becomes your responsibility to setup the tris, but unless you are using changes in I/O direction on a pin, this can be done just once, speeding up every I/O operation.
Best Wishes |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|