Compare commits

..

No commits in common. "05c9888a7514181d9a88d6e668d4640a9b59c3d3" and "0acd0bd98bf99714ea9cafa98bd57a4ddd4f9e36" have entirely different histories.

2 changed files with 53 additions and 141 deletions

View file

@ -5,12 +5,9 @@ PROGRAMMER := usbasp
SPEED := 8000000UL SPEED := 8000000UL
TARGET := main.hex TARGET := main.hex
EEP := main.eep
BIN := main.elf BIN := main.elf
OBJ := main.o OBJ := main.o
HIGH_FUSE := 0xD6
SHELL := sh SHELL := sh
CC := avr-gcc CC := avr-gcc
OBJCOPY := avr-objcopy OBJCOPY := avr-objcopy
@ -19,33 +16,24 @@ AVRDUDE := avrdude
CFLAGS := -mmcu=$(MCU) -D F_CPU=$(SPEED) -Os -Wall -Werror -Wextra CFLAGS := -mmcu=$(MCU) -D F_CPU=$(SPEED) -Os -Wall -Werror -Wextra
all: $(TARGET) $(EEP) all: $(TARGET)
$(TARGET): $(BIN) $(TARGET): $(BIN)
${OBJCOPY} -O ihex -j .text -j .data $< $@ ${OBJCOPY} -O ihex -j .text -j .data $< $@
$(EEP): $(BIN)
${OBJCOPY} --change-section-lma .eeprom=0 -O ihex -j .eeprom $< $@
$(BIN): $(OBJ) $(BIN): $(OBJ)
$(CC) $(CFLAGS) $< -o $@ $(CC) $(CFLAGS) $< -o $@
%.o: %.c %.o: %.c
$(CC) $(CFLAGS) -c $< -o $@ $(CC) $(CFLAGS) -c $< -o $@
.PHONY: flash eeprom fuses clean check size .PHONY: flash clean check size
flash: $(TARGET) size flash: $(TARGET) size
$(AVRDUDE) -p $(MCU) -c $(PROGRAMMER) -U flash:w:$<:a $(AVRDUDE) -p $(MCU) -c $(PROGRAMMER) -U flash:w:$<:a
eeprom: $(EEP) size
$(AVRDUDE) -p $(MCU) -c $(PROGRAMMER) -U eeprom:w:$<:a
fuses:
$(AVRDUDE) -p $(MCU) -c $(PROGRAMMER) -U hfuse:w:$(HIGH_FUSE):m
clean: clean:
$(RM) $(TARGET) $(EEP) $(BIN) $(OBJ) $(RM) $(TARGET) $(BIN) $(OBJ)
check: check:
cppcheck main.c cppcheck main.c

View file

