delay and frequency setting problem in avr studio

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

i am using a 8 MHz oscillator and for creating delay i am using two functions _delay_us() and _delay_ms() but the delay produce by these function is not exact i mean am using _delay_ms(2000) to have a delay of 2 sec but the delay is of just 0.5 ms or less. How can i have exact delay of 2 sec.

Another problem is, while simulating my program (for atmega8535) in avr studio 4 the system frequency in simulator remains 4Mhz, i tried to change this from configuration option and i also used F_CPU but nothing but it remains 4 MHz.

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

There is a CPU speed dependent limit to how long you can delay with those functions. 2000 milliseconds is way too much. Try 100, 10 or 1. If you need 2000 milliseconds, do your own delay routines with for/while loops that use n*1ms delay or something.

I've had no problems changing the simulated timebase in AVR Studio, but I don't remember how to do that.

- Jani

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

To change the simulation frequency in Studio:

Start debugging (with simulator chosen as target)
Use Debug-AVR Simulator options
Change value in the middle drop list

Note that the edit control is not only a droplist but also a text edit. So if the frequency you want isn't already on the list just type it into the tope edit box anyway

Cliff

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

Gentlemen-->

I know you have probably answered his question a dozen times, but I can't figure out why my delays using the delay.h in the GCC library are completing about 30 times too quickly. F_CPU is defined as 8000000UL in the AVRStudio configuration and I'm running the internal oscillator.

Any ideas as to why everything seems to be set up Kosher, but my delays complete so quickly?

Thanks!

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

Do you have optimization turned on? You need to have that on. Set it to something other than -O0.

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

UFAnders wrote:
Gentlemen-->

I know you have probably answered his question a dozen times, but I can't figure out why my delays using the delay.h in the GCC library are completing about 30 times too quickly. F_CPU is defined as 8000000UL in the AVRStudio configuration and I'm running the internal oscillator.

Any ideas as to why everything seems to be set up Kosher, but my delays complete so quickly?

Thanks!

How long delays?

See my post above - You can't have delays longer than 65535*4=262140 clock cycles in one delay_ms() or _delay_loop_2(), which in the case of 8MHz internal oscillator is 8000000/262140=30518 microseconds or 30.5 milliseconds.

Delays longer than this have to be combined with several delay_ms function calls or while/for loops.

- Jani

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

Jepael wrote:

How long delays?

See my post above - You can't have delays longer than 65535*4=262140 clock cycles in one delay_ms() or _delay_loop_2(), which in the case of 8MHz internal oscillator is 8000000/262140=30518 microseconds or 30.5 milliseconds.

Delays longer than this have to be combined with several delay_ms function calls or while/for loops.

- Jani


thanx now i understand the reason behind this :D

clawson wrote:
To change the simulation frequency in Studio:

Start debugging (with simulator chosen as target)
Use Debug-AVR Simulator options
Change value in the middle drop list

Note that the edit control is not only a droplist but also a text edit. So if the frequency you want isn't already on the list just type it into the tope edit box anyway

Cliff

Thnx clawson u gave me the right method