Вы находитесь на странице: 1из 13

2016

Interfacing Temperature sensor


with
MPLAB Xpress Evaluation Board
Using I2C communication

Author: A.Siva
Reviewers:

Introduction:
MPLAB Xpress IDE cost free development platform. Its cloud Based IDE available from
microchip supporting PIC-based microcontrollers. The platform is comprised of code editor,
build automation tools, debugger, code configurator. MPLAB Xpress IDE is an end-to-end
solution enabling engineers to develop their applications from initial evaluation to final
production.
Component Requirement
Hardware:
o MPLAB Xpress Evaluation tool
o Micro-B cable
o Temperature sensor
Software
o MPLAB Xpress IDE
Note: we have onboard Temperature sensor connected with RC3,RC4

Temperature
sensor

Step 1: Open your Browser and go to following link


https://mplabxpress.microchip.com/mplabcloud/ide

Figure 1 MPLAB Xpress IDE main window

Step 2: start creating our new project. Go to File >> New Project. Select microchip embedded
as well as standalone project then click next

Figure 2 Open new project

Step 3: Select device pic16f18855, and click next .

Figure 3 select Device

Step 4: then give project name and click finish.

Figure 4 give project name

Step 5: Now choose mplab xpress code configurator if its not present in your Device please
Download and install from following link. http://www.microchip.com/mplab/mplab-codeconfigurator

Figure 5 Select mplab xpress code configurator

Step 6: Now we can see our mplab xpress configuration window and select system module in
mplab xpress configuration window .

Figure 6 Assign project name

Step 7: Make oscillator configuration and select I2C peripheral from device Resource

Figure 7 select pin

Step 8: Select pin RC3(I2C) ,RC4 (I2C) and select RC0 (UART) and make all selected pin as
digital.

Figure 8 pin configuration set

SOURCE CODE:
#include "mcc_generated_files/mcc.h"
#define EMC1001_ADDRESS

0x38

// SMB device address

bool EMC1001_Read(uint8_t reg, uint8_t *pData)


{
I2C2_MESSAGE_STATUS status = I2C2_MESSAGE_PENDING;
static I2C2_TRANSACTION_REQUEST_BLOCK trb[2];
I2C2_MasterWriteTRBBuild(&trb[0], &reg, 1, EMC1001_ADDRESS);
I2C2_MasterReadTRBBuild(&trb[1], pData, 1, EMC1001_ADDRESS);
I2C2_MasterTRBInsert(2, &trb[0], &status);

//2transactions W + R

while(status == I2C2_MESSAGE_PENDING);

// blocking

return (status == I2C2_MESSAGE_COMPLETE);


}
bool EMC1001_Write(uint8_t reg, uint8_t data)
{
uint8_t buffer[2];
buffer[0] = reg; buffer[1] = data;
I2C2_MESSAGE_STATUS status = I2C2_MESSAGE_PENDING;
static I2C2_TRANSACTION_REQUEST_BLOCK trb;
I2C2_MasterWriteTRBBuild(&trb, buffer, 2, EMC1001_ADDRESS);
I2C2_MasterTRBInsert(1, &trb, &status); // 1 transaction W
while(status == I2C2_MESSAGE_PENDING);

// blocking

return (status == I2C2_MESSAGE_COMPLETE);


}
// EMC1001 registers
#define EMC_TEMP_HI

0x00

// temperature value high byte

#define EMC_STATUS

0x01

// Status register

#define EMC_TEMP_LO

0x02

// low byte containing 1/4 deg

fraction
#define EMC_CONFIG

0x03

#define EMC_LIMIT

0x20

// configuration register
// THERM limit temperature (->ALARM1)

#define EMC_HIST

0x21

// THERM hysteresis

#define EMC_PROD_ID

0xFD

// Product ID

#define EMC_MANUF_ID

0xFE

// Manufacturer ID

#define EMC_REVISION

0xFF

// Revision

void main(void)
{
uint8_t data;
int8_t

temp;

uint8_t templo;
SYSTEM_Initialize();
INTERRUPT_GlobalInterruptEnable();
INTERRUPT_PeripheralInterruptEnable();
EMC1001_Write(EMC_LIMIT, 28);

// set the THERM limit

EMC1001_Write(EMC_HIST,

// reduce the hysteresis

2);

while (1) {
printf("\x0C");

// if terminal does not support Form Feed

puts("Temperature Sensor\n");
if (EMC1001_Read(EMC_PROD_ID, &data))
printf("Product ID: EMC1001%s\n", data ? "-1" : "");
if (EMC1001_Read(EMC_MANUF_ID, &data))
printf("Manufacturer ID: 0x%X\n", data);
if (EMC1001_Read(EMC_REVISION, &data))
printf("Revision : %d\n", data);
if (EMC1001_Read(EMC_TEMP_HI, (uint8_t*)&temp)) {
EMC1001_Read(EMC_TEMP_LO, &templo);

// get lsb

templo = templo >> 6;


if (temp < 0) templo = 3-templo; // complement to 1 if T negative
printf("\nThe temperature is: %d.%d C\n", temp, templo*25);
}
if (EMC1001_Read(EMC_STATUS, (uint8_t*)&temp)) {
if (temp & 1)

puts("ALARM1: The temperature is above the set limit!");


else
puts("The temperature is below the set limit");
}
__delay_ms(1000);
}
}

Step 10: Go to your MPLAP xpress IDE Erase all existing code and copy above code past there
and add library files then make clean and build for Export . if you done this go to download
you can see hex file for your project.

Figure 10 Build the project

Step 11: Now, if all goes well connect the Micro B cable to pic16f18855 (mplab xpress
demonstration board) and connect it to your computer. If you done you can see your devise.
And copy that Hex file to your device. And make hardware connection.

OUTPUT

Figure 12 output

Pin connection

RC0

Prolific cable Rx

MICROCONTROLLER
SDA

SCL

PC

RX

TX

TX

RX

For more information please visit: www.tenettech.com


For technical query please send an e-mail: info@tenettech.com

Temperature
sensor

Вам также может понравиться