// Target : M16
// Crystal: 4.0000Mhz
#include <iom16v.h>
#include <macros.h>
void delay(unsigned int m)
{
unsigned int i,j;
for(i=0;i<m;i++)
for(j=0;j<570;j++);
}
void port_init(void)
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0x00;
DDRB = 0xFF;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0xFF;
}
//TIMER0 initialize - prescale:8
// WGM: PWM Phase correct
// desired value: 1mSec
// actual value: 1.020mSec (-2.0%)
void timer0_init(void)
{
TCCR0 = 0x00; //stop
TCNT0 = 0x00; //set count
OCR0 = 0x64; //set compare
TCCR0 = 0x62; //start timer
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer0_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//
void main(void)
{
init_devices();
while(1)
{
}
}
妳使OCR0的值 由大變小,即輸出的平均電壓變小,就可達到 燈由亮到滅