Hi,
I’am little confused with Timer1 and prescalers etc.
What I need is to setup a Timer which execute a function every one second (exactly one second)
ISR(TIMER1_OVF_vect)
I have a Atmega2560 / 16 Mhz…
Can someone explain me how to properly setup values for timer1_counter?
How exact do you want your second to be? An Arduino's oscillator does not run at precisely 16MHz so it cannot keep time like a clock. If you need precision then you need a Real Time Clock (RTC) module.
If the Arduino is precise enough, then for a long interval such as 1 second why not just use millis() or even micros(). Much simpler than working with a Hardware Timer.
To use a Timer you need a combination of pre-scaler (to slow down the rate at which the Timer counts) and a count value so that the number of counts at that rate give you the duration you require, I find that the Atmel datasheet starts to make sense after the 14th reading.
What I want is to get a frequency… each second I would like to read a counter value which is incremented when a signal is present on pin 2 (attach interrupt)…
I don’t know what I’am doing wrong but instead od 1000 counts I get 1006 or 1007 counts the code is here (just for sample)
const byte interruptPinTemp = 2;
int cnt = 0; //initial value of counter
void setup() {
Serial.begin(9600); // inicializacija serijskega porta
noInterrupts(); // disable all interrupts
//Timer interrupt na 1 sekundo
ISR(TIMER1_OVF_vect) // interrupt service routine
{
cnt1 = cnt ; // copy value to some other variable
bData = true; // set a flag
cnt = 0; // reset counter value
TCNT1 = timer1_counter; // preload timer
}
void loop() {
// if flag is true then the frequency is displayed on serial monitor
if (bData == true)
{
bData = false;
Serial.println(cnt1);
t instead od 1000 counts I get 1006 or 1007 counts
0.6% error would be within the expected accuracy of the Arduino clock (barely?)
The logic looks OK; I mean, it could be off by one tick, or a couple cycles worth of reload time, but 8ms seems unlikely. Do you have a high-accuracy frequency counter that you could use to measure the arduino clock?
peterv6i:
Hi,
I'am little confused with Timer1 and prescalers etc.
What I need is to setup a Timer which execute a function every one second (exactly one second)
If you neeed a very accurate 1 Hz pulse, get yourself a DS3231 RTC module, connect it to I2C and activate the 1Hz square wave output on the DS3231!
westfw:
0.6% error would be within the expected accuracy of the Arduino clock (barely?)
The logic looks OK; I mean, it could be off by one tick, or a couple cycles worth of reload time, but 8ms seems unlikely. Do you have a high-accuracy frequency counter that you could use to measure the arduino clock?
I think you have set up the overflow for .5 second not 1 second.
By your calulation 65536 - 34286 = 31250 .0625256 = .5
The factor of 2 is only relevant when you are using the timer in a mode where it counts up and down.
When you initialize the registers you have Normal Mode (WGM13:WGM10 =0) which only counts up to OxFFFF and resets.
The way to use the counter for a non-power-of-2 is to select one of the modes that counts upto a
register value, and preset that register, typically the ICR1 register.
In this case I have a opamp comparator so I convert sin wave to square pulses (this works fine because with second channel I can exactly count the same frequency)
I think you have set up the overflow for .5 second not 1 second.
By your calulation 65536 - 34286 = 31250 .0625256 = .5
The factor of 2 is only relevant when you are using the timer in a mode where it counts up and down.
When you initialize the registers you have Normal Mode (WGM13:WGM10 =0) which only counts up to OxFFFF and resets.
[/quote]
So in my case I have wrong value for timer1_counter? As I say… when a freq is 1000 Hz I count 1006-1007 pulses. When the frequency is 5000 Hz then I count
I think you have set up the overflow for .5 second not 1 second.
By your calulation 65536 - 34286 = 31250 *.0625*256 = .5
The factor of 2 is only relevant when you are using the timer in a mode where it counts up and down.
When you initialize the registers you have Normal Mode (WGM13:WGM10 =0) which only counts up to OxFFFF and resets.
Will try with this wizzard (ICC AVR) to correctly setup a interrupt (what do you think)?
peterv6i:
So in my case I have wrong value for timer1_counter?
Cattledog is correct. You are only timing 0.5 seconds.
As I say.. when a freq is 1000 Hz I count 1006-1007 pulses.
This is because your "count" interrupt occurs at a rate of 2kHz (twice per cycle of your waveform generator) due to the interrupt mode of "change" (that is, both rising and falling edges).
0.5s between pin inversions is a 1Hz square wave, right? I don't know if that's what you want, but perhaps it explains where the "timer wizard" is getting it's numbers from.
(although, that makes it even stranger that you're seeing ~1008 counts from your frequency generator!)
westfw:
0.5s between pin inversions is a 1Hz square wave, right? I don't know if that's what you want, but perhaps it explains where the "timer wizard" is getting it's numbers from.
(although, that makes it even stranger that you're seeing ~1008 counts from your frequency generator!)
I just want to count frequency in one seccond (pulses in one seccond).
My test frequency is 1000 Hz square wave.. What I want is to get the "same" value in my arduino (counting interrupts on pin 2)..
stewart0:
This is because your "count" interrupt occurs at a rate of 2kHz (twice per cycle of your waveform generator) due to the interrupt mode of "change" (that is, both rising and falling edges).
Yes. Do that and then your cnt variable will increment only once per cycle of your waveform generator.
Anyway, that explains why you were getting approx 1000 counts in only 0.5 seconds, but it still doesn't explain your 0.6% clock discrepancy. The clock discrepancy on my UNO board appears to be only about 0.01 to 0.02 percent.
stuart0:
Yes. Do that and then your cnt variable will increment only once per cycle of your waveform generator.
Anyway, that explains why you were getting approx 1000 counts in only 0.5 seconds, but it still doesn't explain your 0.6% clock discrepancy. The clock discrepancy on my UNO board appears to be only about 0.01 to 0.02 percent.