#include "spi.h"
#include <util/delay.h>
// Nowe przypisanie pinów:
#define CS_PIN PB4
#define CS_DDR DDRB
#define CS_PORT PORTB
void SPI_MasterInit(void) {
// PB5 (MOSI), PB7 (SCK), PB4 (CS) jako wyjścia
// PB6 (MISO) jako wejście
DDRB |= (1 << PB5) | (1 << PB7) | (1 << CS_PIN);
DDRB &= ~(1 << PB6);
// Dezaktywuj CS (wysoki stan)
CS_PORT |= (1 << CS_PIN);
// Włącz SPI w trybie Master
SPCR = (1 << SPE) | (1 << MSTR); // Dla f_CPU/4, możesz dodać (1 << SPR0) jeśli chcesz wolniej
}
uint8_t SPI_Transmit(uint8_t data) {
SPDR = data;
while (!(SPSR & (1 << SPIF)));
return SPDR;
}
int16_t readTemperatureRaw_TC77(void) {
uint8_t msb, lsb;
int16_t result;
CS_PORT &= ~(1 << CS_PIN); // Aktywuj CS
_delay_us(1);
msb = SPI_Transmit(0x00);
lsb = SPI_Transmit(0x00);
CS_PORT |= (1 << CS_PIN); // Dezaktywuj CS
// Jeśli brak odpowiedzi (czujnik niepodłączony)
if (msb == 0xFF && lsb == 0xFF) return 0xFFFF;
result = ((uint16_t)msb << 8) | lsb;
result >>= 3;
if (result & 0x1000) {
result |= 0xE000; // Rozszerzenie znaku (13-bit U2)
}
return result;
}
Read 4 times, last 29 hours ago