dears: we have to Generate 1 second with atmega32A and mikrobasic software. so I have configured timer 0 and avr with this way:
const _THRESHOLD = 250
TCCR0=0x04
TCNT0=0x06
OCR0=0x00
' // Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x01
and overflow function:
sub procedure Timer0Overflow_ISR iv IVT_ADDR_TIMER0_OVF
' // Reinitialize Timer 0 value
TCNT0=0x06
if (counter1 >= _THRESHOLD) then
counter1 = 0
so with TCCR0=0x04
WE USE 256 presqale with external clock 16MHz wich :
16MHz/256=62500
which means timer 0 clock is 1/62500=0.000016
Second
which TCNT0=0x06
timer 0 over flow clock is 256-6=250
which take 0.000016*250=0.004
Second for one overflow of timer 0 with TCNT0=0x06
.
so for creating 1 second, it needed to count timer0 overflow for 1/0.004=250
turn of timer0 overflow. the above codes are written based of this calcuation.
SO when we program this codes into atmega32A, for 5 minutes the clock is Lag for 5 seconds.
Changes based of new comment.
I have changed TCNT0=0x07 for 249 clock of timer0 and 1 clock for overloading ti,er values. but still counter lag for 5 second at 5 minutes .
please help me to correct my codes.
Thanks a lot.
-
1\$\begingroup\$ What do you mean by creating 1 second? \$\endgroup\$Long Pham– Long Pham2018年09月27日 09:22:12 +00:00Commented Sep 27, 2018 at 9:22
-
2\$\begingroup\$ Uhm, you cannot generate time. What you can generate using an ATMega is a signal that's high for 1 second and then low for 4 seconds. \$\endgroup\$Bimpelrekkie– Bimpelrekkie2018年09月27日 09:35:28 +00:00Commented Sep 27, 2018 at 9:35
-
3\$\begingroup\$ @Bimpelrekkie: OP is not stupid. There is obviously a language barrier preventing him from expressing it any better. \$\endgroup\$Rev– Rev2018年09月27日 09:37:01 +00:00Commented Sep 27, 2018 at 9:37
-
1\$\begingroup\$ Read the datasheet carefully. I have seen counters where it requires 1 cycle to re-load the counter thus a limit of 250 is actually 251 clocks. \$\endgroup\$Oldfart– Oldfart2018年09月27日 09:43:39 +00:00Commented Sep 27, 2018 at 9:43
-
1\$\begingroup\$ @LongPham: True, it was just meant to point him into the right direction. And I don't think the 5min/5s is accurate. Its potentially several "1 counter off" things coming together. \$\endgroup\$Rev– Rev2018年09月27日 09:45:07 +00:00Commented Sep 27, 2018 at 9:45
1 Answer 1
Is "mikrobasic" compiled or interpreted? The website calls it a "compiler", but I have found that vendors play fast-and-loose with the distinction these days. Sometimes source code is compiled to an intermediate "bytecode" representation, which is then interpreted.
You have timer0 counting every 16 µs, which means that the ISR must update the TCNT0
register within that amount of time in order for your calculations to be correct.
I find myself wondering whether the ISR actually does the write to TCNT0
before it has already incremented several times, which would explain the roughly 1-part-in-60 error that you're seeing.
One experiment would be to see whether you get the correct timing by increasing the TCNT0
value even more. Try setting TCNT0 = 0x0B
in your ISR. If this gives you something closer to the correct timing, that means that there's a lag on the order of 64 µs between when the overflow interrupt occurs and when the write to TCNT0
occurs. This would not be unheard of in interpreted code.
-
\$\begingroup\$ i have changed TCNT0 to 0x0B and time is almost correct. Thanks a lot \$\endgroup\$Soheil Paper– Soheil Paper2018年09月27日 14:53:12 +00:00Commented Sep 27, 2018 at 14:53
-
\$\begingroup\$ Does mikrobasic allow you to mix in assembly language at all? If so, your ISR would be a good place to take advantage of that, in order to reduce that lag. \$\endgroup\$Dave Tweed– Dave Tweed2018年09月27日 15:06:31 +00:00Commented Sep 27, 2018 at 15:06
-
\$\begingroup\$ mixing is able but i dont know assembly codes. \$\endgroup\$Soheil Paper– Soheil Paper2018年09月27日 16:24:02 +00:00Commented Sep 27, 2018 at 16:24
-
\$\begingroup\$ Well then, there's a learning opportunity for you! \$\endgroup\$Dave Tweed– Dave Tweed2018年09月27日 16:26:51 +00:00Commented Sep 27, 2018 at 16:26