// Copyright 2006 Yamane Akira // Input Capture Test Sample Program // Target : M48 // Crystal: 1.0000Mhz // PB0->ICP1, measuring time from falling edge to falling edge // Programmed by A. Yamane 2005 Sept. #include #include unsigned int cur_v, temp_v, value; void port_init(void) { PORTB = 0xFE; // PB0->icp1 DDRB = 0xFE; PORTC = 0x7F; DDRC = 0x7F; PORTD = 0xFF; DDRD = 0xFF; } //TIMER1 initialize - prescale:8 // WGM: 0) Normal, TOP=0xFFFF // clkio = 8us // void timer1_init(void) { TCCR1B = 0x00; //stop TCNT1H = 0xFF; //setup TCNT1L = 0x83; OCR1AH = 0x00; OCR1AL = 0x7D; OCR1BH = 0x00; OCR1BL = 0x7D; ICR1H = 0x00; ICR1L = 0x00; TCCR1A = 0x00; TCCR1B = 0x02; //start Timer, Prescaler 1/8, Input Capture Edge ICES1=0 } #pragma interrupt_handler timer1_capt_isr:11 void timer1_capt_isr(void) { cur_v = ICR1L; cur_v |= (int)ICR1H << 8; value = cur_v - temp_v; temp_v = cur_v; PORTD = ~(char)value; PORTC = ~(char)(value>>8); } #pragma interrupt_handler timer1_ovf_isr:14 void timer1_ovf_isr(void) { temp_v = 0x0000; } //call this routine to initialize all peripherals void init_devices(void) { //stop errant interrupts until set up CLI(); //disable all interrupts port_init(); timer1_init(); MCUCR = 0x00; EICRA = 0x00; //extended ext ints EIMSK = 0x00; TIMSK0 = 0x00; //timer 0 interrupt sources TIMSK1 = 0x21; //timer 1 bit5 (ICIE1) is set, bit0(TOIE1) is set 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(); temp_v = 0x0000; cur_v = 0x0000; while(1) { } }