-
Notifications
You must be signed in to change notification settings - Fork 0
/
HD44780.c
140 lines (116 loc) · 4.04 KB
/
HD44780.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <mega8.h>
//**********************************************************
//
// DieHard
// Àëüòåðíàòèâíàÿ áèáëèîòåêà LCD äëÿ HD44780 ÌÝËÒ
//
//*********************************************************
#include <delay.h>
#include <HD44780.h>
#define SetBit(x) |= (1<<x)
#define ClearBit(x) &=~(1<<x)
void lcd_puthex(unsigned char i)
{
char hi,lo;
hi=i&0xF0; // High nibble
hi=hi>>4;
hi+='0';
if (hi>'9')
hi+=7;
lo=(i&0x0F)+'0'; // Low nibble
if (lo>'9')
lo=lo+7;
lcd_putchar(hi); lcd_putchar(lo);
}
//Çàïèñü êîìàíä â HD44780 4 bit mode
void lcd(unsigned char cStr)
{
LCDPORT SetBit(LCD_E);
// Âûâîä ñòàðøåãî íèááëà
if( ((cStr&0x80)>>7) ) LCDPORT SetBit(LCD_DB7);
else LCDPORT ClearBit(LCD_DB7);
if( ((cStr&0x40)>>6) ) LCDPORT SetBit(LCD_DB6);
else LCDPORT ClearBit(LCD_DB6);
if( ((cStr&0x20)>>5) ) LCDPORT SetBit(LCD_DB5);
else LCDPORT ClearBit(LCD_DB5);
if( ((cStr&0x10)>>4) ) LCDPORT SetBit(LCD_DB4);
else LCDPORT ClearBit(LCD_DB4);
LCDPORT ClearBit(LCD_E); // Çàïèñü
delay_ms(1);
LCDPORT SetBit(LCD_E); // enable = 1
// Âûâîäèì ìëàäøåãî íèááëà
if( ((cStr&0x08)>>3) ) LCDPORT SetBit(LCD_DB7);
else LCDPORT ClearBit(LCD_DB7);
if( ((cStr&0x04)>>2) ) LCDPORT SetBit(LCD_DB6);
else LCDPORT ClearBit(LCD_DB6);
if( ((cStr&0x02)>>1) ) LCDPORT SetBit(LCD_DB5);
else LCDPORT ClearBit(LCD_DB5);
if( (cStr&0x01) ) LCDPORT SetBit(LCD_DB4);
else LCDPORT ClearBit(LCD_DB4);
LCDPORT ClearBit(LCD_E); // Çàïèñü
delay_ms(1);
}
// Çàïèñü êîìàíäû â HD44780
void lcd_cmd(unsigned char cStr)
{
LCDPORT ClearBit(LCD_RS); // Ðåæèì çàïèñè êîìàíä
lcd(cStr); // Çàïèñü
}
// Çàïèñü äàííûõ â HD44780
void lcd_putchar(unsigned char cStr)
{
LCDPORT SetBit(LCD_RS); // Ðåæèì çàïèñè êîìàíä
lcd(cStr); // Çàïèñü
}
// Çàïèñü êîìàíäû î÷èñòêè â HD44780
void lcd_clear(void)
{
lcd_cmd(0x01);
// delay_ms(1);
}
// Èíèöèàëèçàöèÿ ïîðòà è HD44780
// nPage - íîìåð ñòðàíèöû çíàêîãåíåðàòîðà 0 èëè 1 1-Ðóñ(MELT)
void lcd_init(char nPage)
{
LCDDDR=(1<<LCD_RS)|(1<<LCD_RD)|(1<<LCD_E)|(1<<LCD_DB4)|
(1<<LCD_DB5)|(1<<LCD_DB6)|(1<<LCD_DB7);
delay_ms(20); // Ïðåäâàðèòåëüíàÿ çàäåðæêà
lcd_cmd(0x33);
lcd_cmd(0x32);
if(nPage==0) lcd_cmd(0x28); // 4 áèòà, 2 ñòðîêè êîäîâàÿ ñòðàíèöà 0
else lcd_cmd(0x2A); // 4 áèòà, 2 ñòðîêè êîäîâàÿ ñòðàíèöà 1 äëÿ ÌÝËÒ-Ðóññêàÿ
// Ðóññêàÿ äëÿ MELT
lcd_cmd(0x08); // Âûêëþ÷åíèå ÆÊÈ
lcd_clear(); // Î÷èñòêà ÆÊÈ
lcd_cmd(0x06); // Ñäâèã êóðñîðà âïðàâî
lcd_cmd(0x0C); // Âêëþ÷åíèå áåç êóðñîðà
}
// Âûâîä ñòðîêè íà ýêðàí
void lcd_puts(unsigned char *pStr)
{
unsigned char i;
#pragma warn-
while (i=*pStr)
{
lcd_putchar(i);
pStr++;
}
#pragma warn+
}
// Âûâîä ñòðîêè èç Flash íà ýêðàí
void lcd_putsf(char flash *pStr)
{
unsigned char i;
#pragma warn-
while (i=*pStr)
{
lcd_putchar(i);
pStr++;
}
#pragma warn+
}
// Çàïèñü êîìàíäû ïåðåâîäà êóðñîðà â íóæíóþ ïîçèöèþ â HD44780
void lcd_gotoxy(unsigned char x, unsigned char y)
{
lcd_cmd((0x80+y*64)+x); // Ðàññ÷åò ïîçèöèè. 0õ80 - ëåâ. âåðõí. óãîë
}