00001 /*This file is prepared for Doxygen automatic documentation generation.*/ 00013 00014 /* Copyright (c) 2009 Atmel Corporation. All rights reserved. 00015 * 00016 * Redistribution and use in source and binary forms, with or without 00017 * modification, are permitted provided that the following conditions are met: 00018 * 00019 * 1. Redistributions of source code must retain the above copyright notice, 00020 * this list of conditions and the following disclaimer. 00021 * 00022 * 2. Redistributions in binary form must reproduce the above copyright notice, 00023 * this list of conditions and the following disclaimer in the documentation 00024 * and/or other materials provided with the distribution. 00025 * 00026 * 3. The name of Atmel may not be used to endorse or promote products derived 00027 * from this software without specific prior written permission. 00028 * 00029 * 4. This software may only be redistributed and used in connection with an Atmel 00030 * AVR product. 00031 * 00032 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED 00033 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00034 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE EXPRESSLY AND 00035 * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, 00036 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00037 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00038 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00039 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00040 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 00041 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00042 */ 00043 00044 //_____ I N C L U D E S ___________________________________________________ 00045 00046 #include "config.h" 00047 #include "cal_inter_rc.h" 00048 #include "lib_mcu/temp_sensor/temp_sensor.h" 00049 #include "lib_mcu/flash/flash_drv.h" 00050 #include "lib_mcu/adc/adc_drv.h" 00051 00052 //_____ M A C R O S ________________________________________________________ 00053 00054 00055 //_____ P R I V A T E D E C L A R A T I O N _____________________________ 00056 00057 00058 //_____ D E F I N I T I O N ________________________________________________ 00059 00060 00061 //_____ D E C L A R A T I O N ______________________________________________ 00062 00063 U8 osc_cal_25_3v; 00064 U8 osc_cal_85_3v; 00065 U8 osc_cal_25_5v; 00066 U8 offset; 00067 U16 temp_sensor_25; 00068 U16 temp_sensor_85; 00069 U8 n_step; 00070 U8 period; 00071 U16 temp_value; 00072 00077 void get_cal_values(void) 00078 { 00079 U8 temp_sensor_25_lo; 00080 U8 temp_sensor_25_hi; 00081 U8 temp_sensor_85_lo; 00082 U8 temp_sensor_85_hi; 00083 osc_cal_25_3v = flash_read_sig(0x01); 00084 osc_cal_85_3v = flash_read_sig(0x03); 00085 osc_cal_25_5v = flash_read_sig(0x05); 00086 temp_sensor_25_lo = flash_read_sig(0x38); 00087 temp_sensor_25_hi = flash_read_sig(0x39); 00088 temp_sensor_85_lo = flash_read_sig(0x3A); 00089 temp_sensor_85_hi = flash_read_sig(0x3B); 00090 temp_sensor_25 = (temp_sensor_25_hi << 8) + temp_sensor_25_lo; 00091 temp_sensor_85 = (temp_sensor_85_hi << 8) + temp_sensor_85_lo; 00092 offset = (osc_cal_25_5v) - (osc_cal_25_3v); 00093 n_step = osc_cal_25_3v - osc_cal_85_3v; 00094 period = (temp_sensor_85 - temp_sensor_25)/n_step; 00095 } 00096 00101 void calibrate_inter_osc(enum vcc v) 00102 { 00103 U8 osc_cal; 00104 get_cal_values(); 00105 osc_cal = osc_cal_25_3v; 00106 temp_value = get_temp_sensor_val(); 00107 00108 if(temp_value < temp_sensor_25) 00109 { 00110 for (U16 t = temp_sensor_25; t>0; t-=period) 00111 { 00112 osc_cal++; 00113 if(t<temp_value) 00114 break; 00115 } 00116 } 00117 else if(temp_value >= temp_sensor_85) 00118 osc_cal = osc_cal_85_3v; 00119 else if(temp_value > temp_sensor_25) 00120 { for (U16 t = temp_sensor_25; t<temp_sensor_85; t+=(2*period)) 00121 { 00122 osc_cal--; 00123 if(t>temp_value) 00124 break; 00125 } 00126 } 00127 if(vcc_3) 00128 { 00129 OSCCAL = osc_cal; 00130 } 00131 else OSCCAL = osc_cal + offset; 00132 00133 } 00134 00135 00136 00137 00138 00139
1.7.2