A couple of weeks ago, I showed an example of using the open source MCP2210 Library I created earlier to communicate with the MCP23S08 port expander under Linux. In this post, I will provide another example of using the library with the TC77 temperature sensor included on the MCP2210 evaluation board.

The function below shows how to retrieve the temperature readings from the sensor. First, the GPIO pins were configured to be Chip Select (Note, here I configured all of the GPPins to simply testing. But you only need to configure the pin you are using as CS in general), and then the SPI transfer parameters were set accordingly. These steps are pretty standard and are virtually the same as in my previous example.

To configure SPI communication parameters for different chips, you will need to change ActiveChipSelectValue, IdleChipSelectValue accordingly to match the GPIO pin that you use as the Chip Select.

void TestTC77(hid_device* handle) {
    ChipSettingsDef chipDef;

    //set GPIO pins to be CS
    chipDef = GetChipSettings(handle);

    for (int i = 0; i < 9; i++) {
        chipDef.GP[i].PinDesignation = GP_PIN_DESIGNATION_CS;
        chipDef.GP[i].GPIODirection = GPIO_DIRECTION_OUTPUT;
        chipDef.GP[i].GPIOOutput = 1;
    }
    int r = SetChipSettings(handle, chipDef);

    //configure SPI
    SPITransferSettingsDef def;
    def = GetSPITransferSettings(handle);

    //chip select is GP7
    def.ActiveChipSelectValue = 0xff7f;
    def.IdleChipSelectValue = 0xffff;
    def.BitRate = 6000000l;
    def.BytesPerSPITransfer = 2;

    r = SetSPITransferSettings(handle, def);

    if (r != 0) {
        printf("Errror setting SPI parameters.\n");
        return;
    }

    byte buf[2];

    //read the temperature
    SPIDataTransferStatusDef def1 = SPISendReceive(handle, buf, 2);

    int tempVal = 0;
    int sign = def1.DataReceived[0] & 0x80;

    //13 bit 2's complement left aligned (last three bits are all 1's)
    if (sign == 0)
        tempVal = (def1.DataReceived[0] << 8 | def1.DataReceived[1]) >> 3;
    else
        (((def1.DataReceived[0] & 0x7f) << 8 | def1.DataReceived[1]) >> 3) - 4096;

    float tempC = (float) tempVal * 0.0625;
    float tempF = 1.8 * (float) tempC + 32.0f;

    printf("temp (C) = %3.1f\n", tempC);
    printf("temp (F) = %3.1f\n", tempF);
}

Getting the temperature readings from TC77 is pretty straight forward, no register command is needed. Technically speaking, we should check the third bit (please refer to the datasheet) returned from the SPI call to ensure that the result is valid as the very first conversion takes around 300ms to complete. For all but the most critical applications, this step is not needed as once the devices is powered up and running the temperature values are updated continuously.

The temperature reading is sent back as 13 bits 2’s complement (left aligned) so we need to right shift by 3 bits to get the temperature values. The most significant bit indicates the sign of the value.

Here is the main function. All the up-to-date examples can be found here.

#include "mcp2210.h"

int main(int argc, char** argv) {
    hid_device *handle;

    /**
     * initializing the MCP2210 device.
     */
    handle = InitMCP2210();

    if (handle == 0) {
        printf("ERROR opening device. Try using sudo.\n");
        exit(-1);
    }

    TestTC77(handle);

    /**
     * release the handle
     */
    ReleaseMCP2210(handle);

    return 0;
}
Be Sociable, Share!