Код: Выделить всё
#include <mega8>
unsigned char sec,EdgeR,EdgeF,TimeImp,i;
interrupt [EXT_INT0] void ext_int0_isr(void)
{
TCNT1H=0xF0;
TCNT1L=0xBD;
i++;
switch (i){
//Возрастающий импульс
case '0':
EdgeR=sec;
MCUCR=0b00000010;//установить прерывание по cпадающему импульсу
break;
//Спадающий импульс
case '1': EdgeF=sec;
MCUCR=0b00000011;//установить прерывание по возрастающему импульсу
i=0;
TimeImp=EdgeF-EdgeR; //посчитали длительность импульса
break;
}
}
// Timer 1 overflow interrupt service routine
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
// Reinitialize Timer 1 value
TCNT1H=0xF0;
TCNT1L=0xBD;
// Place your code here
++sec;
}
// Declare your global variables here
void main(void)
{
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x10;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 4000,000 kHz
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: On
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x05;
TCNT1H=0xF0;
TCNT1L=0xBD;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x04;
// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Возрастающий импульс
// INT1: Off
GICR|=0x40;
MCUCR=0b00000011;
GIFR=0x40;
// Global enable interrupts
#asm("sei")
while (1)
{
if(TimeImp>0) {
PORTD=0x10;
}
};
}


