2012-10-02 21:27:52 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <avr/io.h>
|
|
|
|
#include <avr/interrupt.h>
|
|
|
|
#include <avr/pgmspace.h>
|
|
|
|
|
|
|
|
#include "lcd_routines.h"
|
|
|
|
#include "uart.h"
|
|
|
|
|
2012-10-02 22:23:08 +00:00
|
|
|
#define SAVE PD2
|
|
|
|
#define SIGNAL PD3
|
|
|
|
#define FREQ_DOWN PD4
|
|
|
|
#define FREQ_UP PD5
|
|
|
|
#define AMP_DOWN PD6
|
|
|
|
#define AMP_UP PD7
|
|
|
|
#define KEY_DDR DDRD
|
|
|
|
#define KEY_PORT PORTD
|
|
|
|
#define KEY_PIN PIND
|
|
|
|
#define KEY_ON_DDR DDRB
|
|
|
|
#define KEY_ON_PORT PORTB
|
|
|
|
#define KEY_ON_PIN PINB
|
|
|
|
#define KEY_ON PB0
|
2012-10-02 21:27:52 +00:00
|
|
|
|
|
|
|
#define LED_PIN PA7
|
|
|
|
#define LED_PORT PORTA
|
|
|
|
#define LED_DDR DDRA
|
|
|
|
|
2012-10-02 22:23:08 +00:00
|
|
|
#define led_on() LED_PORT |= _BV(LED_PIN);
|
|
|
|
#define led_off() LED_PORT &= ~_BV(LED_PIN);
|
|
|
|
#define led_toggle() LED_PORT ^= _BV(LED_PIN)
|
2012-10-02 21:27:52 +00:00
|
|
|
|
|
|
|
#define UART_BAUD_RATE 9600
|
|
|
|
|
|
|
|
#define ALL_KEYS (_BV(SAVE) | _BV(SIGNAL) | _BV(FREQ_DOWN) | _BV(FREQ_UP) | _BV(AMP_DOWN) | _BV(AMP_UP) )
|
|
|
|
|
|
|
|
#define REPEAT_MASK ALL_KEYS
|
|
|
|
#define REPEAT_START 50
|
|
|
|
#define REPEAT_NEXT 20
|
|
|
|
|
2012-10-02 22:23:08 +00:00
|
|
|
volatile uint16_t key_state;
|
|
|
|
volatile uint16_t key_press;
|
2012-10-02 21:27:52 +00:00
|
|
|
|
|
|
|
/* prototypes */
|
|
|
|
void init_io();
|
2012-10-02 22:23:08 +00:00
|
|
|
uint16_t get_key_press(uint16_t key_mask);
|
2012-10-02 21:27:52 +00:00
|
|
|
|
|
|
|
int main(void) {
|
|
|
|
init_io();
|
|
|
|
lcd_init();
|
|
|
|
uart_init(UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU));
|
|
|
|
|
|
|
|
lcd_setcursor(0, 0);
|
|
|
|
|
|
|
|
unsigned int c;
|
|
|
|
|
|
|
|
sei();
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (uart_available() > 0) {
|
|
|
|
c = uart_getc();
|
|
|
|
|
2012-10-04 22:04:41 +00:00
|
|
|
if (c == '}') {
|
2012-10-02 21:27:52 +00:00
|
|
|
lcd_clear();
|
2012-10-04 22:04:41 +00:00
|
|
|
} else if (c == '*') {
|
2012-10-02 21:27:52 +00:00
|
|
|
lcd_setcursor(0, 2);
|
2012-10-04 22:04:41 +00:00
|
|
|
} else if (c == '+') {
|
|
|
|
lcd_setcursor(0, 1);
|
2012-10-02 22:23:08 +00:00
|
|
|
} else if (c == '\'') {
|
|
|
|
led_off();
|
|
|
|
} else if (c == '#') {
|
|
|
|
led_on();
|
2012-10-02 21:27:52 +00:00
|
|
|
} else {
|
|
|
|
lcd_data(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (get_key_press(_BV(FREQ_DOWN))) {
|
|
|
|
uart_putc('f');
|
|
|
|
}
|
|
|
|
if (get_key_press(_BV(FREQ_UP))) {
|
|
|
|
uart_putc('F');
|
|
|
|
}
|
|
|
|
if (get_key_press(_BV(SIGNAL))) {
|
|
|
|
uart_putc('s');
|
|
|
|
}
|
|
|
|
if (get_key_press(_BV(AMP_UP))) {
|
|
|
|
uart_putc('A');
|
|
|
|
}
|
|
|
|
if (get_key_press(_BV(AMP_DOWN))) {
|
|
|
|
uart_putc('a');
|
|
|
|
}
|
2012-10-02 22:23:08 +00:00
|
|
|
if (get_key_press(_BV(KEY_ON) << 8)) {
|
|
|
|
uart_putc('o');
|
|
|
|
}
|
2012-10-02 21:27:52 +00:00
|
|
|
if (get_key_press(_BV(SAVE))) {
|
|
|
|
uart_putc('v');
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void init_io() {
|
|
|
|
KEY_DDR &= ~ALL_KEYS; // configure key port for input
|
|
|
|
KEY_PORT |= ALL_KEYS;
|
2012-10-02 22:23:08 +00:00
|
|
|
|
|
|
|
KEY_ON_DDR &= ~_BV(KEY_ON);
|
|
|
|
KEY_ON_PORT |= _BV(KEY_ON);
|
|
|
|
|
2012-10-02 21:27:52 +00:00
|
|
|
LED_DDR |= _BV(LED_PIN);
|
2012-10-02 22:23:08 +00:00
|
|
|
LED_PORT &= ~_BV(LED_PIN);
|
2012-10-02 21:27:52 +00:00
|
|
|
|
|
|
|
TCCR0 = (1 << CS02) | (1 << CS00); // divide by 1024
|
|
|
|
TCNT0 = (uint8_t) (int16_t) -(F_CPU / 1024 * 10e-3 + 0.5); // preload for 10ms
|
|
|
|
TIMSK |= 1 << TOIE0; // enable timer interrupt
|
|
|
|
}
|
|
|
|
|
|
|
|
ISR( TIMER0_OVF_vect ) {
|
2012-10-02 22:23:08 +00:00
|
|
|
static uint16_t ct0, ct1;
|
|
|
|
uint16_t i;
|
2012-10-02 21:27:52 +00:00
|
|
|
|
|
|
|
TCNT0 = (uint8_t) (int16_t) -(F_CPU / 1024 * 10e-3 + 0.5); // preload for 10ms
|
|
|
|
|
2012-10-02 22:23:08 +00:00
|
|
|
i = key_state ^ ~(KEY_PIN | (KEY_ON_PIN << 8)); // key changed ?
|
2012-10-02 21:27:52 +00:00
|
|
|
ct0 = ~(ct0 & i); // reset or count ct0
|
|
|
|
ct1 = ct0 ^ (ct1 & i); // reset or count ct1
|
|
|
|
i &= ct0 & ct1; // count until roll over ?
|
|
|
|
key_state ^= i; // then toggle debounced state
|
|
|
|
key_press |= key_state & i; // 0->1: key press detect
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-10-02 22:23:08 +00:00
|
|
|
uint16_t get_key_press(uint16_t key_mask) {
|
2012-10-02 21:27:52 +00:00
|
|
|
cli();
|
|
|
|
// read and clear atomic !
|
|
|
|
key_mask &= key_press; // read key(s)
|
|
|
|
key_press ^= key_mask; // clear key(s)
|
|
|
|
sei();
|
|
|
|
return key_mask;
|
|
|
|
}
|
|
|
|
|