From 161b2ce88ffcce9ab46f7b72aaaa963d67cd1173 Mon Sep 17 00:00:00 2001 From: MightyPork Date: Wed, 29 Apr 2015 18:29:52 +0200 Subject: [PATCH] added some examples, shortened some lcd functions --- examples/Makefile | 4 +-- examples/lcd_config.h | 13 ++++++++++ examples/lcd_test.c | 58 ++++++++++++++++++++++++++++++++++++++++++ examples/uart_isr.c | 5 +++- examples/uart_simple.c | 17 ++++++++----- lib/lcd.c | 55 ++++++++++++++++++++++----------------- lib/lcd.h | 31 ++++++++++++++-------- 7 files changed, 140 insertions(+), 43 deletions(-) create mode 100644 examples/lcd_config.h create mode 100644 examples/lcd_test.c diff --git a/examples/Makefile b/examples/Makefile index 2bd0c16..ded8c57 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -11,14 +11,14 @@ EFUSE = 0x05 ## === Source files === # Main C file -MAIN = uart_isr.c +MAIN = lcd_test.c # Extra C files in this folder LOCAL_SOURCE = # Library directory (with C files) EXTRA_SOURCE_DIR = lib/ # C files in the library directory -EXTRA_SOURCE_FILES = uart.c +EXTRA_SOURCE_FILES = uart.c lcd.c ## === Programmer === diff --git a/examples/lcd_config.h b/examples/lcd_config.h new file mode 100644 index 0000000..594b506 --- /dev/null +++ b/examples/lcd_config.h @@ -0,0 +1,13 @@ +#pragma once + +// Pin config file for LCD. + +#include "lib/arduino_pins.h" + +#define LCD_RS D2 +#define LCD_RW D3 +#define LCD_E D4 +#define LCD_D4 D5 +#define LCD_D5 D6 +#define LCD_D6 D7 +#define LCD_D7 D8 diff --git a/examples/lcd_test.c b/examples/lcd_test.c new file mode 100644 index 0000000..4e6aba3 --- /dev/null +++ b/examples/lcd_test.c @@ -0,0 +1,58 @@ +// +// Example of basic LCD usage +// + +#include +#include +#include + +#include "lib/calc.h" +#include "lib/lcd.h" + +// Bytes for custom glyph + +// Here's a good glyph generator you can use: +// http://omerk.github.io/lcdchargen/ + +const uint8_t glyph[] PROGMEM = { + 0b00000, + 0b00111, + 0b01101, + 0b11101, + 0b10001, + 0b10001, + 0b10001, + 0b11111 +}; + + +void main() +{ + lcd_init(); + + // Define custom glyph at character 0 + lcd_glyph_pgm(0, glyph); + + // After writing a glyph, the address is changed + // So we need to fix that: + lcd_xy(0, 0); + + // Print a string + lcd_puts("Hello"); + + // Use _pgm for strings in program memory + // Here we print string at position xy + lcd_puts_xy_pgm(0, 1, PSTR("String from PGM ")); + + // print the custom glyph + lcd_putc(0); + + // enable blink/bar cursor type + lcd_cursor(CURSOR_BOTH); + + lcd_puts_xy(5, 2, "Third line?"); + lcd_puts_xy(3, 3, "Fourth line!"); + + // end + while(1); +} diff --git a/examples/uart_isr.c b/examples/uart_isr.c index c52000d..a5e144b 100644 --- a/examples/uart_isr.c +++ b/examples/uart_isr.c @@ -1,8 +1,11 @@ +// +// Example of UART with interrupts +// + #include #include #include #include -#include #include #include diff --git a/examples/uart_simple.c b/examples/uart_simple.c index 9973224..9fb360b 100644 --- a/examples/uart_simple.c +++ b/examples/uart_simple.c @@ -1,8 +1,11 @@ +// +// Example of basic UART usage +// + #include #include #include #include -#include #include #include @@ -21,8 +24,8 @@ void main() // Printing a string uart_puts("Simple UART example!\r\n"); - // You can put your string literals in the program memory - // and use the _pgm variant: + // Use _pgm for strings in program memory + // (PSTR macro is defined in avr/pgmspace.h) uart_puts_pgm(PSTR("Program memory string example\r\n")); // print a char. This is an alias for uart_tx @@ -32,24 +35,26 @@ void main() uart_nl(); // Printing numbers is also easy - // Look in the library file for more info + // Look in `uart.h` for more info uart_putn(123); uart_nl(); - // Print int as float (adds decimal point) + // Print int as float (adds decimal point at 4th place from the end) uart_puti(31415, 4); uart_nl(); - // --- receive example --- // Example of listening for input while(1) { + // If something is received if (uart_rx_ready()) { + // Get the received byte char c = uart_rx(); + // Send it back, twice uart_tx(c); uart_tx(c); diff --git a/lib/lcd.c b/lib/lcd.c index 37efa6f..d9162c2 100644 --- a/lib/lcd.c +++ b/lib/lcd.c @@ -61,10 +61,10 @@ void lcd_init() _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_command(LCD_IFACE_4BIT_2LINE); + lcd_command(LCD_DISABLE); + lcd_command(LCD_CLEAR); + lcd_command(LCD_MODE_INC); // mark as enabled lcd_enable(); @@ -130,7 +130,7 @@ uint8_t _lcd_read_byte() /** Write an instruction byte */ -void lcd_write_command(uint8_t bb) +void lcd_command(uint8_t bb) { _lcd_wait_bf(); pin_down(LCD_RS); // select instruction register @@ -139,7 +139,7 @@ void lcd_write_command(uint8_t bb) /** Write a data byte */ -void lcd_write_data(uint8_t bb) +void lcd_write(uint8_t bb) { _lcd_wait_bf(); pin_up(LCD_RS); // select data register @@ -156,7 +156,7 @@ uint8_t lcd_read_bf_addr() /** Read CGRAM or DDRAM */ -uint8_t lcd_read_ram() +uint8_t lcd_read() { pin_up(LCD_RS); return _lcd_read_byte(); @@ -194,17 +194,26 @@ void lcd_puts(char* str_p) } +/** Print from progmem */ +void lcd_puts_pgm(const char* str_p) +{ + char c; + while ((c = pgm_read_byte(str_p++))) + lcd_putc(c); +} + + /** Sedn a char to LCD */ void lcd_putc(const char c) { - lcd_write_data(c); + lcd_write(c); } /** Set cursor position */ void lcd_xy(const uint8_t x, const uint8_t y) { - lcd_set_addr(LCD_ROW_ADDR[y] + (x)); + lcd_addr(LCD_ROW_ADDR[y] + (x)); } @@ -216,14 +225,14 @@ void lcd_cursor(uint8_t type) { _lcd_old_cursor = (type & CURSOR_BOTH); - if (_lcd_enabled) lcd_write_command(LCD_CURSOR_NONE | _lcd_old_cursor); + if (_lcd_enabled) lcd_command(LCD_CURSOR_NONE | _lcd_old_cursor); } /** Display display (preserving cursor) */ void lcd_disable() { - lcd_write_command(LCD_DISABLE); + lcd_command(LCD_DISABLE); _lcd_enabled = false; } @@ -239,46 +248,46 @@ void lcd_enable() /** Go home */ void lcd_home() { - lcd_write_command(LCD_HOME); + lcd_command(LCD_HOME); } /** Clear the screen */ void lcd_clear() { - lcd_write_command(LCD_CLEAR); + lcd_command(LCD_CLEAR); } /** Define a glyph */ -void lcd_define_glyph(const uint8_t index, const uint8_t* array) +void lcd_glyph(const uint8_t index, const uint8_t* array) { - lcd_set_addr_cgram(index * 8); + lcd_addr_cg(index * 8); for (uint8_t i = 0; i < 8; ++i) { - lcd_write_data(array[i]); + lcd_write(array[i]); } } /** Define a glyph */ -void lcd_define_glyph_pgm(const uint8_t index, const uint8_t* array) +void lcd_glyph_pgm(const uint8_t index, const uint8_t* array) { - lcd_set_addr_cgram(index * 8); + lcd_addr_cg(index * 8); for (uint8_t i = 0; i < 8; ++i) { - lcd_write_data(pgm_read_byte(&array[i])); + lcd_write(pgm_read_byte(&array[i])); } } /** Set address in CGRAM */ -void lcd_set_addr_cgram(const uint8_t acg) +void lcd_addr_cg(const uint8_t acg) { - lcd_write_command(0b01000000 | ((acg) & 0b00111111)); + lcd_command(0b01000000 | ((acg) & 0b00111111)); } /** Set address in DDRAM */ -void lcd_set_addr(const uint8_t add) +void lcd_addr(const uint8_t add) { - lcd_write_command(0b10000000 | ((add) & 0b01111111)); + lcd_command(0b10000000 | ((add) & 0b01111111)); } diff --git a/lib/lcd.h b/lib/lcd.h index 0754091..8e37415 100644 --- a/lib/lcd.h +++ b/lib/lcd.h @@ -19,9 +19,12 @@ // #include +#include + +// File with configs #include "lcd_config.h" -// Commands +// --- Commands --- // Clear screen (reset) #define LCD_CLEAR 0b00000001 @@ -70,28 +73,34 @@ void lcd_init(); /** Write an instruction byte */ -void lcd_write_command(uint8_t bb); +void lcd_command(uint8_t bb); /** Write a data byte */ -void lcd_write_data(uint8_t bb); +void lcd_write(uint8_t bb); /** Read BF & Address */ uint8_t lcd_read_bf_addr(); /** Read CGRAM or DDRAM */ -uint8_t lcd_read_ram(); +uint8_t lcd_read(); /** Send a string to LCD */ void lcd_puts(char* str_p); +/** Send a string to LCD from program memory */ +void lcd_puts_pgm(const char* str_p); + /** Sedn a char to LCD */ void lcd_putc(const char c); /** Show string at X, Y */ -#define lcd_str_xy(x, y, str_p) do { lcd_xy((x), (y)); lcd_puts((str_p)); } while(0) +#define lcd_puts_xy(x, y, str_p) do { lcd_xy((x), (y)); lcd_puts((str_p)); } while(0) + +/** Show string at X, Y */ +#define lcd_puts_xy_pgm(x, y, str_p) do { lcd_xy((x), (y)); lcd_puts_pgm((str_p)); } while(0) /** Show char at X, Y */ -#define lcd_char_xy(x, y, c) do { lcd_xy((x), (y)); lcd_putc((c)); } while(0) +#define lcd_putc_xy(x, y, c) do { lcd_xy((x), (y)); lcd_putc((c)); } while(0) /** Set cursor position */ void lcd_xy(const uint8_t x, const uint8_t y); @@ -115,14 +124,14 @@ void lcd_home(); /** Clear the screen */ void lcd_clear(); -/** Define a glyph */ -void lcd_define_glyph(const uint8_t index, const uint8_t* array); +/** Define a glyph - 8 bytes, right 5 bits are used */ +void lcd_glyph(const uint8_t index, const uint8_t* array); /** Define a glyph that's in PROGMEM */ -void lcd_define_glyph_pgm(const uint8_t index, const uint8_t* array); +void lcd_glyph_pgm(const uint8_t index, const uint8_t* array); /** Set address in CGRAM */ -void lcd_set_addr_cgram(const uint8_t acg); +void lcd_addr_cg(const uint8_t acg); /** Set address in DDRAM */ -void lcd_set_addr(const uint8_t add); +void lcd_addr(const uint8_t add);