// Copyright 2006 Yamane Akira //ICC-AVR // Target : M48 // Crystal: 1.0000Mhz // mode:1 Phase Correct PWM #include #include unsigned char i; void port_init(void) { PORTB = 0xFF; DDRB = 0xFF; PORTC = 0x7F; DDRC = 0x00; PORTD = 0xFF; DDRD = 0x60; } //TIMER0 initialize - prescale:64 // WGM: PWM Ph correct m1 // actual value: void timer0_init(void) { TCCR0B = 0x00; //stop TCNT0 = 0x01; //set count OCR0A = 0xC1; OCR0B = 0xA3; TCCR0A = 0xA1; // TCCR0B = 0x02; //start timer } #pragma interrupt_handler timer0_ovf_isr:17 void timer0_ovf_isr(void) { PORTB ^= 0x01; i++; if (i >=10) OCR0A = 0x20; if (i>= 20) { OCR0A = 0xC1; i=0; } } //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; EICRA = 0x00; //extended ext ints EIMSK = 0x00; TIMSK0 = 0x01; //timer 0 interrupt sources TIMSK1 = 0x00; //timer 1 interrupt sources TIMSK2 = 0x00; //timer 2 interrupt sources PCMSK0 = 0x00; //pin change mask 0 PCMSK1 = 0x00; //pin change mask 1 PCMSK2 = 0x00; //pin change mask 2 PCICR = 0x00; //pin change enable PRR = 0x00; //power controller SEI(); //re-enable interrupts //all peripherals are now initialized } // void main(void) { init_devices(); i=0; while(1) ; }