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

speed.c

Go to the documentation of this file.
00001 /*@{*//*@}*/
00014 
00015 /******************************************************************************/
00016 /* Includes (#include)                                                        */
00017 #include <avr/io.h>
00018 #include <avr/interrupt.h>
00019 
00020 /* Own header files */
00021 #include "xtypes.h"
00022 #include "global.h"
00023 
00024 #include "timer.h"
00025 #include "memory.h"
00026 
00027 #define EXPORT
00028   #include "speed.h"
00029 #undef EXPORT
00030 
00031 /******************************************************************************/
00032 /* Constants (#define)                *//*@{*/
00033 
00036 #define MAX_RELIABLE_SPEED  70U
00037 
00038 /* End: Constants (#define)                                             *//*@}*/
00039 /******************************************************************************/
00040 
00041 /******************************************************************************/
00042 /* Macro definitions (#define)           *//*@{*/
00043 
00045 #define SPEED_INT_ENABLE()  EIMSK |=  (1<<INT0);
00046 
00048 #define SPEED_INT_DISABLE() EIMSK &= ~(1<<INT0);
00049 /* End: Macro definitions (#define)                                     *//*@}*/
00050 /******************************************************************************/
00051 
00052 /******************************************************************************/
00053 /* Basic types (typedef)                 *//*@{*/
00054 /* End: Basic types (typedef)                                           *//*@}*/
00055 /******************************************************************************/
00056 
00057 /******************************************************************************/
00058 /* Local constants (const)      *//*@{*/
00059 /* End: Local constants (const)                                         *//*@}*/
00060 /******************************************************************************/
00061 
00062 /******************************************************************************/
00063 /* Local variables              *//*@{*/
00064 
00066 STATIC VOLATILE UINT32 mg_u32TickTime;
00067 
00069 STATIC VOLATILE UINT32 mg_u32Rpm;
00070 
00072 STATIC VOLATILE UINT32 mg_u32Distance;
00073 
00075 STATIC          UINT16 mg_u16WheelSize;
00076 
00078 STATIC UINT8  mg_u8NumMagnetsTacho  = DEFAULT_MAGNET_NUMBER_TACHO;
00079 
00080 /* End: Local variables                                                 *//*@}*/
00081 /******************************************************************************/
00082 
00083 /******************************************************************************/
00084 /* Global variables           *//*@{*/
00085 /* End: Global variables                                                *//*@}*/
00086 /******************************************************************************/
00087 
00088 /******************************************************************************/
00089 /* Prototype declarations                                                     */
00090 /* End: Prototype declarations                                                */
00091 /******************************************************************************/
00092 
00093 /******************************************************************************/
00094 /* Local functions             *//*@{*/
00095 
00096 /* ****************************************************************************/
00111 /* ****************************************************************************/
00112 ISR (SIG_INTERRUPT0)
00113 {
00114   LOCAL_STATIC  UINT32  u32CurrentTime;
00115                 UINT32  u32DiffTime;
00116   
00117   SPEED_INT_DISABLE();
00118   
00119   u32CurrentTime = timer_GetSystemTime();
00120   u32DiffTime = u32CurrentTime - mg_u32TickTime;
00121   
00122   /* Maybe there will be false sensor signals from keybouncing.
00123     To filter these out we use a maximum speed definition that gives a
00124     minimum diff time between two sensor signals:
00125     
00126                 wheel_size[mm] * 3.6
00127     --------------------------------------------
00128     max_reliable_speed[km/h] * number_of_magnets
00129     
00130     And to avoid floating point numbers we sustitute 3.6 with 36/10.
00131   */
00132   if(u32DiffTime >= ( (mg_u16WheelSize * 36U) / 
00133                       (MAX_RELIABLE_SPEED * 10U * mg_u8NumMagnetsTacho) )
00134                     )
00135   {
00136     mg_u32TickTime = u32CurrentTime;
00137     mg_u32Rpm      = ONE_MINUTE_MS / u32DiffTime / mg_u8NumMagnetsTacho;
00138     mg_u32Distance += mg_u16WheelSize;
00139   }
00140   
00141   SPEED_INT_ENABLE();
00142 }
00143 
00144 /* End: Local functions                                                 *//*@}*/
00145 /******************************************************************************/
00146 
00147 /******************************************************************************/
00148 /* Global functions          *//*@{*/
00149 
00150 /* ****************************************************************************/
00162 /* ****************************************************************************/
00163 void speed_Init (void)
00164 {
00165   mg_u8NumMagnetsTacho  = memory_Get()->u8NumMagnetsTacho;
00166   if(0U == mg_u8NumMagnetsTacho)
00167   {
00168     mg_u8NumMagnetsTacho = 1U;  // to avoid division by 0
00169   }
00170   
00171   INT0_DDR  &= ~(1U << INT0_BIT);   /* set ext. interrupt pin as input pin */
00172   
00173   /* the effective wheel size is the actual wheel size divided by the number of
00174      magnets */
00175   mg_u16WheelSize = ( memory_Get()->u16WheelSize / mg_u8NumMagnetsTacho );
00176   
00177   /* setup for falling edge */
00178   EICRA |=  (1<<ISC01);
00179   EICRA &= ~(1<<ISC00);
00180   
00181   mg_u32TickTime  = 0U;
00182   mg_u32Rpm       = 0U;
00183   mg_u32Distance  = 0U;
00184   
00185   /* enable interrupt */
00186   SPEED_INT_ENABLE();
00187 }
00188 
00189 /* ****************************************************************************/
00200 /* ****************************************************************************/
00201 UINT32 speed_GetRpm (void)
00202 {
00203   UINT32 u32Ret;
00204   cli();
00205   u32Ret = mg_u32Rpm;
00206   sei();
00207   return (u32Ret);
00208 }
00209 
00210 
00211 /* ****************************************************************************/
00222 /* ****************************************************************************/
00223 UINT32 speed_GetDistance (void)
00224 {
00225   UINT32 u32Ret;
00226   cli();
00227   u32Ret = mg_u32Distance;
00228   sei();
00229   return (u32Ret);
00230 }
00231 
00232 
00233 /* ****************************************************************************/
00248 /* ****************************************************************************/
00249 void speed_CheckTimeout (UINT32 u32SystemTime)
00250 {
00251   UINT32 u32TickTime;
00252   cli();
00253   u32TickTime = mg_u32TickTime;
00254   sei();
00255   if(u32SystemTime >= (u32TickTime + SPEED_TIMEOUT))
00256   {
00257     cli();
00258     mg_u32Rpm = 0U;
00259     sei();
00260   }
00261 }
00262 
00263 /* End: Global functions                                                *//*@}*/
00264 /******************************************************************************/
00265 
00266 /**** Last line of code                                                    ****/

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