1 | // LCD Output for HD44780 compatible displays
|
2 | // Target: ATMEGA8
|
3 |
|
4 | #define LCD_CLEAR 0x01 // Clear display: 0b 0000 0001
|
5 | #define LCD_HOME 0x02 // Cursor home: 0b 0000 0010
|
6 | #define LCD_ON 0x0C // Cursor invisible: 0b 0000 1100
|
7 | #define LCD_OFF 0x08 // Display off: 0b 0000 1000
|
8 | #define POS_01 0x80 // Line 1 - Column 0 0b 1000 0000
|
9 | #define POS_02 0xC0 // Line 2 - Column 0 0b 1100 0000
|
10 |
|
11 | // definition of port pins - data 4-7 -> LCDPORT4-7
|
12 | #define LCDPORT PORTD
|
13 | #define LCDDDR DDRD
|
14 | #define LCD_PIN_RS 2
|
15 | #define LCD_PIN_E 3
|
16 | #define LCD_PIN_D4 4
|
17 | #define LCD_PIN_D5 5
|
18 |
|
19 | #define COMMAND 0
|
20 | #define DATA 1
|
21 |
|
22 | #include "BitUtilities.h"
|
23 |
|
24 | // Function: Toggle enable pin
|
25 | void toggle_enable_pin(void)
|
26 | { sbi(LCDPORT, LCD_PIN_E); cbi(LCDPORT, LCD_PIN_E);
|
27 | }
|
28 |
|
29 | // Send Byte to LCD Controller
|
30 | void lcd_send(unsigned char type, unsigned char c)
|
31 | { unsigned char sic_c; // Backup for c
|
32 |
|
33 | // send high nibble
|
34 | sic_c = c; // save original c
|
35 | sic_c &= ~0x0f; // set bit 0-3 == 0
|
36 | if (type==DATA)
|
37 | sic_c |= (1<<LCD_PIN_RS); // Data: RS = 1
|
38 | LCDPORT = sic_c; // send high nibble
|
39 | toggle_enable_pin();
|
40 |
|
41 | // send low nibble
|
42 | sic_c = c; // save original c
|
43 | sic_c = sic_c<<4; // exchange nibbles
|
44 | sic_c &= ~0x0f; // set bit 0-3 == 0
|
45 | if (type==DATA)
|
46 | sic_c |= (1<<LCD_PIN_RS); // Data: RS = 1
|
47 | LCDPORT = sic_c; // send low nibble
|
48 | toggle_enable_pin();
|
49 |
|
50 | _delay_ms(5); // Wait for LCD controller
|
51 | }
|
52 |
|
53 | // set cursor to line x and column y
|
54 | void lcd_set_cursor(uint8_t x, uint8_t y)
|
55 | { uint8_t i;
|
56 | switch (x)
|
57 | { case 1: i=0x80+0x00+y; break; // 1. line
|
58 | case 2: i=0x80+0x40+y; break; // 2. line
|
59 | default: return; // invalid line
|
60 | }
|
61 | lcd_send(COMMAND, i);
|
62 | }
|
63 |
|
64 | // write string to LCD
|
65 | void lcd_write(char *data)
|
66 | { while(*data) {lcd_send(DATA, *data); data++;}
|
67 | }
|
68 |
|
69 | // initialize LCD Controller
|
70 | void lcd_init()
|
71 | { // Set Port to Output
|
72 | LCDPORT = 0x00; LCDDDR = 0xFF;
|
73 | _delay_ms(50); // Wait for LCD
|
74 |
|
75 | // 4-bit Modus config
|
76 | sbi(LCDPORT, LCD_PIN_D5); cbi(LCDPORT, LCD_PIN_D4);
|
77 |
|
78 | // 4-Bit Modus start
|
79 | sbi(PORTD, LCD_PIN_E); cbi(PORTD, LCD_PIN_E);
|
80 | _delay_ms(5);
|
81 |
|
82 | // 2 Lines, 4-Bit Mode
|
83 | lcd_send(COMMAND, 0x28);
|
84 | lcd_send(COMMAND, LCD_OFF); lcd_send(COMMAND, LCD_CLEAR);
|
85 | lcd_send(COMMAND, 0x06); lcd_send(COMMAND, LCD_ON);
|
86 | }
|