Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Writing to external EEPROM fails

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
MadAl

Bascom Member



Joined: 26 Jul 2010
Posts: 40

blank.gif
PostPosted: Tue Dec 22, 2020 9:39 pm    Post subject: Writing to external EEPROM fails Reply with quote

Hi,

I've been trying to write data in a simple test program to an external eeprom (25LC1024) via SPI.
The output I get is:
Written to EEPROM : 84
Read from EEPROM ; 255
Written to EEPROM : 85
Read from EEPROM ; 255
Written to EEPROM : 86
Read from EEPROM ; 255
Written to EEPROM : 87
Read from EEPROM ; 255
Written to EEPROM : 88
Read from EEPROM ; 255
Written to EEPROM : 89

And as the spi_result is set to zero at program start and a virgin eeprom has the value 255 on all addresses, I presume the reading (and therefore SPI communication) works.
Can anyone spot the cause of the write fail?
Code:

$regfile = "m328pdef.dat"                                   ' specify the used micro
$crystal = 11059200                                         'this crystal allows the max bootloader speed of 115200 baud as all baud rates have zero error

$baud = 115200                                              '115200 baud rate for hardware serial communication
$hwstack = 256
$swstack = 256
$framesize = 24

Waitms 200

Config Spi = Hard , Interrupt = Off , Data Order = Msb , Clockrate = 16 , Master = Yes , Polarity = Low , Phase = 0 , Noss = 1
Spiinit

Dim Spi_control_write As Byte
Dim Spi_control_read As Byte
Dim Spi_address As Word
Dim Spi_data As Byte
Dim Spi_result As Byte
Dim Spi_write_enable As Byte

Spi_write_enable = &B0000_0110
Spi_control_write = &B0000_0010                             'write command
Spi_control_read = &B0000_0011                              'read command
Spi_address = 0
Rotate Spi_address , Left , 8                               'fix address bytes
Spi_data = 64                                               'test data

Mycs Alias Portd.7
Config Mycs = output
Set Mycs
Spi_result = 0

Do
   Incr Spi_address
   Rotate Spi_address , Left , 8
   Incr Spi_data

   Reset Mycs
   Spiout Spi_write_enable , 1
   Set Mycs
   Waitms 10 'Note, changing this value to zero or higher doesn't solve the issue
   Reset Mycs
   Spiout Spi_control_write , 1
   Spiout Spi_address , 2
   Spiout Spi_data , 1
   Set Mycs

   Print "Written to EEPROM : " ; Spi_data

   Waitms 100

   Reset Mycs
   Spiout Spi_control_read , 1
   Spiout Spi_address , 2
   Spiin Spi_result , 1
   Set Mycs

   Print "Read from EEPROM ; " ; Spi_result

   Waitms 500

Loop

End


thanks.

(BASCOM-AVR version : 2.0.8.3 )
Back to top
View user's profile
MadAl

Bascom Member



Joined: 26 Jul 2010
Posts: 40

blank.gif
PostPosted: Tue Dec 22, 2020 10:14 pm    Post subject: Reply with quote

Alternative code, same problem:

Code:
$regfile = "m328pdef.dat"                                   ' specify the used micro
$crystal = 11059200                                         'this crystal allows the max bootloader speed of 115200 baud as all baud rates have zero error

$baud = 115200                                              '115200 baud rate for hardware serial communication
$hwstack = 256
$swstack = 256
$framesize = 24

Waitms 200

Config Spi = Hard , Interrupt = Off , Data Order = Msb , Clockrate = 16 , Master = Yes , Polarity = Low , Phase = 0 , Noss = 1
Spiinit

Dim Spi_control_write As Byte
Dim Spi_control_read As Byte
Dim Spi_address As Word
Dim Spi_data As Byte
Dim Spi_result As Byte
Dim Spi_write_enable As Byte
Dim Adress As Dword
Dim Adre(4) As Byte At Adress Overlay

Spi_write_enable = &B0000_0110
Spi_control_write = &B0000_0010                             'write command
Spi_control_read = &B0000_0011                              'read command                                               'test data

Mycs Alias Portd.7
Config Mycs = output
Set Mycs

Spi_result = 0
Adress = 0
Spi_data = 0

Do
   Incr Adress
   Incr Spi_data

   Reset Mycs
   Spiout Spi_write_enable , 1
   Set Mycs
'   Waitms 10
   Reset Mycs
   Spiout Spi_control_write , 1
   Spiout Adre(3) , 1                                       'send the write address (in 3 bytes)
   Spiout Adre(2) , 1
   Spiout Adre(1) , 1
   Spiout Spi_data , 1
   Set Mycs

   Print "Written to EEPROM : " ; Spi_data ; "  at address " ; Adress

   Waitms 100

   Reset Mycs
   Spiout Spi_control_read , 1
   Spiout Adre(3) , 1                                       'send the write address (in 3 bytes)
   Spiout Adre(2) , 1
   Spiout Adre(1) , 1
   Spiin Spi_result , 1
   Set Mycs

   Print "Read from EEPROM ; " ; Spi_result ; "  at address " ; Adress

   Waitms 500

Loop

End
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1934

blank.gif
PostPosted: Wed Dec 23, 2020 2:05 am    Post subject: Reply with quote

Either set PB2 as output or pull it high.
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5231
Location: Holland

blank.gif
PostPosted: Tue Dec 29, 2020 1:41 pm    Post subject: Reply with quote

since you set NOSS=1 it means you will not use the SS pin which is ok. But it also means you will set the pin yourself. In that case you must take care that the designated SS pin is set to output mode. Otherwise the pin is by default an input and it could lead to the master set to input mode. and like MWS says, you can also pull the SS pin high.
_________________
Mark
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum