// ******************************************************************************************* // // Include file for PIC24FJ64GA002 microcontroller. This include file defines // MACROS for special function registers (SFR) and control bits within those // registers. #include "p24fj64ga002.h" // ******************************************************************************************* // // Configuration bits for CONFIG1 settings. // // Make sure "Configuration Bits set in code." option is checked in MPLAB. // This option can be set by selecting "Configuration Bits..." under the Configure // menu in MPLAB. // // These settings are appropriate for debugging the PIC microcontroller. If you need to // program the PIC for standalone operation, change the COE_ON option to COE_OFF. _CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & BKBUG_ON & COE_ON & ICS_PGx1 & FWDTEN_OFF & WINDIS_OFF & FWPSA_PR128 & WDTPS_PS32768 ) // ******************************************************************************************* // // Configuration bits for CONFIG2 settings. // Make sure "Configuration Bits set in code." option is checked in MPLAB. // This option can be set by selecting "Configuration Bits..." under the Configure // menu in MPLAB. _CONFIG2( IESO_OFF & SOSCSEL_SOSC & WUTSEL_LEG & FNOSC_PRIPLL & FCKSM_CSDCMD & OSCIOFNC_OFF & IOL1WAY_OFF & I2C1SEL_PRI & POSCMOD_XT ) // ******************************************************************************************* // #define XTFREQ 7372800 // On-board Crystal frequency #define PLLMODE 4 // On-chip PLL setting (Fosc) #define FCY (XTFREQ*PLLMODE)/2 // Instruction Cycle Frequency (Fosc/2) // ******************************************************************************************* // #define I2CSCLRATE 400000 #define I2CBRGVAL ((FCY/I2CSCLRATE)-(FCY/10000000))-1 // ******************************************************************************************* // #define TMP102_ADDR 0x90 #define TMP102_TEMPREG 0x00 // ******************************************************************************************* // // // Initialize I2C component and TMP102 sensor for reading temperature values. This function // will configure the pointer register within the TMP102 sensor to the address for reading // the temperature void TMP102Initialize() { // Configure I2C component for PIC24F // TODO: Configure I2C baud rate generator register (I2C1BRG) // TODO: Enable I2C // TODO: Configure SMEN bit if SMBus voltage levels are used // TODO: Clear Master I2C interrupt flag (MI2C1IF) // TODO: Send start by setting SEN to 1, wait for interrupt, clear interrupt flag // Write to TMP102 Temperature Register to set the Pointer Register by // 1. Writing device address and write command to I2C transmit registers (I2C1TRN), // wait for interrupt, clear interrupt I2C1TRN = TMP102_ADDR | 0x00; while(IFS1bits.MI2C1IF==0); IFS1bits.MI2C1IF=0; // 2. Write value for pointer register within TMP102 sensor, wait for interrupt, // clear interrupt flag I2C1TRN = TMP102_TEMPREG; while(IFS1bits.MI2C1IF==0); IFS1bits.MI2C1IF=0; // TODO: Send stop by asserting PEN to 1, wait for interrupt, clear interrupt flag } // ******************************************************************************************* // // // Reads 12-bit temperature value from TMP102 sensor as a 16-bit value using the I2C component // and returns the temperature in Fahrenheit. // // NOTE: The value read directly from the temperature sensor is in Celsius, and needs to be // converted to Fahrenheit. int TMP102GetTemperature() { long int temperature; // TODO: Send start by setting SEN to 1, wait for interrupt, clear interrupt flag // TODO: Writing device address and read command to I2C transmit registers (I2C1TRN), // wait for interrupt, clear interrupt // TODO: Start I2C receive by setting RCEN to 1, wait for interrupt, clear interrupt flag // Received byte is stored within I2C receive register (I2C1RCV) temperature = ((unsigned long int)I2C1RCV << 8); // TODO: Send acknowledge by setting ACKEN to 1, wait for interrupt, clear interrupt flag // TODO: Start I2C receive by setting RCEN to 1, wait for interrupt, clear interrupt flag // Received byte is stored within I2C receive register (I2C1RCV) temperature = temperature | I2C1RCV; // TODO: Send acknowledge by setting ACKEN to 1, wait for interrupt, clear interrupt flag // TODO: Send stop by asserting PEN to 1, wait for interrupt, clear interrupt flag // TODO: Convert temperature to Fahrenheit // Return temperature reading. return temperature; } // ******************************************************************************************* //