Project

General

Profile

Statistics
| Revision:

root / branches / charging_station / code / projects / recharging / charging_station / buzzer.c @ 85

History | View | Annotate | Download (1.64 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

    
10
#include <avr/io.h>
11
#include "buzzer.h"
12
#include "time.h"
13

    
14
/*
15
initializes buffer
16

17
this function must be called before the buzzer can be used
18
*/
19
void buzzer_init( void )
20
{
21
        // Set pin D7 to output - D7 is buzzer pin
22
        DDRB |= 0x80;
23
        
24
        // Set to Fast PWM mode, toggle the OC0A pin (D6) on output compare
25
        // matches, and select a clock prescalar of 64 for the counter.
26
        // Counter FREQ = 8000000/64 = 125000
27
        //01011000
28
        TCCR2 = 0x1B;
29
        OCR2=100;
30
  
31
  buzzer_off();
32
        
33
}
34

    
35
// Set the buzzer value - takes a value from 0-255
36
// Higher values are lower frequency.  Take a look at 
37
// the buzzer frequency table to see how a given value
38
// correlates to a frequency.
39
void buzzer_set_val(unsigned int buzz_value)
40
{ 
41
        OCR2 = buzz_value;
42
}
43

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

    
51
        if(buzz_value > 255)
52
                buzz_value = 255;
53
        else if(buzz_value < 0)
54
                buzz_value = 0;
55
        
56
        buzzer_set_val(buzz_value);
57
}
58

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

    
70
// Disables timer0 clock counting, turning off the buzzer
71
void buzzer_off() 
72
{
73
        // Disable clock, to halt counter
74
        TCCR2 &= 0xF8;
75
        
76
        // Set buzzer pin low in case it's high
77
        PORTB &= 0x7F;
78
}
79