In some boards which environment temperature goes up could cause high temperature in micro controller. This will effect on micro and bad situation maybe occurs. For example I have designed a device which used in a box. This box was near a motor and after some load to motor I encountered a high temperature near micro. So, anyone could help me if there is any solution for finding temperature of micro? This micro could be AVR or PIC. If micro could find it's temperature, it could terminate working before any bad damage.
At least some (I don't know about all of them) AVRs have a temperature sensor on-chip, which is connected to the ADC.
The temperature measurement is based on an on-chip temperature sensor that is coupled to a single ended ADC channel. The sensor is a diode that produces a temperature dependent voltage. This voltage is measured with the ADC. The voltage has a linear relationship to temperature and the result has approximately a 1 LSB/°C correlation to temperature.
(from this application note)
A number of PIC devices also have an internal temperature indicator, like the PIC16F15xx and 16F18xx.
This family of devices is equipped with a temperature circuit designed to measure the operating temperature of the silicon die. The circuit’s range of operating temperature falls between -40°C and +85°C. The output is a voltage that is proportional to the device temperature. The output of the temperature indicator is internally connected to the device ADC. The circuit may be used as a temperature threshold detector or a more accurate temperature indicator, depending on the level of calibration performed. A one-point calibration allows the circuit to indicate a temperature closely surrounding that point. A two-point calibration allows the circuit to sense the entire range of temperature more accurately.
(from this datasheet)
AVR122: Calibration of the AVR's internal temperature reference, Atmel application note,
Using the AVR internal temperature sensor,
PIC16F15xx datasheet, p.133
AN1333: Use and Calibration of the Internal Temperature Indicator, Microchip application note,
Some micros come with temperature sensors built in. There are some PIC 16, for example, but I don't remember the specific model numbers. On other PICs, the watchdog timer interval is a predictable function of temperature. You can determine temperature with a initial calibration and then measuring the watchdog interval at run time. None of these methods are very accurate, but if you only need a rough idea of too hot, then might be good enough.
For anything else, use a external temperature sensor. There are many available, both digital and analog. If you have a spare analog input pin on the micro, then a simple analog sensor will probably be easiest. Otherwise, you will need to use something like IIC, SPI, or one-wire.
The PIC16F1503 is one that has an internal temperature sensor. Others are the PIC16F1526/7/8/9.