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