The address is 16-bits, and it appears that dimensioning the EEaddr variable as a WORD is in-sufficient to clock out the correct number of address bits. in the SPI_Write command.
How does on parse the high and low bytes from a word in MeBasic?
Code below:
Code: Select all
program SPI_Eeprom_Test
'device = dsPIC30F4013
'
Symbol EEcs = portB.11
symbol PGAcs = portB.12
'
main:
dim x as byte
EEaddr as word '
EEread as byte 'read data from selected address
EEwrite as byte 'write data to selected address
EEwren as byte 'set write enable latch (enable write ops)
EEwrdi as byte 'reset the write enable latch (disable write ops)
EErst as byte 'read status register
EEwst as byte 'write status register
EEpe as byte 'page erase
EEse as byte 'sector erase
EEce as byte 'chip erase
EErdid as byte 'release from deep power down & read ID
EEdpd as byte 'enter deep power down mode
databyte as byte '
'
Spi1_init() 'init hardware SPI port
Uart1_Init(9600) 'init hardware uart
u1mode.10 = 1 'specify alternate uart pin pairs (altio bit)
'
ADCHS = 0xFFFF '
'
TRISA = $0800 '
TRISB = $02c2
TRISC = $4010
TRISD = $0302
TRISF = $0005
'
'goto Blink_LED
'
'******************************************************************************
'
' This routine writes 10 bytes to the Mchip 25LC512 external eeprom
'
'******************************************************************************
'
EEcs = 1
Spi1Stat.15 = 1 'enable SPI ports
Spi1Con.11 = 0 'enable SDO1 pin
'
' Write to eeprom
'
loop1:
EEcs = 0
Spi1_write(EEwren)
EEcs = 1
'
EEaddr = 0
databyte = 100
while EEaddr <> 10
EEcs = 0
Spi1_write(EEwrite)
Spi1_write(EEaddr)
Spi1_write(databyte)
delay_ms(100)
EEaddr = EEaddr + 1
databyte = databyte - 1
' delay_ms(100)
wend
EEcs = 1
EEcs = 0
Spi1_write(EEwrdi)
EEcs = 1
'
'******************************************************************************
'
' Read 10 bytes from eeprom and write to uart
'
'******************************************************************************
'
EEaddr = 0
'
while EEaddr <> 10
EEcs = 0
Spi1_write(EEread) 'write the READ command
' Spi1_write(0x00) 'try dummy hi byte
Spi1_write(EEaddr) 'write the 16-bit address
databyte = Spi1_read(0) 'read the eeprom
EEcs = 1
'
Uart1_Write_char(databyte) '
EEaddr = EEaddr + 1
' delay_ms(100)
wend
'
goto loop1
end.
'
'******************************************************************************
'
' This routine blinks LED on same pin as Hardware SPI Dout
'
'******************************************************************************
'
Blink_lED:
'
x = 5
'
while x <> 0
portf.3 = 1
Delay_ms(500)
portf.3 = 0
Delay_ms(500)
x = x - 1
wend
end.