avr_firmware: Keyboard support; LCD initialization fixes
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
CC=avr-gcc
|
||||
#CCOPTS=-mmcu=atmega164a -std=c11 -flto -Os -ggdb -Wall -Wextra -pedantic -DF_CPU=8000000UL
|
||||
CCOPTS=-mmcu=atmega32 -std=c11 -flto -Os -ggdb -Wall -Wextra -pedantic -DF_CPU=8000000UL
|
||||
CCOPTS=-mmcu=atmega32 -std=c11 -flto -Os -ggdb -mrelax -Wall -Wextra -pedantic -DF_CPU=8000000UL
|
||||
|
||||
CCSRCS ::= uart.c main.c modem.c timer.c lcd.c
|
||||
|
||||
all: fw.elf
|
||||
|
||||
prog: fw.elf
|
||||
sudo avrdude -c avrisp2 -p m32 -U flash:w:$<
|
||||
avrdude -c avrisp2 -p m32 -U flash:w:$<
|
||||
|
||||
fw.elf: $(addprefix objs/,$(CCSRCS:.c=.o))
|
||||
$(CC) $(CCOPTS) -o $@ $^
|
||||
|
||||
@@ -9,6 +9,13 @@
|
||||
#define LCD_RW 1
|
||||
#define LCD_EN 2
|
||||
|
||||
#define KEYB_DDDR DDRC
|
||||
#define KEYB_DPIN PINC
|
||||
|
||||
#define KEYB_CPORT PORTA
|
||||
#define KEYB_CDDR DDRA
|
||||
#define KEYB_EN 3
|
||||
|
||||
#define UART0_CTS_PORT PIND
|
||||
#define UART0_RTS_PORT PIND
|
||||
#define UART0_CTS 5
|
||||
|
||||
@@ -9,8 +9,12 @@
|
||||
|
||||
#define LCD_CMASK ((_BV(LCD_RS) | _BV(LCD_RW) | (_BV(LCD_EN))))
|
||||
|
||||
volatile bool lcdInUse;
|
||||
|
||||
uint8_t _lcdRead(bool rs)
|
||||
{
|
||||
lcdInUse = true;
|
||||
|
||||
LCD_CPORT = (LCD_CPORT & ~LCD_CMASK) | _BV(LCD_RW) | (rs?_BV(LCD_RS):0);
|
||||
|
||||
LCD_DDDR = 0x00;
|
||||
@@ -21,15 +25,19 @@ uint8_t _lcdRead(bool rs)
|
||||
|
||||
uint8_t ret = LCD_DPIN;
|
||||
|
||||
LCD_CPORT &= ~_BV(LCD_EN);
|
||||
LCD_CPORT &= ~(_BV(LCD_EN) | _BV(LCD_RW));
|
||||
|
||||
_delay_us(0.06);
|
||||
|
||||
lcdInUse = false;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void _lcdWrite(bool rs, uint8_t data)
|
||||
{
|
||||
lcdInUse = true;
|
||||
|
||||
LCD_CPORT = (LCD_CPORT & ~LCD_CMASK) | (rs?_BV(LCD_RS):0);
|
||||
|
||||
LCD_DDDR = 0xff;
|
||||
@@ -40,6 +48,8 @@ void _lcdWrite(bool rs, uint8_t data)
|
||||
_delay_us(0.45);
|
||||
|
||||
LCD_CPORT &= ~_BV(LCD_EN);
|
||||
|
||||
lcdInUse = false;
|
||||
}
|
||||
|
||||
void _lcdBusyWait()
|
||||
@@ -76,18 +86,27 @@ void lcdWriteStrP(char const* str)
|
||||
|
||||
static const uint8_t lineLUT[4] PROGMEM = {0x00, 0x40, 0x14, 0x54};
|
||||
|
||||
void lcdSetPos(unsigned x, unsigned y)
|
||||
void lcdSetPos(unsigned char x, unsigned char y)
|
||||
{
|
||||
const uint8_t adr = pgm_read_byte(&lineLUT[y]) + x;
|
||||
lcdWrite(false, 0x80 | adr);
|
||||
}
|
||||
|
||||
int lcdInit()
|
||||
char lcdInit()
|
||||
{
|
||||
LCD_DDDR = 0x00;
|
||||
LCD_CDDR |= LCD_CMASK;
|
||||
|
||||
_delay_ms(15.0);
|
||||
_lcdWrite(false, 0x38);
|
||||
|
||||
_delay_ms(4.1);
|
||||
_lcdWrite(false, 0x38);
|
||||
|
||||
_delay_us(100.0);
|
||||
_lcdWrite(false, 0x38);
|
||||
|
||||
|
||||
lcdWrite(false, 0x38);
|
||||
lcdWrite(false, 0x0c);
|
||||
lcdWrite(false, 0x01);
|
||||
lcdWrite(false, 0x06);
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#ifndef DMXGSM_AVR_LCD_H
|
||||
#define DMXGSM_AVR_LCD_H
|
||||
|
||||
int lcdInit();
|
||||
char lcdInit();
|
||||
|
||||
void lcdWriteStr(char const* str);
|
||||
void lcdWriteStrP(char const* str);
|
||||
|
||||
void lcdSetPos(unsigned x, unsigned y);
|
||||
void lcdSetPos(unsigned char x, unsigned char y);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -46,6 +46,17 @@ int main()
|
||||
lcdWriteStr(buf);
|
||||
oldSeconds = seconds;
|
||||
}
|
||||
|
||||
char key, action = getKey(&key);
|
||||
if (action != KEY_NONE) {
|
||||
lcdSetPos(0, 3);
|
||||
utoa(key, buf, 10);
|
||||
lcdWriteStr(buf);
|
||||
if (action == KEY_PRESSED)
|
||||
lcdWriteStrP(PSTR(" pressed! "));
|
||||
else if (action == KEY_RELEASED)
|
||||
lcdWriteStrP(PSTR(" released!"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,13 +1,22 @@
|
||||
#include <stdbool.h>
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
#include <avr/cpufunc.h>
|
||||
#include <util/atomic.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "timer.h"
|
||||
|
||||
volatile unsigned timerTicks, timerSeconds;
|
||||
|
||||
volatile char keyNum, keyAction;
|
||||
|
||||
extern volatile bool lcdInUse;
|
||||
|
||||
ISR(TIMER1_COMPA_vect, ISR_NOBLOCK)
|
||||
{
|
||||
static unsigned ledTicks = 0;
|
||||
static unsigned char ledTicks = 0;
|
||||
static uint8_t oldKeyData = 0;
|
||||
|
||||
++timerTicks;
|
||||
|
||||
@@ -17,9 +26,33 @@ ISR(TIMER1_COMPA_vect, ISR_NOBLOCK)
|
||||
++timerSeconds;
|
||||
} else
|
||||
++ledTicks;
|
||||
|
||||
if (!lcdInUse) {
|
||||
KEYB_DDDR = 0x00;
|
||||
KEYB_CPORT &= ~_BV(KEYB_EN);
|
||||
|
||||
_NOP();
|
||||
|
||||
uint8_t keyData = KEYB_DPIN & 0x7f;
|
||||
|
||||
KEYB_CPORT |= _BV(KEYB_EN);
|
||||
|
||||
uint8_t keyChange = keyData ^ oldKeyData;
|
||||
char changed = __builtin_ffs(keyChange);
|
||||
|
||||
if (changed) {
|
||||
keyNum = changed-1;
|
||||
if (keyData & (1<<keyNum))
|
||||
keyAction = KEY_PRESSED;
|
||||
else
|
||||
keyAction = KEY_RELEASED;
|
||||
}
|
||||
|
||||
oldKeyData = keyData;
|
||||
}
|
||||
}
|
||||
|
||||
int timerInit()
|
||||
char timerInit()
|
||||
{
|
||||
#ifndef __AVR_ATmega32__
|
||||
TCCR0A = 0; // CTC mode
|
||||
@@ -30,6 +63,11 @@ int timerInit()
|
||||
|
||||
TIMSK0 = _BV(TOIE0);
|
||||
#else
|
||||
KEYB_CPORT |= _BV(KEYB_EN);
|
||||
KEYB_CDDR |= _BV(KEYB_EN);
|
||||
keyNum = 0;
|
||||
keyAction = KEY_NONE;
|
||||
|
||||
TCCR1A = 0;
|
||||
TCCR1B = _BV(WGM12) | _BV(CS02); // CTC mode, /256 clk
|
||||
OCR1AH = 0x02;
|
||||
@@ -44,10 +82,26 @@ int timerInit()
|
||||
|
||||
unsigned timerGetTicks()
|
||||
{
|
||||
return timerTicks;
|
||||
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
|
||||
return timerTicks;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned timerGetSeconds()
|
||||
{
|
||||
return timerSeconds>>1;
|
||||
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
|
||||
return timerSeconds>>1;
|
||||
}
|
||||
}
|
||||
|
||||
char getKey(char *key)
|
||||
{
|
||||
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
|
||||
if (keyAction != KEY_NONE)
|
||||
*key = keyNum;
|
||||
|
||||
char ret = keyAction;
|
||||
keyAction = KEY_NONE;
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,15 @@
|
||||
#ifndef DMXGSM_AVR_TIMER_H
|
||||
#define DMXGSM_AVR_TIMER_H
|
||||
|
||||
int timerInit();
|
||||
char timerInit();
|
||||
|
||||
unsigned timerGetTicks();
|
||||
unsigned timerGetSeconds();
|
||||
|
||||
#define KEY_NONE 0
|
||||
#define KEY_PRESSED 1
|
||||
#define KEY_RELEASED 2
|
||||
|
||||
char getKey(char *key);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
#define UART_RXBUF_SIZE 16
|
||||
|
||||
static volatile unsigned char _uart0RXbuf[UART_RXBUF_SIZE];
|
||||
static volatile size_t _uart0RXwr, _uart0RXrd;
|
||||
static volatile unsigned char _uart0RXwr, _uart0RXrd;
|
||||
|
||||
#ifdef __AVR_ATmega32__
|
||||
#define USART0_RX_vect USART_RXC_vect
|
||||
|
||||
Reference in New Issue
Block a user