title: How to read 32 bits variable from Software SPI in ATmega32?
body: <p>it is my first try to set up SPI communication on ATmega32 and MAX31855. I've to read 32 bits from MAX31855 <a href="https://www.adafruit.com/datasheets/MAX31855.pdf" rel="nofollow">datasheet</a>. I have writen my own function but it seems to read 0 all time (I have checked it on display). Here are my definitions from *.h file:</p>
  
  <pre><code>#ifndef SPI_SOFTWARE_H_
  #define SPI_SOFTWARE_H_
  
  #define ilosc_urzadzen 1
  
  #define SoftSPI_MOSI    PD0
  #define SoftSPI_MISO    PD1
  #define SoftSPI_SCK     PD2
  #define SoftSPI_CS1     PD3
  #define SoftSPI_CS2     PD4
  #define SoftSPI_CS3     PD5
  #define SoftSPI_CS4     PD6
  #define SoftSPI_CS5     PD7
  
  #define SoftSPI_DDR     DDRD
  #define SoftSPI_PIN     PIND
  #define SoftSPI_PORT    PORTD
  
  void init_SoftSPI();
  uint32_t odczyt32bit(int CSx);
  
  #endif
  </code></pre>
  
  <p>I have connected MAX31855: SO -> PD1, SCK -> PD2, CS -> PD3.</p>
  
  <p>And here is my code from *.c:</p>
  
  <pre><code>#include &lt;avr/io.h&gt;
  #include "SPI_software.h"
  
  void init_SoftSPI(){
      SoftSPI_DDR |= (1 &lt;&lt; SoftSPI_MOSI) | (1 &lt;&lt; SoftSPI_SCK);
  
      if(ilosc_urzadzen == 1)  SoftSPI_DDR |= (1 &lt;&lt; SoftSPI_CS1);
      else if(ilosc_urzadzen == 2) SoftSPI_DDR |= (1 &lt;&lt; SoftSPI_CS1) | (1 &lt;&lt; SoftSPI_CS2);
      else if(ilosc_urzadzen == 3) SoftSPI_DDR |= (1 &lt;&lt; SoftSPI_CS1) | (1 &lt;&lt; SoftSPI_CS2) | (1 &lt;&lt; SoftSPI_CS3);
      else if(ilosc_urzadzen == 4) SoftSPI_DDR |= (1 &lt;&lt; SoftSPI_CS1) | (1 &lt;&lt; SoftSPI_CS2) | (1 &lt;&lt; SoftSPI_CS3) | (1 &lt;&lt; SoftSPI_CS4);
      else if(ilosc_urzadzen == 5) SoftSPI_DDR |= (1 &lt;&lt; SoftSPI_CS1) | (1 &lt;&lt; SoftSPI_CS2) | (1 &lt;&lt; SoftSPI_CS3) | (1 &lt;&lt; SoftSPI_CS4) | (1 &lt;&lt; SoftSPI_CS5);
  }
  
  uint32_t odczyt32bit(int CSx){
      uint32_t liczba = 0;
  
      if (CSx == 1){
          SoftSPI_PORT &amp;= ~SoftSPI_CS1;
  
          for (int i = 0; i &lt; 32; i++)
          {
              SoftSPI_PORT |= SoftSPI_SCK;
              if (SoftSPI_PIN &amp; (1 &lt;&lt; SoftSPI_MISO)) liczba += 1;
              liczba = liczba &lt;&lt; 1;
              SoftSPI_PORT &amp;= ~SoftSPI_SCK;
          }
  
          SoftSPI_PORT |= SoftSPI_CS1;        
      }
  
      return liczba;
  }
  </code></pre>
  
  <p>After reading value from MAX31855 I shift it (20 places no 18 becouse I dont need fraction part):</p>
  
  <pre><code>temp = odczyt32bit(1);
  temp = temp &gt;&gt; 20;
  </code></pre>
  
  <p>Where is my problem?</p>