Xplore007 (talk) 17:42, 4 February 2016 (IST)
Contents
[hide]Objective
After blinking the leds and Controlling them using switches, its time to make some noise using the buzzer. In the first part of the tutorial, we will beep the buzzer with a delay of 1sec. At the end we will control the buzzer using a switch.
Register Configuration
Please refer the below tutorial for basics of GPIO register configuration.
Buzzer Beep Code
Below points needs to be considered for this example.
- Include the io.h file as it has the definitions for all the PORT registers.
- Include delay.h file to use the delay functions.
- Configure the PORT as Output before writing any data to PORT pins.
- Turn ON and OFF the buzzer with a delay of 1sec.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <avr/io.h> | |
#include <util/delay.h> | |
int main() | |
{ | |
DDRC = 0xff; // Configure PORTC as output | |
while(1) | |
{ | |
PORTC = 0xff; // Turn ON the Buzzer conneted to PORTC | |
_delay_ms(1000); // Wait for some time | |
PORTC = 0x00; // Turn OFF the Buzzer connected to PORTC | |
_delay_ms(1000); // Wait for some time | |
} | |
return 0; | |
} |
Switch And Buzzer Code
Below points needs to be considered for this example.
- Include the io.h file as it has the definitions for all the PORT registers.
- Configure PORTA as inputs to read the switch status.
- Configure the PORTC as Output to turn ON/OFF the Buzzer.
- Continuously read the switch status and control the buzzer accordingly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <avr/io.h> | |
int main() | |
{ | |
DDRC = 0xff; // Configure PORTC as output to connect Buzzer | |
DDRA = 0x00; // Configure PORTA as Input to read the switchs | |
PORTA = 0xff; // Enable The PullUps of PORTA. | |
while(1) | |
{ | |
PORTC = PINA; // Read the switch status and Turn ON/OFF the Buzzer | |
} | |
return 0; | |
} |
Wiring diagram