I’ve been working on a CDI (Capacitor Discharge Ignition) system using an ATmega328P (without Arduino bootloader or framework, pure C with register manipulation). It worked fine when using the Arduino framework, but after switching to pure C code, the ignition logic seems to stop working — the engine won't start anymore.
Setup:
- Microcontroller: ATmega328P @16MHz
- Trigger: Pulser signal from the engine (digital HIGH pulse)
- Output: SCR gate control via digital output
- External serial control from ESP32 via UART
What works:
- Serial communication between ESP32 and ATmega works fine.
- I’ve verified trigger pulses are reaching the ATmega (checked with oscilloscope).
- The code compiles and runs (no infinite loop or reset).
Problem:
- The SCR never fires, and the engine won’t start.
- It seems like the timer-based ignition delay logic isn't functioning.
- I used
while(TCNT1 < ...)to implement delay between trigger and spark firing — could this cause the code to hang? - I also control limiter and map via serial commands like
L0,M1, etc.
Here’s a simplified version of my main ignition loop:
if (PIND & (1 << PD2)) { // Trigger detected
TCNT1 = 0;
while (TCNT1 < ignitionDelay); // Wait for specific degrees
PORTB |= (1 << PB1); // Fire SCR
_delay_us(100);
PORTB &= ~(1 << PB1); // Reset output
}
Could the while(TCNT1 < ignitionDelay) be preventing the loop from working properly? Or is there another issue I should be looking into when moving from Arduino to pure C with direct register access?
P.S: I use main(), init(), and sei, not setup and loop
Any advice or tips on debugging this would be greatly appreciated. Thanks in advance!