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