Statistics
| Revision:

root / branches / colonetmk2 / code / projects / libdragonfly / buzzer.c @ 1456

History | View | Annotate | Download (3.56 KB)

1
/**
2
 * Copyright (c) 2007 Colony Project
3
 * 
4
 * Permission is hereby granted, free of charge, to any person
5
 * obtaining a copy of this software and associated documentation
6
 * files (the "Software"), to deal in the Software without
7
 * restriction, including without limitation the rights to use,
8
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 * copies of the Software, and to permit persons to whom the
10
 * Software is furnished to do so, subject to the following
11
 * conditions:
12
 * 
13
 * The above copyright notice and this permission notice shall be
14
 * included in all copies or substantial portions of the Software.
15
 * 
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
 * OTHER DEALINGS IN THE SOFTWARE.
24
 **/
25

    
26

    
27
/**
28
 * @file buzzer
29
 * @brief Functions for using the buzzer
30
 *
31
 * Contains functions for using the buzzer.
32
 *
33
 * @author Colony Project, CMU Robotics Club
34
 *
35
 * much taken from FWR's firefly library (author: Tom Lauwers)
36
 **/
37

    
38
#include <avr/io.h>
39
#include "buzzer.h"
40
#include "time.h"
41

    
42
/**
43
 * @defgroup buzzer Buzzer
44
 * @brief Functions for controlling the buzzer.
45
 * Functions for controlling the buzzer. Include
46
 * buzzer.h to access these functions.
47
 * @{
48
 **/
49

    
50
/**
51
 * Initializes the buzzer. Must be called before any other buzzer
52
 * function may be used.
53
 **/
54
void buzzer_init( void )
55
{
56
        //NOTE: This is mostly handled by buzzer_Set_val for now
57
        // Set pin B7 to output - B7 is buzzer pin
58
        //DDRB |= _BV(DDB7);
59
        //Fast PWM Mode, Clear OCR0 on compare match and set at bottom, clock/64
60
        //TCCR2 = _BV(COM20) | _BV(WGM21)  | _BV(CS22);
61
}
62

    
63
/**
64
 * Sets the value of the buzzer's pitch.
65
 * Higher values are lower frequencies.
66
 *
67
 * @param buzz_value the value to set the buzzer's frequency too,
68
 * in the range 0-255
69
 *
70
 * @see buzzer_init, buzzer_set_freq, buzzer_off
71
 **/
72
void buzzer_set_val(unsigned int buzz_value)
73
{ 
74
        TCCR2 = _BV(COM20) | _BV(WGM21)  | _BV(CS22);
75
        DDRB |= _BV(DDB7);
76
        OCR2 = buzz_value;
77
}
78

    
79
/**
80
 * Sets the buzzer frequency. Usage of constants such as C4
81
 * is highly recommended as input to this function.
82
 * buzzer_init must be called before this function may
83
 * be used.
84
 *
85
 * @param buzz_freq the frequency to set the buzzer to
86
 *
87
 * @see buzzer_init, buzzer_set_val, buzzer_off
88
 **/
89
void buzzer_set_freq(unsigned int buzz_freq)
90
{
91
  int buzz_value;
92
  
93
  buzz_value = 62500/buzz_freq - 1;
94

    
95
  if(buzz_value > 255){
96
    buzz_value = 255;
97
  }else if(buzz_value < 0){
98
    buzz_value = 0;
99
  }
100
  
101
  buzzer_set_val(buzz_value);
102
}
103

    
104
/**
105
 * Plays the specified frequency for the specified
106
 * amount of time. This function blocks execution
107
 * until the time is completed. buzzer_init must be
108
 * called before this function can be used.
109
 *
110
 * @param ms the time in milliseconds to play the frequency
111
 * @param buzz_freq the frequency to play
112
 *
113
 * @see buzzer_init, buzzer_set_freq
114
 **/
115
void buzzer_chirp(unsigned int ms, unsigned int buzz_freq) 
116
{ 
117
  buzzer_set_freq(buzz_freq);
118
  delay_ms(ms);
119
  buzzer_off();
120
}
121

    
122
/**
123
 * Turns off the buzzer by disabling the timer0 clock.
124
 *
125
 * @see buzzer_init
126
 **/
127
void buzzer_off()
128
{
129
  // Disable clock, to halt counter
130
  TCCR2 &= 0xF8;//0b11111000;
131
  
132
  // Set buzzer pin low in case it's high
133
  PORTB &= 0xBF;//0b10111111;
134
}
135

    
136
/** @} **/ // end buzzer group
137