root / branches / encoders / code / lib / src / libdragonfly / buzzer.c @ 1345
History | View | Annotate | Download (2.41 KB)
1 |
/*
|
---|---|
2 |
buzzer.c - Contains the functions necessary for running the buzzer
|
3 |
|
4 |
author: Robotics Club, Colony Project
|
5 |
|
6 |
much taken from FWR's firefly library (author: Tom Lauwers)
|
7 |
*/
|
8 |
|
9 |
#include <avr/io.h> |
10 |
#include <buzzer.h> |
11 |
#include <time.h> |
12 |
|
13 |
/**
|
14 |
* @defgroup buzzer Buzzer
|
15 |
* @brief Functions for controlling the buzzer.
|
16 |
* Functions for controlling the buzzer. Include
|
17 |
* buzzer.h to access these functions.
|
18 |
* @{
|
19 |
**/
|
20 |
|
21 |
/**
|
22 |
* Initializes the buzzer. Must be called before any other buzzer
|
23 |
* function may be used.
|
24 |
**/
|
25 |
void buzzer_init( void ) |
26 |
{ |
27 |
//NOTE: This is mostly handled by buzzer_Set_val for now
|
28 |
// Set pin B7 to output - B7 is buzzer pin
|
29 |
//DDRB |= _BV(DDB7);
|
30 |
//Fast PWM Mode, Clear OCR0 on compare match and set at bottom, clock/64
|
31 |
//TCCR2 = _BV(COM20) | _BV(WGM21) | _BV(CS22);
|
32 |
} |
33 |
|
34 |
/**
|
35 |
* Sets the value of the buzzer's pitch.
|
36 |
* Higher values are lower frequencies.
|
37 |
*
|
38 |
* @param buzz_value the value to set the buzzer's frequency too,
|
39 |
* in the range 0-255
|
40 |
*
|
41 |
* @see buzzer_init, buzzer_set_freq, buzzer_off
|
42 |
**/
|
43 |
void buzzer_set_val(unsigned int buzz_value) |
44 |
{ |
45 |
TCCR2 = _BV(COM20) | _BV(WGM21) | _BV(CS22); |
46 |
DDRB |= _BV(DDB7); |
47 |
OCR2 = buzz_value; |
48 |
} |
49 |
|
50 |
/**
|
51 |
* Sets the buzzer frequency. Usage of constants such as C4
|
52 |
* is highly recommended as input to this function.
|
53 |
* buzzer_init must be called before this function may
|
54 |
* be used.
|
55 |
*
|
56 |
* @param buzz_freq the frequency to set the buzzer to
|
57 |
*
|
58 |
* @see buzzer_init, buzzer_set_val, buzzer_off
|
59 |
**/
|
60 |
void buzzer_set_freq(unsigned int buzz_freq) |
61 |
{ |
62 |
int buzz_value;
|
63 |
|
64 |
buzz_value = 62500/buzz_freq - 1; |
65 |
|
66 |
if(buzz_value > 255){ |
67 |
buzz_value = 255;
|
68 |
}else if(buzz_value < 0){ |
69 |
buzz_value = 0;
|
70 |
} |
71 |
|
72 |
buzzer_set_val(buzz_value); |
73 |
} |
74 |
|
75 |
/**
|
76 |
* Plays the specified frequency for the specified
|
77 |
* amount of time. This function blocks execution
|
78 |
* until the time is completed. buzzer_init must be
|
79 |
* called before this function can be used.
|
80 |
*
|
81 |
* @param ms the time in milliseconds to play the frequency
|
82 |
* @param buzz_freq the frequency to play
|
83 |
*
|
84 |
* @see buzzer_init, buzzer_set_freq
|
85 |
**/
|
86 |
void buzzer_chirp(unsigned int ms, unsigned int buzz_freq) |
87 |
{ |
88 |
buzzer_set_freq(buzz_freq); |
89 |
delay_ms(ms); |
90 |
buzzer_off(); |
91 |
} |
92 |
|
93 |
/**
|
94 |
* Turns off the buzzer by disabling the timer0 clock.
|
95 |
*
|
96 |
* @see buzzer_init
|
97 |
**/
|
98 |
void buzzer_off()
|
99 |
{ |
100 |
// Disable clock, to halt counter
|
101 |
TCCR2 &= 0xF8;//0b11111000; |
102 |
|
103 |
// Set buzzer pin low in case it's high
|
104 |
PORTB &= 0xBF;//0b10111111; |
105 |
} |
106 |
|
107 |
/** @} **/ // end buzzer group |
108 |
|