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 CCS Technical Support

EEPROM SPI problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
lyke



Joined: 03 Aug 2006
Posts: 4

View user's profile Send private message

EEPROM SPI problem
PostPosted: Thu Aug 03, 2006 9:01 am     Reply with quote

I've been having trouble communicating between a PIC 18F2455 and a 25AA256 EEPROM. There is no file in the drivers folder for the 25AA256 so I attempted to edit a similar file so it would work with my eeprom. Unfortunately I have not had any luck. Here is my code:
Code:

#include<18f2455.h>

#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use rs232 (baud=9600,xmit=PIN_C6,rcv=PIN_C7, bits=8)

#define EEPROM_SELECT PIN_B2
#define EEPROM_DI     PIN_B0   
#define EEPROM_DO     PIN_C7
#define EEPROM_CLK    PIN_B1

#define EEPROM_ADDRESS BYTE
#define EEPROM_SIZE    256

void init_ext_eeprom();
BOOLEAN ext_eeprom_ready();
void write_ext_eeprom(EEPROM_ADDRESS address, BYTE data);
BYTE read_ext_eeprom(EEPROM_ADDRESS address);

void main() {
      
   byte value;
   int i=0;

   init_ext_eeprom();
   delay_ms(15);
   write_ext_eeprom(0,7);
   delay_ms(15);

   
   while(1){
      
      output_toggle(PIN_B3);
      value=read_ext_eeprom(i++);
      delay_ms(15);
      printf("%X\r\n", value);
      delay_ms(50);
   
   }
}

void init_ext_eeprom() {
      
   output_high(EEPROM_SELECT);
   output_low(EEPROM_DI);
   output_low(EEPROM_CLK);
   //setup_spi(spi_master |spi_l_to_h |spi_clk_div_16 );
}

BOOLEAN ext_eeprom_ready() {
   BYTE cmd[1], i, data;

   cmd[0] = 0x05;                  //rdsr opcode

   output_low(EEPROM_SELECT);

   for(i=1; i<=8; ++i) {
      output_bit(EEPROM_DI, shift_left(cmd,1,0));
      output_high(EEPROM_CLK);   //data latches
      output_low(EEPROM_CLK);      //back to idle
   }

   for(i=1; i<=8; ++i) {
        output_high(EEPROM_CLK);   //data latches
        shift_left(&data,1,input(EEPROM_DO));
        output_low(EEPROM_CLK);  //back to idle
   }
   output_high(EEPROM_SELECT);
   return !bit_test(data, 0);
}

void write_ext_eeprom(EEPROM_ADDRESS address, BYTE data) {
   BYTE cmd[4];
   BYTE i;
   BYTE wren;
   wren=0x06;

   // Wait until the eeprom is done with a previous write
   while(!ext_eeprom_ready());

   for(i=0; i<8; ++i)
   {
      output_bit(EEPROM_DI, shift_left(&wren,1,0));
      output_high(EEPROM_CLK);
      output_low(EEPROM_CLK);
   }
   output_high(EEPROM_SELECT);
   output_low(EEPROM_SELECT);
   for(i=0; i<64; ++i)
   {
      output_bit(EEPROM_DI, shift_left(cmd,4,0));
      output_high(EEPROM_CLK);
      output_low(EEPROM_CLK);
   }
   output_high(EEPROM_SELECT);
}

BYTE read_ext_eeprom(EEPROM_ADDRESS address) {
   BYTE cmd[3];
   BYTE i,data;

   // Wait until the eeprom is done with a previous write
   while(!ext_eeprom_ready());

   output_low(EEPROM_SELECT);
   for(i=0; i<56; ++i)
   {
      output_bit(EEPROM_DI, shift_left(cmd,3,0));
      output_high(EEPROM_CLK);
      output_low(EEPROM_CLK);
   }
   for(i=0; i<8; ++i)
   {
      shift_left(&data,1,input(EEPROM_DO));
      output_high(EEPROM_CLK);
      output_low(EEPROM_CLK);
   }
   output_high(EEPROM_SELECT);
   return(data);
}


I can't include this in the code section for some reason, but for the write cmd is:
cmd[0]=data;
cmd[1]=address;
cmd[2]=(address>>8);
cmd[3]=0x02;

read:
cmd[0]=address;
cmd[1]=(address>>8);
cmd[2]=0x03;

I'm not sure what the problem is. I have tried changing the numbers in the for loops with no success. I was also wondering why in the write function it uses EEPROM_DI as opposed to EEPROM_DO. Changing that did not help me either though. Any help would be appreciated.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Aug 03, 2006 11:50 am     Reply with quote

Your loop counts are wrong. You have arrays of 3 bytes for a read
and 4 bytes for a write. These are 24 bits and 32 bits long, respectively,
but you have loop counts of 56 and 64. This needs to be fixed.
lyke



Joined: 03 Aug 2006
Posts: 4

View user's profile Send private message

PostPosted: Thu Aug 03, 2006 1:08 pm     Reply with quote

I changed the loop counts, but still no luck reading the values from the eeprom.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Aug 03, 2006 2:13 pm     Reply with quote

Try using the CCS 25640.c driver file. The 25AA256 interface looks
nearly the same as the 25LC640, so it should work. You're using
different pins than the default pins in the driver. To tell the driver
to use your pins, put the #define statements right above the #include
statement for the driver file. Example:
Code:

#define EEPROM_SELECT PIN_B2
#define EEPROM_DI     PIN_B0   
#define EEPROM_DO     PIN_C7
#define EEPROM_CLK    PIN_B1
#include <25640.c>


If you have problems posting code with angle brackets in it, it's because
you have HTML enabled in your posting window. There's a little checkbox
right below the window, which you can select to disable HTML.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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