Hello!
I continue to study the process of signal synchronization.
I have external PWM signal - 400 Hz.
This signal is connected to the INT0 pin on Atmega2560. The rising edge of INT0 generates an interrupt request ISR(INT0_vect). In this interrupt I enable the internal 400Hz PWM.
The main task is synchronize the rising edge of external and internal 400 Hz PWM.
The idea in as follows:
Interrupt triggers the internal PWM. When the couner reaches the value near 90% of the maximum - timer turns off.
Maybe you thing that this is unclear option, but it suints my task well.
The problem is:
I'm trying to compare TCNT1 value with 39000, but this condition is not working.
my code:
#define F_CPU 16000000UL #include <avr/io.h> #include <avr/interrupt.h> volatile unsigned int t_1=0; volatile unsigned int t_2=0; uint16_t period=0; unsigned long frequency =0; //capture Flag volatile uint8_t flag; int TOP=40000; //Задаем период в битах. void ExternalInterruptIni(void) { EICRA|=(1<<ISC01)|(1<<ISC00); EIMSK|=(1<<INT0); } void Timer_400Hz_ini(void) { ICR1=TOP; OCR1A=20000; TCCR1A|=(1<<COM1A1)|(1<<WGM11); TCCR1B|=(1<<WGM13)|(1<<WGM12)|(1<<CS10); } void UART0_Init(void) { UBRR0L = 103; UBRR0H = 0; UCSR0B=(1<<RXEN0)|(1<<TXEN0); UCSR0C=(1<<UPM01)|(1<<UPM00)|(1<<UCSZ01)|(1<<UCSZ00);//(1<<UPM01)| } //Функция отправки на терминал void uart0_tr(char value) { while(!(UCSR0A & (1 << UDRE0))); // Ожидаем когда очистится буфер передачи UDR0 = value; } // print both bytes of a 16 bit hex number void UART_puthex16(uint16_t n) { uart0_tr(n >> 8); uart0_tr(n & 0xFF); } int main(void) { DDRB|=(1<<PB5); UART0_Init(); // initialize timer ExternalInterruptIni(); sei(); while (1) { if (TCNT1>=39000) { TCCR1A=0; } } } ISR(INT0_vect) { Timer_400Hz_ini(); }