1 | #include <lpc21xx.h>
|
2 |
|
3 | #define buzz 1<<25 /*lcd rs pin*/
|
4 |
|
5 | #define pin_rs 1<<21 /*lcd rs pin*/
|
6 | #define pin_rw 1<<23 /*lcd rw pin*/
|
7 | #define pin_en 1<<20 /*lcd en pin*/
|
8 |
|
9 | #define pin_ctrl pin_rs|pin_rw|pin_en /*lcd control pins. pin_ctrl include all 4 [pin_rs, pin_rw, pin_en]*/
|
10 |
|
11 | #define lcd_d4 1<<16 /*lcd data pins D4,D5,D6,D7*/
|
12 | #define lcd_d5 1<<17
|
13 | #define lcd_d6 1<<18
|
14 | #define lcd_d7 1<<19
|
15 |
|
16 | #define lcd_data lcd_d4|lcd_d5|lcd_d6|lcd_d7 /*lcd_data include all 4 [lcd_d4, lcd_d5, lcd_d6, lcd_d7]*/
|
17 |
|
18 | #define lcd_rs(x) ((x)?(IOSET1 = pin_rs):(IOCLR1 = pin_rs)); /*lcd_rs(x) if x=1 will set the lcd_rs pin will clear if x=0*/
|
19 | #define lcd_rw(x) ((x)?(IOSET1 = pin_rw):(IOCLR1 = pin_rw)); /*lcd_rw(x) if x=1 will set the lcd_rs pin will clear if x=0*/
|
20 | #define lcd_en(x) ((x)?(IOSET1 = pin_en):(IOCLR1 = pin_en)); /*lcd_en(x) if x=1 will set the lcd_rs pin will clear if x=0*/
|
21 |
|
22 | #define data_in ((IOPIN0>>10)&(0x0F)) /*direction of the data pins as input*/
|
23 | #define data_out(x) IOCLR0 = lcd_data; IOSET0 = (x & 0x0F)<<10; /*direction of the data pins and control pins as output*/
|
24 |
|
25 | #define lcd_all_dir_out IODIR0 |= lcd_data ; IODIR1 |= pin_ctrl; /*setting as output for both data and control pins*/
|
26 |
|
27 | #define lcd_dir_data_out IODIR0 |= lcd_data /*set*/
|
28 | #define lcd_dir_data_in IODIR0 &= ~(lcd_data) /*clear*/
|
29 |
|
30 | #define lcd_clear lcd_cmd_write(0x01);
|
31 |
|
32 |
|
33 |
|
34 | static void delay(int);
|
35 |
|
36 |
|
37 | static void delay(int cnt)
|
38 | {
|
39 | cnt <<= 4;
|
40 | while(cnt--); //delay
|
41 | }
|
42 |
|
43 | static unsigned char busy_wait()
|
44 | {
|
45 | unsigned char (status);
|
46 | do
|
47 | {
|
48 | lcd_dir_data_in;//set pins as inputs
|
49 | lcd_rs(0); // rs=0 indecates that data is being passed
|
50 | lcd_rw(1); // rw=0 indicates that Read function is enabled
|
51 | lcd_en(1); //enabeled
|
52 | delay(10);
|
53 | status = (data_in << 4)&(0xF0); //data_in shift by 4 AND 0xF0
|
54 | lcd_en(0); //disabled
|
55 | delay(10);
|
56 | lcd_en(1); //enabeled
|
57 | delay(10);
|
58 | status |= data_in; //bit wise OR
|
59 | lcd_en(0); //disabled
|
60 | lcd_dir_data_out; //set pins as outputs
|
61 | delay(10);
|
62 | }
|
63 | while(status & 0x80); //bit wise AND
|
64 | return (status);
|
65 | }
|
66 |
|
67 | void lcd_write_4bit(unsigned char c)
|
68 | {
|
69 | lcd_rw(0); // rw=0 indicates that Read function is enabled
|
70 | lcd_en(1); // enables the lcd
|
71 | data_out(c & 0x0F);//bit wise AND
|
72 | data_out(c);
|
73 | delay(10);
|
74 | lcd_en(0);
|
75 | delay(10);
|
76 | }
|
77 |
|
78 | static void lcd_cmd_write(unsigned char c)
|
79 | {
|
80 | busy_wait();
|
81 | lcd_rs(0); // 0 indecates that data is being passed
|
82 | delay(10);
|
83 | lcd_write_4bit(c >> 4);// c is shifted to the right by 4 and passed to lcd_write_4bit()
|
84 | lcd_write_4bit(c); // c is passed to lcd_write_4bit()
|
85 | }
|
86 |
|
87 | static void lcd_data_write(unsigned char c)
|
88 | {
|
89 | busy_wait(); //
|
90 | lcd_rs(1); // 1 indecates that a command is being passed
|
91 | delay(10);
|
92 | lcd_write_4bit(c >> 4);// c is shifted to the right by 4 and passed to lcd_write_4bit()
|
93 | lcd_write_4bit(c); // c is passed to lcd_write_4bit()
|
94 | }
|
95 |
|
96 | static void lcd_init()
|
97 | {
|
98 | lcd_all_dir_out; //set pins as outputs
|
99 | delay(10);
|
100 | lcd_rs(0); // 0 indecates that data is being passed
|
101 | lcd_write_4bit(0x3);
|
102 | delay(10);
|
103 | lcd_write_4bit(0x3);
|
104 | delay(10);
|
105 | lcd_write_4bit(0x3);
|
106 | lcd_write_4bit(0x2);
|
107 |
|
108 | lcd_cmd_write(0x28); // 1 01000 = 1[(0)DL (1)N (0)F (0)* (0)*] Sets interface data length (DL) number of display lines (L) and character font(F).
|
109 | lcd_cmd_write(0x0C); // 1 100 = 1[(1)Disp (0)Cursor (0)Blink] display ON curser OFF blink OFF
|
110 | lcd_cmd_write(0x06); // courser auto increment
|
111 | }
|
112 |
|
113 | void lcd_print(unsigned char const *mess)
|
114 | {
|
115 | while(*mess)
|
116 | {
|
117 | lcd_data_write(*mess++);//repeats this function for every constant in string mess
|
118 | }
|
119 | }
|
120 |
|
121 |
|
122 | int main(void)
|
123 | {
|
124 | PINSEL2 &= 0x0;//SET IO PORT FUNCTIONALITY
|
125 | IODIR1 = 0xFFFF; //set all as outpuT
|
126 |
|
127 | delay(1000);
|
128 |
|
129 | lcd_init(); //initilize lcd
|
130 | lcd_clear; //clears lcd
|
131 |
|
132 | lcd_print("test"); //used for testing
|
133 | delay(1000);
|
134 |
|
135 |
|
136 | while(1);
|
137 |
|
138 | }
|