I am using an Atmega 328p (8mhz) as a fuel injection controller. This is my first 'real' project that does something of significance instead of just tinkering. My main concern is how I should setup all my programmable devices, and when I should poll them or use interrupts. I am using quite a few things on the uC such as PWM, USART, ADC and general timer. I'll give you a quick rundown of how my minimal system will work. Basically there is a mass airflow sensor (MAF) that reads the intake air charge in g/sec. This is sent to the ADC to determine the amount of fuel/air mixture that the engine will need. From this, the uC calculates the injector pulse width and outputs a varying time from either 4.0ms to 60ms. Also, there is an RPM sensor on the flywheel to determine the engine speed. With these three basic things, the engine should run in a pre-mapped fuel mode without needing a closed loop device such as an o2 sensor. I also want to send out the current data (pulse width, rpm..etc) through the serial port to be monitored in a terminal window.
As it stands, I have most of this stuff coded and working except the rpm sensor (which will probably be a hall sensor), but my code seems cumbersome and inefficient. I am curious as to what the optimal way to run my programmable devices would be.
I heard people talking about having the adc/usart etc using interrupts and then servicing them in the main loop. If I were to do this I guess I would need to make my own flags for each ISR; then in the main loop I would service the flag if it was set? Does this seem right?
Timing is fairly critical as I don't want to miss an injection shot or something. I want to inject fuel once every 4 engine rotations (when the intake opens) which will be running anywhere from 600 to 4000rpm. Would it be better to have my ADC free-running and continuously reading the MAP sensor, or just do it once right before I calculate the pulse width? I am also a little curious as to how I am going to determine where the engine is in the cycle so I can shoot the fuel in at the right time.
Lastly, I want to send a data stream to a pc so I can view the variable data, but I don't want it to hinder any calculations. Should I setup a timer to interrupt once every second and then maybe send out the serial data once a second? Is this a good way of doing this?
Sorry for the long post. This has been a really fun project so far and I would like it to work as efficiently as possible.