<<< Zpět

#define	B4	0x10
#define BIT(pos) (1 << (pos))

/*=============================================================================

      Square wave tone generator (speaker connected to PB4, using Timer0)

=============================================================================*/
/* http://extremeelectronics.co.in/avr-tutorials/avr-timers-an-introduction/ */

/* F_CPU = 7372800 Hz, prescaled to 921600 Hz, divided 256 to 3.6 kHz */

#include <avr/io.h>
#include <avr/interrupt.h>

int main(void)
{
	/* prescaler = F_CPU/8 */
	TCCR0 |= BIT(CS01);

	/* enable overflow interrupt */
	TIMSK |= BIT(TOIE0);

	/* initialize counter */
	TCNT0 = 0;

	/* set PB4 to output mode */
	DDRB |= B4;

	/* enable global interrupt */
	sei();

	/* infinite loop */
	while (1);
}

/* interrupt routine */
ISR(TIMER0_OVF_vect)
{
	PORTB ^= B4;
}

<<< Zpět