title: ATMEGA8A + SPI memory strange behavior
body: <p>I've bought SPI memory <a href="http://www.ebay.com/itm/MX25L8006EPI-12G-MX25L8006EPI-IC-DIP-8-MX-BIOS-Serial-Flash-Memory-NEW-/291877678999" rel="nofollow noreferrer">MX25L8006EPI</a> and now I try to connect it to ATMEGA8A to test it. The schematic is very simple and doesn't change from others over the internet:</p>
  
  <p><a href="https://i.stack.imgur.com/H7R7k.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/H7R7k.png" alt="enter image description here"></a></p>
  
  <p>ATMEGA8A works at 8Mz internal oscillator, 3.3V. The fuses are HIGH: 0xD1, LOW: 0xE4.</p>
  
  <p>I use Atmel Studio 7 for coding and AVRISP mkII to programming the uC.
  Since EEPROM needs 3.3V I've connected 5V 700mA power supply module using LM317 as you can see on the scheme. I don't think it matters but anyway ...</p>
  
  <p>The code is also very simple, I've partly copied that from some page in the Internet:</p>
  
  <pre class="lang-c prettyprint-override"><code>#define SLAVESELECT (PORTB &amp;= ~(1&lt;&lt;PORTB2))
  #define SLAVEDESELECT (PORTB |= (1&lt;&lt;PORTB2))
  
  #define WREN  6
  #define WRDI  4
  #define RDSR  5
  #define WRSR  1
  #define READ  3
  #define WRITE 2
  #define RDID 0x9F
  
  #define byte unsigned char
  
  void SPI_Init()
  {
      DDRB = (1&lt;&lt;PORTB2)|(1&lt;&lt;PORTB3)|(1&lt;&lt;PORTB5);
      SLAVEDESELECT;
      //SPSR |= (1&lt;&lt;SPI2X);
      SPCR |= (1&lt;&lt;SPE)|(1&lt;&lt;MSTR)|(1 &lt;&lt; SPR1);
      _delay_ms(10);
  }
  
  byte SPI_Transfer(volatile byte data)
  {
      SPDR = data;
      while (!(SPSR &amp; (1&lt;&lt;SPIF)));
      return SPDR;
  }
  
  void Get_Identification(byte data[3])
  {
      SLAVESELECT;
      SPI_Transfer(RDID);
      data[0] = SPI_Transfer(0xFF);
      data[1] = SPI_Transfer(0xFF);
      data[2] = SPI_Transfer(0xFF);
      SLAVEDESELECT;
  }
  
  byte GetStatus()
  {
      byte status;
      SLAVESELECT;
      SPI_Transfer(RDSR);
      status = SPI_Transfer(0xFF);
      SLAVEDESELECT;
      return status;
  }
  
  void SetWriteEnable(short ebable)
  {
      SLAVESELECT;
      SPI_Transfer((ebable != 0) ? WREN : WRDI);
      SLAVEDESELECT;
  }
  
  void SetStatus(byte status)
  {
      SetWriteEnable(1);
      SLAVESELECT;
      SPI_Transfer(WRSR);
      SPI_Transfer(status);
      SLAVEDESELECT;
      _delay_ms(100);
  }
  
  int main(void)
  {
      SPI_Init();
      byte status = 0x1C;
      DEBUG_PRINT("\r\nSet status:%d\r\n",status);
      SetStatus(status);
      status = GetStatus();
      DEBUG_PRINT("\r\nGet status:%d\r\n",status);
      byte buffer[3];
      Get_Identification(buffer);
      DEBUG_PRINT("Byte0:%x\r\n", buffer[0]);
      DEBUG_PRINT("Byte1:%x\r\n", buffer[1]);
      DEBUG_PRINT("Byte2:%x\r\n", buffer[2]);
  
      while (1) 
      {       
      }
  }
  </code></pre>
  
  <p><code>DEBUG_PRINT</code> is my function to send formatted string throught USART. I use it to print out debug info.</p>
  
  <p>Everything looks fine but when I run that I get some strange behavior. The output is:</p>
  
  <pre><code>Set status:28
  Get status:24
  Byte0:80
  Byte1:0
  Byte2:10
  </code></pre>
  
  <p>Firstly I set status register using <code>WRSR</code> opcode. it should set it to 00011100 but when I read the status using <code>RDSR</code> opcode I get 00011000.</p>
  
  <p>Ok, maybe there is some trouble with status register. Then I try to read ID register using <code>RDID</code> opcode. It should return 3 bytes - 0xC2 0x20 0x14. But insted I get 0x80 0x0 0x10. </p>
  
  <p>It would be understandable if I get no answer or all 0x00 or 0xFF. But here I read some answer, the problem that the response is totally wrong. Even more interesting is that if I connect the EEPROM to Arduino using at least the same code it works without problem.</p>
  
  <p>I feel that the problem is quite small. It looks that maybe some data order wrong or transfer speed or something like that but I can't get it work.</p>