Home 9 The Art of Technology 9 PIC10F322 XC8 Code: How to Access the Internal Temperature Module

PIC10F322 Internal Temperature Module

The PIC10F322 microcontroller is equipped with an internal temperature indicator module that measures the operating temperature of the silicon die. This feature is crucial for ensuring that the microcontroller operates within its thermal limits, which helps protect the device and other components in enclosed systems.

Why Monitor the Die Temperature?

Monitoring the temperature of the silicon die is essential for several reasons:

  • Thermal Management: Ensures the microcontroller is operating within safe temperature limits, preventing overheating and potential damage.
  • System Protection: In enclosed products, monitoring the internal temperature helps protect other components by providing early warnings of thermal issues.
  • Reliability: Maintaining the microcontroller within its thermal ratings improves the overall reliability and longevity of the system.

Accessing the Internal Temperature Sensor

Let’s explore how to read the internal temperature sensor using the XC8 compiler with the PIC10F322.

/****************************************************************************
* Title                 :   PIC10F322 Temperature Module Example
* Filename              :   10F322_TempMod.c
* Author                :   Jamie Starling
* Origin Date           :   2022/09/21
* Version               :   1.0.0
* Compiler              :    
* Target                :    
* Copyright             :   Jamie Starling
* All Rights Reserved
*
* THIS SOFTWARE IS PROVIDED BY JAMIE STARLING "AS IS" AND ANY EXPRESSED
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL JAMIE STARLING OR ITS CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*
*******************************************************************************/

/******************************************************************************
* Includes
*******************************************************************************/
#include <xc.h>
#include <stdint.h>

/******************************************************************************
* Device Configuration
*******************************************************************************/
#pragma config FOSC = INTOSC  // Oscillator Selection 
#pragma config BOREN = ON    // Brown-out Reset
#pragma config WDTE = OFF    // Watchdog Timer
#pragma config PWRTE = ON    // Power-up Timer
#pragma config MCLRE = OFF   // MCLR Pin Function Select bit->MCLR pin function is digital input, MCLR internally tied to VDD
#pragma config CP = OFF      // Code Protection 
#pragma config LVP = OFF     // Low-Voltage Programming 
#pragma config LPBOR = ON    // Brown-out Reset Selection bits
#pragma config BORV = LO    // Brown-out Reset Voltage Selection
#pragma config WRT = OFF    // Flash Memory Self-Write Protection

//Used to calculate the delay time - Change depending on processor Speed
#define _XTAL_FREQ 16000000  //16Mhz



/******************************************************************************
* Function Prototypes
*******************************************************************************/
void DeviceSetup(void);
void Setup_EnableTempModule(void);
void Setup_ConfigADCForTempRead(void);
uint8_t ADC_Read(void);



/******************************************************************************
* Functions
*******************************************************************************/

/******************************************************************************
* Function : main()
*//** 
*  Description: MAIN Function 
*
*  
********************************************************************************/
void main(void)
{
    DeviceSetup();    
    
    uint8_t TempMod_Value = 0;
    
    while(1)
    {
        TempMod_Value = ADC_Read();
        __delay_ms(50);        
        
    }
}

/******************************************************************************
* Function : DeviceSetup()
*//** 
*  Description: 
     * Sets Operating Freq to 16Mhz
     * Calls The Temperature Module Setup Function
     * Calls The ADC COnfigurtion / Setup Function
*
*  
********************************************************************************/
void DeviceSetup(void){
    OSCCONbits.IRCF = 0b111;  //Set System Clock to 16Mhz
    Setup_EnableTempModule();
    Setup_ConfigADCForTempRead();
}

/******************************************************************************
* Function : Setup_EnableTempModule()
*//** 
*  Description: 
     * Enabled Temperature Module for High Range - The high range, selected by
     -  setting the TSRNG bit of the FVRCON register, provides a wider output
     -  voltage. This provides more resolution over the temperature range.
*
*  
********************************************************************************/

void Setup_EnableTempModule(void)
{
    FVRCONbits.TSRNG = 1; //Enable High Range
    FVRCONbits.TSEN = 1; //Enable TEMPERATURE INDICATOR MODULE   
}

/******************************************************************************
* Function : Setup_ConfigADCForTempRead()
*//** 
*  Description: 
     * Sets the ADC conversion clock 
     * Sets the ADC channel to Temperature Module
*
*  
********************************************************************************/

void Setup_ConfigADCForTempRead(void)
{
    //Set Analog conversion clock FOSC/32, since we are running 16Mhz,
    //we need to have a conversion time at or greater 1uS
    //FOSC/32 will give us 2uS
    ADCONbits.ADCS = 0b010;    
    //Select the Analog channel - to Temp Mod
    ADCONbits.CHS = 0b110;    
    //Turn on the ADC module
    ADCONbits.ADON = 1;       
}


/******************************************************************************
* Function : ADC_Read()
*//** 
*  Description: 
    *Starts the ADC read and waits until a conversion is complete before returning
    *Returns the ADC value - on the PIC10F322 it is 8bits. 
    **Note we already set the ADC channel to the Temp Mod in the ADC setup function
     -all we have to do is read it. 
*
*  
********************************************************************************/
uint8_t ADC_Read(void)
{

    ADCONbits.GO_nDONE = 1;  //Start the conversion
    
    while (ADCONbits.GO_nDONE == 1){
        NOP();
    }
    
    return ADRES;
}



Have a Project or Idea!?

Seeking Bespoke Technology Solutions?

jamie@jamiestarling.com


Pin It on Pinterest

Share This