Hi! I ve been trying to use an EEPROM to store results from a/d conversion.

Can you spot anything wrong with my code?
================================================== ========
DEFINE HSER_RCSTA 90h 'Enable Serial PORT
'8 bit reception
'Disable Single receive
'Enable continuous receive
'All bytes received, diable address detection
'No framming Error
'No overrun error

DEFINE HSER_TXSTA 20h '8 bit transmission
'Enable transmit
'Asyncronous mode
'Low baud rate (BRGH=0)
'TSR full

DEFINE HSER_SPBRG 64 'set USART to 2400 baud (when BRGH=0)

DEFINE HSER_CLROERR 1 'Enable automatic overrun error


'Oscillation Settings
'---------------------
DEFINE OSC 10 'Oscillation Speed 10MHz


'ADC settings
'-------------

DEFINE ADC_BITS 10 '10 bits conversion
DEFINE ADC_CLOCK 3 'Clock cycle (3=rc)
DEFINE ADC_SAMPLEUS 50 'Sampling time in ìs

INCLUDE "modedefs.bas"

CS var PORTA.5 ' Chip select pin
SCK var PORTC.3 ' Clock pin
SI var PORTC.4 ' Data in pin
SO var PORTC.5 ' Data out pin

addr var word ' Address
adc var word ' Result from ADC
adc1 var word ' read from memory result

TRISA.5 = 0 ' Set CS to output

'ADCON1 = 7 ' Set PORTA and PORTE to digital

trisa = %11011111
ADCON1 = %10001110 'Right Justified result format
'Only PortA.0 analogue input, Vref+ = Vcc, Vref-=Vss

addr = 0 'Inital address 0000h
MAIN:

ADCIN 0, ADC
hserout [#adc,13] 'check a/d conversion before writting to mem
gosub eewrite
pause 50 ' Delay after each write
gosub eeread
hserout[#addr, "," , #adc1,13]
pause 1000
addr = addr + 1
goto main



' Subroutine to read data from addr in serial EEPROM
eeread:
ADDR = ADDR - 1
CS = 0 ' Enable serial EEPROM
Shiftout SI, SCK, MSBFIRST, [$03, addr.byte1, addr.byte0]
Shiftin SO, SCK, MSBPRE, [adc1.Byte1] ' Read data
cs = 1
pause 50
addr = addr + 1
cs = 0
Shiftout SI, SCK, MSBFIRST, [$03, addr.byte1, addr.byte0]
Shiftin SO, SCK, MSBPRE, [adc1.byte0] ' 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
CS = 0 ' Enable
Shiftout SI, SCK, MSBFIRST, [$02, addr.byte1, addr.byte0, adc.byte1]
cs = 1
pause 50
addr = addr + 1
cs=0
Shiftout SI, SCK, MSBFIRST, [$02, addr.byte1, addr.byte0, adc.byte0]
CS = 1
Return
================================================== =======

I canot get the correct result when reading from eeprom


The eeprom i am using is an 25LC640 SPI and I have tested it with a single data store to an address and it works...