Platform agnostic C driver for the Maxim MAX6675 Cold-Junction-Compensated K-Thermocouple-to-Digital Converter
Explore the docs »
View Demo
·
Report Bug
·
Request Feature
To get a local copy up and running follow these simple steps.
-
Navigate to your project's source directory
-
Clone the repo
git clone https://github.com/raulgotor/maxim_max6675.git
-
Write a transfer function (see next section)
A transfer function glues the driver logic with the current device specific API. The transfer function needs to be injected to the driver, so it can access your device specific SPI peripheral while remaining agnostic to the platform you are using it at.
The transfer function should match with the following prototype:
/*!
* @brief SPI Transfer function
*
* Function should write the specified number of bytes in the provided buffer
*
* @param[out] p_rx_buffer Pointer to a buffer where to read
* the data to
* @param[in] size Number of bytes to read
*
* @return bool Operation result
*/
typedef bool (*pf_read_func_t)(uint8_t * const p_rx_buffer, size_t const size);
An example of a transfer function for Espressif's esp-idf would be:
static spi_device_handle_t m_max6675_spi_handle
bool max6675_spi_xchg(uint8_t * const rx_buffer, size_t const size)
{
// Configure the transaction type
spi_transaction_t const transaction = {
.tx_buffer = NULL,
.rx_buffer = rx_buffer,
.length = 8 * size,
.rxlength = 8 * size,
};
bool success = (NULL != rx_buffer) && (0 != size);
esp_err_t esp_result;
// Lock the SPI bus for our use
if (success) {
esp_result = spi_device_acquire_bus(m_max6675_spi_handle,
portMAX_DELAY);
success = (ESP_OK == esp_result);
}
// Init transaction (request in this case) and unlock the bus at the end
if (success) {
esp_result = spi_device_transmit(m_max6675_spi_handle,
&transaction);
spi_device_release_bus(m_max6675_spi_handle);
success = (ESP_OK == esp_result);
}
return success;
}
Below an implementation example for an ESP32 MCU using esp-idf is suggested:
- Device specific SPI bus and peripheral initialization
- Transfer function definition
- Driver initialization with injection of the transfer function
- Initialize the SPI bus and add a MAX6675 instance
bool spi_init(void) { bool success; spi_bus_config_t const bus_cfg = { .miso_io_num = MAX6675_MISO_PIN, .mosi_io_num = MAX6675_MOSI_PIN, .sclk_io_num = MAX6675_CLK_PIN, .max_transfer_sz = (4 * 8) }; spi_device_interface_config_t const max6675_cfg = { .mode = 0, .clock_speed_hz = 2 * 1000 * 1000, .spics_io_num = 4, .queue_size = 3 }; esp_err_t esp_result; // Initialize ESP32's SPI bus esp_result = spi_bus_initialize(HSPI_HOST, &bus_cfg, 2); success = (ESP_OK == esp_result); if (success) { // Make esp-idf aware of our MAX6675 esp_result = spi_bus_add_device(HSPI_HOST, &max6675_cfg, &m_max6675_spi_handle); success = (ESP_OK == esp_result); } return success; }
- Define the transfer function as indicated above
- Initialize the module and inject the transfer function
- Declare a handle for this specific instance of MAX6675
max6675_handle_t handle;
- Pass both the handle and the transfer function to the driver's initializing function
max6675_error_t max6675_result = max6675_init(&handle, max6675_spi_xchg);
- Declare a handle for this specific instance of MAX6675
Bellow there is an example for performing temperature readings.
- Check whether the thermocouple cable is properly connected
- If everything is fine, read and use the temperature value. The temperature is provided in centi-degrees Celsius.
int16_t temperature;
bool sensor_is_connected;
bool success;
max6675_error_t result;
result = max6675_is_sensor_connected(&handle, &sensor_is_connected);
success = ((MAX6675_ERROR_SUCCESS == result) && (sensor_is_connected));
if (success) {
result = max6675_read_temperature(&handle, &temperature);
success = (MAX6675_ERROR_SUCCESS == result);
}
if (success) {
printf("The temperature is %f degrees celsius\n", ((float)temperature)/100);
}
Please refer to the in code documentation and to the Maxim MAX6675 Datasheet
See the open issues for a list of proposed features (and known issues).
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the MIT License. See LICENSE
for more information.
Raúl Gotor
Project Link: https://github.com/raulgotor/maxim_max6675