100ms Pulse CTC Mode

 avatar
subratasarkar20
c_cpp
14 days ago
799 B
1
Indexable
Never
/*
 * GccApplication13.cpp
 *
 * Created: 27-09-2024 15:16:43
 * Author : subrs
 */ 

#define F_CPU 1000000UL
#include <avr/io.h>

void timer1_init_ctc(void)
{
	TCCR1B |= (1 << WGM12);						// Configure timer 1 for CTC mode
	
	TCCR1B |= (1 << CS11) | (1 << CS10);		// Set prescaler to 64

	OCR1A = 1562;								// 100ms delay with 1MHz clock and 64 prescaler { ((FCPUxTime)/Prescaler)-1 }

	TIMSK |= (1 << OCIE1A);						// Enable interrupt on compare match
}

void delay_100ms()
{
	TCNT1 = 0;									// Reset the timer counter
	
	TIFR |= (1 << OCF1A);						// Clear the output compare match flag
	
	while (!(TIFR & (1 << OCF1A)));				// Wait until the OCF1A flag is set
}

int main(void)
{
	DDRB |= 1<<PB0;
	timer1_init_ctc();

	while (1) 
	{
		PORTB ^= 1<<PB0;
		delay_100ms();
	}
	
	return 0;
}

Leave a Comment