]> git.gir.st - tmk_keyboard.git/blob - tmk_core/protocol/lufa/LUFA-git/Projects/SerialToLCD/Lib/HD44780.c
Merge commit 'f6d56675f9f981c5464f0ca7a1fbb0162154e8c5'
[tmk_keyboard.git] / tmk_core / protocol / lufa / LUFA-git / Projects / SerialToLCD / Lib / HD44780.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2014.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11 Copyright 2012 Simon Foster (simon.foster [at] inbox [dot] com)
12
13 Permission to use, copy, modify, distribute, and sell this
14 software and its documentation for any purpose is hereby granted
15 without fee, provided that the above copyright notice appear in
16 all copies and that both that the copyright notice and this
17 permission notice and warranty disclaimer appear in supporting
18 documentation, and that the name of the author not be used in
19 advertising or publicity pertaining to distribution of the
20 software without specific, written prior permission.
21
22 The author disclaims all warranties with regard to this
23 software, including all implied warranties of merchantability
24 and fitness. In no event shall the author be liable for any
25 special, indirect or consequential damages or any damages
26 whatsoever resulting from loss of use, data or profits, whether
27 in an action of contract, negligence or other tortious action,
28 arising out of or in connection with the use or performance of
29 this software.
30 */
31
32 #include "HD44780.h"
33
34 static void HD44780_WriteNibble(const uint8_t nib)
35 {
36 /* Read PORTD and clear the ENABLE and PD0..3 bits
37 then OR in the data */
38
39 PORTD = (PORTD & ~(ENABLE | LO4_MASK)) | (nib & LO4_MASK);
40
41 /* Enforce address setup time (tAS) 60ns
42 60 @ 16MHz = <1
43 Let's us a few NOPs for good measure */
44 asm volatile("nop\n\t"
45 "nop\n\t"
46 :: );
47
48 /* Take enable high and enforce Enable High time (tEH=450ns)
49 450ns @ 16MHz = 7.2 => 7 NOPs */
50
51 PORTD |= ENABLE;
52
53 asm volatile("nop\n\t"
54 "nop\n\t"
55 "nop\n\t"
56 "nop\n\t"
57 "nop\n\t"
58 "nop\n\t"
59 "nop\n\t"
60 "nop\n\t"
61 :: );
62
63 /* Take enable low and enforce Enable Low time (tEL=500ns)
64 500ns @ 16MHz = 8.0 => 7 NOPs */
65 PORTD &= ~ENABLE;
66
67 asm volatile("nop\n\t"
68 "nop\n\t"
69 "nop\n\t"
70 "nop\n\t"
71 "nop\n\t"
72 "nop\n\t"
73 "nop\n\t"
74 "nop\n\t"
75 :: );
76 }
77
78 static void HD44780_WriteByte(const uint8_t c)
79 {
80 HD44780_WriteNibble(HI4(c));
81 HD44780_WriteNibble(LO4(c));
82 }
83
84 static void HD44780_PowerUp4Bit(void)
85 {
86 /* Wait for more than 40 ms after VCC rises to 2.7 V */
87 _delay_ms(40);
88 HD44780_WriteNibble(0x03); // FN_SET 8-bit
89
90 /* Wait for more than 4.1 ms */
91 _delay_ms(5);
92 HD44780_WriteNibble(0x03); // FN_SET 8-bit
93
94 /* Wait for more than 100 µs */
95 _delay_us(100);
96 HD44780_WriteNibble(0x03); // FN_SET 8-bit
97
98 /* From now on we must allow 40us for each command */
99 _delay_us(50);
100 HD44780_WriteNibble(0x02); // FN_SET 4-bit
101
102 /* The LCD is now in 4-bit mode so we can continue
103 using the 4-bit API */
104 _delay_us(50);
105 }
106
107 void HD44780_Initialize(void)
108 {
109 PORTD &= ~ALL_BITS;
110 DDRD |= ALL_BITS;
111 HD44780_PowerUp4Bit();
112 }
113
114 void HD44780_WriteCommand(const uint8_t c)
115 {
116 PORTD &= ~RS;
117 HD44780_WriteByte(c);
118 _delay_us(50);
119 }
120
121 void HD44780_WriteData(const uint8_t c)
122 {
123 PORTD |= RS;
124 HD44780_WriteByte(c);
125 PORTD &= ~RS;
126 _delay_us(50);
127 }
Imprint / Impressum