1 | #include <avr/interrupt.h> // für sei(), cli() und ISR()
|
2 | #include <avr/iom8515.h>
|
3 | #include <stdlib.h>
|
4 | #include <util/delay.h>
|
5 | #include "LCD.h"
|
6 |
|
7 |
|
8 | #define BAUD 9600UL
|
9 | #define F_CPU 1000000UL
|
10 |
|
11 |
|
12 | int main(void)
|
13 | {
|
14 | //EINGÄNGE
|
15 | DDRA = 0x00; //Die PINA's sind Eingänge
|
16 | PORTA = 0xff; //PULLUP AKTIV --> zum schalten wird ein
|
17 | //Low_signal benötigt
|
18 | //AUSGÄNGE
|
19 | DDRB = 0xff; //DATENRICHTUNGSREGISTER AUF AUSGANG
|
20 | PORTB = 0xff; //SCHALTE AUSGÄNGE LOW; STANDARTFALL: HIGHPEGEL
|
21 |
|
22 |
|
23 | //**************************** INITIALIESIERUNG LCD *******************************
|
24 |
|
25 | ////////////////////////////////////////////////////////////////////////////////
|
26 | // Erzeugt einen Enable-Puls
|
27 | void lcd_enable( void )
|
28 | {
|
29 | LCD_PORT |= (1<<LCD_EN); // Enable auf 1 setzen
|
30 | _delay_us( LCD_ENABLE_US ); // kurze Pause
|
31 | LCD_PORT &= ~(1<<LCD_EN); // Enable auf 0 setzen
|
32 |
|
33 | }
|
34 |
|
35 | ////////////////////////////////////////////////////////////////////////////////
|
36 | // Sendet eine 4-bit Ausgabeoperation an das LCD
|
37 | void lcd_out( uint8_t data )
|
38 | {
|
39 | data &= 0xF0; // obere 4 Bit maskieren
|
40 |
|
41 | LCD_PORT &= ~(0xF0>>(4-LCD_DB)); // Maske löschen
|
42 | LCD_PORT |= (data>>(4-LCD_DB)); // Bits setzen
|
43 | lcd_enable();
|
44 | }
|
45 |
|
46 | ////////////////////////////////////////////////////////////////////////////////
|
47 | // Sendet einen Befehl an das LCD
|
48 | void lcd_command( uint8_t data )
|
49 | {
|
50 | LCD_PORT &= ~(1<<LCD_RS); // RS auf 0 setzen
|
51 |
|
52 | lcd_out( data ); // zuerst die oberen,
|
53 | lcd_out( data<<4 ); // dann die unteren 4 Bit senden
|
54 |
|
55 | _delay_us( LCD_COMMAND_US );
|
56 | }
|
57 |
|
58 |
|
59 | /////////////////////////////////////////////////////////////////////////////////
|
60 | //Löschen des Displays
|
61 | void lcd_clear( void )
|
62 | {
|
63 | lcd_command( LCD_CLEAR_DISPLAY );
|
64 | _delay_ms( LCD_CLEAR_DISPLAY_MS );
|
65 | }
|
66 |
|
67 |
|
68 | ////////////////////////////////////////////////////////////////////////////////
|
69 | // Initialisierung: muss ganz am Anfang des Programms aufgerufen werden.
|
70 |
|
71 |
|
72 | // verwendete Pins auf Ausgang schalten
|
73 | uint8_t pins = (0x0F << LCD_DB) | // 4 Datenleitungen
|
74 | (1<<LCD_RS) | // R/S Leitung
|
75 | (1<<LCD_EN); // Enable Leitung
|
76 | LCD_DDR |= pins;
|
77 |
|
78 | // initial alle Ausgänge auf Null
|
79 | LCD_PORT &= ~pins;
|
80 |
|
81 |
|
82 | // warten auf die Bereitschaft des LCD
|
83 | _delay_ms( LCD_BOOTUP_MS );
|
84 |
|
85 | // Soft-Reset muss 3mal hintereinander gesendet werden zur Initialisierung
|
86 | lcd_out( LCD_SOFT_RESET );
|
87 | _delay_ms( LCD_SOFT_RESET_MS1 );
|
88 |
|
89 | lcd_enable();
|
90 | _delay_ms( LCD_SOFT_RESET_MS2 );
|
91 |
|
92 | lcd_enable();
|
93 | _delay_ms( LCD_SOFT_RESET_MS3 );
|
94 |
|
95 | // 4-bit Modus aktivieren
|
96 | lcd_out( LCD_SET_FUNCTION |
|
97 | LCD_FUNCTION_4BIT );
|
98 | _delay_ms( LCD_SET_4BITMODE_MS );
|
99 |
|
100 | // 4-bit Modus / 2 Zeilen / 5x7
|
101 | lcd_command( LCD_SET_FUNCTION |
|
102 | LCD_FUNCTION_4BIT |
|
103 | LCD_FUNCTION_2LINE |
|
104 | LCD_FUNCTION_5X7 );
|
105 |
|
106 | // Display ein / Cursor aus / Blinken aus
|
107 | lcd_command( LCD_SET_DISPLAY |
|
108 | LCD_DISPLAY_ON |
|
109 | LCD_CURSOR_OFF |
|
110 | LCD_BLINKING_OFF);
|
111 |
|
112 | // Cursor inkrement / kein Scrollen
|
113 | lcd_command( LCD_SET_ENTRY |
|
114 | LCD_ENTRY_INCREASE |
|
115 | LCD_ENTRY_NOSHIFT );
|
116 |
|
117 | lcd_clear();
|
118 |
|
119 |
|
120 | ////////////////////////////////////////////////////////////////////////////////
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 | //*******************************HAUPTPROGRAMM**************************************
|
129 |
|
130 | while(1) //MAINSCHLEIFE
|
131 | {
|
132 | PORTB |= (1<<PINB);
|
133 | _delay_ms(1000);
|
134 | PORTB &= ~(1<<PINB);
|
135 | _delay_ms(1000);
|
136 | }
|
137 |
|
138 |
|
139 |
|
140 | return(0); //Nichts zurückmelden
|
141 | } //Ende main
|