Problem so senzorom tlaku MS5541C

Caute,

potreboval by som poradit s programom pre tlakovy senzor MS5541C. Pouzivam MCU ATMEGA32. V programe najprv vycitavam kalibracne koeficienty (4 words), tie sa mi podari uspesne vycitat. Hodonty koeficientov su podla datasheetu spravne. Potom dam precitat hodnotu teploty a tlaku, ale obidve su nulove (bez odpovede). Senzor je pripojeny cez SPI. Pre zapis a citanie po zbernici pouzivam rovnake funkcie pre kalibracne koefficienty, aj pre teplotu a tlak.
Neviete mi prosim niekto poradit co robim zle?

Tu je moj kod:

/*****************************************************
Chip type : ATmega32L
Program type : Application
AVR Core Clock frequency: 1.000000 MHz
Memory model : Small
External RAM size : 0
Data Stack size : 512
*****************************************************/

#include <mega32.h>
#include <delay.h>

// Declare your global variables here
unsigned int teplota = 33;
/// PINY pre FLASH SPI pre ATMEGA32 PORTB
#define MOSI_F 5 // MOSI
#define SCK_F 7 // SCK
#define CS_F 1 // CS
//#define CS_F PORTB.1 // CS
#define CD_F 3 // CD = ‘L’-command, ‘H’-data
#define RST_F 2 // RST
/

#define SPI_DDR DDRB // DDR of SPI port
#define SPI_PORT PORTB // SPI port
#define SPI_MOSI 5 // MOSI pin (Master out, Slave in)
#define SPI_MISO 6 // MISO pin (Master in, Slave out)
#define SPI_SCK 7 // SCK pin (SPI clock)
#define SPI_SS 4 // SS pin (Slave Select)
// wait for an SPI read/write operation to complete
#define SPI_WAIT() while (!(SPSR & (1<<SPIF))); // wait until byte is sent and received

// bit patterns for reading calib words and sensor data, and for sending reset
#define READ_W1 0b0001110101010000
#define READ_W2 0b0001110101100000
#define READ_W3 0b0001110110010000
#define READ_W4 0b0001110110100000
//#define READ_W_BITS 12

#define READ_D1 0b0000111101000000 // pressure
#define READ_D2 0b0000111100100000 // temperature
//#define READ_D_BITS 10

#define RESET 0b1010101010101010
///#define RESET_BITS 21

struct konvert_t{ unsigned char znaky[6];
unsigned char pocet_znakov;
}convert_t;

typedef struct {
unsigned int SENST1;
unsigned int OFFT1;
unsigned int TCS;
unsigned int TCO;
unsigned int Tref;
unsigned int TEMPSENS;
} coeff_t;

void send_bytes(unsigned int data)
{
// SPCR &= ~_BV(CPOL);

SPCR &= ~(1<<CPOL);
SPDR = data >> 8;
SPI_WAIT();
SPDR = data & 0xFF;
SPI_WAIT();
}
unsigned int get_bytes()
{
unsigned int data;
//SPCR |= _BV(CPOL);
SPCR |= (1<<CPOL);
SPDR = 0;
SPI_WAIT();
data = SPDR;
data <<= 8;
SPDR = 0;
SPI_WAIT();
data |= SPDR;
return data;
}

void get_coeffs(coeff_t* coefficients)
{
unsigned int w1, w2, w3, w4;

send_bytes(READ_W1); //(READ_W1); READ_D2
w1 = get_bytes();
send_bytes(READ_W2);
w2 = get_bytes();
send_bytes(READ_W3);
w3 = get_bytes();
send_bytes(READ_W4);
w4 = get_bytes();

// print(“w1 = %u\tw2 = %u\tw3 = %u\tw4 = %u\n\r”, w1, w2, w3, w4);

coefficients->SENST1 = w1 >> 3;

coefficients->OFFT1 = (w1 & 0b111) << 10;
coefficients->OFFT1 |= w2 >> 6;

coefficients->TCS = w3 >> 6;

coefficients->TCO = w4 >> 7;

coefficients->Tref = (w2 & 0b111111) << 6;
coefficients->Tref |= w3 & 0b111111;

coefficients->TEMPSENS = w4 & 0b1111111;
}

