LM75A - Temperature data streaming using the PIT on the Kinetis KL25Z MCU

Document created by Tomas Vaverka Employee on Feb 24, 2016
Version 1Show Document
  • View in full screen mode

Hi Everyone,

 

In this document I would like to present a very simple bare-metal example code I created for the LM75A. This I2C digital temperature sensor offers an improved resolution of 0.125°C with an accuracy of ±2°C from –25°C to 100°C and ±3°C over –55°C to 125°C. It operates with a single supply from 2.8V to 5.5V and has three address pins, allowing up to eight LM75A devices to operate on a single I2C bus without address collisions. The device also includes an open-drain output (OS) which becomes active when the temperature exceeds the programmed limits.

 

NXP offers the OM13257 temperature sensor daughter card for easy evaluation of most NXP’s temperature sensors including the LM75ADP in a small 8-pin TSSOP8 package. The daughter card is shipped with no temperature sensors soldered to the board, so the LM75ADP needs to be purchased and soldered separately. The daughter board is designed to connect directly to the OM13320 Fm+ Development kit, but I have decided to pair it with one of the most popular Freedom development boards – FRDM-KL25Z.

 

The wiring is very straightforward as shown on the picture below. Both SCL (JP4-2) and SDA (JP3-2) lines are connected through the on-board 10K pull-up resistors to the I2C1 module (PTE1 and PTE0 pins) on the KL25Z128 MCU. The Vcc pin (JP2-2) is connected to the 3.3V supply voltage and the GND pin to common ground. The address select pins A2, A1 and A0 are connected to GND using jumpers between pins 1 and 2 of JP12, JP11 and JP10, resulting in a 7-bit I2C slave address of 0x48.

 

OM13257_KL25Z.JPG

 

 

The LM75A does not have a data ready interrupt, so the PIT (Periodic Interrupt Timer) module is used to read the Temp register periodically at a fixed rate of about 10Hz since the temp-to-digital conversion is executed every 100 ms. The MCU is configured as follows.

 

/******************************************************************************
* MCU initialization function
******************************************************************************/ 

void MCU_Init(void)
{
  //I2C1 module initialization
  SIM_SCGC4 |= SIM_SCGC4_I2C1_MASK; // Turn on clock to I2C1 module 
  SIM_SCGC5 |= SIM_SCGC5_PORTE_MASK; // Turn on clock to Port E module 
  PORTE_PCR1 |= PORT_PCR_MUX(6); // PTE1 pin is I2C1 SCL line 
  PORTE_PCR0 |= PORT_PCR_MUX(6); // PTE0 pin is I2C1 SDA line  
  I2C1_F  |= I2C_F_ICR(0x14); // SDA hold time = 2.125us, SCL start hold time = 4.25us, SCL stop hold time = 5.125us
  I2C1_C1 |= I2C_C1_IICEN_MASK; // Enable I2C1 module 

  //PIT initialization 
  SIM_SCGC6 |= SIM_SCGC6_PIT_MASK; // Turn on clock to to the PIT module
  PIT_LDVAL0 = 1048000; // Timeout period = 100 ms
  PIT_MCR = PIT_MCR_FRZ_MASK; // Enable clock for PIT, freeze PIT in debug mode
  PIT_TCTRL0 = PIT_TCTRL_TIE_MASK | // Enable PIT interrupt
  PIT_TCTRL_TEN_MASK; // and PIT 

  //Enable PIT interrupt on NVIC  
  NVIC_ICPR |= 1 << ((INT_PIT - 16) % 32);
  NVIC_ISER |= 1 << ((INT_PIT - 16) % 32);
}

 

 

In the PIT ISR, the Temp register is read and then the real temperature in °C is calculated as shown below. For more information on how to convert the raw values from the Temp register to real values in °C, please refer to the LM75A data sheet (chapter 7.4.3).

 

/******************************************************************************
* Interrupt handler for PIT
******************************************************************************/ 

void PIT_IRQHandler()
{
  PIT_TFLG0 |= PIT_TFLG_TIF_MASK; // Clear the PIT interrupt flag

  I2C_ReadMultiRegisters(LM75A_I2C_ADDRESS, TEMP_REG, 2, TemperatureData); // Read LM75A Temperature register (MSB + LSB) 
  Temperature_11_bit = ((short) (TemperatureData[0]<<8 | TemperatureData[1])) >> 5; // Compute 11-bit temperature output value
  Temperature = ((float) Temperature_11_bit) * 0.125; // Compute temperature in °C
}

 

 

The screenshot below shows the two bytes read of the Temp register. It also illustrates how the OS output is activated in interrupt mode by crossing the Tos threshold value stored in the Tos register. The threshold value was set to 26°C, which corresponds to 16-bit value of 0x1A00 read out of the Temp register.

 

Temp register read.JPG

 

 

The calculated temperature can be watched in the "Variables" window on the top right of the Debug perspective or in the FreeMASTER application. To open and run the FreeMASTER project, install the FreeMASTER 2.0 application and FreeMASTER Communication Driver. You can try touching your finger to the sensor to see the temperature rise.

 

CW LM75A.JPG

 

FreeMASTER LM75A.JPG

 

 

Attached you can find the complete source code written in the CW for MCU's v10.6 including the FreeMASTER project.

 

If there are any questions regarding this simple application, please feel free to ask below. Your feedback or suggestions are also welcome.

 

Regards,

Tomas

Outcomes