added some examples, shortened some lcd functions

pull/1/head
Ondřej Hruška 10 years ago
parent c75086ea00
commit 161b2ce88f
  1. 4
      examples/Makefile
  2. 13
      examples/lcd_config.h
  3. 58
      examples/lcd_test.c
  4. 5
      examples/uart_isr.c
  5. 17
      examples/uart_simple.c
  6. 55
      lib/lcd.c
  7. 31
      lib/lcd.h

@ -11,14 +11,14 @@ EFUSE = 0x05
## === Source files === ## === Source files ===
# Main C file # Main C file
MAIN = uart_isr.c MAIN = lcd_test.c
# Extra C files in this folder # Extra C files in this folder
LOCAL_SOURCE = LOCAL_SOURCE =
# Library directory (with C files) # Library directory (with C files)
EXTRA_SOURCE_DIR = lib/ EXTRA_SOURCE_DIR = lib/
# C files in the library directory # C files in the library directory
EXTRA_SOURCE_FILES = uart.c EXTRA_SOURCE_FILES = uart.c lcd.c
## === Programmer === ## === Programmer ===

@ -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

@ -0,0 +1,58 @@
//
// Example of basic LCD usage
//
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#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);
}

@ -1,8 +1,11 @@
//
// Example of UART with interrupts
//
#include <avr/io.h> #include <avr/io.h>
#include <util/delay.h> #include <util/delay.h>
#include <avr/pgmspace.h> #include <avr/pgmspace.h>
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>

