Here’s a little Arduino code I’ve cooked up for the purposes of providing a nice crank signal to the FPGA-based ECU, for the purposes of bench testing the output. It’s a simulation of a nicely cleaned up, digital signal generated by a 36-1 toothed crank wheel on your typical Ford engine.
Essentially, a little throttle position sensor (read, potentiometer) is hooked up to adc pin 5. This is purely to give me something to twiddle to see if the FPGA is reacting quickly enough to the change in frequency (Not that I have any fear of this, really- damn thing is pretty fast- but alas, I must have something to fiddle with while testing, or it’s no fun!).
The LED flashing part is purely for visual effect; an ‘is it working’ before I had chance to hook it up to an oscilloscope for testing.
The code is simple enough, my written code is starting to look more user-friendly, and- importantly- it works. Onward with the FPGA testing, then?
Maybe after my exams; this was put together in my ‘evening off’. (Oh, ninjaedit- it only ‘works’ when debug = 1)
//------------------------------------------------------------------------------- #include <Wire.h> //----------------------------------- // Calibration Values for TPS float minimumReading = 48; float maximumReading = 919; //----------------------------------- //Setttings for TPS int analogPin = 5; // Throttle Position Sensor Wiper Pin int throttleRead= 0; // variable to store the value read int throttleCorrected= 0; // variable to store the corrected value //LED flashy bit setup const int ledPin = 13; // The number of the onboard LED pin int ledState = LOW; // Used to store the LED state //Variables for Pulse Generator long pulseLength = 0; // Variable to store the generated pulse length long gapLength = 0; int toggle = 1; // Variable to act as a toggle long lastMicros = 0; // Variable to act as timer function int count = 1; // Variable to store the current tooth count int calibrate = 92.59259259259; // Calibration for top speed = appx. 9k rpm int rpm1 = 1; // Variable to store the RPM number int debug = 1; // Debug setting- changes output from RPM & pulse length to counter & gap indicator void setup() { Serial.begin(9600); // Setup serial pinMode(ledPin, OUTPUT); } void loop() { tpsRead(); // Run TPS Read loop pulseGenerate(); } void tpsRead() { throttleRead = analogRead(analogPin); // Read the input pin throttleCorrected = ((throttleRead-minimumReading)/maximumReading)*100; // Subtract minimum from TPS. Calculate percentage of max if (throttleCorrected<1) { throttleCorrected=1; } if (debug == 0) { rpm1 = 60000000/(pulseLength*36); Serial.print("Simulated RPM :"); Serial.print(rpm1); Serial.print("RPM; Pulse Width :"); Serial.print(pulseLength); Serial.println("uS"); } } void pulseGenerate() { pulseLength = (throttleCorrected*calibrate); // Calculate length of 'high' pulse (in microseconds) gapLength = pulseLength*3; if ( (micros() > (lastMicros + pulseLength)) && (toggle==HIGH) && (count <= 35) ) { lastMicros = micros(); toggle = LOW; } if ( (micros() > (lastMicros + pulseLength)) && (toggle==LOW) && (count < 35) ) { lastMicros = micros(); toggle = HIGH; count += 1; if (debug == 1) { Serial.print (count); Serial.print (","); } } if ( (micros() > (lastMicros + gapLength)) && (toggle==LOW) && (count == 35) ) { lastMicros = micros(); toggle = HIGH; count = 1; if (debug == 1) { Serial.println ("GAP"); Serial.print(count); Serial.print (","); } } digitalWrite(ledPin,toggle); } |