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))