@ -1,8 +1,11 @@
//
// Example of basic UART usage
//
#include <avr/io.h> #include <avr/io.h>
#include <util/delay.h> #include <util/delay.h>
#include <avr/pgmspace.h> #include <avr/pgmspace.h>
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
@ -21,8 +24,8 @@ void main()
// Printing a string // Printing a string
uart_puts("Simple UART example!\r\n"); uart_puts("Simple UART example!\r\n");
// You can put your string literals in the program memory // Use _pgm for strings in program memory
// and use the _pgm variant: // (PSTR macro is defined in avr/pgmspace.h)
uart_puts_pgm(PSTR("Program memory string example\r\n")); uart_puts_pgm(PSTR("Program memory string example\r\n"));
// print a char. This is an alias for uart_tx // print a char. This is an alias for uart_tx
@ -32,24 +35,26 @@ void main()
uart_nl(); uart_nl();
// Printing numbers is also easy // Printing numbers is also easy
// Look in the library file for more info // Look in `uart.h` for more info
uart_putn(123); uart_putn(123);
uart_nl(); 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_puti(31415, 4);
uart_nl(); uart_nl();
// --- receive example --- // --- receive example ---
// Example of listening for input // Example of listening for input
while(1) { while(1) {
// If something is received // If something is received
if (uart_rx_ready()) { if (uart_rx_ready()) {
// Get the received byte // Get the received byte
char c = uart_rx(); char c = uart_rx();
// Send it back, twice // Send it back, twice
uart_tx(c); uart_tx(c);
uart_tx(c); uart_tx(c);

@ -61,10 +61,10 @@ void lcd_init()
_delay_us(100); _delay_us(100);
// Configure the display // Configure the display
lcd_write_command(LCD_IFACE_4BIT_2LINE); lcd_command(LCD_IFACE_4BIT_2LINE);
lcd_write_command(LCD_DISABLE); lcd_command(LCD_DISABLE);
lcd_write_command(LCD_CLEAR); lcd_command(LCD_CLEAR);
lcd_write_command(LCD_MODE_INC); lcd_command(LCD_MODE_INC);
// mark as enabled // mark as enabled
lcd_enable(); lcd_enable();
@ -130,7 +130,7 @@ uint8_t _lcd_read_byte()
/** Write an instruction byte */ /** Write an instruction byte */
void lcd_write_command(uint8_t bb) void lcd_command(uint8_t bb)
{ {
_lcd_wait_bf(); _lcd_wait_bf();
pin_down(LCD_RS); // select instruction register pin_down(LCD_RS); // select instruction register
@ -139,7 +139,7 @@ void lcd_write_command(uint8_t bb)
/** Write a data byte */ /** Write a data byte */
void lcd_write_data(uint8_t bb) void lcd_write(uint8_t bb)
{ {
_lcd_wait_bf(); _lcd_wait_bf();
pin_up(LCD_RS); // select data register pin_up(LCD_RS); // select data register
@ -156,7 +156,7 @@ uint8_t lcd_read_bf_addr()
/** Read CGRAM or DDRAM */ /** Read CGRAM or DDRAM */
uint8_t lcd_read_ram() uint8_t lcd_read()
{ {
pin_up(LCD_RS); pin_up(LCD_RS);
return _lcd_read_byte(); 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 */ /** Sedn a char to LCD */
void lcd_putc(const char c) void lcd_putc(const char c)
{ {
lcd_write_data(c); lcd_write(c);
} }
/** Set cursor position */ /** Set cursor position */
void lcd_xy(const uint8_t x, const uint8_t y) 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); _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) */ /** Display display (preserving cursor) */
void lcd_disable() void lcd_disable()
{ {
lcd_write_command(LCD_DISABLE); lcd_command(LCD_DISABLE);
_lcd_enabled = false; _lcd_enabled = false;
} }
@ -239,46 +248,46 @@ void lcd_enable()
/** Go home */ /** Go home */
void lcd_home() void lcd_home()
{ {
lcd_write_command(LCD_HOME); lcd_command(LCD_HOME);
} }
/** Clear the screen */ /** Clear the screen */
void lcd_clear() void lcd_clear()
{ {
lcd_write_command(LCD_CLEAR); lcd_command(LCD_CLEAR);
} }
/** Define a glyph */ /** 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) { for (uint8_t i = 0; i < 8; ++i) {
lcd_write_data(array[i]); lcd_write(array[i]);
} }
} }
/** Define a glyph */ /** 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) { 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 */ /** 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 */ /** 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));
} }

@ -19,9 +19,12 @@
// //
#include <stdint.h> #include <stdint.h>
#include <stdbool.h>
// File with configs
#include "lcd_config.h" #include "lcd_config.h"
// Commands // --- Commands ---
// Clear screen (reset) // Clear screen (reset)
#define LCD_CLEAR 0b00000001 #define LCD_CLEAR 0b00000001
@ -70,28 +73,34 @@
void lcd_init(); void lcd_init();
/** Write an instruction byte */ /** Write an instruction byte */
void lcd_write_command(uint8_t bb); void lcd_command(uint8_t bb);
/** Write a data byte */ /** Write a data byte */
void lcd_write_data(uint8_t bb); void lcd_write(uint8_t bb);
/** Read BF & Address */ /** Read BF & Address */
uint8_t lcd_read_bf_addr(); uint8_t lcd_read_bf_addr();
/** Read CGRAM or DDRAM */ /** Read CGRAM or DDRAM */
uint8_t lcd_read_ram(); uint8_t lcd_read();
/** Send a string to LCD */ /** Send a string to LCD */
void lcd_puts(char* str_p); 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 */ /** Sedn a char to LCD */
void lcd_putc(const char c); void lcd_putc(const char c);
/** Show string at X, Y */ /** 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 */ /** 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 */ /** Set cursor position */
void lcd_xy(const uint8_t x, const uint8_t y); void lcd_xy(const uint8_t x, const uint8_t y);
@ -115,14 +124,14 @@ void lcd_home();
/** Clear the screen */ /** Clear the screen */
void lcd_clear(); void lcd_clear();
/** Define a glyph */ /** Define a glyph - 8 bytes, right 5 bits are used */
void lcd_define_glyph(const uint8_t index, const uint8_t* array); void lcd_glyph(const uint8_t index, const uint8_t* array);
/** Define a glyph that's in PROGMEM */ /** 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 */ /** 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 */ /** Set address in DDRAM */
void lcd_set_addr(const uint8_t add); void lcd_addr(const uint8_t add);

Loading…
Cancel
Save