View previous topic :: View next topic |
Author |
Message |
sindyme
Joined: 05 Aug 2009 Posts: 37
|
|
Posted: Fri Feb 05, 2010 11:52 pm |
|
|
OK ~ I will test the code on next week.
Today and tomorrow is sunday. |
|
 |
sindyme
Joined: 05 Aug 2009 Posts: 37
|
|
Posted: Fri Feb 05, 2010 11:54 pm |
|
|
Code: | SPI_nCS = 0;
spi_read(0x06); // WREN
SPI_nCS = 1;
SPI_nCS = 0;
spi_read(0xd8); // Sector erase
spi_read(eeaddr.b[2]);
spi_read(0);
spi_read(0);
SPI_nCS = 1; |
OK ~ I will test the code next week.
Today and tomorrow is sunday. |
|
 |
sindyme
Joined: 05 Aug 2009 Posts: 37
|
|
Posted: Sun Feb 07, 2010 8:49 pm |
|
|
Update Msg :
would you teach me that how can i set these ?
//spi_read(eeaddr.b[2]);
//erase_tp = spi_read(0);
//erase_tp = spi_read(0);
I do not understand
Code: | SPI_nCS = 0;
spi_read(0x06); // WREN
SPI_nCS = 1;
SPI_nCS = 0;
spi_read(0xd8); // Sector erase
spi_read(eeaddr.b[2]);
spi_read(0);
spi_read(0);
SPI_nCS = 1;
|
Good morning FVM & everybody.
Excuse me, how can I include your code ?
I don't understand how I should write this code.
Would you teach me how can I write it.
I have tried these.... but I am indefinite.
Code: |
#define SPI_CLK_DIV_2 0x001B
#define SPI_CKE 0x0100
#define SPI_CKP 0x0040
#define SPI_MODE_0 (SPI_CKE)
#define SPI_MODE_1 (0)
#define SPI_MODE_2 (SPI_CKP | SPI_CKE)
#define SPI_MODE_3 (SPI_CKP)
#pin_select SDI1=PIN_B4
#pin_select SDO1=PIN_B5
#pin_select SCK1OUT=PIN_B8
#define EEPROM_SELECT PIN_B9
int1 eeprom_Sector_erase(void)
{
char erase_tp;
output_low(EEPROM_SELECT); //SPI_nCS = 0;
spi_read(0x06); // WREN
output_high(EEPROM_SELECT); //SPI_nCS = 1;
output_low(EEPROM_SELECT); //SPI_nCS = 0;
spi_read(0xd8); // Sector erase
//spi_read(eeaddr.b[2]);
//erase_tp = spi_read(0);
//erase_tp = spi_read(0);
output_high(EEPROM_SELECT); //SPI_nCS = 1;
return(!bit_test(erase_tp, 0));
}
//-------------------------------------------------------------------
int1 ext_eeprom_ready(void)
{
char spi_data;
output_low(EEPROM_SELECT);
spi_write(0x05);
spi_data = spi_read(0);
output_high(EEPROM_SELECT);
return(!bit_test(spi_data, 0));
}
//-------------------------------------------------------------------
void write_ext_eeprom(int32 address, char spi_data)
{
while(!ext_eeprom_ready());
output_low(EEPROM_SELECT);
spi_write(0x06);
output_high(EEPROM_SELECT);
output_low(EEPROM_SELECT);
spi_write(0x02);
spi_write(address >> 8);
spi_write(address >> 8);
spi_write(address);
spi_write(spi_data);
output_high(EEPROM_SELECT);
}
//-------------------------------------------------------------------
int32 read_ext_eeprom(int32 address)
{
char spi_data;
while(!eeprom_Sector_erase());
output_low(EEPROM_SELECT);
spi_write(0x03);
spi_write(address >> 8);
spi_write(address >> 8);
spi_write(address);
spi_data = spi_read(0);
output_high(EEPROM_SELECT);
return(spi_data);
}
//-------------------------------------------------------------------
|
|
|
 |
sindyme
Joined: 05 Aug 2009 Posts: 37
|
|
Posted: Sun Feb 07, 2010 10:20 pm |
|
|
Hello Fvm
I edit my code that all spi_write change to spi_read().
It already might work.
But have a wrong.
EX : I send all ASCII to test, I find the "ASCII of "H"、"I" & "J" clock is wrong !! "H"、"I" & "J" become --> "@" "A" "B" .............
what's happend
Code: |
#define SPI_CLK_DIV_2 0x001B
#define SPI_CKE 0x0100
#define SPI_CKP 0x0040
#define SPI_MODE_0 (SPI_CKE)
#define SPI_MODE_1 (0)
#define SPI_MODE_2 (SPI_CKP | SPI_CKE)
#define SPI_MODE_3 (SPI_CKP)
#pin_select SDI1=PIN_B4
#pin_select SDO1=PIN_B5
#pin_select SCK1OUT=PIN_B8
#define EEPROM_SELECT PIN_B9
char TESTTB[50]="1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//HIJ will become @AB .... :shock: :shock:
//-------------------------------------------------------------------
int1 ext_eeprom_ready(void)
{
char spi_data;
output_low(EEPROM_SELECT);
spi_read(0x05);
spi_data = spi_read(0);
output_high(EEPROM_SELECT);
return(!bit_test(spi_data, 0));
}
//-------------------------------------------------------------------
void write_ext_eeprom(int32 address, char spi_data)
{
while(!ext_eeprom_ready());
output_low(EEPROM_SELECT);
spi_read(0x06);
output_high(EEPROM_SELECT);
output_low(EEPROM_SELECT);
spi_read(0x02);
spi_read(address >> 8);
spi_read(address >> 8);
spi_read(address);
spi_read(spi_data);
output_high(EEPROM_SELECT);
}
//-------------------------------------------------------------------
int32 read_ext_eeprom(int32 address)
{
char spi_data;
while(!ext_eeprom_ready());
output_low(EEPROM_SELECT);
spi_read(0x03);
spi_read(address >> 8);
spi_read(address >> 8);
spi_read(address);
spi_data = spi_read(0);
output_high(EEPROM_SELECT);
return(spi_data);
}
//-------------------------------------------------------------------
void main()
{
setup_spi(SPI_MASTER | SPI_MODE_3 |SPI_CLK_DIV_64);
setup_spi2(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer1(TMR_DISABLED|TMR_DIV_BY_1);
enable_interrupts(INT_RDA);
enable_interrupts(INT_RDA2);
output_high(EEPROM_SELECT);
output_high(pin_e4);
fprintf(coma,"coma working OK ... \r\n");
fprintf(comb,"comb working OK ... \r\n");
for(;;)
{
tesflag = input_state(pin_e4);
if(tesflag==1)
{
for(addr = 0; addr < 36 ; addr++)
{
write_ext_eeprom(addr,TESTTB[addr]);
}
for(addr=0; addr <36 ; addr++)
{
SPI_Temp = read_ext_eeprom(addr);
fprintf(comb,"%c",SPI_Temp);
}
delay_ms(1000);
tesflag = 0;
}
}
}
|  |
|
 |
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Mon Feb 08, 2010 12:53 am |
|
|
One error in your code is, that you don't wait for each write to finish. You must either check the EEPPROM status or
insert a delay. It's all in the EEPROM datasheet. |
|
 |
sindyme
Joined: 05 Aug 2009 Posts: 37
|
|
Posted: Mon Feb 08, 2010 2:39 am |
|
|
This discussion might end.
I solved all problems.
Thanks "Fvm & everybody help ~ |
|
 |
|