1 | #ifndef LCDROUTINE_H_
|
2 | #define LCDROUTINE_H_
|
3 |
|
4 | /************************************************************
|
5 | * definations
|
6 | ************************************************************/
|
7 | //Port1
|
8 | #define LCD_D0 (0x01) // Befehlsatz D0
|
9 | #define LCD_D1 (0x02) // Befehlsatz D1
|
10 | #define LCD_D2 (0x04) // Befehlsatz D2
|
11 | #define LCD_D3 (0x08) // Befehlsatz D3
|
12 | #define LCD_D4 (0x10) // Befehlsatz D4
|
13 | #define LCD_D5 (0x20) // Befehlsatz D5
|
14 | #define LCD_D6 (0x40) // Befehlsatz D6
|
15 | #define LCD_D7 (0x80) // Befehlsatz D7
|
16 |
|
17 | //Port2
|
18 | #define LCD_RS (0x01) // RS --> selects register 0: Instruction register (write) 1: Data register ( write & read)
|
19 | #define LCD_RW (0x02) // R/W --> selects read or write
|
20 | #define LCD_E (0x04) // E --> Starts data read/write.
|
21 |
|
22 |
|
23 | /************************************************************
|
24 | * declarations
|
25 | ************************************************************/
|
26 |
|
27 | void delay_50us(unsigned int delay); // Delay 50µs
|
28 | void LCD_Clock(void); // erzeugt ein Clock-Singal
|
29 | void lcd_command(unsigned char uch_command); //sendet command
|
30 | void LCD_Init(void); // Initialisierung
|
31 | void LCD_Clear(void); // löscht das Display und setzt Cursor den Anfang zurück
|
32 | void LCD_CursorHome(void); // Cursor auf Startpostion 0
|
33 | void LCD_OutChar(unsigned char uch_data); // gibt einen Character aus
|
34 | void LCD_OutString( const char* c ); // gibt einen String aus
|
35 |
|
36 | unsigned short ush_test_array = 0;
|
37 | unsigned short i = 0;
|
38 |
|
39 | /************************************************************
|
40 | * functions
|
41 | ************************************************************/
|
42 |
|
43 | void delay_50us(unsigned int uint_delay)
|
44 | {
|
45 | while (uint_delay--) // Loop für Delay
|
46 | {
|
47 | __delay_cycles(5000); // Delay 50*Masterclock
|
48 | }
|
49 | }
|
50 |
|
51 | void LCD_Clock(void) // erzeugt ein Clock-Singal
|
52 | {
|
53 | delay_50us(4); // 200µs Delay
|
54 | P2OUT &= ~LCD_E; // zuerst das clock-bit sicherheitshalber auf low
|
55 | delay_50us(4); // 200µs Delay
|
56 | P2OUT |= LCD_E; // clock-bit auf high
|
57 | delay_50us(4); // 200µs Delay
|
58 | P2OUT &= ~LCD_E; // und jetzt wieder auf low
|
59 | delay_50us(4); // 200µs Delay
|
60 | }
|
61 |
|
62 | void LCD_Init(void) //// Initialisierung
|
63 | {
|
64 | P2OUT = 0; // Enable, RW und RS auf 0
|
65 | delay_50us(500); // 2500µs Delay
|
66 |
|
67 | P1OUT = LCD_D5 // Befehlssatz: Funktioen
|
68 | // + LCD_D4 // 1 --> 8-Bit Interface; 0 --> 4-Bit Interface
|
69 | ;
|
70 | //for ( i = 0; i < 2; i++ )
|
71 | // {
|
72 | LCD_Clock(); // befehl ausfuehren
|
73 | delay_50us(500); // 2500µs Delay
|
74 | LCD_Clock(); // befehl ausfuehren
|
75 | delay_50us(500); // 2500µs Delay
|
76 | // }
|
77 |
|
78 | lcd_command(LCD_D5 + LCD_D3 ); // Befehlssatz: Funktionen --> 2/4 Zeilen,
|
79 | lcd_command(LCD_D4 + LCD_D2 ); // Befehlssatz: Cursor/Display schieben --> Cursor rechts schieben
|
80 | lcd_command(LCD_D3 + LCD_D2); // Befehlssatz: Display/Cursor --> Display an
|
81 | lcd_command(LCD_D2 + LCD_D1); // Befehlssatz: Modus festlegen --> Cursor inkrementieren(rechts)
|
82 | LCD_Clear();
|
83 | }
|
84 |
|
85 | void LCD_Clear(void) // löscht das Display und setzt Cursor den Anfang zurück
|
86 | {
|
87 | lcd_command(LCD_D0); // clear display
|
88 | delay_50us(50); // 2500µs Delay
|
89 |
|
90 | lcd_command(LCD_D1); // Cursor Homeposition
|
91 | delay_50us(50); // 2500µs Delay
|
92 | }
|
93 |
|
94 | void LCD_CursorHome(void) // löscht das Display und setzt Cursor den Anfang zurück
|
95 | {
|
96 | lcd_command(LCD_D1); // Cursor Homeposition
|
97 | delay_50us(50); // 2500µs Delay
|
98 | }
|
99 |
|
100 | void LCD_OutChar(unsigned char uch_data)
|
101 | {
|
102 | unsigned char uch_byte = uch_data; // übergibt das byte
|
103 | P2OUT = LCD_RS; // Wählt Datenregister aus
|
104 |
|
105 | //High Nibble
|
106 | P1OUT = (uch_byte >> 4) & 0x0F; // 4 Bits inkrementieren und maskieren und anschließen high nibble ausgeben
|
107 | LCD_Clock(); // Clock signal
|
108 | //Low Nibble
|
109 | P1OUT = (uch_byte & 0x0F); // unteres nibble maskieren und ausgeben
|
110 | LCD_Clock(); // Clock singal
|
111 | }
|
112 |
|
113 | void lcd_command(unsigned char uch_command)
|
114 | {
|
115 | unsigned char uch_byte = uch_command; // übergibt das byte
|
116 | P2OUT = 0; // Wählt Datenregister aus
|
117 |
|
118 | //High Nibble
|
119 | P1OUT = (uch_byte >> 4) & 0x0F; // 4 Bits inkrementieren und maskieren und anschließen high nibble ausgeben
|
120 | LCD_Clock(); // Clock signal
|
121 | //Low Nibble
|
122 | P1OUT = (uch_byte & 0x0F); // unteres nibble maskieren und ausgeben
|
123 | LCD_Clock(); // Clock singal
|
124 | }
|
125 |
|
126 | void LCD_OutString( const char *c ) // gibt einen String aus
|
127 | {
|
128 | while( *c != '\0' )
|
129 | {
|
130 | LCD_OutChar( *c++ );
|
131 | }
|
132 | }
|
133 |
|
134 |
|
135 |
|
136 | #endif /*LCDROUTINE_H_*/
|