@ -1,4 +1,3 @@
#include <avr/eeprom.h>
#include <avr/io.h> #include <avr/io.h>
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include <util/delay.h> #include <util/delay.h>
@ -16,9 +15,6 @@
#define ENC_A (PINB & (1 << PB6)) #define ENC_A (PINB & (1 << PB6))
#define ENC_B (PINB & (1 << PB7)) #define ENC_B (PINB & (1 << PB7))
uint8_t EEMEM eeprom_contrast = 0;
uint8_t EEMEM eeprom_backlight = 0;
static const uint8_t sacred_chao[200] = { 0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF0, 0xF8, 0xFC, 0xFC, 0xFE, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFE, 0xFE, 0xFC, 0xFC, 0xF8, 0xF0, 0xF0, 0xE0, 0xC0, 0x80, 0x00, 0x00, 0x00, 0x00, static const uint8_t sacred_chao[200] = { 0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF0, 0xF8, 0xFC, 0xFC, 0xFE, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFE, 0xFE, 0xFC, 0xFC, 0xF8, 0xF0, 0xF0, 0xE0, 0xC0, 0x80, 0x00, 0x00, 0x00, 0x00,
0x80, 0xF0, 0xFC, 0xFE, 0xFF, 0xFF, 0x7F, 0x3F, 0x1F, 0x3F, 0x3F, 0x7F, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x1F, 0x0F, 0x0F, 0x07, 0x07, 0x07, 0x03, 0xC3, 0xE3, 0x73, 0x37, 0x17, 0x07, 0x0F, 0x1E, 0x3C, 0xF0, 0x80, 0x80, 0xF0, 0xFC, 0xFE, 0xFF, 0xFF, 0x7F, 0x3F, 0x1F, 0x3F, 0x3F, 0x7F, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x1F, 0x0F, 0x0F, 0x07, 0x07, 0x07, 0x03, 0xC3, 0xE3, 0x73, 0x37, 0x17, 0x07, 0x0F, 0x1E, 0x3C, 0xF0, 0x80,
0xFF, 0xFF, 0xFF, 0xE7, 0xC3, 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0x00, 0x00, 0xFC, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFC, 0x30, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xE7, 0xC3, 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0x00, 0x00, 0xFC, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFC, 0x30, 0x00, 0x00, 0xFF,
@ -97,13 +93,10 @@ enum input {cw, ccw, click, hold};
static volatile enum state {home, ch1, ch2, ch3, setup} current_state = home; static volatile enum state {home, ch1, ch2, ch3, setup} current_state = home;
static volatile enum state home_state = ch1; static volatile enum state home_state = ch1;
static volatile enum setup_state {contrast, backlight, back, change_contrast, change_backlight} setup_state = contrast; static volatile enum setup_state {contrast, backlight, back} setup_state = contrast;
static volatile uint8_t enc = 0; static volatile uint8_t enc = 0;
static volatile uint8_t value_contrast;
static volatile uint8_t value_backlight;
void spi_init(void) { void spi_init(void) {
SPI_DDR |= (1 << SPI_SCK) | (1 << SPI_MOSI) | (1 << SPI_SS); SPI_DDR |= (1 << SPI_SCK) | (1 << SPI_MOSI) | (1 << SPI_SS);
SPI_PORT |= (1 << SPI_SS); SPI_PORT |= (1 << SPI_SS);
@ -340,8 +333,6 @@ static void lcd_setup(void) {
bool contrast_selected = false; bool contrast_selected = false;
bool backlight_selected = false; bool backlight_selected = false;
bool back_selected = false; bool back_selected = false;
bool change_contrast_selected = false;
bool change_backlight_selected = false;
switch (setup_state) { switch (setup_state) {
case contrast: case contrast:
@ -350,15 +341,9 @@ static void lcd_setup(void) {
case backlight: case backlight:
backlight_selected = true; backlight_selected = true;
break; break;
case back: default: // back
back_selected = true; back_selected = true;
break; break;
case change_contrast:
change_contrast_selected = true;
break;
case change_backlight:
change_backlight_selected = true;
break;
} }
for (uint8_t i = 0; i < 2; i++) { for (uint8_t i = 0; i < 2; i++) {
@ -383,33 +368,31 @@ static void lcd_setup(void) {
for (uint8_t i = 0; i < 2; i++) { for (uint8_t i = 0; i < 2; i++) {
SPI_PORT &= ~(1 << LCD_CD); SPI_PORT &= ~(1 << LCD_CD);
lcd_write(0x00); lcd_write(0x02);
lcd_write(0x10); lcd_write(0x10);
lcd_write(0xB2 + i); lcd_write(0xB2 + i);
SPI_PORT |= (1 << LCD_CD); SPI_PORT |= (1 << LCD_CD);
lcd_write_symbol_page(&sym_c, i, false);
lcd_write_kerning(2, false);
lcd_write_symbol_page(&sym_o, i, false);
lcd_write_kerning(2, false);
lcd_write_symbol_page(&sym_n, i, false);
lcd_write_kerning(2, false);
lcd_write_symbol_page(&sym_t, i, false);
lcd_write_kerning(2, false);
lcd_write_symbol_page(&sym_r, i, false);
lcd_write_kerning(2, false);
lcd_write_symbol_page(&sym_a, i, false);
lcd_write_kerning(2, false);
lcd_write_symbol_page(&sym_s, i, false);
lcd_write_kerning(2, false);
lcd_write_symbol_page(&sym_t, i, false);
lcd_write_kerning(2, false);
lcd_write_symbol_page(&sym_colon, i, false);
lcd_write_kerning(11, false);
lcd_write_integer_page(123, 1, i, contrast_selected);
lcd_write_kerning(2, contrast_selected); lcd_write_kerning(2, contrast_selected);
lcd_write_symbol_page(&sym_c, i, contrast_selected);
lcd_write_kerning(2, contrast_selected);
lcd_write_symbol_page(&sym_o, i, contrast_selected);
lcd_write_kerning(2, contrast_selected);
lcd_write_symbol_page(&sym_n, i, contrast_selected);
lcd_write_kerning(2, contrast_selected);
lcd_write_symbol_page(&sym_t, i, contrast_selected);
lcd_write_kerning(2, contrast_selected);
lcd_write_symbol_page(&sym_r, i, contrast_selected);
lcd_write_kerning(2, contrast_selected);
lcd_write_symbol_page(&sym_a, i, contrast_selected);
lcd_write_kerning(2, contrast_selected);
lcd_write_symbol_page(&sym_s, i, contrast_selected);
lcd_write_kerning(2, contrast_selected);
lcd_write_symbol_page(&sym_t, i, contrast_selected);
lcd_write_kerning(2, contrast_selected);
lcd_write_symbol_page(&sym_colon, i, contrast_selected);
lcd_write_kerning(2, contrast_selected);
lcd_write_kerning(9, false);
lcd_write_integer_page(value_contrast, 3, i, change_contrast_selected);
lcd_write_kerning(2, change_contrast_selected);
} }
for (uint8_t i = 0; i < 2; i++) { for (uint8_t i = 0; i < 2; i++) {
@ -419,29 +402,28 @@ static void lcd_setup(void) {
lcd_write(0xB4 + i); lcd_write(0xB4 + i);
SPI_PORT |= (1 << LCD_CD); SPI_PORT |= (1 << LCD_CD);
lcd_write_symbol_page(&sym_b, i, backlight_selected); lcd_write_symbol_page(&sym_b, i, false);
lcd_write_kerning(2, false);
lcd_write_symbol_page(&sym_a, i, false);
lcd_write_kerning(2, false);
lcd_write_symbol_page(&sym_c, i, false);
lcd_write_kerning(2, false);
lcd_write_symbol_page(&sym_k, i, false);
lcd_write_kerning(2, false);
lcd_write_symbol_page(&sym_l, i, false);
lcd_write_kerning(2, false);
lcd_write_symbol_page(&sym_i, i, false);
lcd_write_kerning(2, false);
lcd_write_symbol_page(&sym_g, i, false);
lcd_write_kerning(2, false);
lcd_write_symbol_page(&sym_h, i, false);
lcd_write_kerning(2, false);
lcd_write_symbol_page(&sym_t, i, false);
lcd_write_kerning(2, false);
lcd_write_symbol_page(&sym_colon, i, false);
lcd_write_kerning(7, false);
lcd_write_integer_page(123, 3, i, backlight_selected);
lcd_write_kerning(2, backlight_selected); lcd_write_kerning(2, backlight_selected);
lcd_write_symbol_page(&sym_a, i, backlight_selected);
lcd_write_kerning(2, backlight_selected);
lcd_write_symbol_page(&sym_c, i, backlight_selected);
lcd_write_kerning(2, backlight_selected);
lcd_write_symbol_page(&sym_k, i, backlight_selected);
lcd_write_kerning(2, backlight_selected);
lcd_write_symbol_page(&sym_l, i, backlight_selected);
lcd_write_kerning(2, backlight_selected);
lcd_write_symbol_page(&sym_i, i, backlight_selected);
lcd_write_kerning(2, backlight_selected);
lcd_write_symbol_page(&sym_g, i, backlight_selected);
lcd_write_kerning(2, backlight_selected);
lcd_write_symbol_page(&sym_h, i, backlight_selected);
lcd_write_kerning(2, backlight_selected);
lcd_write_symbol_page(&sym_t, i, backlight_selected);
lcd_write_kerning(2, backlight_selected);
lcd_write_symbol_page(&sym_colon, i, backlight_selected);
lcd_write_kerning(2, backlight_selected);
lcd_write_kerning(5, false);
lcd_write_integer_page(value_backlight, 3, i, change_backlight_selected);
lcd_write_kerning(2, change_backlight_selected);
} }
for (uint8_t i = 0; i < 2; i++) { for (uint8_t i = 0; i < 2; i++) {
@ -508,84 +490,30 @@ static void update_home(enum input event) {
static void update_setup(enum input event) { static void update_setup(enum input event) {
switch (event) { switch (event) {
case cw: case cw:
switch (setup_state) { setup_state++;
case contrast: if (setup_state > back)
case backlight: setup_state = contrast;
case back:
setup_state++;
if (setup_state > back)
setup_state = contrast;
break;
case change_contrast:
value_contrast++;
break;
case change_backlight:
value_backlight++;
break;
}
lcd_setup(); lcd_setup();
break; break;
case ccw: case ccw:
switch (setup_state) { setup_state--;
case contrast: if (setup_state > back)
case backlight: setup_state = back;
case back:
setup_state--;
if (setup_state > back)
setup_state = back;
break;
case change_contrast:
value_contrast--;
break;
case change_backlight:
value_backlight--;
break;
}
lcd_setup(); lcd_setup();
break; break;
case click: case click:
switch (setup_state) { switch (setup_state) {
case contrast: case contrast:
setup_state = change_contrast;
lcd_setup();
break; break;
case backlight: case backlight:
setup_state = change_backlight;
lcd_setup();
break; break;
case back: case back:
change_state(home); change_state(home);
break; break;
case change_contrast:
eeprom_update_byte(&eeprom_contrast, value_contrast);
setup_state = contrast;
lcd_setup();
break;
case change_backlight:
eeprom_update_byte(&eeprom_backlight, value_backlight);
setup_state = backlight;
lcd_setup();
break;
} }
break; break;
case hold: case hold:
switch (setup_state) { change_state(home);
case contrast:
case backlight:
case back:
change_state(home);
break;
case change_contrast:
setup_state = contrast;
value_contrast = eeprom_read_byte(&eeprom_contrast);
lcd_setup();
break;
case change_backlight:
setup_state = backlight;
value_backlight = eeprom_read_byte(&eeprom_backlight);
lcd_setup();
break;
}
break; break;
} }
} }
@ -684,10 +612,6 @@ ISR(TIMER1_COMPA_vect) {
} }
int main(void) { int main(void) {
// Load contrast and backlight values from EEPROM
value_contrast = eeprom_read_byte(&eeprom_contrast);
value_backlight = eeprom_read_byte(&eeprom_backlight);
// FastPWM: 1.25kHz // FastPWM: 1.25kHz
// TODO: Try to get the backlit even more dim // TODO: Try to get the backlit even more dim
TCCR0A = (1 << WGM01) | (1 << WGM00) | (1 << COM0B1); TCCR0A = (1 << WGM01) | (1 << WGM00) | (1 << COM0B1);