Project

General

Profile

Statistics
| Revision:

root / branches / simulator / projects / libdragonfly / buzzer.c @ 976

History | View | Annotate | Download (2.31 KB)

1 241 bcoltin
/**
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 8 bcoltin
26 241 bcoltin
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 8 bcoltin
#include <avr/io.h>
39
#include <buzzer.h>
40
#include <time.h>
41
42
void buzzer_init( void )
43
{
44
        //NOTE: This is mostly handled by buzzer_Set_val for now
45
        // Set pin B7 to output - B7 is buzzer pin
46
        //DDRB |= _BV(DDB7);
47
        //Fast PWM Mode, Clear OCR0 on compare match and set at bottom, clock/64
48
        //TCCR2 = _BV(COM20) | _BV(WGM21)  | _BV(CS22);
49
}
50
51
void buzzer_set_val(unsigned int buzz_value)
52
{
53
        TCCR2 = _BV(COM20) | _BV(WGM21)  | _BV(CS22);
54
        DDRB |= _BV(DDB7);
55
        OCR2 = buzz_value;
56
}
57
58
void buzzer_set_freq(unsigned int buzz_freq)
59
{
60
  int buzz_value;
61
62
  buzz_value = 62500/buzz_freq - 1;
63
64
  if(buzz_value > 255){
65
    buzz_value = 255;
66
  }else if(buzz_value < 0){
67
    buzz_value = 0;
68
  }
69
70
  buzzer_set_val(buzz_value);
71
}
72
73
void buzzer_chirp(unsigned int ms, unsigned int buzz_freq)
74
{
75
  buzzer_set_freq(buzz_freq);
76
  delay_ms(ms);
77
  buzzer_off();
78
}
79
80
void buzzer_off()
81
{
82
  // Disable clock, to halt counter
83
  TCCR2 &= 0xF8;//0b11111000;
84
85
  // Set buzzer pin low in case it's high
86
  PORTB &= 0xBF;//0b10111111;
87
}