Project

General

Profile

Statistics
| Revision:

root / branches / rbom / code / projects / colonet / utilities / robot_wireless_relay / buzzer.c @ 1390

History | View | Annotate | Download (1.57 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
  // Set pin D7 to output - D7 is buzzer pin
20
  DDRB |= 0x80;
21
  
22
  // Set to Fast PWM mode, toggle the OC0A pin (D6) on output compare
23
  // matches, and select a clock prescalar of 64 for the counter.
24
  // Counter FREQ = 8000000/64 = 125000
25
  //01011000
26
  TCCR2 = 0x1B;
27
  OCR2=100;
28
}
29

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

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

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

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

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