hall_sensor_ISR.c

Go to the documentation of this file.
00001 /*
00002 **
00003 ****************************************************************************
00004 **
00005 **
00006 **             Copyright (c) 2005 - Atmel Corporation
00007 **             Proprietaty Information
00008 **
00009 ** Project     : AT90CAN128 / ATmega88 window lift
00010 ** Module      : hall_sensor_ISR.c
00011 ** Description : Module, interrupt subroutine
00012 **
00013 ** Version :     Date:         Author:      Comment:
00014 **    1.0        June.2005    F.G.          Creation
00015 **    1.1        July.2006    F.G.          Adaptability improvements
00016 **    1.2        May.2007     F.G           Add GCC compiler support
00017 **
00018 **
00019 **
00020 **              The following Fuses should be set:-
00021 **
00022 **
00023 **
00024 ** LICENSE -
00025 **
00026 ** ATMEL - 2005
00027 ** All software programs are provided 'as is' without warranty of any kind:
00028 ** Atmel does not state the suitability of the provided materials for any
00029 ** purpose. Atmel hereby disclaim all warranties and conditions with regard
00030 ** to the provided software, including all implied warranties, fitness for
00031 ** a particular purpose, title and non-infringement.In no event will Atmel
00032 ** be liable for any indirect or consequential damages or any damages
00033 ** whatsoever resulting from the usage of the software program.
00034 ****************************************************************************
00035 **
00036 */
00037 
00038 #include "config.h"
00039 #include "lib_window\window_lib.h"
00040 #include "lib_window\hall_sensor_ISR.h"
00041 #include "timer1_ovf_isr.h"
00042 
00043 /*_____ G L O B A L S ________________________________________________________*/
00044 
00047 U8 hall_direction;
00048 
00050 signed int dv;      
00051 
00052 /*_____ P R I V A T E - F U N C T I O N S ____________________________________*/
00053 
00054 #if defined(__ICCAVR__)                       // IAR COMPILER USED
00055   #pragma vector = TIMER0_OVF_vect
00056   __interrupt void TIMER0_OVF_ISR (void)      
00057 #elif defined (__AVR__)                       // GCC Compiler is being used
00058   ISR(TIMER0_OVF_vect)
00059 #endif
00060   {
00061      ADCSRA |= 1<<ADEN | 1 << ADSC; // enable ADC and Start Conversion
00062   }
00063   
00064   
00076 #if defined(__ICCAVR__)                       // IAR COMPILER USED  
00077   #pragma vector = PCINT1_vect   //Pin Change interrupt vector for ATmega88
00078   __interrupt void hall_sensor_ISR (void)     
00079 #elif defined (__AVR__)                       // GCC Compiler is being used
00080   ISR(PCINT1_vect)
00081 #endif
00082 {
00083   unsigned int vit;   // to save content of timer1, to compute derivative speed
00084  
00085                       // time difference between two samples
00086   static U16 vitm1;   // Period (equivalent to speed) at last occurence
00087   static U16 vitm2;   // 2nd older Period (equivalent to speed) 
00088 
00089 #if (HALL_SENSOR_NUMBER == 2)
00090   static U16 change=0;// number of consecutive direction changes
00091 #endif
00092   
00093   vit=TCNT1;
00094   TCNT1=0;            // clear Timer Counter 1
00095   
00096 /* ---------------------- TWO SENSORS ARE USED -------------------------------*/
00097 #if (HALL_SENSOR_NUMBER == 2)
00098   if( _TEST_SENS() ) //_TEST_SENS: Using both sensors to determine direction
00099   {
00100     if (hall_direction == DOWNWARD) // Did a direction change occured?
00101       change=0;                     // No direction changes
00102     else
00103       change++;   // compt successive direction changes to detect malfunction
00104     hall_direction = DOWNWARD;    // measured direction
00105     position++;                   // determine position
00106     save_k=0;                     // position modified flag 
00107   }                               // parameters storage is necessary
00108   else
00109   {
00110     if (hall_direction == UPWARD) //Did a direction change occured?
00111       change=0;
00112     else
00113       change++;   // compt successive direction changes to detect malfunction
00114     hall_direction = UPWARD;
00115     if (position>0)
00116     {
00117       position--;
00118       save_k=0;   // position modified flag : parameters storage is necessary
00119     }
00120   }
00121   if (change > 30)              // number of consecutive direction changes > 30
00122   {                             // hall sensor may be disconnected
00123       /*INSERT CODE HERE
00124     TO REPORT A DEFAULT*/
00125     force_window_state(STOPING_STATE); //force motor to stop after 30 increments
00126     change=0;                          // of the last working sensor
00127   }
00128   
00129 /* ------------------------ ONE SENSOR IS USED -------------------------------*/  
00130 #elif (HALL_SENSOR_NUMBER == 1)
00131   // Use command direction to determine position
00132   if ((direction == DOWNWARD) && (position < 0xFFFF))
00133   {
00134     position++;   // Origin is at window top: increment position
00135     save_k=0;     // position modified: parameters storage is necessary
00136   }
00137   else if ((direction == UPWARD) && (position > 0))
00138   {
00139     position--;   // Origin is at window top: decrement position
00140     save_k=0;     // position modified: parameters storage is necessary
00141   }
00142 #else
00143 #error "HALL_SENSOR_NUMBER (1 or 2) shall be defined in config .h"
00144 #endif
00145       
00146 /* ------------------------ REFERENCE COMPUTATION ----------------------------*/      
00147   dv=vit-((vitm2 + vitm1)>>1);
00148   if ((dv<0) || (ref>current))   // if derivative period <= 0 (equivalent to 
00149   {                              // derivative speed >= 0)                             
00150     ref = (current+ref)>>1;      //update current reference
00151   }
00152   vitm2 = vitm1;
00153   vitm1 = vit;                   // backup speed: to become last sample
00154 }
00155 
00156 // get position: to get position safely
00157 U16 get_position(void)
00158 {
00159   U16 temp;
00160   __disable_interrupt();
00161   temp = position;
00162   __enable_interrupt();
00163   return (temp);
00164 }
00165 
00166 // put position: to put position safely
00167 void put_position(U16 position_temp)
00168 {
00169   __disable_interrupt();
00170   position = position_temp;
00171   __enable_interrupt();
00172 }
00173 
00174 signed int get_hall_delta_period(void)
00175 {
00176   signed int temp;
00177   __disable_interrupt();
00178   temp = dv;
00179   __enable_interrupt();  
00180   return (temp);
00181 }

Generated on Mon May 28 20:26:50 2007 for WINDOWLIFTLIBRARY by  doxygen 1.4.7