unsigned int read_temp()
{
send_bytes(READ_D2);
// send_bytes(READ_W1);
//send_bytes(0);
//while (MISO_HIGH()); // MISO goes low on conversion completion
delay_ms(35);
return get_bytes();
}

unsigned int get_temp_diff(coeff_t* coeffs)
{
unsigned int T = read_temp();

//print(“Raw T reading = %u\r\n”, T);
// find reference temperature
unsigned int UT1 = 8 * coeffs->Tref + 10000; // maximum value is 42760
unsigned int dT = T - UT1; // find difference between temperature reading and reference
// calculate second-order temperature differential
unsigned long dT_sq = dT;
teplota = T;
dT_sq *= dT; // dT squared
dT_sq /= 128;
dT_sq /= 128; // looking for (dT/128) * (dT/128)
if (dT >= 0) dT_sq /= 8;
else dT_sq /= 2;
// correct dT using second-order differential
//print(“dT = %d\r\n”, dT - dT_sq);
return dT - dT_sq;
return 0;
}

unsigned int get_temperature(unsigned int dT, coeff_t* coeffs)
{
unsigned long acc = dT;
acc = (coeffs->TEMPSENS + 100);
acc /= 2048;
acc += 200;
return ((unsigned int)acc);
}
unsigned int get_pressure(unsigned int dT, coeff_t
coeffs)
{
unsigned int off; // offset at temperature
unsigned long sens; // sensitivity at temperature
unsigned int P; // pressure value
unsigned long pressure; // actual pressure in mbar

off = coeffs->TCO - 250;
off *= dT; // this shouldn’t overflow, but watch out.
off /= 4096;
off += coeffs->OFFT1 + 10000;
//print(“off=%d\n\r”, off);

sens = coeffs->TCS + 200;
sens *= dT;
sens /= 8192;
sens += coeffs->SENST1 / 2 + 3000;
send_bytes(READ_D1);
//send_bytes(0);
delay_ms(33);
P = get_bytes();
pressure = P;
pressure -= off;
pressure *= sens;
pressure /= 2048;
pressure += 1000;
return (unsigned int)pressure;
}

unsigned char display_cislo1(unsigned char cislo)
{
switch(cislo)
{
case 0:
return(0b01000000);
break;
case 1:
return(0b11111001);
break;
case 2:
return(0b00100100);
break;
case 3:
return(0b00110000);
break;
case 4:
return(0b00011001);
break;
case 5:
return(0b00010010);
break;
case 6:
return(0b00000010);
break;
case 7:
return(0b01111000);
break;
case 8:
return(0b00000000);
break;
case 9:
return(0b00010000);
break;
default:
return(0b11011111);
break;

}
}

