it is my first try to set up SPI communication on ATmega32 and MAX31855. I've to read 32 bits from MAX31855 datasheet. 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:
#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
I have connected MAX31855: SO -> PD1, SCK -> PD2, CS -> PD3.
And here is my code from *.c:
#include <avr/io.h>
#include "SPI_software.h"
void init_SoftSPI(){
SoftSPI_DDR |= (1 << SoftSPI_MOSI) | (1 << SoftSPI_SCK);
if(ilosc_urzadzen == 1) SoftSPI_DDR |= (1 << SoftSPI_CS1);
else if(ilosc_urzadzen == 2) SoftSPI_DDR |= (1 << SoftSPI_CS1) | (1 << SoftSPI_CS2);
else if(ilosc_urzadzen == 3) SoftSPI_DDR |= (1 << SoftSPI_CS1) | (1 << SoftSPI_CS2) | (1 << SoftSPI_CS3);
else if(ilosc_urzadzen == 4) SoftSPI_DDR |= (1 << SoftSPI_CS1) | (1 << SoftSPI_CS2) | (1 << SoftSPI_CS3) | (1 << SoftSPI_CS4);
else if(ilosc_urzadzen == 5) SoftSPI_DDR |= (1 << SoftSPI_CS1) | (1 << SoftSPI_CS2) | (1 << SoftSPI_CS3) | (1 << SoftSPI_CS4) | (1 << SoftSPI_CS5);
}
uint32_t odczyt32bit(int CSx){
uint32_t liczba = 0;
if (CSx == 1){
SoftSPI_PORT &= ~SoftSPI_CS1;
for (int i = 0; i < 32; i++)
{
SoftSPI_PORT |= SoftSPI_SCK;
if (SoftSPI_PIN & (1 << SoftSPI_MISO)) liczba += 1;
liczba = liczba << 1;
SoftSPI_PORT &= ~SoftSPI_SCK;
}
SoftSPI_PORT |= SoftSPI_CS1;
}
return liczba;
}
After reading value from MAX31855 I shift it (20 places no 18 becouse I dont need fraction part):
temp = odczyt32bit(1);
temp = temp >> 20;
Where is my problem?