1089 |
mija |
1 |
/* mija 2008 |
|
|
2 |
|
|
|
3 |
source file for lcd HITACHI 16x2,8x2,... |
|
|
4 |
|
|
|
5 |
!!! must be set PINs,PORTs,DDR in lcd.h |
|
|
6 |
|
|
|
7 |
ver.: 0.0 TESTED |
|
|
8 |
(source from lcd.c from (c)miho 2005) |
|
|
9 |
*/ |
|
|
10 |
|
|
|
11 |
|
|
|
12 |
#include <avr/io.h> |
|
|
13 |
#include <util/delay.h> |
|
|
14 |
#include <avr/pgmspace.h> |
|
|
15 |
#include "lcd.h" |
|
|
16 |
|
|
|
17 |
#include <stdio.h> |
|
|
18 |
|
|
|
19 |
//************************************************************* |
|
|
20 |
|
|
|
21 |
#define H_D0 PORT_D0 |=_BV(PIN_D0) |
|
|
22 |
#define L_D0 PORT_D0 &= ~(_BV(PIN_D0)) |
|
|
23 |
|
|
|
24 |
#define H_D1 PORT_D1 |=_BV(PIN_D1) |
|
|
25 |
#define L_D1 PORT_D1 &= ~(_BV(PIN_D1)) |
|
|
26 |
|
|
|
27 |
#define H_D2 PORT_D2 |=_BV(PIN_D2) |
|
|
28 |
#define L_D2 PORT_D2 &= ~(_BV(PIN_D2)) |
|
|
29 |
|
|
|
30 |
#define H_D3 PORT_D3 |=_BV(PIN_D3) |
|
|
31 |
#define L_D3 PORT_D3 &= ~(_BV(PIN_D3)) |
|
|
32 |
|
|
|
33 |
#define H_E PORT_E |=_BV(PIN_E) |
|
|
34 |
#define L_E PORT_E &= ~(_BV(PIN_E)) |
|
|
35 |
|
|
|
36 |
#define H_RS PORT_RS |=_BV(PIN_RS) |
|
|
37 |
#define L_RS PORT_RS &= ~(_BV(PIN_RS)) |
|
|
38 |
|
|
|
39 |
//************************************************************* |
|
|
40 |
|
|
|
41 |
// Definice konstant pro LCD display |
|
|
42 |
// |
|
|
43 |
#define LCD_CURSOR_ON_ 0x0E // kurzor jako blikajici radka pod znakem |
|
|
44 |
#define LCD_CURSOR_OFF_ 0x0C // zadny kurzor |
|
|
45 |
#define LCD_LINE_2 0x40 // adresa 1. znaku 2. radky |
|
|
46 |
|
|
|
47 |
|
|
|
48 |
// Definice rezimu LCD displeje |
|
|
49 |
// |
|
|
50 |
|
|
|
51 |
uint8_t PROGMEM LCD_INIT_STRING[4] = |
|
|
52 |
{ |
|
|
53 |
0x28, // intrfejs 4 bity, 2 radky, font 5x7 |
|
|
54 |
LCD_CURSOR_OFF_, // display on, kurzor off, |
|
|
55 |
0x01, // clear displeje |
|
|
56 |
0x06 // inkrement pozice kurzoru (posun kurzoru doprava) |
|
|
57 |
}; |
|
|
58 |
|
|
|
59 |
void LCD_init_IO_PIN(void) |
|
|
60 |
{ |
|
|
61 |
DDR_D0 |= _BV(PIN_D0); |
|
|
62 |
DDR_D1 |= _BV(PIN_D1); |
|
|
63 |
DDR_D2 |= _BV(PIN_D2); |
|
|
64 |
DDR_D3 |= _BV(PIN_D3); |
|
|
65 |
DDR_E |= _BV(PIN_E); |
|
|
66 |
DDR_RS |= _BV(PIN_RS); |
|
|
67 |
} |
|
|
68 |
|
|
|
69 |
void LCD_init(void) |
|
|
70 |
{ |
|
|
71 |
uint8_t a; |
|
|
72 |
|
|
|
73 |
LCD_init_IO_PIN(); |
|
|
74 |
_delay_ms(20); |
|
|
75 |
L_E; |
|
|
76 |
L_RS; |
|
|
77 |
for (a=0;a<3;a++) |
|
|
78 |
{ |
|
|
79 |
_delay_ms(2); |
|
|
80 |
LCD_send_nibble(0x3); |
|
|
81 |
|
|
|
82 |
} |
|
|
83 |
_delay_us(40); |
|
|
84 |
LCD_send_nibble(0x2); |
|
|
85 |
_delay_us(40); |
|
|
86 |
for (a=0;a<3;a++) |
|
|
87 |
{ |
|
|
88 |
LCD_send_command(pgm_read_byte(&(LCD_INIT_STRING[a]))); |
|
|
89 |
_delay_ms(2); |
|
|
90 |
} |
|
|
91 |
} |
|
|
92 |
|
|
|
93 |
void LCD_putc(uint8_t data) |
|
|
94 |
{ |
|
|
95 |
LCD_send_data(data); |
|
|
96 |
} |
|
|
97 |
|
|
|
98 |
void LCD_send_command(uint8_t data) |
|
|
99 |
{ |
|
|
100 |
L_RS; |
|
|
101 |
LCD_send(data); |
|
|
102 |
H_RS; |
|
|
103 |
} |
|
|
104 |
|
|
|
105 |
void LCD_send_data(uint8_t data) |
|
|
106 |
{ |
|
|
107 |
H_RS; |
|
|
108 |
LCD_send(data); |
|
|
109 |
} |
|
|
110 |
|
|
|
111 |
void LCD_send(uint8_t data) |
|
|
112 |
{ |
|
|
113 |
LCD_send_nibble(data >> 4); |
|
|
114 |
LCD_send_nibble(data); |
|
|
115 |
_delay_us(40); |
|
|
116 |
} |
|
|
117 |
|
|
|
118 |
void LCD_send_nibble(uint8_t data) |
|
|
119 |
{ |
|
|
120 |
L_D0;L_D1;L_D2;L_D3; |
|
|
121 |
if (data & 0x1) H_D0; |
|
|
122 |
if (data & 0x2) H_D1; |
|
|
123 |
if (data & 0x4) H_D2; |
|
|
124 |
if (data & 0x8) H_D3; |
|
|
125 |
H_E; |
|
|
126 |
_delay_us(1); |
|
|
127 |
L_E; |
|
|
128 |
} |
|
|
129 |
|
|
|
130 |
|
|
|
131 |
void LCD_clear(void) |
|
|
132 |
{ |
|
|
133 |
LCD_send_command(1); |
|
|
134 |
_delay_ms(2); |
|
|
135 |
} |
|
|
136 |
|
|
|
137 |
void LCD_gotoxy( uint8_t x, uint8_t y) |
|
|
138 |
{ |
|
|
139 |
uint8_t Adr; |
|
|
140 |
|
|
|
141 |
Adr=x-1; |
|
|
142 |
if(y==2) |
|
|
143 |
Adr+=LCD_LINE_2; |
|
|
144 |
|
|
|
145 |
LCD_send_command(0x80|Adr); |
|
|
146 |
} |