Buzzer


Controls the buzzer on TB-1

Toolkit:AVR Development System

Location:/bipom/devtools/WinAVR/minimaxavrc/Examples/Labs/Lab10

Code Example


#define ONE_SECOND 500

#define BUZZER          7        //PB7


#define TimeBuzzer  1500

#define BUZZER_ON() 		(PORTB	&=~ 0x80)
#define BUZZER_OFF() 	    (PORTB	|= 0x80)

unsigned char  Flag;
unsigned int Ticks = 0 ;
unsigned char  RedFlag;         // Flag for Red LED state

unsigned int counter = 0;


ISR (TIMER0_OVF_vect)
{
	// Interrupt every 2 mS

	if(!Flag) 
	{
		Flag=1;	
		BUZZER_ON() ;// Turn on Buzzer

		counter++;
		if(counter>TimeBuzzer)
		{
			BUZZER_OFF();//Turn off Buzzer				

			uart0Printf("Buzzer Off\n");
		}
	}
	else
	{
		Flag=0;
		BUZZER_OFF();//Turn off Buzzer

	}	
}


// *******************************************************************************

int main(void)
{
	//Initialize port pins

 	/* Define pull-ups and set outputs */
	/* Define directions for port pins */
	PORTB = (1<<BUZZER);
	DDRB  = (1<<BUZZER);
	
	uart0Init(19200);
	
	//Initialization Timer0

	// Timer/Counter 0 initialization

	// Clock source: System Clock

	// Clock value: 125,000 kHz

	// Interrupt every 2mS

	TCCR0A=0x00;
	TCCR0B=0x03;
	TCNT0=0x00;
	OCR0A=0x00;
	OCR0B=0x00;	

	//Timer0 Overflow Interrupt Enable													

	TIMSK0 = 0x01;
	Ticks = 0;

	// Global enable interrupts

	sei();

	while (1) ;
}