Clock timing on atmega32

Thread Starter

clintonb

Joined Mar 6, 2011
52
I used my atmega32 out of the box and I programmed it in winavr. I noticed it was really slow and I found out that apparently it is set to run on an 8mhz internal clock that is divided by 8 by default in the fuse bits.

I changed the makefile F_CPU to 1000000 and things worked as normal because it was infact running at 1mhz. I setup a clock and factored it by 1024 in the register giving 1000 ticks per second or millisecond intervals. I counted up to 1000 of these ticks and it did in fact give 1 second.

I now want more than just 1mhz and would like to not divide by 8. I used the calculator and removed the divide by 8 to get back to 8mhz. My program then did not run but I then changed the F_CPU to 8000000 and just like that it ran which I would expect.

I expected to have to go and change my code to cater for the faster clock count but I didn't have to? The program still reads the TCNT value and the timer factors by 1024 and counts to 1000 for the old second, I would have expected this to increment faster because it counts up 8 times faster.

Am I misunderstanding something about timing?
 

kubeek

Joined Sep 20, 2005
5,795
It would be nice if you could post your actual code.
But as I recall the timers should be tied to Fcpu, so the changed fuse should affect them as well.
 

Thread Starter

clintonb

Joined Mar 6, 2011
52
It would be nice if you could post your actual code.
But as I recall the timers should be tied to Fcpu, so the changed fuse should affect them as well.
I thought the F_CPU was linked only to the timing functions inside the program such as _delay_ms() etc. If you didn't know the cpu speed you couldn't work a delay. I'll try post the code in a minute but its just a timer with 1024 prescalar and an interrupt.
 

Robartes

Joined Oct 1, 2014
57
F_CPU is used by libavr to calculate the timing for _delay_ms() and delay_us(), indeed. It does not do anything to influence the speed of the timers - that is determined by the combination of clock source and prescaler. If your clock source (in the vast majority of cases this is your system clock) is faster, the timer will count faster.
 

kubeek

Joined Sep 20, 2005
5,795
Sorry for misleading you, what i actually meant was ClkCPU, not the FCPU constant defined in C code. In other words the counter should increment with every 1024 cycles of the cpu, if you have the prescaler set to 1024.
 

Thread Starter

clintonb

Joined Mar 6, 2011
52
It took a while to get to the bottom of this so I'll post the approach and problem if it can be of any help to a newbie like myself.

The problem was that I thought I had set my speed to 8mhz but the timer ticked slower than expected.

I created a simple program that just had a main code section that just dispalys TCNT1 onto the dispaly and then clears sets it to 0. The program dispayed approximately twice every second and dispayed 895 each cycle. This meant it ticked at +-1600 ticks per second. If you multiply this by 1024 precalar it came to what looks more like 2mHz. I looked on the web and found a more reliable calculator

http://www.engbedded.com/fusecalc/

That gives the fuse settings for avrdude. I now set the fuse settings and the makefile to 8mHz and the program runs as expected. JUST make sure that SPIEN is ticked before you apply the settings otherwise you won't be able to program it using you serial programmer.
 
Top