Forum: Mikrocontroller und Digitale Elektronik LCD_Routine_atmega32


von cler (Gast)


Lesenswert?

Hallo!

Habe ein Problem bezüglich einer LCD_Routine.

Und zwar: Es wird zwar der einzelne Buchstabe angezeigt, jedoch wird 
dieser  von der angegebenen Position verschoben.


Meine Frage ist daher: Was mach ich falsch?

Takt: 4 MHz
LCD_display: HD44780 2 zeilig
AVR: atmega32

Pinkonfigurationen:

DB7...PC7
 6      6
 5      5
 4      4
EN     PC2
RW     GND
RS      3


Da ich das Busyflag nicht abfragen kann, musste ich dies durch 
Delayfunktionen lösen.

siehe Quellcode:
1
//LCD
2
void    _long_delay(void);
3
void    _lcd_write_command(unsigned char);
4
void    LCD_Write(unsigned char *);
5
void    LCD_Init(void);
6
char    LCD_Putchar(char);
7
#define LCD_Clear  {_lcd_write_command(0x01); _long_delay();_long_delay();}
8
void    LCD_Gotoxy(unsigned char  , unsigned char );
9
void    LCD_Gotoxy2(unsigned char  , unsigned char );
10
void    LCD_DezAusgabe(unsigned long wert , char stellen , unsigned char punkt);
11
void LCD_Puts(const char *s);
12
13
14
void _long_delay(void)
15
{
16
long t = 3000;
17
while (t--);
18
}
19
20
void _short_delay(void)
21
{
22
int t = 350;
23
while (t--);
24
}
25
26
27
void _lcd_write_command(unsigned char data)
28
{
29
  DDRC |= (0x80+0x40+0x20+0x10+0x08);
30
  LCD_PORT_w = (data & 0xf0) | DISPLAY_EN;
31
  LCD_PORT_w = (data & 0xf0) | DISPLAY_EN;
32
  LCD_PORT_w = (data & 0xf0) | DISPLAY_EN;
33
  LCD_PORT_w = (data & 0xf0);
34
  LCD_PORT_w = (data & 0xf0);
35
  LCD_PORT_w = (data & 0xf0);
36
  LCD_PORT_w = (data << 4) | DISPLAY_EN;
37
  LCD_PORT_w = (data << 4) | DISPLAY_EN;
38
  LCD_PORT_w = (data << 4) | DISPLAY_EN;
39
  LCD_PORT_w = (data << 4);
40
    DDRC &= ~(0x80+0x40+0x20+0x10+0x08);
41
   PORTD &= ~0x08;
42
 
43
}
44
45
void _lcd_write_4bit(unsigned char data)
46
{
47
 DDRC |= (0x80+0x40+0x20+0x10+0x08);
48
  LCD_PORT_w = (data << 4) | DISPLAY_EN;
49
  LCD_PORT_w = (data << 4) | DISPLAY_EN;
50
  LCD_PORT_w = (data << 4) | DISPLAY_EN;
51
  LCD_PORT_w = (data << 4);
52
  LCD_PORT_w = (data << 4);
53
    DDRC &= ~(0x80+0x40+0x20+0x10+0x08);
54
 PORTD &= ~0x08;
55
 
56
}
57
58
void lcd_write_byte(unsigned char data)
59
{
60
 DDRC |= (0x80+0x40+0x20+0x10+0x08);
61
  LCD_PORT_w = (data & 0xf0) | DISPLAY_EN | DISPLAY_RS;
62
  LCD_PORT_w = (data & 0xf0) | DISPLAY_EN | DISPLAY_RS;
63
  LCD_PORT_w = (data & 0xf0) | DISPLAY_RS;
64
  LCD_PORT_w = (data & 0xf0) | DISPLAY_RS;
65
  LCD_PORT_w = (data << 4)   | DISPLAY_EN | DISPLAY_RS;
66
  LCD_PORT_w = (data << 4)   | DISPLAY_EN | DISPLAY_RS;
67
  LCD_PORT_w = (data << 4)   | DISPLAY_RS;
68
    DDRC &= ~(0x80+0x40+0x20+0x10+0x08);
69
 PORTD &= ~0x08;
70
 
71
}
72
73
74
int my_pput(int zeichen)
75
{
76
 lcd_write_byte((char) zeichen);
77
 return(1);
78
}
79
80
// initialize the LCD controller
81
void LCD_Init(void)
82
{
83
84
LCD_PORT_DDR = 0xff;// - (0x01 + 0x02) ;//0xf0 | DISPLAY_RS | DISPLAY_EN;
85
_long_delay();
86
_long_delay();
87
_lcd_write_4bit(0x03);     // noch 8 Bit
88
_long_delay();
89
_lcd_write_4bit(0x03);     // noch 8 Bit
90
_long_delay();
91
_lcd_write_4bit(0x03);     // noch 8 Bit
92
_long_delay();
93
_lcd_write_4bit(0x02);     // jetzt 4 Bit
94
_long_delay();
95
96
_lcd_write_command(0x28);     // 4 Bit Zweizeilig
97
//-
98
_long_delay();
99
_long_delay();
100
_lcd_write_command(0x1C);     
101
_long_delay();
102
_lcd_write_command(0x74);     
103
_long_delay();
104
_lcd_write_command(0x52);     // 
105
_long_delay();
106
_lcd_write_command(0x69);     // Contrast 
107
108
// ----------------------------
109
//-
110
_long_delay();
111
_lcd_write_command(0x08);     // Display aus
112
_long_delay();
113
_lcd_write_command(0x01);     // Clear
114
_long_delay();
115
_lcd_write_command(0x06);     //Entry mode
116
_long_delay();
117
_lcd_write_command(0x08 + 4); // Display an
118
_long_delay();
119
}
120
121
122
void LCD_Gotoxy(unsigned char x , unsigned char y)
123
 {
124
  _short_delay();
125
  switch(y)
126
  { case 0 : _lcd_write_command(x + 0x80); break;
127
    case 1 : _lcd_write_command(x + 0xC0); break;
128
    case 2 : _lcd_write_command(x + (0x80 + 20)); break;
129
    case 3 : _lcd_write_command(x + (0xC0 + 20)); break;
130
  }
131
  
132
}
133
134
135
void LCD_Write(unsigned char *this_text)
136
{
137
 unsigned char i = 0;
138
  
139
 while(this_text[i] != 0) 
140
  {
141
   lcd_write_byte(this_text[i++]);
142
   _long_delay();
143
   
144
  }
145
    
146
  
147
}
148
149
150
char LCD_Putchar(char zeichen)
151
{
152
_short_delay();
153
 lcd_write_byte((char) zeichen);
154
 return(1);
155
}
156
void LCD_Puts(const char *s)
157
{
158
   register char c;
159
160
    while ( (c = *s++) ) {
161
        LCD_Putchar(c);
162
    }
163
}
164
165
166
167
int main()
168
{
169
LCD_Init();
170
LCD_Clear;
171
LCD_Gotoxy(0,0);
172
LCD_Puts("Hallo Welt");
173
for(;;)
174
{
175
}
176
return 0;
177
}

von Stefan B. (Gast)


Lesenswert?

> Und zwar: Es wird zwar der einzelne Buchstabe angezeigt, jedoch wird
> dieser von der angegebenen Position verschoben.

Wundert mich, weil du im angegebenen Programm nur eine Ausgabe eines 
Textes (Hallo Welt") machst und keinen einzelnen Buchstaben aus gibst.

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.