Buzzer code.

master
q3k 2013-09-15 13:17:38 +02:00
parent 7b47838be6
commit b373d816c2
6 changed files with 91 additions and 21 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
openwrt
sdk
*swp

View File

@ -1,5 +1,5 @@
PRG = uart_hello
OBJ = main.o
OBJ = main.o buzzer.o
PROGRAMMER = buspirate
PORT = /dev/ttyACM0
MCU_TARGET = atmega8

38
software/keypad/buzzer.c Normal file
View File

@ -0,0 +1,38 @@
#include <avr/io.h>
#include <avr/interrupt.h>
#include "io.h"
IMPORT_IO(BUZZER);
void buzzer_init(void)
{
// Set up Timer1 - CTC and /256 prescaler
TCCR1A = 0;
TCCR1B = _BV(WGM12) | _BV(CS12);
TCNT1 = 0;
OCR1A = 4;
OCR1B = 0xFFFF;
}
void buzzer_start(uint16_t ticks)
{
cli();
TIMSK &= ~_BV(OCIE1A);
OCR1A = ticks;
TIMSK = _BV(OCIE1A);
TCNT1 = 0;
sei();
}
void buzzer_stop(void)
{
cli();
TIMSK &= ~_BV(OCIE1A);
sei();
}
ISR(TIMER1_COMPA_vect)
{
IO_TOGGLE(BUZZER);
}

10
software/keypad/buzzer.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef __BUZZER_H__
#define __BUZZER_H__
void buzzer_init(void);
#define TONE_LOW 8
#define TONE_MID 4
#define TONE_HIGH 2
#endif

26
software/keypad/io.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef _Q__IO_H__
#define _Q__IO_H__
typedef struct
{
volatile uint8_t *DDR;
volatile uint8_t *PORT;
volatile uint8_t *PIN;
uint8_t Bitmap;
} TIOPort;
#define DECLARE_IO(name, port, bit) TIOPort g_IO_##name = { &DDR##port, &PORT##port, &PIN##port, (1 << bit) }
#define IMPORT_IO(name) extern TIOPort g_IO_##name
#define IO_SET_OUTPUT(name) do { *g_IO_##name.DDR |= g_IO_##name.Bitmap; } while(0)
#define IO_SET_INPUT(name) do { *g_IO_##name.DDR &= ~g_IO_##name.Bitmap; } while(0)
#define IO_INPUT_PULLUP(name) do { *g_IO_##name.PORT |= g_IO_##name.Bitmap; } while(0)
#define IO_OUT(name, value) do { if (value) \
*g_IO_##name.PORT |= g_IO_##name.Bitmap; \
else \
*g_IO_##name.PORT &= ~g_IO_##name.Bitmap; } while(0)
#define IO_TOGGLE(name) do { *g_IO_##name.PORT ^= g_IO_##name.Bitmap; } while(0)
#define IO_IN(name) (*g_IO_##name.PIN & g_IO_##name.Bitmap)
#endif

View File

@ -1,31 +1,15 @@
#include <avr/io.h>
#include <avr/wdt.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include "io.h"
#include "buzzer.h"
/////////////////////
// I/O ports setup //
/////////////////////
typedef struct
{
volatile uint8_t *DDR;
volatile uint8_t *PORT;
volatile uint8_t *PIN;
uint8_t Bitmap;
} TIOPort;
#define DECLARE_IO(name, port, bit) TIOPort g_IO_##name = { &DDR##port, &PORT##port, &PIN##port, (1 << bit) }
#define IO_SET_OUTPUT(name) do { *g_IO_##name.DDR |= g_IO_##name.Bitmap; } while(0)
#define IO_SET_INPUT(name) do { *g_IO_##name.DDR &= ~g_IO_##name.Bitmap; } while(0)
#define IO_INPUT_PULLUP(name) do { *g_IO_##name.PORT |= g_IO_##name.Bitmap; } while(0)
#define IO_OUT(name, value) do { if (value) \
*g_IO_##name.PORT |= g_IO_##name.Bitmap; \
else \
*g_IO_##name.PORT &= ~g_IO_##name.Bitmap; } while(0)
#define IO_TOGGLE(name) do { *g_IO_##name.PORT ^= g_IO_##name.Bitmap; } while(0)
#define IO_IN(name) (*g_IO_##name.PIN & g_IO_##name.Bitmap)
// Buzzer on PC2
DECLARE_IO(BUZZER, C, 2);
// Red LED pn PC1
@ -54,6 +38,17 @@ int main (void)
IO_OUT(LED_GREEN, 1);
IO_OUT(LED_RED, 1);
buzzer_init();
sei();
buzzer_start(TONE_LOW);
_delay_ms(10);
buzzer_start(TONE_MID);
_delay_ms(10);
buzzer_start(TONE_HIGH);
_delay_ms(10);
buzzer_stop();
for (;;) {}
return 0;
}