First time with the ATMEGA644...Questions

Go To Last Post
11 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hi,

Just got a piece of the ATMEGA644-20PU PDIP format.

Able to do a blink LED program.

I changed the fuses to be 0xCF for low byte and 0x91 for high byte, so that it can work with an external crystal greater than or equal to 8Mhz, which is the 20Mhz crystal in this case. Fuses changed successfully via AVRDude.

I recompiled my blink program with F_CPU=20000000U and the program blinks the LED as if the uC running at 1 Mhz.

So, I recompiled again with F_CPU=1000000U and program blinks the LED really fast, as it should. But, how comes only with F_CPU=1000000U but not with F_CPU=20000000U?

Please explain if I have missed or done something wrong here.

Thanks!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Looks like you have the divide by 8 fuse set.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

dksmall wrote:
Looks like you have the divide by 8 fuse set.

I don't think so. I changed the fuses to low value 0xCF and high value 0x91. The new fuse bits show CLKDIV8 is NOT checked.

Please see http://www.engbedded.com/fusecalc/ and enter the above values for the ATMEGA644.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Would an L for Long give a different result than U for Unsigned?

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Using 'U' for unsigned int will ensure that you throw away all the upper bits.

Using 'UL' will work fine. As will 'L' since you are nowhere near overflowing into the sign bit.

The obvious thing to do is Simulate any timing related problems.

The compiler will normally decide for itself that 1000000 is a 32 bit constant. i.e. the L or UL is not really needed.
I am still a little surprised that you would get the correct timing at all.

Please can you post your actual program.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:

The compiler will normally decide for itself that 1000000 is a 32 bit constant. i.e. the L or UL is not really needed.
I am still a little surprised that you would get the correct timing at all.

Correct. For me "UL" or "U" does not make a difference, because 20,000,000 (even with a signed long) should be covered nicely with a 32-bit number, since 2^(32-1) - 1 is the max positive number for a signed representation.

Quote:

Please can you post your actual program.

Please see below. Thanks.

#include 
#include 

//Define functions
//======================
void ioinit(void);      //Initializes IO
//======================

int main (void)
{
    ioinit(); //Setup IO pins and defaults

    while(1)
    {
		PORTB |= 0b00000001;
		_delay_ms(1500);
		PORTB &= 0b11111110;
		_delay_ms(1500);
    }
   
    return(0);
}

void ioinit (void)
{
    //1 = output, 0 = input
    DDRB = 0b00000001;
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

you are using _delay_ms with the time parameter out of the allowed maximum value of 262,14ms / F_CPU (in MHz).

-Erwin

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
_delay_ms with the time parameter out of the allowed maximum value of 262,14ms / F_CPU (in MHz
That has not been the case for quite a while, one can use up to 65535 (0xffff), with slightly less accuracy, now.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have just simulated your program with F_CPU = 20000000U.
It works correctly. i.e. it takes 1503.00095ms between the two PORTB statements.
Your LED will flash at 0.333Hz.

All the same, I would take care with numeric constants. e.g. use UL for uint32_t, L for int32_t.

I had always worked on the basis that 123456789UL is the same as (uint32_t)123456789
On this basis 12345U is the same as (uint16_t)12345

Simulating at 1MHz, it takes 1560.019ms

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:
I have just simulated your program with F_CPU = 20000000U.
It works correctly. i.e. it takes 1503.00095ms between the two PORTB statements.
Your LED will flash at 0.333Hz.

All the same, I would take care with numeric constants. e.g. use UL for uint32_t, L for int32_t.

I had always worked on the basis that 123456789UL is the same as (uint32_t)123456789
On this basis 12345U is the same as (uint16_t)12345

Simulating at 1MHz, it takes 1560.019ms

David.

Ok, great! Curious how did you measure the 1503.00095ms (5 decimal place) number, for example, without a very fine-tuned clock routine? Or you used interrupts?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

unebonnevie wrote:
david.prentice wrote:
I have just simulated your program...

Ok, great! Curious how did you measure the 1503.00095ms (5 decimal place) number, for example, without a very fine-tuned clock routine? Or you used interrupts?

...by using the simulator within AVR Studio which gives, pretty much, instruction level timing accuracy.