I'd like to generate a frequency up to 1 Mhz on pin 8 and measure them on pin 3 (timer1).
The problem ist that i need a timer interrupt which runs the function "frequenz" every second.
How can i use the a timer interrupt.
Thanks
Peter
--- Code ---
#define Ausgangspin 8
unsigned int OutFrequenz = 10000; //Hz (Maximum: 65535 Hz)
long InFrequenz = 0;
long Counter = 0;
I’ve got a little code tht will output to 1Mhz on pin 9 nd pin 10 inverted
you can easily modify it to use only pin 9 or 10, inverted or not
just don’t add COM1B0(pin 10) or COM1A0(pin 9) toTCCR1B
don’t know about timing something coming in but I suspect you will have to use another timer to create your frequency (a little bit less accurate generation), to use timer1s extra option of input capture to directly advance the counter
for generation this will work tho
thanks for the generator code, but i also need the code to measure the incoming frequenz every second.
What ist the best solution ? An overflow interrupt or timer interrupt ?
The variable counter should be made volatile, otherwise when you read it outside of the IRQ it may be in the process of being changed by the IRQ. It should be an unsigned long. Long because you can count past 1,000,000 and unsigned because you can still know elapsed count past rollover -- then you won't need to zero or synchronize count the same way you don't with millis() and micros(). Count becomes a clock and your IRQ is minimal.
In loop() you need to be able to know the value of counter at a start time and then as close as you can to 1 second later. Subtract counter value at end of 1 second from counter at start and you have your frequency. At no point is counter needed to be made 0, you only need to know when 1 second has passed, which you must do in any case. Again, the minimum. I would use micros() to watch for 1 second, that's as close as I can get without adding external hardware.
With rollover-safe code you can check frequency again and again for a very long time if desired.
My question is why stop at 1 second? 10 seconds will give more accuracy and 100, even more. There is always a small time between one value being set and another instruction reading that due to the sequential nature of the machine, longer intervals will reduce the effect of that.
Hi.
What is the limit for the command attachInterrupt for the arduino Mega 2560 ?
If i measure lower frequencies like 50kHz it works perfectly.
If i count a higher frequency like 350kHz the Serial.print do nothing till i remove pin21 (timer2).
Then the program print a very high number and it works normally.
Peter
//Timer2 Inputpin 21
volatile long InFrequenz = 0;
volatile long counter = 0;
Maybe get rid of ISR() and the associated timer code (comment them out, you can un-comment later) then just print the value of counter or counter/5 in loop() and see what that will let you do.
i tried the options, but it is still the same problem !
If the pin is connected the serial.print do nothing.
If i disconnect the pin 20 it will print the text ??
What is the max. frequency which an Port can handle ?
Peter
Code:
//Timer3 Inputpin 20
volatile long InFrequenz = 0;
volatile long counter = 0;