25LC640 problem using Software SPI [SOLVED]

General discussion relating to the library modules supplied with the compiler

Moderators: David Barker, Jerry Messina

Post Reply
TimC
Registered User
Registered User
Posts: 5
Joined: Sun Nov 19, 2006 4:51 am
Location: Boston MA

25LC640 problem using Software SPI [SOLVED]

Post by TimC » Wed Oct 03, 2007 5:13 pm

Hi all
First wanted to get more comfortable with SPI so I could start using the faster parts from ramtron but I am stuck. Would be great just to do a simple 25LC640 read!
Hardware: microEngineering X1 board
Software: Swordfish V2.0.1
What works: The picBasic Pro demo code that came with the X1 board. So I know the chips are good and the programmer is working.
http://www.melabs.com/products/labx1.htm

What I first tried to do was to run the "24LC640" (should be renamed to 25LC640) sample in the SPI directory after changing the Chip select pin to PORTA.5. - No workey! Never gets past the spi25640.Available statement

Next what I did was to preload the 24LC640 via picBasic pro starting at location 0 with: 50,51,52,53,...

Now all I want to do is just read the EEPROM but all I get is "FF"
The code below comes from another thread on this board, with a few changes such as just using bytes and not words.

If I can get reading to work maybe writing will not be so hard.

Thank you for your help
Tim

Code: Select all

// not working!
// hardware platform is microEngineering Labs X1
// Testing the 25LC640, a SPI eeprom 

Device = 18F452
Clock = 20

#option LCD_DATA = PORTD.4
#option LCD_RS = PORTE.0
#option LCD_EN = PORTE.1

Include "LCD.bas"
Include "convert.bas"
Include "utils.bas"


Dim
   SCK As PORTC.3,                   // clock pin
   SDI As PORTC.4,                   // data in pin
   SDO As PORTC.5,                   // data out pin
   CS As PORTA.5,					 // Chip select
   Clock_Delay As Byte

// inititalise...  default speed is  5, 178KBit/Sec larger slower, 1 is fastest
Public Sub Initialize(T_Clock_Delay As Byte = 5)
   Clock_Delay = T_Clock_Delay
   Output(SDO)  // data out
   Input(SDI)   // data in
   Low(SCK)     // clock out, idle low
   Output(CS)	// chip select is output
End Sub

// toggle clock
Inline Sub ToggleClock()
   Dim Delay As WREG
   SCK = 1
   Delay = Clock_Delay
   ASM-
      decfsz delay
      bra $ - 2
   End ASM
   SCK = 0
End Sub

// Read
Function Read(pBitsToShift As Byte = 8) As Byte
   Result = 0
   CS = 0
   Repeat
      Result = Result << 1
      ToggleClock
      Result.0 = SDI
      Dec(pBitsToShift)
   Until pBitsToShift = 0
   CS = 1
End Function

// write...
Sub Write(pValue As Byte, pBitsToShift As Byte = 8)
   pValue = Reverse(pValue,pBitsToShift) // MSB first
   CS = 0
   Repeat
      SDO = pValue.0
      ToggleClock
      pValue = pValue >> 1
      Dec(pBitsToShift)
   Until pBitsToShift = 0
   CS = 1
End Sub


// main program...
Dim Value As Byte
ADCON1 = $07 // PORTE as digital (LCD)
LCD.Cls
Initialize	 // init the software SPI
LCD.WriteAt(1,1, "N Starting..")
Write($03)		// send write command
Write(0)		// send high address
Write(0)		// send low address

Value = Read(8) // read 8 bits

LCD.WriteAt(2,10, HexToStr(Value))
Last edited by TimC on Thu Oct 04, 2007 1:40 pm, edited 1 time in total.

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Post by David Barker » Thu Oct 04, 2007 11:26 am

Swordfish is supplied with a module to read and write to a 24LC640 - see the samples folder. However, I've posted some code on the wiki which strips away the module layers so you can see how it is done...

http://www.sfcompiler.co.uk/wiki/pmwiki ... er.24LC640

TimC
Registered User
Registered User
Posts: 5
Joined: Sun Nov 19, 2006 4:51 am
Location: Boston MA

Post by TimC » Thu Oct 04, 2007 1:39 pm

YES!
All your samples now work. For me I needed Software SPI and another problem was the swap of C.4 and C.5:

#if SPI25640_SOFTWARE
#option SSPI_SCK = PORTC.3
#option SSPI_SDI = PORTC.5 // reversed for X1 board
#option SSPI_SDO = PORTC.4 // reversed
#endif

Also got one example to work with the shift.bas module

I wished we used MOSI and MISO it would be so much easier.

Thanks
Tim

Post Reply