CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Who can help me SPI EEPROM on PIC24 series
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
sindyme



Joined: 05 Aug 2009
Posts: 37

View user's profile Send private message MSN Messenger

PostPosted: Fri Feb 05, 2010 11:52 pm     Reply with quote

OK ~ I will test the code on next week.
Today and tomorrow is sunday.
sindyme



Joined: 05 Aug 2009
Posts: 37

View user's profile Send private message MSN Messenger

PostPosted: Fri Feb 05, 2010 11:54 pm     Reply with quote

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

View user's profile Send private message MSN Messenger

PostPosted: Sun Feb 07, 2010 8:49 pm     Reply with quote

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 Sad


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

View user's profile Send private message MSN Messenger

PostPosted: Sun Feb 07, 2010 10:20 pm     Reply with quote

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 Shocked


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;
      }     
     
   
   }

}
Shocked
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Mon Feb 08, 2010 12:53 am     Reply with quote

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

View user's profile Send private message MSN Messenger

PostPosted: Mon Feb 08, 2010 2:39 am     Reply with quote

This discussion might end.
I solved all problems.
Thanks "Fvm & everybody help ~
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
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