hd44780 library, start of snake

master
Ondřej Hruška 10 years ago
parent 90042d221d
commit bbaf8bad62
  1. 166
      devel/lcdsnake/Makefile
  2. 351
      devel/lcdsnake/lcd_default.asm
  3. 1
      devel/lcdsnake/lib
  4. 182
      devel/lcdsnake/main.c
  5. 374
      devel/lib/hd44780.h
  6. 3
      devel/lib/nsdelay.h
  7. 2
      devel/lib/yeolde.h

@ -0,0 +1,166 @@
MCU = atmega328p
F_CPU = 16000000
LFUSE = 0xFF
HFUSE = 0xDE
EFUSE = 0x05
MAIN = main.c
## If you've split your program into multiple files,
## include the additional .c source (in same directory) here
## (and include the .h files in your foo.c)
LOCAL_SOURCE =
## Here you can link to one more directory (and multiple .c files)
# EXTRA_SOURCE_DIR = ../AVR-Programming-Library/
EXTRA_SOURCE_DIR =
EXTRA_SOURCE_FILES =
##########------------------------------------------------------##########
########## Programmer Defaults ##########
########## Set up once, then forget about it ##########
########## (Can override. See bottom of file.) ##########
##########------------------------------------------------------##########
#19200
PROGRAMMER_TYPE = arduino
PROGRAMMER_ARGS = -b 57600 -P /dev/ttyUSB0
##########------------------------------------------------------##########
########## Makefile Magic! ##########
########## Summary: ##########
########## We want a .hex file ##########
########## Compile source files into .elf ##########
########## Convert .elf file into .hex ##########
########## You shouldn't need to edit below. ##########
##########------------------------------------------------------##########
## Defined programs / locations
CC = avr-gcc
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump
AVRSIZE = avr-size
AVRDUDE = sudo avrdude
## Compilation options, type man avr-gcc if you're curious.
CFLAGS = -std=gnu99 -mmcu=$(MCU) -DF_CPU=$(F_CPU)UL -I. -I$(EXTRA_SOURCE_DIR)
CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
CFLAGS += -Wall -Wno-main -Wno-strict-prototypes -Wno-comment
CFLAGS += -g2 -Wextra -Wfatal-errors
CFLAGS += -ffunction-sections -fdata-sections -Wl,--gc-sections -Wl,--relax
CFLAGS_BUILD = $(CFLAGS) -Os
# CFLAGS += -lm
## CFLAGS += -Wl,-u,vfprintf -lprintf_flt -lm ## for floating-point printf
## CFLAGS += -Wl,-u,vfprintf -lprintf_min ## for smaller printf
## Lump target and extra source files together
TARGET = $(strip $(basename $(MAIN)))
SRC1 = $(TARGET).c
SRC = $(SRC1)
EXTRA_SOURCE = $(addprefix $(EXTRA_SOURCE_DIR), $(EXTRA_SOURCE_FILES))
SRC += $(EXTRA_SOURCE)
SRC += $(LOCAL_SOURCE)
## List of all header files
HEADERS = $(SRC:.c=.h)
## For every .c file, compile an .o object file
OBJ = $(SRC:.c=.o)
## Generic Makefile targets. (Only .hex file is necessary)
all: $(TARGET).hex size
pre: $(TARGET).pre
%.hex: %.elf
$(OBJCOPY) -R .eeprom -O ihex $< $@
%.elf: $(SRC)
$(CC) $(CFLAGS_BUILD) $(SRC) --output $@
%.pre: $(SRC1)
$(CC) $(CFLAGS) -E $(SRC1) --output $@
%.eeprom: %.elf
$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@
debug:
@echo
@echo "Source files:" $(SRC)
@echo "MCU, F_CPU, BAUD:" $(MCU), $(F_CPU), $(BAUD)
@echo
# Optionally create listing file from .elf
# This creates approximate assembly-language equivalent of your code.
# Useful for debugging time-sensitive bits,
# or making sure the compiler does what you want.
disassemble: $(TARGET).lst
dis: disassemble
lst: disassemble
eeprom: $(TARGET).eeprom
%.lst: %.elf
$(OBJDUMP) -S $< > $@
# Optionally show how big the resulting program is
size: $(TARGET).elf
$(AVRSIZE) -C --mcu=$(MCU) $(TARGET).elf
clean:
rm -f $(TARGET).elf $(TARGET).hex $(TARGET).obj \
$(TARGET).o $(TARGET).d $(TARGET).eep $(TARGET).lst \
$(TARGET).lss $(TARGET).sym $(TARGET).map $(TARGET)~ \
$(TARGET).eeprom
squeaky_clean:
rm -f *.elf *.hex *.obj *.o *.d *.eep *.lst *.lss *.sym *.map *~
##########------------------------------------------------------##########
########## Programmer-specific details ##########
########## Flashing code to AVR using avrdude ##########
##########------------------------------------------------------##########
flash: $(TARGET).hex
$(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -U flash:w:$<
flash_eeprom: $(TARGET).eeprom
$(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -U eeprom:w:$<
terminal:
$(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -nt
flash_arduino: PROGRAMMER_TYPE = arduino
flash_arduino: PROGRAMMER_ARGS =
flash_arduino: flash
flash_dragon_isp: PROGRAMMER_TYPE = dragon_isp
flash_dragon_isp: PROGRAMMER_ARGS =
flash_dragon_isp: flash
##########------------------------------------------------------##########
########## Fuse settings and suitable defaults ##########
##########------------------------------------------------------##########
## Generic
FUSE_STRING = -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m -U efuse:w:$(EFUSE):m
fuses:
$(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) \
$(PROGRAMMER_ARGS) $(FUSE_STRING)
show_fuses:
$(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -nv
## Called with no extra definitions, sets to defaults
set_default_fuses: FUSE_STRING = -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m -U efuse:w:$(EFUSE):m
set_default_fuses: fuses

@ -0,0 +1,351 @@
; Zapojeni (Připojen DMC-50399 v 4-bitovem modu):
; +------u------+
; Vcc -> reset --+ /RST Vcc +-- napajeni +5V
; --+ PD0 PB7 +-- RS (0=instr W, BF+addr R; 1=data W/R)
; --+ PD1 PB6 +-- R/W (1=read,0=write)
; --+ PA1 PB5 +-- E (clock, active falling edge)
; --+ PA0 PB4 +--
; --+ PD2 PB3 +-- DATA 7
; --+ PD3 PB2 +-- DATA 6
; --+ PD4 PB1 +-- DATA 5
; --+ PD5 PB0 +-- DATA 4
; GND --+ GND PD6 +--
; +-------------+
;DMC-50399:
; 1 - GND
; 2 - +5V
; 3 - 0V (lcd driver)
; 4 - RS
; 5 - R/W
; 6 - E
; 7 - DATA 0
; 8 - DATA 1
;...
;14 - DATA 7
.device attiny2313
;běží na 4MHz, ckdiv8=1 (vypnuto)
;250x16=1ms=1000us
;LFUSE: 0xE2 -U lfuse:w:0xE2:m
;HFUSE: 0xDF -U hfuse:w:0xDF:m
;K O N S T A N T Y + P R E Z D I V K Y P O R T U A P I N U
.equ LCDPORT = PORTB
.equ LCDPIN = PINB
.equ LCDDDR = DDRB
.equ RS = 7
.equ RW = 6
.equ E = 5
.equ LCD_CLEAR = 0b00000001
.equ LCD_HOME = 0b00000010
.equ LCD_MODE_INC_NOSHIFT = 0b00000110
.equ LCD_MODE_INC_SHIFT = 0b00000111
.equ LCD_MODE_DEC_NOSHIFT = 0b00000100
.equ LCD_MODE_DEC_SHIFT = 0b00000101
.equ LCD_DISPLAY_DISABLED = 0b00001000
.equ LCD_DISPLAY_NOCURSOR = 0b00001100
.equ LCD_DISPLAY_CURSOR = 0b00001110
.equ LCD_DISPLAY_ALTER = 0b00001101
.equ LCD_DISPLAY_CURSOR_ALTER = 0b00001111
.equ LCD_CURSOR_LEFT = 0b00010000
.equ LCD_CURSOR_RIGHT = 0b00010100
.equ LCD_SHIFT_LEFT = 0b00011000
.equ LCD_SHIFT_RIGHT = 0b00011100
;5x7 font, 1-line
.equ LCD_MODE_4BIT_1LINE = 0b00100000
;.equ LCD_MODE_8BIT_1LINE = 0b00110000
;5x7 font, 2-line
.equ LCD_MODE_4BIT_2LINE = 0b00101000
;.equ LCD_MODE_8BIT_2LINE = 0b00111000
.equ ROW1_ADDR = 0x00
.equ ROW2_ADDR = 0x40
.equ ROW3_ADDR = 0x14
.equ ROW4_ADDR = 0x54
;aliases
.def ZH = r31
.def ZL = r30
.def YH = r29
.def YL = r28
.def XH = r27
.def XL = r26
; Z A C A T E K P R O G R A M U
;vektory preruseni
.org 0x0000 ;RESET
rjmp RESET ;skok na start po resetu
.org 0x0013
;nastaveni po resetu
.DB "HD44780 INTERFACE" ;(nazev programu)
RESET:
ldi r16,low(RAMEND) ;nastavi stack pointer
out SPL,r16
cli ;zakazat vsechna preruseni
; Nastaveni portu
;PORTB = LCDPORT
ldi r16,0b11111111 ;smer portu B
out LCDDDR,r16
ldi r16,0b00000000 ;vypnout B
out LCDPORT,r16
sei ;Global Interrupt Enable
; == display init ==
rcall LCD_INIT
ldi r17,LCD_MODE_INC_NOSHIFT
rcall TX_INSTR
ldi r17,LCD_DISPLAY_NOCURSOR
rcall TX_INSTR
; == load user-defined characters to CGRAM == (default, array label named MYCHARS, end-mark=0xFE)
ldi r17,0
rcall CGRAM_SET_ADDR
ldi ZH,high(MYCHARS*2)
ldi ZL,low(MYCHARS*2)
CGRAM_loop:
lpm r17,Z+
cpi r17,0xFE
breq CGRAM_loop_end
rcall TX_DATA
rjmp CGRAM_loop
CGRAM_loop_end:
; == pgm body ==
;load text to DDRAM
ldi r17,ROW1_ADDR
rcall DDRAM_SET_ADDR
ldi ZH,high(MYTEXT1*2)
ldi ZL,low(MYTEXT1*2)
DDRAM_loop:
lpm r17,Z+
cpi r17,0xFE
breq DDRAM_loop_end
rcall TX_DATA
rjmp DDRAM_loop
DDRAM_loop_end:
;load text to DDRAM
ldi r17,ROW2_ADDR
rcall DDRAM_SET_ADDR
ldi ZH,high(MYTEXT2*2)
ldi ZL,low(MYTEXT2*2)
DDRAM2_loop:
lpm r17,Z+
cpi r17,0xFE
breq DDRAM2_loop_end
rcall TX_DATA
rjmp DDRAM2_loop
DDRAM2_loop_end:
;direct write to X,Y - example
ldi r16,3 ;Y, zacina 0 a roste smerem dolu
ldi r17,5 ;X, zacina nulou a roste smerem doprava
rcall LCD_CURSOR_XY
ldi r17,"%"
rcall TX_DATA
ldi r17,"%"
rcall TX_DATA
ldi r17,"%"
rcall TX_DATA
;infinite loop
loop: rjmp loop
MYTEXT1:
.DB 0,0,0," POKUSNY TEXT ",0,0,0,0xFE
MYTEXT2:
.DB "Opravdu pekny text!",0xFE
; == USER-DEFINED CHARS ==
MYCHARS:
; 5x8, first 3 bits are not used
;end of mychars
.DB 0xe,0x1f,0x15,0x1f,0x1f,0x1f,0x15 ;smajlik
;konec
.DB 0xFE
;r16=Y
;r17=X
LCD_CURSOR_XY:
cpi r16,0
brne test1
fail: ldi r16,ROW1_ADDR
rjmp addrdone
test1:
cpi r16,1
brne test2
ldi r16,ROW2_ADDR
rjmp addrdone
test2:
cpi r16,2
brne test3
ldi r16,ROW3_ADDR
rjmp addrdone
test3:
cpi r16,3
brne fail
ldi r16,ROW4_ADDR
addrdone:
add r17,r16
rcall DDRAM_SET_ADDR
ret
;r16=počet ms (cca)
delay:
push r17 ;2
push r18 ;2
d1:
ldi r17,250 ;1
d2:
ldi r18,14 ;1
d3:
dec r18 ;1
nop
brne d3 ;2 (1
dec r17 ; +1)
brne d2 ;2 (1
dec r16 ; +1)
brne d1 ;2 (1)
pop r18 ;2
pop r17 ;2
ret
LCD_INIT:
ldi r16,16
rcall delay
ldi r16,0b00000010 ;4bit
out PORTB,r16
rcall LCD_CLK
ldi r16,5
rcall delay
ldi r17,LCD_MODE_4BIT_2LINE ;set 4-bit mode
rcall TX_INSTR
ret
;r17
TX_INSTR:
swap r17 ;send high nibble
mov r16,r17
andi r16,0b00001111
out LCDPORT,r16
rcall LCD_CLK
swap r17 ;send low nibble
mov r16,r17
andi r16,0b00001111
out LCDPORT,r16
rcall LCD_CLK
ret
;r17
TX_DATA:
swap r17 ;send high nibble
mov r16,r17
andi r16,0b00001111
sbr r16,(1<<RS)
out LCDPORT,r16
rcall LCD_CLK
swap r17 ;send low nibble
mov r16,r17
andi r16,0b00001111
sbr r16,(1<<RS)
out LCDPORT,r16
rcall LCD_CLK
ret
;r17 disabled to reduce code size
;RX_DATA:
; ;input
; ldi r16,0b11110000 ;LCDPORT dirrection (RS RW E n.c.) output, (D7 D6 D5 D4) input
; out LCDDDR,r16
; ldi r16,0b00001111 ;pullups to data pins enabled
; out LCDPORT,r16
;
; clr r17
; ldi r16,(1<<RW)|(1<<RS)
; out LCDPORT,r16 ;set LCD to read mode, for data
;
; rcall LCD_CLK ;receive high nibble
; in r16,LCDPIN
; andi r16,0b00001111
; or r17,r16
; swap r17 ;store high nibble
;
; rcall LCD_CLK ;receive low nibble
; in r16,LCDPIN
; andi r16,0b00001111
; or r17,r16 ;store low nibble
;
; ;output
; ldi r16,0b11111111 ;LCDPORT as output (RS RW E n.c. D7 D6 D5 D4)
; out LCDDDR,r16
; ldi r16,0b00000000 ;LCDPORT off
; out LCDPORT,r16
;
; ;r17=received value (D7 D6 D5 D4 D3 D2 D1 D0)
; ret
LCD_CLK:
sbi LCDPORT,E ;EXECUTE on
nop
nop
nop
cbi LCDPORT,E ;EXECUTE off
ldi r16,150 ;pause: 100 for 4MHZ
clkw:
dec r16
brne clkw
ret
;r17
;7 bitu (1.radek zacina 00,druhej 40)
DDRAM_SET_ADDR:
clr r16
sbr r16,0b10000000
or r17,r16
rcall TX_INSTR
ret
;r17
;6 bitu (5,4,3 = znak, 2,1,0 = radek - shora)
CGRAM_SET_ADDR:
clr r16
sbr r16,0b01000000
or r17,r16
rcall TX_INSTR
ret

@ -0,0 +1 @@
/home/ondra/devel/avr/avr-projects/devel/lib

@ -0,0 +1,182 @@
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include "lib/meta.h"
#include "lib/arduino_pins.h"
#include "lib/calc.h"
#define LCD_PIN_RS D10
#define LCD_PIN_RW D11
#define LCD_PIN_E D12
#define LCD_PIN_D4 D13
#define LCD_PIN_D5 D14
#define LCD_PIN_D6 D15
#define LCD_PIN_D7 D16
// D17 = A3 = source of entropy for random.
#include "lib/hd44780.h"
// Buttons (to ground)
#define BTN_LEFT D2
#define BTN_RIGHT D3
#define BTN_UP D4
#define BTN_DOWN D5
#define BTN_SELECT D6
#define BTN_RESTART D7
// Debouncer channels for buttons
// (Must be added in this order to debouncer)
#define D_LEFT 0
#define D_RIGHT 1
#define D_UP 2
#define D_DOWN 3
#define D_SELECT 4
#define D_RESTART 5
#define DEBO_CHANNELS 6
#define DEBO_TICKS 1 // in 0.01s
#include "lib/debounce.h"
// Board size (!!! rows = 2x number of display lines, max 2*4 = 8 !!!)
#define ROWS 4
#define COLS 20
// proto
void render();
void update();
void SECTION(".init8") init()
{
// Randomize RNG
adc_init();
srand(adc_read_word(3));
// Init LCD
lcd_init();
// gamepad buttons
as_input_pu(BTN_LEFT);
as_input_pu(BTN_RIGHT);
as_input_pu(BTN_UP);
as_input_pu(BTN_DOWN);
as_input_pu(BTN_SELECT);
as_input_pu(BTN_RESTART);
// add buttons to debouncer
debo_add_rev(BTN_LEFT);
debo_add_rev(BTN_RIGHT);
debo_add_rev(BTN_UP);
debo_add_rev(BTN_DOWN);
debo_add_rev(BTN_SELECT);
debo_add_rev(BTN_RESTART);
// setup timer
TCCR0A = _BV(WGM01); // CTC
TCCR0B = _BV(CS02) | _BV(CS00); // prescaler 1024
OCR0A = 156; // interrupt every 10 ms
sbi(TIMSK0, OCIE0A);
sei();
}
/** timer 0 interrupt vector */
ISR(TIMER0_COMPA_vect)
{
debo_tick(); // poll debouncer
update(); // update game state
render();
}
// sub-glyphs
#define _HEAD_ 31, 21, 21, 31
#define _BODY_ 31, 31, 31, 31
#define _FOOD_ 10, 21, 17, 14
#define _NONE_ 0, 0, 0, 0
// complete glyphs for loading into memory
// Only one food & one head glyph have to be loaded at a time.
// Body - Body
const uint8_t BB[] PROGMEM = {_BODY_, _BODY_};
// Body - None
const uint8_t BX[] PROGMEM = {_BODY_, _NONE_};
const uint8_t XB[] PROGMEM = {_NONE_, _BODY_};
// Head - None
const uint8_t HX[] PROGMEM = {_HEAD_, _NONE_};
const uint8_t XH[] PROGMEM = {_NONE_, _HEAD_};
// Body - Head
const uint8_t BH[] PROGMEM = {_BODY_, _HEAD_};
const uint8_t HB[] PROGMEM = {_HEAD_, _BODY_};
// Head - Food
const uint8_t HF[] PROGMEM = {_HEAD_, _FOOD_};
const uint8_t FH[] PROGMEM = {_FOOD_, _HEAD_};
// Food - None
const uint8_t FX[] PROGMEM = {_FOOD_, _NONE_};
const uint8_t XF[] PROGMEM = {_NONE_, _FOOD_};
// Body - Food
const uint8_t BF[] PROGMEM = {_BODY_, _FOOD_};
const uint8_t FB[] PROGMEM = {_FOOD_, _BODY_};
// board block (snake, food...)
typedef enum {
EMPTY = 0x00,
HEAD = 0x01,
FOOD = 0x02,
BODY_LEFT = 0x80;
BODY_RIGHT = 0x81,
BODY_UP = 0x82,
BODY_DOWN = 0x83,
} block_t;
typedef struct {
uint8_t x;
uint8_t y;
} coord_t;
#define is_body(blk) ((blk) & 0x80)
// y, x indexing
block_t board[ROWS][COLS];
coord_t head_pos;
coord_t food_pos;
void main()
{
lcd_define_glyph_pgm(0, BX);
lcd_define_glyph_pgm(1, BB);
lcd_define_glyph_pgm(2, XB);
lcd_define_glyph_pgm(3, XH);
lcd_define_glyph_pgm(4, XF);
// Test
lcd_char_xy(5, 0, 0);
lcd_char_xy(6, 0, 0);
lcd_char_xy(7, 0, 1);
lcd_char_xy(7, 1, 0);
lcd_char_xy(8, 1, 0);
lcd_char_xy(9, 1, 1);
lcd_char_xy(10, 1, 2);
lcd_char_xy(11, 1, 3);
lcd_char_xy(14, 1, 4);
while(1);
}

@ -0,0 +1,374 @@
#pragma once
#include <stdbool.h>
#include <stdint.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include "arduino_pins.h"
#include "calc.h"
#include "pins.h"
#include "nsdelay.h"
/*
HD44780 LCD display driver - 4-bit mode
Required macros - pin settings (eg. B,3 or D,0)
LCD_PIN_RS
LCD_PIN_RW
LCD_PIN_E
LCD_PIN_D7
LCD_PIN_D6
LCD_PIN_D5
LCD_PIN_D4
Define those before including the header file.
*/
// Commands for user
// Clear screen (reset)
#define LCD_CLEAR 0b00000001
// Move cursor to (0,0), unshift...
#define LCD_HOME 0b00000010
// Set mode: Increment + NoShift
#define LCD_MODE_INC 0b00000110
// Set mode: Increment + Shift
#define LCD_MODE_INC_SHIFT 0b00000111
// Set mode: Decrement + NoShift
#define LCD_MODE_DEC 0b00000100
// Set mode: Decrement + Shift
#define LCD_MODE_DEC_SHIFT 0b00000101
// Disable display (data remains untouched)
#define LCD_DISABLE 0b00001000
// Disable cursor
#define LCD_CURSOR_NONE 0b00001100
// Set cursor to still underscore
#define LCD_CURSOR_BAR 0b00001110
// Set cursor to blinking block
#define LCD_CURSOR_BLINK 0b00001101
// Set cursor to both of the above at once
#define LCD_CURSOR_BOTH (LCD_CURSOR_BAR | LCD_CURSOR_BLINK)
// Move cursor
#define LCD_MOVE_LEFT 0b00010000
#define LCD_MOVE_RIGHT 0b00010100
// Shift display
#define LCD_SHIFT_LEFT 0b00011000
#define LCD_SHIFT_RIGHT 0b00011100
// Set iface to 5x7 font, 1-line
#define LCD_IFACE_4BIT_1LINE 0b00100000
#define LCD_IFACE_8BIT_1LINE 0b00110000
// Set iface to 5x7 font, 2-line
#define LCD_IFACE_4BIT_2LINE 0b00101000
#define LCD_IFACE_8BIT_2LINE 0b00111000
// Start address of rows
const uint8_t LCD_ROW_ADDR[] = {0x00, 0x40, 0x14, 0x54};
// prototypes
// --- PUBLIC API ---
/** Init the display */
void lcd_init();
/** Write a command */
void lcd_write_command(const uint8_t bb);
/** Write data byte */
void lcd_write_data(const uint8_t bb);
/** Read busy flag & address */
uint8_t lcd_read_bf_addr();
/** Read byte from ram */
uint8_t lcd_read_ram();
/** Show string */
void lcd_str(char* str);
/** Show string at X, Y */
#define lcd_str_xy(x, y, str_p) do { lcd_xy((x), (y)); lcd_str((str_p)); } while(0)
/** Show char */
void lcd_char(const char c);
/** Show char at X, Y */
#define lcd_char_xy(x, y, c) do { lcd_xy((x), (y)); lcd_char((c)); } while(0)
/** Move cursor to X, Y */
void lcd_xy(const uint8_t x, const uint8_t y);
/** Set address in CGRAM */
void lcd_set_addr_cgram(const uint8_t acg);
/** Set address in DDRAM */
void lcd_set_addr(const uint8_t add);
/** Set cursor */
#define CURSOR_NONE 0b00
#define CURSOR_BAR 0b10
#define CURSOR_BLINK 0b01
#define CURSOR_BOTH 0b11
void lcd_cursor(uint8_t type);
/** Disable / enable, preserving cursor */
void lcd_disable();
void lcd_enable();
/** Define a custom glyph */
void lcd_define_glyph(const uint8_t index, const uint8_t* array);
// Internals
void _lcd_mode_r();
void _lcd_mode_w();
void _lcd_clk();
void _lcd_wait_bf();
void _lcd_write_byte(uint8_t bb);
uint8_t _lcd_read_byte();
// Write utilities
#define _lcd_write_low(bb) _lcd_write_nibble((bb) & 0x0F)
#define _lcd_write_high(bb) _lcd_write_nibble(((bb) & 0xF0) >> 4)
#define _lcd_write_nibble(nib) do { \
write_pin(LCD_PIN_D7, get_bit((nib), 3)); \
write_pin(LCD_PIN_D6, get_bit((nib), 2)); \
write_pin(LCD_PIN_D5, get_bit((nib), 1)); \
write_pin(LCD_PIN_D4, get_bit((nib), 0)); \
} while(0)
// 0 W, 1 R
bool _lcd_mode;
/** Initialize the display */
void lcd_init()
{
// configure pins as output
as_output(LCD_PIN_E);
as_output(LCD_PIN_RW);
as_output(LCD_PIN_RS);
_lcd_mode = 1; // force data pins to output
_lcd_mode_w();
// Magic sequence to enter 4-bit mode
_delay_ms(16);
_lcd_write_nibble(0b0011);
_lcd_clk();
_delay_ms(5);
_lcd_clk();
_delay_ms(5);
_lcd_clk();
_delay_ms(5);
_lcd_write_nibble(0b0010);
_lcd_clk();
_delay_us(100);
// Configure the display
lcd_write_command(LCD_IFACE_4BIT_2LINE);
lcd_write_command(LCD_DISABLE);
lcd_write_command(LCD_CLEAR);
lcd_write_command(LCD_MODE_INC);
lcd_enable();
}
/** Send a pulse on the ENABLE line */
void _lcd_clk()
{
pin_up(LCD_PIN_E);
delay_ns(420);
pin_down(LCD_PIN_E);
}
/** Enter READ mode */
void _lcd_mode_r()
{
if (_lcd_mode == 1) return; // already in R mode
pin_up(LCD_PIN_RW);
as_input_pu(LCD_PIN_D7);
as_input_pu(LCD_PIN_D6);
as_input_pu(LCD_PIN_D5);
as_input_pu(LCD_PIN_D4);
_lcd_mode = 1;
}
/** Enter WRITE mode */
void _lcd_mode_w()
{
if (_lcd_mode == 0) return; // already in W mode
pin_down(LCD_PIN_RW);
as_output(LCD_PIN_D7);
as_output(LCD_PIN_D6);
as_output(LCD_PIN_D5);
as_output(LCD_PIN_D4);
_lcd_mode = 0;
}
/** Read a byte */
uint8_t _lcd_read_byte()
{
_lcd_mode_r();
uint8_t res = 0;
_lcd_clk();
res = (read_pin(LCD_PIN_D7) << 7) | (read_pin(LCD_PIN_D6) << 6) | (read_pin(LCD_PIN_D5) << 5) | (read_pin(LCD_PIN_D4) << 4);
_lcd_clk();
res |= (read_pin(LCD_PIN_D7) << 3) | (read_pin(LCD_PIN_D6) << 2) | (read_pin(LCD_PIN_D5) << 1) | (read_pin(LCD_PIN_D4) << 0);
return res;
}
/** Write an instruction byte */
void lcd_write_command(uint8_t bb)
{
_lcd_wait_bf();
pin_down(LCD_PIN_RS); // select instruction register
_lcd_write_byte(bb); // send instruction byte
}
/** Write a data byte */
void lcd_write_data(uint8_t bb)
{
_lcd_wait_bf();
pin_up(LCD_PIN_RS); // select data register
_lcd_write_byte(bb); // send data byte
}
/** Read BF & Address */
uint8_t lcd_read_bf_addr()
{
pin_down(LCD_PIN_RS);
return _lcd_read_byte();
}
/** Read CGRAM or DDRAM */
uint8_t lcd_read_ram()
{
pin_up(LCD_PIN_RS);
return _lcd_read_byte();
}
/** Write a byte using the 8-bit interface */
void _lcd_write_byte(uint8_t bb)
{
_lcd_mode_w(); // enter W mode
_lcd_write_high(bb);
_lcd_clk();
_lcd_write_low(bb);
_lcd_clk();
}
/** Wait until the device is ready */
void _lcd_wait_bf()
{
while(lcd_read_bf_addr() & _BV(7));
}
/** Send a string to LCD */
void lcd_str(char* str_p)
{
while (*str_p)
lcd_char(*str_p++);
}
/** Sedn a char to LCD */
void lcd_char(const char c)
{
lcd_write_data(c);
}
/** Set cursor position */
void lcd_xy(const uint8_t x, const uint8_t y)
{
lcd_set_addr(LCD_ROW_ADDR[y] + (x));
}
uint8_t _lcd_old_cursor = CURSOR_NONE;
bool _lcd_enabled = false;
/** Set LCD cursor. If not enabled, only remember it. */
void lcd_cursor(uint8_t type)
{
_lcd_old_cursor = (type & CURSOR_BOTH);
if (_lcd_enabled) lcd_write_command(LCD_CURSOR_NONE | _lcd_old_cursor);
}
/** Display display (preserving cursor) */
void lcd_disable()
{
lcd_write_command(LCD_DISABLE);
_lcd_enabled = false;
}
/** Enable display (restoring cursor) */
void lcd_enable()
{
_lcd_enabled = true;
lcd_cursor(_lcd_old_cursor);
}
/** Define a glyph */
void lcd_define_glyph(const uint8_t index, const uint8_t* array)
{
lcd_set_addr_cgram(index * 8);
for (uint8_t i = 0; i < 8; ++i) {
lcd_write_data(array[i]);
}
}
/** Define a glyph */
void lcd_define_glyph_pgm(const uint8_t index, const uint8_t* array)
{
lcd_set_addr_cgram(index * 8);
for (uint8_t i = 0; i < 8; ++i) {
lcd_write_data(pgm_read_byte(&array[i]));
}
}
/** Set address in CGRAM */
void lcd_set_addr_cgram(const uint8_t acg)
{
lcd_write_command(0b01000000 | ((acg) & 0b00111111));
}
/** Set address in DDRAM */
void lcd_set_addr(const uint8_t add)
{
lcd_write_command(0b10000000 | ((add) & 0b01111111));
}

@ -16,3 +16,6 @@
/** Wait n nanoseconds, plus c cycles */
#define delay_ns_c(ns, c) delay_c(ns2cycles(ns) + (c))
/** Wait n nanoseconds */
#define delay_ns(ns) delay_c(ns2cycles(ns))

@ -27,3 +27,5 @@
#define raise(what) (what)++
#define number int
#warning "This is a joke. Do not use YeOlde.h in serious code!"

Loading…
Cancel
Save