LAB1X sample code not working with 25LC640.
Hello.
I've decided to use 25LC640 EEPROM with 16F870. For that purpose, I've took the LAB1X code from here:
http://melabs.com/samples/LABX1-16F887/spix.htm
and modified it to my needs( just changed LCD and EEPROM ports and added details to info screen):
Code:
' Define LCD registers and bits
include "modedefs.bas"
DEFINE OSC 4
DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 0
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 1
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE LCD_COMMANDUS 1500
DEFINE LCD_DATAUS 44
ADCON1=%00000110 'CONFIGURE PORT A AS DIGITAL
CS Var PORTB.3 ' Chip select pin
SCK Var PORTC.6 ' Clock pin
SI Var PORTC.5 ' Data in pin
SO Var PORTB.2 ' Data out pin
addr Var Word ' Address
B0 Var Byte ' Data
For addr = 0 To 15 ' Loop 16 times
B0 = addr + 100 ' B0 is data for SEEPROM
Lcdout $fe,1,#addr,": ",#B0 ' Display
Lcdout $fe,$C0,"Writing... " ' Display
Gosub eewrite ' Write to SEEPROM
Pause 10 ' Delay 10ms after each write
Next addr
pause 1000
mainloop:
For addr = 0 To 15 ' Loop 16 times
Gosub eeread ' Read from SEEPROM
Lcdout $fe,1,#addr,": ",#B0 ' Display
Lcdout $fe,$C0,"Reading... " ' Display
Pause 1000
Next addr
Goto mainloop
' Subroutine to read data from addr in serial EEPROM
eeread:
CS = 0 ' Enable serial EEPROM
Shiftout SI, SCK, MSBFIRST, [$03, addr.byte1, addr.byte0] ' Send read command and address
Shiftin SO, SCK, MSBPRE, [B0] ' Read data
CS = 1 ' Disable
Return
' Subroutine to write data at addr in serial EEPROM
eewrite:
CS = 0 ' Enable serial EEPROM
Shiftout SI, SCK, MSBFIRST, [$06] ' Send write enable command
CS = 1 ' Disable to execute command
CS = 0 ' Enable
Shiftout SI, SCK, MSBFIRST, [$02, addr.byte1, addr.byte0, B0] ' Send address and data
CS = 1 ' Disable
Return
End
It appears to not write anything and reads the same sequence of 0s and 255 in each cycle, but the sequence is different on each power up.
I was not able to find schematics how correctly wire another pins of EEPROM, such as HOLD. This can be wrong connection issue?
Re: LAB1X sample code not working with 25LC640.
The datasheet http://ww1.microchip.com/downloads/e...Doc/21223H.pdf says
2.6 Hold (HOLD)
The HOLD pin is used to suspend transmission to the
25XX640 while in the middle of a serial sequence without
having to retransmit the entire sequence over
again. It must be held high any time this function is not
being used.
So tie the HOLD pin high.
1 Attachment(s)
Re: LAB1X sample code not working with 25LC640.
Here is an app note http://ww1.microchip.com/downloads/e...tes/01018A.pdf
which contains a wiring diagram.
Attachment 7393
I hope this gives you some clues as to why the code is not working.
Re: LAB1X sample code not working with 25LC640.
Does not works anyway....
Re: LAB1X sample code not working with 25LC640.
Quote:
Originally Posted by
CuriousOne
Does not works anyway....
In the original program there is this line
TRISA.5 = 0 ' Set CS to output
I do not see the equivalent line in your program.
Which would be
TRISB.3 = 0 ' Set CS to output
Re: LAB1X sample code not working with 25LC640.
Aren't ports automatically set to required state by apropriate statement, when needed?
Anyways, found another code that worked:
Code:
' Program 25LC640.BAS
' *************************************************************
' * For use with EXPERIMENTING WITH THE PICBASIC PRO COMPILER *
' * *
' * This source code may be freely used within your own *
' * programs. However, if it is used for profitable reasons, *
' * please give credit where credit is due. *
' * And make a reference to myself or Rosetta Technologies *
' * *
' * Les. Johnson *
' *************************************************************
'
' Write to the first 11 locations of a MICROCHIP 25LC640 SPI serial eeprom
' Then Read the 11 locations back and display them on a serial LCD
' The configuration is 8192 words x 8-bits
' To WRITE to the eeprom:-
' The 16-bit address is loaded into the variable "ADDR"
' The byte to write is loaded into the variable "E_BYTEOUT"
' And a call is made to the subroutine "EWRITE"
' To READ from the eeprom:-
' The 16-bit address is loaded into the variable "ADDR"
' And a call is made to the subroutine "EREAD"
' The byte at the specified address is returned in the variable "E_BYTEIN"
Include "Modedefs.Bas"
' ** Setup the Crystal Frequency, in Mhz **
Define OSC 4 ' Set Xtal Frequency
' ** Set Debug Defines **
Define DEBUG_REG PortA ' Debug PORT ?
Define DEBUG_BIT 0 ' *** Debug pin Bit ? ***
Define DEBUG_BAUD 9600 ' *** Debug Baud Rate ***
Define DEBUG_MODE 1 ' Set Serial Mode 1=Inverted
Define DEBUG_PACING 300 ' Delay 'in uS' between characters sent
' ** Define LCD Constants **
I Con 254 ' Control Byte
Clr Con 1 ' Clear the display
Line1 Con 128 ' Point to beginning of line 1
Line2 Con 192 ' Point to beginning of line 2
Line3 Con 148 ' Point to beginning of line 3
Line4 Con 212 ' Point to beginning of line 4
' ** Define the Pin assignments **
CS Var PortB.0 ' Chip select pin
SCK Var PortB.1 ' Clock pin
SI Var PortB.2 ' Data in pin
SO Var PortB.2 ' Data out pin
' ** Declare the Variables **
Addr var Word ' Memory address within the eeprom (0-511)
E_Byteout Var Byte ' Byte to be placed into the eeprom
E_Bytein Var Byte ' Byte read from the eeprom
' ** Define the eeprom's op-codes **
WRSR Con 1 ' Write to STATUS REGISTER op-code
EWR Con 2 ' WRITE op-code
ERD Con 3 ' READ op-code
WRDI Con 4 ' DISABLE WRITES op-code
RDSR Con 5 ' Read the STATUS REGISTER op-code
EWEN Con 6 ' ENABLE WRITES op-code
' ** THE MAIN PROGRAM STARTS HERE **
' Write the string, "HELLO WORLD" into the first 11 address's of the Eeprom
For Addr=0 To 10 ' Create a loop of 11
Lookup Addr,["H","E","L","L","O"," ","W","O","R","L","D"],E_ByteOut' Build up the string
Gosub EWrite ' Write the byte to the Eeprom
Next ' Close the Loop
' Read the first 11 address's within the Eeprom and display them on the LCD
Again: Debug I,Clr:Pause 30 ' Clear the LCD
Pause 500 ' Pause for drama
For Addr=0 To 10 ' Create a loop of 11
Gosub ERead ' Read the byte from the Eeprom
Debug E_ByteIn ' Display the characters read in from the eeprom
Pause 300 ' Pause between characters being displayed
Next ' Close the loop
Goto Again ' Do it forever
' Read a single byte from the Eeprom
' The address is held in the variable "ADDR"
' The byte read is returned in the variable "E_BYTEIN"
Eread: Low CS ' Enable the eeprom
Shiftout SI,SCK,MSBFIRST,[ERD,Addr.highbyte,Addr.lowbyte]' Send READ COMMAND and address
Shiftin SO,SCK,MSBPRE,[E_Bytein] ' Read data
High CS ' Disable the eeprom
Return
' Write a single byte to the Eeprom
' The address is held in the variable "ADDR"
' The byte to be written is held in the variable "E_BYTEOUT"
Ewrite: Low CS ' Enable the eeprom
Shiftout SI,SCK,MSBFIRST,[EWEN] ' Send WRITE ENABLE command
High CS ' Disable the eeprom, to execute the command
Low CS ' Re-enable the eeprom
Shiftout SI,SCK,MSBFIRST,[EWR,Addr.highbyte,Addr.lowbyte,E_Byteout]' Send address and data
High CS ' Disable the eeprom
Pause 5 ' Allow the eeprom to allocate the byte
Return
Re: LAB1X sample code not working with 25LC640.
Quote:
Aren't ports automatically set to required state by apropriate statement, when needed?
maybe if you
high cs or low cs
but
cs=0 or c=1
I don't think so
Re: LAB1X sample code not working with 25LC640.
Quote:
Originally Posted by
CuriousOne
I was not able to find schematics how correctly wire another pins of EEPROM, such as HOLD. This can be wrong connection issue?
It was not a connection issue then?
What are the differences in the two programs that made the code in #1 not work?
Re: LAB1X sample code not working with 25LC640.
Quote:
Originally Posted by
richard
maybe if you
high cs or low cs
but
cs=0 or c=1
I don't think so
So the first program will work if this line is included
TRISB.3 = 0 ' Set CS to output
Re: LAB1X sample code not working with 25LC640.
The difference is that SI and SO pins are tied together in 2nd code, via 1k resistor.
Re: LAB1X sample code not working with 25LC640.
Quote:
Originally Posted by
CuriousOne
The difference is that SI and SO pins are tied together in 2nd code, via 1k resistor.
From the first code
' Subroutine to write data at addr in serial EEPROM
eewrite:
CS = 0 ' Enable serial EEPROM
Shiftout SI, SCK, MSBFIRST, [$06] ' Send write enable command
CS = 1 ' Disable to execute command
CS = 0 ' Enable
Shiftout SI, SCK, MSBFIRST, [$02, addr.byte1, addr.byte0, B0] ' Send address and data
CS = 1 ' Disable
Return
You are shifting out on the data in pin? Which is the same in the original SPIX.htm so I guess I am confused.