00001 /* FreeEMS - the open source engine management system 00002 * 00003 * Copyright 2009 Sean Keys 00004 * 00005 * This file is part of the FreeEMS project. 00006 * 00007 * FreeEMS software is free software: you can redistribute it and/or modify 00008 * it under the terms of the GNU General Public License as published by 00009 * the Free Software Foundation, either version 3 of the License, or 00010 * (at your option) any later version. 00011 * 00012 * FreeEMS software is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with any FreeEMS software. If not, see http://www.gnu.org/licenses/ 00019 * 00020 * We ask that if you make any changes to this file you email them upstream to 00021 * us at admin(at)diyefi(dot)org or, even better, fork the code on github.com! 00022 * 00023 * Thank you for choosing FreeEMS to run your engine! 00024 */ 00025 00026 00048 #define LT1_360_8_C 00049 #include "inc/freeEMS.h" 00050 #include "inc/interrupts.h" 00051 #include "inc/LT1-360-8.h" 00052 00053 00054 unsigned short VCAS = 0; /* create our virtual Cam Angle Sensor */ 00055 00060 void LT1PTInit(void){ 00061 /* set pt1 to capture on rising and falling */ 00062 00063 } 00064 00065 00066 /* set overflow to 8 so that it will fire an interrupt on every 8th tooth, basically making it a 45tooth CAS */ 00071 void PrimaryRPMISR(void){ 00072 /* Clear the interrupt flag for this input compare channel */ 00073 TFLG = 0x01; 00074 00075 /* Save all relevant available data here */ 00076 // unsigned short codeStartTimeStamp = TCNT; /* Save the current timer count */ 00077 // unsigned short edgeTimeStamp = TC0; /* Save the edge time stamp */ 00078 unsigned char PTITCurrentState = PTIT; /* Save the values on port T regardless of the state of DDRT */ 00079 // unsigned short PORTS_BACurrentState = PORTS_BA; /* Save ignition output state */ 00080 00081 // unsigned char risingEdge; /* in LT1s case risingEdge means signal is high */ 00082 // if(fixedConfigs1.coreSettingsA & PRIMARY_POLARITY){ 00083 // risingEdge = PTITCurrentState & 0x01; 00084 // }else{ 00085 // risingEdge = !(PTITCurrentState & 0x01); 00086 // } 00087 00088 PORTJ |= 0x80; /* Echo input condition on J7 */ 00089 if(!isSynced){ /* If the CAS is not in sync get window counts so SecondaryRPMISR can set position */ 00090 if (PTITCurrentState & 0x02){ 00091 PrimaryTeethDuringHigh++; /* if low resolution signal is high count number of pulses */ 00092 }else{ 00093 PrimaryTeethDuringLow++; /* if low resolution signal is low count number of pulses */ 00094 } 00095 }else{ /* The CAS is synced and we need to update our 360/5=72 tooth wheel */ 00097 VCAS = VCAS + 10; /* Check/correct for 10deg of CAM movement */ 00099 } 00100 } 00101 00102 00109 void SecondaryRPMISR(void){ 00110 /* Clear the interrupt flag for this input compare channel */ 00111 TFLG = 0x02; 00112 00113 /* Save all relevant available data here */ 00114 // unsigned short codeStartTimeStamp = TCNT; /* Save the current timer count */ 00115 // unsigned short edgeTimeStamp = TC1; /* Save the timestamp */ 00116 unsigned char PTITCurrentState = PTIT; /* Save the values on port T regardless of the state of DDRT */ 00117 // unsigned short PORTS_BACurrentState = PORTS_BA; /* Save ignition output state */ 00118 unsigned char risingEdge; 00119 if(fixedConfigs1.coreSettingsA & PRIMARY_POLARITY){ 00120 risingEdge = PTITCurrentState & 0x01; 00121 }else{ 00122 risingEdge = !(PTITCurrentState & 0x01); 00123 } 00124 PORTJ |= 0x40; /* echo input condition */ 00125 00126 if (!isSynced & risingEdge){ /* If the CAS is not in sync get window counts and set virtual CAS position */ 00127 /* if signal is high that means we can count the lows */ 00128 switch (PrimaryTeethDuringLow){ 00129 case 23: /* wheel is at 0 deg TDC #1, set our virtual CAS to tooth 0 of 720 */ 00130 { 00131 VCAS = 0 ; 00132 changeSyncStatus((unsigned char) 1); 00133 break; 00134 } 00135 case 38: /* wheel is at 90 deg TDC #4, set our virtual CAS to tooth 180 of 720 */ 00136 { 00137 VCAS = 180; 00138 changeSyncStatus((unsigned char) 1); 00139 break; 00140 } 00141 case 33: /* wheel is at 180 deg TDC #6 set our virtual CAS to tooth 360 of 720 */ 00142 { 00143 VCAS = 360; 00144 changeSyncStatus((unsigned char) 1); 00145 break; 00146 } 00147 case 28: /* wheel is at 270 deg TDC #7 set our virtual CAS to tooth 540 of 720 */ 00148 { 00149 VCAS = 540; 00150 changeSyncStatus((unsigned char) 1); 00151 break; 00152 } 00153 default : 00154 { 00155 Counters.crankSyncLosses++; /* use crankSyncLosses variable to store number of invalid count cases while attempting to sync*/ 00156 break; 00157 } 00158 PrimaryTeethDuringLow = 0; /* In any case reset counter */ 00159 } 00160 } 00161 if(!isSynced & !risingEdge){/* if the signal is low that means we can count the highs */ 00162 switch (PrimaryTeethDuringHigh){ /* will need to additional code to off-set the initialization of PACNT since they are not 00163 evenly divisible by 5 */ 00164 case 7: /* wheel is at 52 deg, 7 deg ATDC #8 set our virtual CAS to tooth 104 of 720 */ 00165 { 00166 00167 break; 00168 } 00169 case 12: /* wheel is at 147 deg, 12 deg ATDC #3 set our virtual CAS to tooth 294 of 720 */ 00170 { 00171 00172 break; 00173 } 00174 case 17: /* wheel is at 242 deg, 17 deg ATDC #5 set our virtual CAS to tooth 484 of 720 */ 00175 { 00176 00177 break; 00178 } 00179 case 22: /* wheel is at 337 deg, 22 deg ATDC #2 set our virtual CAS to tooth 674 of 720 */ 00180 { 00181 00182 break; 00183 } 00184 default : 00185 { 00186 Counters.crankSyncLosses++; /* use crankSyncLosses variable to store number of invalid/default count cases while attempting to sync*/ 00187 break; 00188 } 00189 00190 } 00191 PrimaryTeethDuringHigh = 0; /* In any case reset counter */ 00192 } 00193 if(isSynced & risingEdge){ /* We are in sync and need to make sure our counts are good */ 00194 00195 /* System is synced so use adjusted count numbers to check sync */ 00197 } 00198 } 00199 // Counter.primaryTeethAfterSecondaryRise = 0; 00200 // Counter.primaryTeethAfterSecondaryFall = 0; 00201 00202 // unsigned char risingEdge; 00203 // if(fixedConfigs1.coreSettingsA & SECONDARY_POLARITY){ 00204 // risingEdge = PTITCurrentState & 0x02; 00205 // }else{ 00206 // risingEdge = !(PTITCurrentState & 0x02); 00207 // } 00208 00215 void changeSyncStatus(unsigned char synced){ 00216 if (synced != 1) { /* disable accumulator counter, so an ISR is fired on all 360 teeth */ 00217 PACTL = 0x00; /* disable PAEN and PBOIV */ 00218 /* (PACTL) 7 6 5 4 3 2 1 0 00219 PAEN PAMOD PEDGE CLK1 CLK0 PAOVI PAI */ 00220 }else{ /* enable accumulator so an ISR is only fired on every "5th tooth of the 360x track" */ 00221 // TIOS = TIOS & "0xCC0x83" WTF!?LOL!! 0x80; /* PT7 input */ 00222 // TCTL1 = TCTL1 & "0xCC0x83" WTF!?LOL!! 0xC0; /* Disconnect IC/OC logic from PT7 */ 00223 TIOS = TIOS & 0x80; /* PT7 input */ 00224 TCTL1 = TCTL1 & 0xC0; /* Disconnect IC/OC logic from PT7 */ 00226 PACN0 = 0xFB ; /* Calculation, $00 – $05 = $FB. This will overflow in 5 more edges. */ 00227 PACTL = 0x52; /* Enable PA in count mode, rising edge and interrupt on overflow 01010010 */ 00228 } 00229 }