// Crankshaft / camshaft rotation simulation
// Engine is V-twin with cylinders at 80 degrees opposed
// Connecting rods share a common crankshaft journal
// Code is based on a full camshaft cycle (720 degrees rotation)
// and when spark plugs should fire. Actual ignition advance is
// not factored in yet since advance would move right and left cylinders'
// 'trigger' points accordingly and that would cause the right
// cylinder to be less than zero. So until I work in that logic,
// as of now, both cylinders fire spark plugs with 0 degrees ignition advance.
// This means right spark plug would fire when the cam trigger occurs and
// left spark plug would fire 440 degrees later:
// Left cylinder fires 360 + 80 = 440;
// right cylinder would fire 280 degrees afterwards:
// 440 + 280 = 720 (or 0 if you prefer). ;)
//
// Target in Energia IDE is MSP430G2553 @ 16MHz
//
const uint16_t t1 = 0; // right cylinder baseline spark
const uint16_t t2 = 440; // left cylinder baseline spark relative right cylinder
const uint8_t cam = 0; // camshaft trigger relative right cylinder TDC
const uint8_t AMBER_LED = 5; // LP pin indicating camshaft trigger signal occurrence
// I am using existing RED_LED, GREEN_LED for right and left cylinder TDC indicators
uint16_t degree = 0; // counter for crankshaft rotation in degrees
void setup()
{
pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED,OUTPUT);
pinMode(AMBER_LED, OUTPUT);
}
void loop()
{
for (degree = 0; degree < 720; degree+= 11.25) {
// 11.25 = spacing between tooth centers on a 32 tooth wheel
// Camshaft:
digitalWrite(AMBER_LED,degree == cam);
// Right cylinder:
digitalWrite(RED_LED, degree == t1);
// Left cylinder:
digitalWrite(GREEN_LED, degree == t2);
}
}I don't have a scope here so I hope that it can toggle any one of the LED's at greater than 5KHz.And then... anyone have any good advice relating to getting this into a timer / interrupt handler?