Author Topic: Confusion: Timing Control  (Read 1902 times)

0 Members and 1 Guest are viewing this topic.

Offline reefatTopic starter

  • Full Member
  • ***
  • Posts: 61
  • Helpful? 0
Confusion: Timing Control
« on: September 16, 2008, 07:06:33 AM »
Few days ago, when I first started working with ATmega8 AVR, I tried to drive servo. I used PWM on PORTB, instead of using regular pulse-timing-loop on PORTD. The PWM on PORTB seemed a lot complex to me. Rather than that, the regular software controled timing on PORTD I used is as follows:

    PORTD = 0b00000001;
    _delay_us(1000/4.2);
    PORTD = 0b00000000;
    _delay_us(20000/4.2);

I divide 1ms and 20ms by 4.2, because without dividing it by 4.2, my Oscilloscope was indicating that it was 12Hz(approx.). So I figured out if I divide the time by 4.2, the frequency will be multiplied by 4.2, which is nearly 50Hz (required for servo). And duty cycle becomes nearly 5%.

Now, my questions are,

1) Where the hack this 4.2 came from? Why doesn't the _delay_us() function do something to correct the right frequency?

2) I didn't use any crystal oscillator on my circuit. I left the OSC1 and OSC2 pins open. Does it mean that the ATmega8 chip is using the internal system clock? So why and when do we need to use external Crystal Oscillator.
« Last Edit: September 16, 2008, 07:12:47 AM by reefat »

Offline Superman

  • Beginner
  • *
  • Posts: 5
  • Helpful? 0
Re: Confusion: Timing Control
« Reply #1 on: September 16, 2008, 07:10:00 PM »
Well, for one thing, your second delay is wrong if you're trying to create a 50 Hz wave with 5% duty cycle.

A 50 Hz wave has a period of 1/50 seconds, or 20 ms.

For 5% duty cycle, that gives you a "on" time of 20 ms * 5% = 1 ms (you got that one right).
The off time would be 20 ms * 95% = 19 ms (not 20 ms, like your second delay shows).

If you used 19000 us, instead of 20000 us, I think your "magic" 4.2 would become a "magic" 4.0.

From there, I suspect the _delay_us function is not using the proper frequency (I don't have one of those chips, so I don't know if there is something special you have to do to get it to work right).

Superman

Offline Superman

  • Beginner
  • *
  • Posts: 5
  • Helpful? 0
Re: Confusion: Timing Control
« Reply #2 on: September 16, 2008, 07:32:06 PM »
After further review on google...

I found the following embedded in "delay.h"
Two wrapper functions allow the specification of microsecond, and
    millisecond delays directly, using the application-supplied macro
    F_CPU as the CPU clock frequency (in Hertz).

I suspect that if you add a like like the following prior to "#include <util/delay.h>", your problem will be solved.
Code: [Select]
#define F_CPU 1000000
#include <util/delay.h>

Of course, you will want to use the correct frequency for your chip.

Superman

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,165
  • Helpful? 111
Re: Confusion: Timing Control
« Reply #3 on: September 16, 2008, 07:37:23 PM »
Yeah F_CPU should be set correctly in your makefile and in global.h.

By default the ATMega8 will run at 1MHz so hence 1000000 for F_CPU

You can also set a fuse with the ISP programmer to make it run at 8MHz in which case change F_CPU to 8000000

Also: by using '4.2' then you have started using floating point numbers which will include an enormous floating point emulator library.
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,165
  • Helpful? 111
Re: Confusion: Timing Control
« Reply #4 on: September 16, 2008, 07:38:35 PM »
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

 


Get Your Ad Here