Hello everyone,
I'm new to this forum and would like to ask for help. I'm trying to bring OCR2A to work but I'm lost. Using Arduino Nano.
Task: INT0 will receive a falling edge every 26us for about 63 periodes.
I would like to measure the cyle time with an accuacy of 1 us and expect values about 1638 us.
I got always a measurement of 8us.
Do something first that works and then play wit TC2 etc.
Generate exacltly 63 pulses (ontime 13 us and Offtime 13 us) over DPin-4 (say) and use them to interrupt the MCU over INT0-pin; count the number of inerruptions (n) and then dsiplay nx26 (= 1638) on the display.
micros() on the Arduino Nano has only 4 µs resolution.You always read 8 µs because micros() updates every 4 µs. Your code measures interrupt latency, not pulse duration.Timer2 never reaches OCR2A because INT0 keeps resetting it. The compare interrupt almost never fires. Timer2 is also too small for a 1.6 ms measurement. Do not use micros() for 1 µs accuracy. Use Timer1 as a free-running counter instead. Set Timer1 prescaler to 8 for 0.5 µs ticks. Start Timer1 on the first falling edge. Stop it on the 63rd falling edge and read TCNT1.
Thank you for the explanatin. I didn't know that micros() has this behavior. I'll try to use Timer1.
The challenge is that I didn't know how many falling edges I will get. The only thing I know is: if there is no further falling edge after 26us the package is complete.
This is the start of a long signal (IR sender of a remote control) I want to decode. At the end I need to measure the pulse packages AND the gaps between them. My first approach was to measure the first pulse package which failed.
You want to receive and decode an IR signal coming from a Remote Control which yu have not told before except now.
First of all, you need to know the structure of an IR signal (Fig-1, demodulated from 38/40 kHz carrier for Sony CD remote control (RM-DC335)). There is a long gap. Tell your program to search for that gap and then you statrt counting the pulses. The 40 kHz carrier signal (not shown in Fig-1 which is a decode signal) is there when the signal is at HIGH level.
The best way to do that with AVR Arduinos is to use the Input Capture function of Timer1. Tutorial here (scroll down to "input capture unit"). The highest accuracy possible is 1/16 microseconds, or 62.5 ns, on a 16 MHz Arduino.
An interesting library that I wasn't aware of before. I'll examine it to see if it's suitable, as my remote control isn't from the consumer electronics sector. But I'm sure the detailed article contains valuable information.
I have already received some initial help, namely that micros() does not count microseconds in this case.
I also will dive into the Input Capture of the ATmega328.
Even though there are great libraries, I would like to understand why my code isn't working.
The intention was:
A rectangular signal is present at the INTERRUPT_PIN: 38kHz, i.e., every 26us the falling edge triggers an INT0.
Timer 2 runs at 2us and should generate a TIMER2_COMPA interrupt at 30us.
Timer2 will never trigger during the entire pulse packet. This is intentional.
Only when the pulse packet ends, i.e., when INT0 is no longer firing, does Timer2 have the chance to fire TIMER2_COMPA.
As soon as TIMER2_COMPA is triggered, both interrupts are disabled and the time is determined via micros().
Even if micros() does not have the desired resolution, a quarter of the expected time should be measured.
I don't see anything obviously wrong with the code, and the problem may be incorrect assumptions about the signal. Forum members are unlikely to be able to help, because they don't have that signal to test with.
I suggest to use a 38 kHz IR receiver to eliminate the 38 kHz carrier signal. Then, use the IRRemote library to get pulse timings of the raw data (one of its built in functions).
Does that mean that the TC2 is being driven by a 500 kHz clock coming from 16 MHz internal oscillator after divided by 32?
And the OCF2A flag will trigger interrupt after counting 16 clock pulses? How do you verify that the interrupt has occured? 32 us is too short to visualize unless you count 75000 interrupts and flash a LED at 2.4 sec interval. Test sketch using UNO R3.
All of the IR libraries assume your input will be the output of an IR receiver module. Those modules go low when carrier is present, and back high when it stops. In effect, they demodulate the 38KHz carrier, which is what you're trying to do the hard way. But I wonder if you can't just capture the signal on your scope and figure out what it is. If it's a pulse position protocol, like NEC, the On periods are always the same duration, and it's the Off periods that determine the bit values. But usually there are only two Off possibilities - long and short. So it's pretty easy to figure it out. And if it's not NEC, this site will help you figure out what protocol is being used:
Or, as has been suggested, you could get an IR receiver module, and then one of the libraries can decode it for you.
What is your input coming from? Are you tapping into the LED driver circuit of the remote control, so it's digital input, or are you firing the remote into a photodiode or phototransistor?
I understand that it is not possible for you to help without having my test signal.
Ultimately, the easiest solution will be to use an IR receiver that eliminates the 38 kHz carrier.
But it annoys me that I can't get the TIMER2_COMPA interrupt under control