Programmer
In order to program the ATmega8, you need an 8 MHz oscillator, AVR programmer, and AVR Studio with WinAVR. asdf Here is the basic setup of what you will need: (All page numbers will be in referance to the ATmega8(L) manual, unless otherwise specified). This schematic is based on Pages 27, 237, and AVRISP User Guide.
Once your programmer is built, you will want to include a sample program. This will blink port c at approximately 285 KHz (found experimentally). If you want to actually see the LED blink, uncomment the 'int i;' and the 'for(i = 0; i < 20000; i++);'
---------------------------------------------------------------------------------------------------
//Toggles PORTC at 285 KHz
//Daniel Langenberg 12-02-2006
#include <avr/io.h> //Required to use commands like PORTC and DDRC
int main()
{
// int i; //used in the 'for' loop (as a delay)
DDRC = 0xFF; //Make Port C output values
while(1)
{
PORTC = !PORTC*255; //take the value of PORTC and invert it
//for(i = 0; i < 20000; i++); //will introduce a visible delay
}
return 0;
}
---------------------------------------------------------------------------------------------------
PWM
Now that you have a basic program, you might want to play with the PWM. Here is some code that will generate a 56KHz PWM signal at 50% duty cycle
---------------------------------------------------------------------------------------------------
//Generate a PWM Pulse at approximately 56KHz with 50% duty cycle
//Daniel Langenberg 12-02-2006
#include <avr/io.h>
int main()
{
//Set up the PWM signal (page 118-119, ATmega8(L))
//The PWM signal is triggered when Timer/Counter register = Output Compare register. (TCNT2 = OCR2)
//attemtp to make the signal 56 KHz
// Clk/(2*Dividor*(1+OCR2))
// 8MHz/(2*1*(1+70)) = 56.338 KHz
OCR2 = 0x46; //set OCR2 to overflow at 70
// FOC2 = 0;
// WGM20 = 0; //CTC mode. Reset the timer each overflow of OC2 and toggle the PWM value
// COM21 = 0; //
// COM20 = 1; //Toggle on overflow
// WGM21 = 1; //
// CS22 = 0; //
// CS21 = 0; //
// CS20 = 1; //clk/(no prescaling)
TCCR2 = 0x19; //set TCCR2 to the above values (0x00011001b)
DDRB = 0x08; //as of right now, only enable the PWM pin as output
while(1);
return 0;
}
---------------------------------------------------------------------------------------------------
Free tidbit of information that is not very accurate, but close enough
for(i=0;i<65535;i++);
PORTC = ~PORTC;
//delay of 65535 = 164mSec for the For loop to complete
//delay of 40000 = 150mSec
//delay of 10000 = 25mSec
//delay of 1000 = 2.5 mSec
//Delay of 100 = .024 mSec
//typical FOR loop delay: 2.5uS
