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