Select Page

In the pervious post – we setup the PIC10F322 ADC to read the Internal Temperature Sensor.

Mostly the sensor is used to monitor the die to ensure that it is operating within its limits of thermal rating. In enclosed products, the microcontroller’s internal temperature can be monitored for protection of other components that are part of the system.

However, we can get creative with it if we don’t need it for that function.  For example to seed a random number generator.

Here is some XC8 code that does just that, and a Random generator function that I use.

/****************************************************************************
* 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


/******************************************************************************
* Variables
*******************************************************************************/
uint8_t RANDOM;  //Used for the Random Number generator 

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



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

/******************************************************************************
* Function : main()
*//** 
*  Description: MAIN Function 
*
*  
********************************************************************************/
void main(void)
{
    DeviceSetup();
    
    uint8_t RandomNumber; //Var to hold our random number
    
    
    //Read the ADC to get the temp value - Assign it to var RANDOM which is the seed 
    //for the Random number generator. 
    RANDOM = ADC_Read();    
    
    while(1)
    {
        RandomNumber = GenRandom();
        __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;
}


/******************************************************************************
* Function : GenRandom()
*//** 
*  Description: Returns a random number 
     * Set Varible RANDOM for the Seed 
*
*  
********************************************************************************/
uint8_t GenRandom(void)
{
__asm("RLF     _RANDOM,W");
__asm("RLF     _RANDOM,W");
__asm("BTFSC   _RANDOM,4");
__asm("XORLW   1");
__asm("BTFSC   _RANDOM,5");
__asm("XORLW   1");
__asm("BTFSC   _RANDOM,3");
__asm("XORLW   1");
__asm("MOVWF   _RANDOM");          

return RANDOM;
}

Let’s talk!

I am always looking for oppunities to put my skills to work.
I am always looking to connect with like minded people.

Want to connect – drop me an email

jamie@jamiestarling.com