-1

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?

Nichten
  • 85
  • 1
  • 2
  • 13
  • What type is `temp` declared as? –  Jan 11 '16 at 23:21
  • `temp` is also uint32_t. After shifting it it is assign to global variable of type int. But it is problem with temp - it is always reading zero... – Nichten Jan 11 '16 at 23:39
  • Are you sure the MISO pin is configured as input? – JimmyB Jan 13 '16 at 15:01
  • Did you solve this problem? I think I am trying to use the same method and am getting the same result. I think the problem is duplicating Arduino code and it might be the completely wrong method as I read the SPI chapter 23 from the datasheet. – geofrey rainey Sep 28 '17 at 11:26
  • Hi, yes, finally I have get it into work, Above part of code have few error and in few days I will post corrected version for software SPI read for MAX31855 (I have to look for it on hard drive to which I do not have access currently. – Nichten Oct 18 '17 at 19:53

1 Answers1

0

The clocking timing looks okay, but note that you only need to clock out 14 cycles (or just 12 for less precision) to get the result. You can stop reading the rest of the results whenever you want. (You don't need to read all 32 bits if you aren't using them.) If you do read all the bits, then you can detect any fault codes that indicate the thermocouple is not connected correctly.

Are you giving enough time for the conversion to occur? It needs 100 ms between readings.

UncleO
  • 8,299
  • 21
  • 29
  • There is 2 seconds between next reading, so it should be ok. I have seen now that I havent'g driven high CS1 after setting it as output. But after adding this to code it has not helped... – Nichten Jan 12 '16 at 08:37