Main Page | File List | Globals

timer.c

Go to the documentation of this file.
00001 /* timer.c --- 
00002  * 
00003  * Filename: timer.c
00004  * Description: 
00005  * Author: Bryce Himebaugh
00006  * Maintainer: 
00007  * Created: Mon Oct 16 11:03:33 2006
00008  * Version: 
00009  * Last-Updated: 
00010  *           By: 
00011  *     Update #: 0
00012  * Keywords: 
00013  * Compatibility: 
00014  * 
00015  */
00016 
00017 /* Commentary: 
00018  * 
00019  * 
00020  * 
00021  */
00022 
00023 /* Change log:
00024  * 
00025  * 
00026  */
00027 
00028 /* This program is free software; you can redistribute it and/or modify
00029  * it under the terms of the GNU General Public License as published by
00030  * the Free Software Foundation; either version 2, or (at your option)
00031  * any later version.
00032  * 
00033  * This program is distributed in the hope that it will be useful,
00034  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00035  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00036  * GNU General Public License for more details.
00037  * 
00038  * You should have received a copy of the GNU General Public License
00039  * along with this program; see the file COPYING.  If not, write to the
00040  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00041  * Boston, MA 02111-1307, USA.
00042  */
00043 
00044 /* Code: */
00045 #include <msp430x16x.h>
00046 #include <vcs_bsp/vcsbsp.h>
00047 #include <signal.h>
00048 
00049 volatile unsigned long TB0_edge_time = 0;
00050 volatile unsigned long vspeed4_period_uS = 0;
00051 volatile int vspeed4_data_ready=0;
00052 volatile int tick_10mS=0;
00053 
00054 void init_timer_a(void) {
00055   // SMCLK/8 SOURCE 
00056   // Timer in Continuous Mode 
00057   // 1mS interrupt rate 
00058   TACTL=TASSEL1|ID1|ID0|MC1|TACLR; 
00059   TACCTL0=CCIE;            
00060   TACCR0=50000;              
00061 }
00062 
00063 interrupt (TIMERA0_VECTOR) Timer_A0_Handler(void) {
00064   TACCR0+=10000;
00065   tick_10mS=1;
00066 }
00067 
00068 void init_timer_b(void) {
00069   // Timer Setup
00070   TBCTL = TBSSEL1+ID1+ID0+TBIE;          // SMCLK/8, Enable the Timer B interrupt, Timer off
00071 
00072   // TB0 Capture Setup 
00073   TBCCTL0 = CM0+CCIS0+SCS+CAP+CCIE;      // Rising Edge, Synchronize, Enable Interrupt   
00074   TBCCTL3 = CM0+CCIS0+SCS+CAP+CCIE;      // Rising Edge, Synchronize, Enable Interrupt   
00075 
00076   // Start Timer B 
00077   TBCTL |= MC1;                          // Continuous 16 bit counter mode 
00078 }
00079 
00080 interrupt (TIMERB0_VECTOR) Timer_B0(void) {
00081   static unsigned int TB0_edge_last = 0;
00082   static int TB0_indicator = 0;
00083   static unsigned int TB0_filter_index=0;
00084   static unsigned long TB0_filter_bank[8] = {TB0_PERIOD_MAX,TB0_PERIOD_MAX,TB0_PERIOD_MAX,TB0_PERIOD_MAX,
00085                                              TB0_PERIOD_MAX,TB0_PERIOD_MAX,TB0_PERIOD_MAX,TB0_PERIOD_MAX};
00086   unsigned int TB0_temp;
00087   unsigned long TB0_accumulator=0;
00088   int i;
00089   
00090   // TB0 Vspeed 4
00091   TB0_temp = TBCCR0;                              // Latch the timer value in the event that another edge arrives. 
00092   if ((TBCTL&TBIFG) && (TB0_temp<0x7fff)) {       // Handle case of timer overflow pending. TB0 is higher priority. 
00093     TB0_edge_time += (unsigned long) 0xFFFF;      // Add in the overflow value. 
00094     TBCTL&=~TBIFG;                                // Clear the int flag so that we do not handle this rollover again
00095   }
00096 
00097   // 1 or more rollovers have occured. 
00098   if (TB0_edge_time) {                            
00099     TB0_edge_time += (unsigned long) 0xFFFF - (unsigned long) TB0_edge_last + (unsigned long) TB0_temp;
00100     TB0_edge_time -= (unsigned long) 0xFFFF;
00101   }
00102   // No rollovers have occured.
00103   else {
00104     TB0_edge_time = (unsigned long) TB0_temp - (unsigned long) TB0_edge_last; 
00105   }
00106   
00107   // Clamp the largest possible period to TB0_PERIOD_MAX
00108   if (TB0_edge_time > TB0_PERIOD_MAX) {
00109       TB0_edge_time=TB0_PERIOD_MAX;
00110   }
00111 
00112   // Filter the signal
00113   TB0_filter_bank[TB0_filter_index++] = TB0_edge_time; // grab the latest data
00114   TB0_filter_index&=0x07;                              // Wrap the index to make the array circular     
00115   for (i=0;i<8;i++) {                                  // sweep through the array
00116     TB0_accumulator+=TB0_filter_bank[i];               // Sum the terms in the filter array   
00117   }
00118   TB0_accumulator>>=3;                                 // Divide by the array length (8);
00119   
00120   // report the new data if the last piece has been read. Prevents read coherency issue with unsigned long. 
00121   if (!vspeed4_data_ready) {
00122     vspeed4_period_uS = TB0_accumulator;// TB0_edge_time;// TB0_accumulator; // 
00123     vspeed4_data_ready=1;
00124   }
00125 
00126   // prepare for the next edge. 
00127   TB0_edge_last = TB0_temp;
00128   TB0_edge_time = 0;
00129 
00130   // Blink the TB0 Led 
00131   if ((TB0_indicator^=1)) {
00132     DS1_ON;
00133   }
00134   else {
00135     DS1_OFF;
00136   }
00137 }
00138 
00139 interrupt (TIMERB1_VECTOR) Timer_B1(void) {
00140   static int gps_pulse_indicator=0;
00141   static int gps_pulse_timeout=0;
00142 
00143   switch (TBIV) {
00144   case 0x02: 
00145     // TB1 VSpeed3
00146     break;
00147   case 0x04:
00148     // TB2 VSpeed2  
00149     break;
00150   case 0x06:
00151     // TB3 VSpeed1  
00152     if (gps_pulse_indicator) {
00153       DS4_ON;
00154     }
00155     else {
00156       DS4_OFF;
00157     }
00158     gps_pulse_indicator^=1;
00159     gps_pulse_timeout=0;
00160     break;
00161   case 8:
00162     // Channel 4
00163     break;
00164   case 0x0A:
00165     // Channel 5
00166   case 0x0C:
00167     // Channel 6
00168     break;
00169   case 0x0E:
00170     // Timer Overflow 
00171     TB0_edge_time += (unsigned long) 0xFFFF;
00172     if (TB0_edge_time > TB0_PERIOD_MAX) {
00173       TB0_edge_time=TB0_PERIOD_MAX;
00174       if (!vspeed4_data_ready) {
00175         vspeed4_period_uS = TB0_edge_time;
00176         vspeed4_data_ready=1;
00177       }
00178     }
00179     if (gps_pulse_timeout>7) {
00180       DS4_OFF;                        // if we have not seen a transition in 500mS then turn off the indicator LED. 
00181     }
00182     else {
00183       gps_pulse_timeout++;
00184     }
00185 
00186     break;
00187   }
00188 }
00189 
00190 
00191 /* timer.c ends here */

Generated on Fri Nov 3 14:40:12 2006 for hwiflib by  doxygen 1.3.9.1