Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / colonet / utilities / dragonfly_wireless_relay / buzzer.c @ 13

History | View | Annotate | Download (1.61 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
initializes buffer
15
this function must be called before the buzzer can be used
16
*/
17
void buzzer_init( void )
18
{
19
//NOTE: This is mostly handled by buzzer_Set_val for now
20
        // Set pin B7 to output - B7 is buzzer pin
21
//        DDRB |= _BV(DDB7);
22
        
23
                
24
        //Fast PWM Mode, Clear OCR0 on compare match and set at bottom, clock/64
25
//        TCCR2 = _BV(COM20) | _BV(WGM21)  | _BV(CS22);
26

    
27
}
28

    
29
// Set the buzzer value - takes a value from 0-255
30
// Higher values are lower frequency.  Take a look at 
31
// the buzzer frequency table to see how a given value
32
// correlates to a frequency.
33
void buzzer_set_val(unsigned int buzz_value)
34
{ 
35
        TCCR2 = _BV(COM20) | _BV(WGM21)  | _BV(CS22);
36
        DDRB |= _BV(DDB7);
37
  OCR2 = buzz_value;
38
}
39

    
40
// Set the buzzer frequency - takes any value and tries to find the nearest 
41
// buzzer frequency
42
void buzzer_set_freq(unsigned int buzz_freq)
43
{
44
  int buzz_value;
45
  
46
  buzz_value = 62500/buzz_freq - 1;
47

    
48
  if(buzz_value > 255){
49
    buzz_value = 255;
50
  }else if(buzz_value < 0){
51
    buzz_value = 0;
52
  }
53
  
54
  buzzer_set_val(buzz_value);
55
}
56

    
57
// Chirps the buzzer for a number of milliseconds
58
void buzzer_chirp(unsigned int ms, unsigned int buzz_freq) 
59
{ 
60
  buzzer_set_freq(buzz_freq);
61
  delay_ms(ms);
62
  buzzer_off();
63
}
64

    
65
// Disables timer0 clock counting, turning off the buzzer
66
void buzzer_off()
67
{
68
  // Disable clock, to halt counter
69
  TCCR2 &= 0xF8;
70
  
71
  // Set buzzer pin low in case it's high
72
  PORTB &= 0xBF;
73
}