Hello,
I've got a few problems analyzing a square wave that is fed into an Atmega328P-PU-based standalone circuit using the Atmega's interrupts and I would appreciate any help.
I'm currently still working on a standalone circuit which switches my car stereo into mute when reverse gear is detected... which is standard nowadays in many cars.
But because that would be far too simple and unexciting , my muting device circuit will also monitor the car's speed using the car's GALA speed signal to determine when to switch in and out of mute. The speed signal is basically a square wave with fixed peak/trough ratio. At low speed, the wave cycles in milliseconds become longer (or zero at standstill), and the faster the car moves, the shorter they get. The Atmega analyzes this wave using one of the interrupt pins.
So far so good, I've done enough tests with my car to know what the wave looks like, and which frequency cycle lengths occur at which vehicle speeds.
I've rigged up a simulation environment around my muting device circuit, where another Atmega328P-PU on a breadboard creates a square wave identical to that in my car by means of simple HIGH/LOW bit banging. This bit banging circuit also features an analog rotary potentiometer to manipulate the wave cycle length.
Additionally, a second Arduino Uno board is used to monitor the bit-banging wave by means of the XOscillo oscilloscope software.
The problem now is that what I see on my Xoscillo doesnt equal the wave cycle length which the Arduino on the muting device circuit gets. For my muting device to work as planned in my car, however, it is crucial that the wave cycles are measured/interpreted accurately.
This is the part of my code I've uploaded to the muting device which picks up the bit-banging square wave, and when the project is finished, the car's GALA signal (the entire code is over 400 lines and not important for us here):
// GALA signal cycle length calculation variables
volatile boolean countBegin = false;
volatile unsigned long CycleLength;
volatile unsigned long MillisAtFirstChange;
volatile unsigned long time;
int SquareWave = 2;
void setup(){
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(SquareWave), DeactMute, RISING);
}
// GALA Signal ISR
void DeactMute(){
if(!countBegin){
time = millis();
MillisAtFirstChange = time;
countBegin = true;
}
else{
time = millis();
CycleLength = time - MillisAtFirstChange;
countBegin = false;
}
//Just for testing purposes, cycle length is printed out via serial:
Serial.println(CycleLength);
}
So far, so good... but here's what happens when I compare the cycle length measured by my muting device circuit with what the XOscillo gives me:
As you can see, the XOscillo gives out shorter cycle lengths than the muting device circuit. I've dialed the cycle length up and down using the bit banging circuit's potentiometer, and the difference seems to be a more or less constant factor of 1.4 between the muting device and the XOscillo.
Can somebody give me a few pointers how I can fix this?
- carguy