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.