Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (1.61 KB)

1 13 emarinel
/*
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
}
32
33
// Set the buzzer value - takes a value from 0-255
34
// Higher values are lower frequency.  Take a look at
35
// the buzzer frequency table to see how a given value
36
// correlates to a frequency.
37
void buzzer_set_val(unsigned int buzz_value)
38
{
39
        OCR2 = buzz_value;
40
}
41
42
// Set the buzzer frequency - takes any value and tries to find the nearest buzzer frequency
43
void buzzer_set_freq(unsigned int buzz_freq)
44
{
45
        int buzz_value;
46
47
        buzz_value = 62500/buzz_freq - 1;
48
49
        if(buzz_value > 255)
50
                buzz_value = 255;
51
        else if(buzz_value < 0)
52
                buzz_value = 0;
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
62
        delay_ms(ms);
63
64
        buzzer_off();
65
}
66
67
// Disables timer0 clock counting, turning off the buzzer
68
void buzzer_off()
69
{
70
        // Disable clock, to halt counter
71
        TCCR2 &= 0xF8;
72
73
        // Set buzzer pin low in case it's high
74
        PORTB &= 0x7F;
75
}