Statistics
| Revision:

## root / trunk / code / projects / colonet / testing / robot_routine_reg_test / buzzer.c @ 13

 1 ```/* ``` ``` buzzer.c - Contains the functions necessary for running the buzzer ``` ``` ``` ``` author: Robotics Club, Colony Project ``` ``` ``` ``` much taken from FWR's firefly library (author: Tom Lauwers) ``` ``` ``` ```*/ ``` ```#include ``` ```#include "buzzer.h" ``` ```#include "time.h" ``` ```/* ``` ```initializes buffer ``` ``` ``` ```this function must be called before the buzzer can be used ``` ```*/ ``` ```void buzzer_init( void ) ``` ```{ ``` ``` // Set pin D7 to output - D7 is buzzer pin ``` ``` DDRB |= 0x80; ``` ``` ``` ``` // Set to Fast PWM mode, toggle the OC0A pin (D6) on output compare ``` ``` // matches, and select a clock prescalar of 64 for the counter. ``` ``` // Counter FREQ = 8000000/64 = 125000 ``` ``` //01011000 ``` ``` TCCR2 = 0x1B; ``` ``` OCR2=100; ``` ``` ``` ```} ``` ```// Set the buzzer value - takes a value from 0-255 ``` ```// Higher values are lower frequency. Take a look at ``` ```// the buzzer frequency table to see how a given value ``` ```// correlates to a frequency. ``` ```void buzzer_set_val(unsigned int buzz_value) ``` ```{ ``` ``` OCR2 = buzz_value; ``` ```} ``` ```// Set the buzzer frequency - takes any value and tries to find the nearest buzzer frequency ``` ```void buzzer_set_freq(unsigned int buzz_freq) ``` ```{ ``` ``` int buzz_value; ``` ``` ``` ``` buzz_value = 62500/buzz_freq - 1; ``` ``` if(buzz_value > 255) ``` ``` buzz_value = 255; ``` ``` else if(buzz_value < 0) ``` ``` buzz_value = 0; ``` ``` ``` ``` buzzer_set_val(buzz_value); ``` ```} ``` ```// Chirps the buzzer for a number of milliseconds ``` ```void buzzer_chirp(unsigned int ms, unsigned int buzz_freq) ``` ```{ ``` ``` buzzer_set_freq(buzz_freq); ``` ``` ``` ``` delay_ms(ms); ``` ``` ``` ``` buzzer_off(); ``` ```} ``` ```// Disables timer0 clock counting, turning off the buzzer ``` ```void buzzer_off() ``` ```{ ``` ``` // Disable clock, to halt counter ``` ``` TCCR2 &= 0xF8; ``` ``` ``` ``` // Set buzzer pin low in case it's high ``` ``` PORTB &= 0x7F; ``` ```} ```