rezaf
Advanced Member level 4
Hi,
I have problem for write and read to/from 25LC256 SPI EEPROM with PIC MCU. Please review my code for mistakes(the crystal oscillator of PIC is 25MHz).
the result in Serialcom software(RS232 port) : (the status register is always correct but the read data is always 2 instead of 196)
TS1 : 0
TS2 : 2
TS3 : 2
buf :
2
TS4 : 2
TS5 : 0
Thanks,
Best Regards.
I have problem for write and read to/from 25LC256 SPI EEPROM with PIC MCU. Please review my code for mistakes(the crystal oscillator of PIC is 25MHz).
Code:
SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
Delay_ms(100);
do{
buffer1 = 0;
do //test status
{
CS = 0; //Activate CS
Delay_ms(200);
SPI1_Write(COMM_RDSR); //RDSR
TEST_result1 = SPI1_Read(buffer);
ShortToStr(TEST_result1, TEST_result2);
UART1_Write_Text("TS1 : ");
UART1_Write_Text(TEST_result2);
UART1_Write_Text("\n");
if(TEST_result1 == RDSR_idle)
break;
CS = 1;
Delay_ms(200);
}while(1);
CS = 0;
Delay_ms(200);
SPI1_Write(COMM_WREN); //WREN
CS = 1;
Delay_ms(200);
CS = 0;
Delay_ms(200);
SPI1_Write(COMM_WRSR); //WRSR
SPI1_Write(RDSR_idle); //unprotecting
CS = 1;
Delay_ms(200);
CS = 0;
Delay_ms(200);
SPI1_Write(COMM_WREN); //WREN
CS = 1;
Delay_ms(200);
do //test status
{
CS = 0; //Activate CS
Delay_ms(200);
SPI1_Write(COMM_RDSR); //RDSR
TEST_result1 = SPI1_Read(buffer);
ShortToStr(TEST_result1, TEST_result2);
UART1_Write_Text("TS2 : ");
UART1_Write_Text(TEST_result2);
UART1_Write_Text("\n");
if(TEST_result1 == RDSR_ready)
break;
CS = 1;
Delay_ms(400);
}while(1);
CS = 0;
Delay_ms(200);
SPI1_Write(COMM_WRITE); //WRITE
SPI1_Write(ipn1MSB); //ADDRESS MSB
SPI1_Write(ipn1LSB); // LSB
SPI1_Write(0b11000100); //Data
CS = 1; //Deactivate CS
Delay_ms(2000);
do //test status
{
CS = 0; //Activate CS
Delay_ms(200);
SPI1_Write(COMM_RDSR); //RDSR
TEST_result1 = SPI1_Read(buffer);
ShortToStr(TEST_result1, TEST_result2);
UART1_Write_Text("TS3 : ");
UART1_Write_Text(TEST_result2);
UART1_Write_Text("\n");
if(TEST_result1 == RDSR_ready) break;
CS = 1;
Delay_ms(200);
}while(1);
CS = 0;
Delay_ms(200); //Activate CS
SPI1_Write(COMM_READ); //READ
SPI1_Write(ipn1MSB); //ADDRESS MSB
SPI1_Write(ipn1LSB); // LSB
buffer1 = SPI1_Read(buffer); //Data
CS = 1; //Deactivate CS
Delay_ms(200);
UART1_Write_Text("buf : \n");
IntToStr(buffer1, buffer2);
UART1_Write_Text(buffer2);
UART1_Write_Text("\n");
CS = 0;
Delay_ms(200);
SPI1_Write(COMM_WREN); //WREN
CS = 1;
Delay_ms(200);
do //test status
{
CS = 0; //Activate CS
Delay_ms(200);
SPI1_Write(COMM_RDSR); //RDSR
TEST_result1 = SPI1_Read(buffer);
ShortToStr(TEST_result1, TEST_result2);
UART1_Write_Text("TS4 : ");
UART1_Write_Text(TEST_result2);
UART1_Write_Text("\n");
if(TEST_result1 == RDSR_ready) break;
CS = 1;
Delay_ms(200);
}while(1);
CS = 0;
Delay_ms(200);
SPI1_Write(COMM_WRDI); //WRDI
CS = 1;
Delay_ms(200);
CS = 0;
Delay_ms(200);
SPI1_Write(COMM_WRDI); //WRDI
CS = 1;
Delay_ms(200);
do //test status
{
CS = 0; //Activate CS
Delay_ms(200);
SPI1_Write(COMM_RDSR); //RDSR
TEST_result1 = SPI1_Read(buffer);
ShortToStr(TEST_result1, TEST_result2);
UART1_Write_Text("TS5 : ");
UART1_Write_Text(TEST_result2);
UART1_Write_Text("\n");
if(TEST_result1 == RDSR_idle) break;
CS = 1;
Delay_ms(200);
}while(1);
UART1_Write_Text("\n");
the result in Serialcom software(RS232 port) : (the status register is always correct but the read data is always 2 instead of 196)
TS1 : 0
TS2 : 2
TS3 : 2
buf :
2
TS4 : 2
TS5 : 0
Thanks,
Best Regards.