Using the SPI-bus
Posted: Fri Mar 17, 2017 8:35 am
hello everyone,
I wanted to use the max6675 chip to read out a k-type temperature sensor.
You find the datasheet here https://cdn-shop.adafruit.com/datasheets/MAX6675.pdf
If you tie CS low and apply a CLK signal at the clk pin, the max6675 spits out one 16bit package, which looks like that:
https://www.dropbox.com/s/yytkafnku97o5 ... 2.png?dl=0
I use the following code t read out the package, but it doesn't work till now and the data variable always stays empty.
I studied the ESP-IDF SPI documentation but couldn't find out, if I use the apis properly.
Maybe some of you guys and girls have an idea on how to fix this.
I wanted to use the max6675 chip to read out a k-type temperature sensor.
You find the datasheet here https://cdn-shop.adafruit.com/datasheets/MAX6675.pdf
If you tie CS low and apply a CLK signal at the clk pin, the max6675 spits out one 16bit package, which looks like that:
https://www.dropbox.com/s/yytkafnku97o5 ... 2.png?dl=0
I use the following code t read out the package, but it doesn't work till now and the data variable always stays empty.
I studied the ESP-IDF SPI documentation but couldn't find out, if I use the apis properly.
Maybe some of you guys and girls have an idea on how to fix this.
Code: Select all
//define pins for MAX6675's SPI bus
#define PIN_NUM_MISO 25
#define PIN_NUM_CLK 19
#define PIN_NUM_CS 22
void read_temp_task(void *pvParameter) {
max6675_event_group = xEventGroupCreate();
spi_device_handle_t spi;
spi_bus_config_t buscfg={
.miso_io_num=PIN_NUM_MISO,
.mosi_io_num=-1,
.sclk_io_num=PIN_NUM_CLK,
.quadwp_io_num=-1,
.quadhd_io_num=-1
};
spi_device_interface_config_t devcfg={
.clock_speed_hz=10000000, //Clock out at 10 MHz
.mode=1, //Falling edge
.spics_io_num=PIN_NUM_CS, //CS pin
.queue_size=1, //We want to be able to queue 7 transactions at a time
};
//Initialize the SPI bus
ESP_ERROR_CHECK(spi_bus_initialize(HSPI_HOST, &buscfg, 1)); //
//Attach the MAX6675 to the SPI bus
ESP_ERROR_CHECK(spi_bus_add_device(HSPI_HOST, &devcfg, &spi));
uint16_t data = NULL; //16 bit
spi_transaction_t trans_desc={
.address=0,
.command=0,
.flags=0,
.length=16,
.rxlength=16,
.tx_buffer=0,
.rx_buffer=data,
};
while (1) {
ESP_ERROR_CHECK(spi_device_transmit(spi, &trans_desc));
if(data&0x4) //check if thermocouple is not attached
{
ESP_LOGI(TAG, "Thermocouple not attached \n");
xEventGroupSetBits(max6675_event_group,THERMOCOUPLE_NOT_ATTACHED_BIT);
}
uint16_t temp; //extract temp from payload by applying a bit mask
temp = data & 0b0111111111111000;
max6675_temp = temp;
xEventGroupSetBits(max6675_event_group,GOT_TEMP);
printf("temp: %d\n",max6675_temp );
printf("raw data: %d\n",data );
vTaskDelay(1000/portTICK_RATE_MS);
}
}