// Convert char to string
void convertIntToUChar(int i)
{
int pom = 0; // pomocna premenna pre zostovanie cifier
unsigned char pocet_cifier = 0; //pomocna premenna pre pocitanie cifier

if(i > 9999)
{
pom = i/10000; //zisti cifru 4 (tisicky)
convert_t.znaky[pocet_cifier] = pom; // konvertuje cislo na znak
i = i - (pom * 10000); // zmensi cislo o tisicky
pocet_cifier++; // pripocita cifru
}
else
{

pom = 0; //nastavy cifru 3 na nulu (stovky)
convert_t.znaky[pocet_cifier] = pom; // konvertuje cislo na znak
pocet_cifier++; // pripocita cifru

}

if(i > 999)
{
pom = i/1000; //zisti cifru 4 (tisicky)
convert_t.znaky[pocet_cifier] = pom; // konvertuje cislo na znak
i = i - (pom * 1000); // zmensi cislo o tisicky
pocet_cifier++; // pripocita cifru
}
else
{

pom = 0; //nastavy cifru 3 na nulu (stovky)
convert_t.znaky[pocet_cifier] = pom; // konvertuje cislo na znak
pocet_cifier++; // pripocita cifru

}
/*
// poslanie znaku desatinnej ciarky
pom = ‘.’; //nastavy cifru 3 na nulu (stovky)
convert_t.znaky[pocet_cifier] = pom; // konvertuje cislo na znak
pocet_cifier++;
*/
if(i > 99)
{
pom = i/100; //zisti cifru 3 (stovky)
convert_t.znaky[pocet_cifier] = pom; // konvertuje cislo na znak
i = i - (pom * 100); // zmensi cislo o stovky
pocet_cifier++; // pripocita cifru
}
else
{
if(pocet_cifier != 0)
{
pom = 0; //nastavy cifru 3 na nulu (stovky)
convert_t.znaky[pocet_cifier] = pom; // konvertuje cislo na znak
pocet_cifier++; // pripocita cifru
}
}

if(i > 9)
{
pom = i/10; //zisti cifru 2 (desiatky)
convert_t.znaky[pocet_cifier] = pom; // konvertuje cislo na znak
i = i - (pom * 10); // zmensi cislo o desiatky
pocet_cifier++; // pripocita cifru
}
else
{
if(pocet_cifier != 0)
{
pom = 0; //nastavy cifru 2 na nulu (desiatky)
convert_t.znaky[pocet_cifier] = pom; // konvertuje cislo na znak
pocet_cifier++; // pripocita cifru
}
}

if(0<= i < 10)
{
pom = i; //zisti cifru 1 (jednotky)
convert_t.znaky[pocet_cifier] = pom; // konvertuje cislo na znak
pocet_cifier++; // pripocita cifru
}
else
{
if(pocet_cifier != 0)
{
pom = ‘x’; //nastavy cifru 1 na nulu (jednotky)
convert_t.znaky[pocet_cifier] = pom; // konvertuje cislo na znak
pocet_cifier++; // pripocita cifru
}
}
/*
// poslanie znaku V
pom = ‘V’; //nastavy cifru 3 na nulu (stovky)
convert_t.znaky[pocet_cifier] = pom; // konvertuje cislo na znak
pocet_cifier++; */

convert_t.pocet_znakov = pocet_cifier; // ulozi pocet cifier do struktury
}

void main(void)
{
// Declare your local variables here
unsigned char i = 0;
unsigned int dT;
unsigned int v,vt;
coeff_t c = { 0 };

// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0xFF;
DDRA=0xFF;

// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=0xFF
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFFFF
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=0xFF
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;

// USART initialization
// USART disabled
UCSRB=0x00;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// ADC initialization
// ADC disabled
ADCSRA=0x00;

// SPI initialization
// SPI disabled
SPCR=0x00;

// TWI initialization
// TWI disabled
TWCR=0x00;

// set up SPI for emulating MS5541C’s protocol

SPI_DDR &= ~(1 << SPI_MOSI);
SPI_DDR &= ~(1 << SPI_MISO);
SPI_DDR &= ~(1 << SPI_SS);
SPI_DDR &= ~(1 << SPI_SCK);

// Define the following pins as output

SPI_DDR |= (1<<SPI_MOSI)|(1<<SPI_SS)|(1<<SPI_SCK);
SPI_PORT |= (1<<SPI_SS);
SPCR |= (1<<SPE)|(1<<MSTR)|(1<<SPR1); // enable SPI, set as master, set prescaler to f(osc)/64
SPSR |= (1<<SPI2X); // Double SCK to f(osc)/32 (0.5 MHz)

//send_bits(RESET, RESET_BITS);
send_bytes(RESET);
send_bytes(0x40);
get_coeffs(&c);

v=0;
dT = get_temp_diff(&c);
v = get_temperature(dT, &c);
vt = get_pressure(dT, &c);

// Show in 7 segments display
convertIntToUChar(v);
for(i=0;i<convert_t.pocet_znakov;i++)
{
PORTA=display_cislo1(convert_t.znaky*);
delay_ms(800);
PORTA=display_cislo1(10);
delay_ms(800);
}

while (1)
{

}
}*