Untitled
Untitled
//
// To set engine speed: open serial monitor, send "+" to increase and "-" to decrease engine
speed
//
// Pins used:
// 13 = onboard LED (toggles once per ignition cycle)
// 12 = CPS signal XB (6 slots in cup)
// 11 = crank signal (toggles once per crankd degree)
// 10 = CPS signal S1 (2 slots in cup)
// 8 = O2 signal (toggles once per 8 cycles) - voltage divider required for ECM input!
//
#define O2 PINB0
#define CPSS1 PINB2
#define CRANK PINB3
#define CPSXB PINB4
#define LED PINB5
// 6 slot rotor cup for fuel injected engines, degrees crankshaft where CPSXB signal toggles
static unsigned int blipsXB[12] = {45, 90, 135, 180, 225, 405, 450, 495, 540, 585, 630, 720};
// 2 slot rotor cup for carbed engines, degrees crankshaft where CPSS1 signal toggles
static unsigned int blipsS1[4] = {270, 315, 675, 720};
//static unsigned int compares[18] = {168, 178, 192, 199, 207, 225, 244, 269, 299, 338, 384,
448, 537, 673, 897, 1224, 1346, 1682}; // @ 8 MHz system clock
static unsigned int compares[18] = {332, 355, 380, 394, 409, 443, 484, 532, 592, 665,
761, 888, 1066, 1332, 1778, 2425, 2670, 3340}; // @ 16 MHz system clock
static unsigned int rpms[18] = {8000, 7500, 7000, 6750, 6500, 6000, 5500, 5000, 4500, 4000,
3500, 3000, 2500, 2000, 1500, 1100, 1000, 800};
// main loop
void loop()
{
int input = 0;
if(Serial.available()) {
input = Serial.read();
//Serial.println(input, HEX); // mirror input, for testing only
// parse input
// index rotation
if (speedIndex < 0)
speedIndex = 17;
else if (speedIndex > 17)
speedIndex = 0;
Serial.println(rpms[speedIndex]);
Serial.print ("? ");
}
}
ISR(TIMER1_COMPA_vect)
{
// called, when a timer1/counter1 matches compare value
// reset index
if (blipXBIndex >= 12)
blipXBIndex = 0;
}
// reset index
if (blipS1Index >= 4)
blipS1Index = 0;
}
if (degCount == 720) {
// toggle LED @ every ignition cycle
PORTB ^= (1 << LED);
degCount = 0;
revCount ++;
}
if (revCount == 8) {
// toggle O2 signal @ 8 ignition cycles
PORTB ^= (1 << O2);
revCount = 0;
}
}
void setup()
{
// set pins of port B to output mode
DDRB = 0xFF;
// initialize Timer1
cli(); // disable global interrupts
TCCR1A = 0; // set entire TCCR1A register to 0
TCCR1B = 0; // same for TCCR1B
// Set CS10 and CS12 bits for 1024 prescaler (test only)
//TCCR1B |= (1 << CS10);
//TCCR1B |= (1 << CS12);