• Main Page
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

voltage.c

Go to the documentation of this file.
00001 /*@{*//*@}*/
00014 
00015 /******************************************************************************/
00016 /* Includes (#include)                                                        */
00017 #include <avr/io.h>
00018 
00019 /* Own headers */
00020 #include "xtypes.h"
00021 #include "global.h"
00022 
00023 #include "adc.h"
00024 #include "safety.h"
00025 
00026 #define EXPORT
00027   #include "voltage.h"
00028 #undef EXPORT
00029 
00030 /******************************************************************************/
00031 /* Constants (#define)                *//*@{*/
00032 
00039 #define AVG_COUNT   40U
00040 
00041 /* End: Constants (#define)                                             *//*@}*/
00042 /******************************************************************************/
00043 
00044 /******************************************************************************/
00045 /* Macro definitions (#define)           *//*@{*/
00046 /* End: Macro definitions (#define)                                     *//*@}*/
00047 /******************************************************************************/
00048 
00049 /******************************************************************************/
00050 /* Basic types (typedef)                 *//*@{*/
00051 /* End: Basic types (typedef)                                           *//*@}*/
00052 /******************************************************************************/
00053 
00054 /******************************************************************************/
00055 /* Local constants (const)      *//*@{*/
00056 /* End: Local constants (const)                                         *//*@}*/
00057 /******************************************************************************/
00058 
00059 /******************************************************************************/
00060 /* Local variables              *//*@{*/
00061 
00063 STATIC UINT16 mg_u16Ref                 = 0U;
00064 
00066 STATIC UINT16 mg_u16Value               = 0U;
00067 
00070 STATIC UINT16 mg_au16Data[AVG_COUNT];
00071 
00073 STATIC UINT16 mg_u16Avg                 = 0U;
00074 
00076 STATIC UINT8  mg_u8Idx                  = 0U;
00077 
00078 /* End: Local variables                                                 *//*@}*/
00079 /******************************************************************************/
00080 
00081 /******************************************************************************/
00082 /* Global variables           *//*@{*/
00083 /* End: Global variables                                                *//*@}*/
00084 /******************************************************************************/
00085 
00086 /******************************************************************************/
00087 /* Prototype declarations                                                     */
00088 /* End: Prototype declarations                                                */
00089 /******************************************************************************/
00090 
00091 /******************************************************************************/
00092 /* Local functions             *//*@{*/
00093 /* End: Local functions                                                 *//*@}*/
00094 /******************************************************************************/
00095 
00096 /******************************************************************************/
00097 /* Global functions          *//*@{*/
00098 
00099 /* ****************************************************************************/
00110 /* ****************************************************************************/
00111 void voltage_Init (void)
00112 {
00113   UINT16 u16Help = adc_Read(ADC_VOLTAGE);
00114   
00115   mg_u16Ref   = (adc_Read(ADC_VREF)-695U) * 26/10;
00116   mg_u16Value = u16Help;
00117   
00118   for(UINT8 i=0U; i<AVG_COUNT; i++)
00119   {
00120     mg_au16Data[i] = u16Help;
00121   }
00122   mg_u16Avg = u16Help;
00123   mg_u8Idx = 0U;
00124 }
00125 
00126 
00127 /* ****************************************************************************/
00136 /* ****************************************************************************/
00137 void voltage_Update (void)
00138 {
00139   mg_u16Ref   = (adc_Read(ADC_VREF)-695U) * 26/10;
00140   mg_u16Value = adc_Read(ADC_VOLTAGE);
00141 }
00142 
00143 
00144 /* ****************************************************************************/
00161 /* ****************************************************************************/
00162 void voltage_UpdateAvg (void)
00163 {
00164   UINT16 u16Help = adc_Read(ADC_VOLTAGE);
00165   
00166   /* insert voltage value in ring array */
00167   mg_au16Data[mg_u8Idx] = u16Help;
00168   
00169   /* step to next position in array */
00170   mg_u8Idx++;
00171   if(mg_u8Idx >= AVG_COUNT)
00172   {
00173     mg_u8Idx = 0U;
00174   }
00175   
00176   /* calculate new average value */
00177   u16Help = 0U;
00178   for(UINT8 i=0U; i<AVG_COUNT; i++)
00179   {
00180     u16Help += mg_au16Data[i];
00181   }
00182   u16Help /= AVG_COUNT;
00183   mg_u16Avg = u16Help;
00184   
00185   if(mg_u16Avg < mg_u16Ref)
00186   {
00187     safety_SetCondition(COND_VOLT_ERR);
00188   }
00189   else
00190   {
00191     safety_ClearCondition(COND_VOLT_ERR);
00192   }
00193 }
00194 
00195 
00196 /* ****************************************************************************/
00206 /* ****************************************************************************/
00207 UINT16 voltage_GetValue (void)
00208 {
00209   return (mg_u16Value);
00210 }
00211 
00212 
00213 /* ****************************************************************************/
00223 /* ****************************************************************************/
00224 UINT16 voltage_GetAvg (void)
00225 {
00226   return (mg_u16Avg);
00227 }
00228 
00229 
00230 /* ****************************************************************************/
00240 /* ****************************************************************************/
00241 UINT16 voltage_GetRef (void)
00242 {
00243   return (mg_u16Ref);
00244 }
00245 
00246 
00247 /* End: Global functions                                                *//*@}*/
00248 /******************************************************************************/
00249 
00250 /**** Last line of code                                                    ****/

Generated on Sun Jan 23 2011 14:05:40 for ULA replacement firmware by  doxygen 1.7.2