Measuring Timer Interrupt Frequency with Oscilloscope - Page 1
Forum section: electronics / beginners

author: elefurtronik
author_rank: Regular Contributor
timestamp: January 12, 2023, 09:34:10 pm
content: Hello,I am using Arduino Mega 2560 to learn Timer Interrupts. In the video(44:30s) below the tutor is saying that if we adjust 256 Prescaler & 31249 compare match, we get a 2Hz signal. Everything is OK till here.I also made my own code and adjustments by reading the chip's datasheet. It worked and then I wanted to measure the signal with my oscilloscope. When I measured it, I saw that my signal frequency is 228,3mHz.Code:const byte ledPin = 12;const byte ledPin2=10;const byte buttonPin = 2;const byte buttonPin2= 7;volatile bool toggleState = false;volatile bool toggleState2 = false;int timer1_compare_match;int timer2_compare_match;ISR(TIMER2_COMPA_vect){TCNT2=timer2_compare_match;digitalWrite(ledPin2,digitalRead(ledPin2)^1);}ISR(TIMER1_COMPB_vect){TCNT1=timer1_compare_match;digitalWrite(ledPin,digitalRead(ledPin)^1);}void checkSwitch(){if (digitalRead(buttonPin) == LOW) {toggleState = !toggleState;digitalWrite(ledPin,toggleState);}}void setup() {pinMode(ledPin, OUTPUT);pinMode(ledPin2, OUTPUT);pinMode(buttonPin, INPUT_PULLUP);pinMode(buttonPin2, INPUT_PULLUP);noInterrupts();TCCR1A=0;TCCR1B=0;TCCR2A=0;TCCR2B=0;timer1_compare_match = 31249;TCNT1=timer1_compare_match;TCCR1B|= B00000101;TIMSK1|=(1<<OCIE1B);timer2_compare_match = 15624;TCNT2=timer2_compare_match;TCCR2B|=B00000111;TIMSK2|=(1<<OCIE2A);interrupts();}I attach the circuit and measurement.

author: mwb1100
author_rank: Frequent Contributor
timestamp: January 12, 2023, 10:25:14 pm
content: Your oscilloscope is showing a period of about 4 seconds (2 seconds high, 2 seconds low). You should be able to see that the LED is on for about 2 seconds and off for about 2 seconds. A 4 second period corresponds to a 250mHz frequency. So if your own timing of the LED shows 2 seconds on/2 seconds off then the scope is correct (as I'm sure it is).I don't know Arduino so I can't say what might be wrong with the code.QuoteMaybe the terminology was confused and meant a 2 second toggling of the signal?

author: elefurtronik
author_rank: Regular Contributor
timestamp: January 12, 2023, 10:42:23 pm
content: Quote from: mwb1100 on January 12, 2023, 10:25:14 pmI don't think that he was confused with terminology. Because Arduino works with 16MHz crystal and the formula is correct. Clock Freq./(Prescaler x (Compare Match Value+1))=Desired FrequencyStill I don't understand why I can't read 2Hz on the oscilloscope.

author: elefurtronik
author_rank: Regular Contributor
timestamp: January 12, 2023, 11:36:22 pm
content: Quote from: langwadt on January 12, 2023, 11:08:31 pmIf I check the units, it also seems Hz should come at the end. Prescaler or compare match value: those are just values and they don't have any unit. However, the Clock signal has a unit as Hz. Then the result should be Hz from my understanding. Please correct me if I am wrong.

author: eugene
author_rank: Frequent Contributor
timestamp: January 13, 2023, 01:03:00 am
content: Code:TCCR1B|= B00000101;This sets the prescaler to 1024 (not 256) so an interrupt frequency of 0.5Hz is correct. You're toggling the output pin at a rate of 0.5Hz, so it goes through one complete cycle at a rate of 0.25Hz or 250mHz.

author: langwadt
author_rank: Super Contributor
timestamp: January 13, 2023, 07:54:39 am
content: Quote from: elefurtronik on January 12, 2023, 11:36:22 pmyou are right

author: elefurtronik
author_rank: Regular Contributor
timestamp: January 13, 2023, 10:22:44 am
content: Quote from: eugene on January 13, 2023, 01:03:00 amOh Thanks. I didn't get it. I toggle means; I on and off the output. One on and off together mean one period/cycle. I see, this period is equal to 0,25Hz. What do you mean about the rate of 0.5Hz? Could you explain more detail, please?

author: armandine2
author_rank: Frequent Contributor
timestamp: January 13, 2023, 11:16:44 am
content: Think he means f = 1/T

author: eugene
author_rank: Frequent Contributor
timestamp: January 13, 2023, 02:13:01 pm
content: Quote from: elefurtronik on January 13, 2023, 10:22:44 amYou wrote on reply#2:QuoteIf you assume prescaler=256, then the interrupt fires at a rate of 2Hz. (You call it "Desired Frequency." I call it "rate.") On the other hand, if prescaler=1024, then the interrupt fires at a rate of 0.5Hz. I'm not sure how else to say it

author: elefurtronik
author_rank: Regular Contributor
timestamp: January 13, 2023, 10:15:34 pm
content: I change the Prescaler from 1024 to 256 and the frequency is 1Hz. :/ Why not 2Hz?

author: mwb1100
author_rank: Frequent Contributor
timestamp: January 13, 2023, 11:30:44 pm
content: For a similar reason that the 0.5Hz interrupt rate gave a frequency of 0.25Hz or 250mHz.The1024256 prescaler gives an interrupt rate of 2Hz.But it takes 2 interrupts to create one period (interrupt A causes the waveform to go high, interrupt B causes the waveform to go low). Therefore the waveform has a period of 1 second == frequency of 1Hz.

author: elefurtronik
author_rank: Regular Contributor
timestamp: January 13, 2023, 11:42:36 pm
content: OK. From all the information here and all the searches on google, I think finally I got the idea.When I use the calculation, I set the timer frequency. Not my signal's frequency. Therefore; 2Hz(0.5s) timer creates one interrupt. One interrupt means on or off sequentially. When I measure my signal, I see a 1Hz signal.I am now curious how I can see this timer signal. Where do I need to touch with probe to see this signal on Arduino mega 2560.

author: mwb1100
author_rank: Frequent Contributor
timestamp: January 13, 2023, 11:48:54 pm
content: The oscilloscope is already showing that to you. Each time the timer fires, it toggles the GPIO. That shows on the oscilloscope as an edge (whether rising or falling) - each edge is produced by the timer handling function.