1 | #include <asf.h>
|
2 | #include "conf_board.h"
|
3 | #include "conf_clock.h"
|
4 |
|
5 | /**********************
|
6 | PA11 -----> RS
|
7 | PA12 -----> E
|
8 | GND -----> RW
|
9 | PA13 -----> DB4
|
10 | PA14 -----> DB5
|
11 | PA15 -----> DB6
|
12 | PA16 -----> DB7
|
13 | ***********************/
|
14 |
|
15 | #define LEDnaPD22 IOPORT_CREATE_PIN(PIOD, 22) //PPin na kterem je pripojena LED
|
16 |
|
17 | #define LCD_RS IOPORT_CREATE_PIN(PIOA, 11)
|
18 | #define LCD_EN IOPORT_CREATE_PIN(PIOA, 12)
|
19 | #define LCD_D4 IOPORT_CREATE_PIN(PIOA, 13)
|
20 | #define LCD_D5 IOPORT_CREATE_PIN(PIOA, 14)
|
21 | #define LCD_D6 IOPORT_CREATE_PIN(PIOA, 15)
|
22 | #define LCD_D7 IOPORT_CREATE_PIN(PIOA, 16)
|
23 |
|
24 | void lcd_strobe(void);
|
25 | void lcd_write(unsigned char c);
|
26 | void lcd_clear(void);
|
27 | void lcd_puts(const char * s);
|
28 | void lcd_putch(unsigned char c);
|
29 | void lcd_goto(unsigned char pos,unsigned char line);
|
30 | void lcd_init(void);
|
31 |
|
32 | void lcd_strobe(void)
|
33 | {
|
34 | ioport_set_pin_level(LCD_EN, 1);
|
35 | delay_us(1);
|
36 | ioport_set_pin_level(LCD_EN, 0);
|
37 | }
|
38 |
|
39 | void lcd_write(unsigned char c)
|
40 | {
|
41 | //upper
|
42 | if(c & 0x80) ioport_set_pin_level(LCD_D7, IOPORT_PIN_LEVEL_HIGH); else ioport_set_pin_level(LCD_D7, IOPORT_PIN_LEVEL_LOW);
|
43 | if(c & 0x40) ioport_set_pin_level(LCD_D6, IOPORT_PIN_LEVEL_HIGH); else ioport_set_pin_level(LCD_D6, IOPORT_PIN_LEVEL_LOW);
|
44 | if(c & 0x20) ioport_set_pin_level(LCD_D5, IOPORT_PIN_LEVEL_HIGH); else ioport_set_pin_level(LCD_D5, IOPORT_PIN_LEVEL_LOW);
|
45 | if(c & 0x10) ioport_set_pin_level(LCD_D4, IOPORT_PIN_LEVEL_HIGH); else ioport_set_pin_level(LCD_D4, IOPORT_PIN_LEVEL_LOW);
|
46 | lcd_strobe();
|
47 | //lowwer
|
48 | if(c & 0x08) ioport_set_pin_level(LCD_D7, IOPORT_PIN_LEVEL_HIGH); else ioport_set_pin_level(LCD_D7, IOPORT_PIN_LEVEL_LOW);
|
49 | if(c & 0x04) ioport_set_pin_level(LCD_D6, IOPORT_PIN_LEVEL_HIGH); else ioport_set_pin_level(LCD_D6, IOPORT_PIN_LEVEL_LOW);
|
50 | if(c & 0x02) ioport_set_pin_level(LCD_D5, IOPORT_PIN_LEVEL_HIGH); else ioport_set_pin_level(LCD_D5, IOPORT_PIN_LEVEL_LOW);
|
51 | if(c & 0x01) ioport_set_pin_level(LCD_D4, IOPORT_PIN_LEVEL_HIGH); else ioport_set_pin_level(LCD_D4, IOPORT_PIN_LEVEL_LOW);
|
52 | lcd_strobe();
|
53 | delay_us(40);
|
54 | }
|
55 |
|
56 | void lcd_clear(void)
|
57 | {
|
58 | ioport_set_pin_level(LCD_RS, IOPORT_PIN_LEVEL_LOW);
|
59 | lcd_write(0x1);
|
60 | delay_ms(2);
|
61 | }
|
62 |
|
63 | void lcd_puts(const char * s)
|
64 | {
|
65 | ioport_set_pin_level(LCD_RS, IOPORT_PIN_LEVEL_HIGH); // write characters
|
66 | while(*s) lcd_write(*s++);
|
67 | }
|
68 |
|
69 | void lcd_putch(unsigned char c)
|
70 | {
|
71 | ioport_set_pin_level(LCD_RS, IOPORT_PIN_LEVEL_HIGH); // write characters
|
72 | lcd_write(c);
|
73 | }
|
74 |
|
75 | void lcd_goto(unsigned char pos,unsigned char line)
|
76 | {
|
77 | ioport_set_pin_level(LCD_RS, IOPORT_PIN_LEVEL_LOW);
|
78 | if (line==0)
|
79 | lcd_write(0x80 + pos);
|
80 | else
|
81 | lcd_write(0x80 + pos+ 0x40);
|
82 | }
|
83 |
|
84 | void lcd_init(void)
|
85 | {
|
86 | ioport_set_pin_level(LCD_RS, IOPORT_PIN_LEVEL_LOW); // write control bytes
|
87 | delay_ms(15);// power on delay
|
88 | ioport_set_pin_level(LCD_D4, IOPORT_PIN_LEVEL_HIGH); // init
|
89 | ioport_set_pin_level(LCD_D5, IOPORT_PIN_LEVEL_HIGH); //
|
90 | lcd_strobe();
|
91 | delay_ms(5);
|
92 | lcd_strobe(); // init
|
93 | delay_us(100);
|
94 | lcd_strobe(); // init
|
95 | delay_ms(5);
|
96 | ioport_set_pin_level(LCD_D4, IOPORT_PIN_LEVEL_LOW); // set 4 bit mode
|
97 | lcd_strobe();
|
98 | delay_us(40);
|
99 |
|
100 | lcd_write(0x28);// 4 bit mode, 1/16 duty, 5x8 font, 2lines
|
101 | lcd_write(0x0C);// display on
|
102 | lcd_write(0x06);// entry mode advance cursor
|
103 | lcd_write(0x01);// clear display and reset cursor
|
104 | }
|
105 |
|
106 | int main (void)
|
107 | {
|
108 | sysclk_init();
|
109 | board_init();
|
110 | ioport_init();
|
111 |
|
112 | ioport_set_pin_dir(LEDnaPD22, IOPORT_DIR_OUTPUT); //PIN PD18 jako vystupni
|
113 |
|
114 | ioport_set_pin_dir(LCD_RS, IOPORT_DIR_OUTPUT); //PIN PD18 jako vystupni
|
115 | ioport_set_pin_dir(LCD_EN, IOPORT_DIR_OUTPUT); //PIN PD18 jako vystupni
|
116 | ioport_set_pin_dir(LCD_D4, IOPORT_DIR_OUTPUT); //PIN PD18 jako vystupni
|
117 | ioport_set_pin_dir(LCD_D5, IOPORT_DIR_OUTPUT); //PIN PD18 jako vystupni
|
118 | ioport_set_pin_dir(LCD_D6, IOPORT_DIR_OUTPUT); //PIN PD18 jako vystupni
|
119 | ioport_set_pin_dir(LCD_D7, IOPORT_DIR_OUTPUT); //PIN PD18 jako vystupni
|
120 |
|
121 | ioport_set_pin_level(LEDnaPD22, IOPORT_PIN_LEVEL_LOW); //inicializuje jako NULA
|
122 |
|
123 | lcd_init();
|
124 |
|
125 | while (1)
|
126 | {
|
127 | delay_ms(250);
|
128 | ioport_toggle_pin_level(LEDnaPD22);
|
129 |
|
130 | lcd_clear();
|
131 | lcd_goto(0,0);
|
132 | lcd_puts("Hello") ;
|
133 | lcd_goto(5,1);
|
134 | lcd_puts("jelito") ;
|
135 | }
|
136 | }
|