sources from initial AVR turn-on for TeleTerra
[fw/altos] / avr / tmp / test_lcd.c
1 /*************************************************************************\r
2 Title:    testing output to a HD44780 based LCD display.\r
3 Author:   Peter Fleury  <pfleury@gmx.ch>  http://jump.to/fleury\r
4 File:     $Id: test_lcd.c,v 1.6 2004/12/10 13:53:59 peter Exp $\r
5 Software: AVR-GCC 3.3\r
6 Hardware: HD44780 compatible LCD text display\r
7           ATS90S8515/ATmega if memory-mapped LCD interface is used\r
8           any AVR with 7 free I/O pins if 4-bit IO port mode is used\r
9 **************************************************************************/\r
10 #include <stdlib.h>\r
11 #include <avr/io.h>\r
12 #include <avr/pgmspace.h>\r
13 #include "lcd.h"\r
14 \r
15 \r
16 /*\r
17 ** constant definitions\r
18 */\r
19 static const PROGMEM unsigned char copyRightChar[] =\r
20 {\r
21         0x07, 0x08, 0x13, 0x14, 0x14, 0x13, 0x08, 0x07,\r
22         0x00, 0x10, 0x08, 0x08, 0x08, 0x08, 0x10, 0x00\r
23 };\r
24 \r
25 \r
26 /*\r
27 ** function prototypes\r
28 */ \r
29 void wait_until_key_pressed(void);\r
30 \r
31 \r
32 void wait_until_key_pressed(void)\r
33 {\r
34     unsigned char temp1, temp2;\r
35     unsigned int i;\r
36     \r
37     do {\r
38         temp1 = PINC;                  // read input\r
39         for(i=0;i<65535;i++);\r
40         temp2 = PINC;                  // read input\r
41         temp1 = (temp1 & temp2);       // debounce input\r
42     } while ( temp1 & _BV(PINC4) );\r
43     \r
44     loop_until_bit_is_set(PINC,PINC4);            /* wait until key is released */\r
45 }\r
46 \r
47 \r
48 int main(void)\r
49 {\r
50     char buffer[7];\r
51     int  num=134;\r
52     unsigned char i;\r
53     \r
54     \r
55     DDRD &=~ (1 << PC4);        /* Pin PC4 input              */\r
56     PORTD |= (1 << PC4);        /* Pin PC4 pull-up enabled    */\r
57 \r
58 \r
59     /* initialize display, cursor off */\r
60     lcd_init(LCD_DISP_ON);\r
61 \r
62     for (;;) {                           /* loop forever */\r
63         /* \r
64          * Test 1:  write text to display\r
65          */\r
66 \r
67         /* clear display and home cursor */\r
68         lcd_clrscr();\r
69         \r
70         /* put string to display (line 1) with linefeed */\r
71         lcd_puts("LCD Test Line 1\n");\r
72 \r
73         /* cursor is now on second line, write second line */\r
74         lcd_puts("Line 2");\r
75         \r
76         /* move cursor to position 8 on line 2 */\r
77         lcd_gotoxy(7,1);  \r
78         \r
79         /* write single char to display */\r
80         lcd_putc(':');\r
81         \r
82         /* wait until push button PD2 (INT0) is pressed */\r
83         wait_until_key_pressed();\r
84         \r
85         \r
86         /*\r
87          * Test 2: use lcd_command() to turn on cursor\r
88          */\r
89         \r
90         /* turn on cursor */\r
91         lcd_command(LCD_DISP_ON_CURSOR);\r
92 \r
93         /* put string */\r
94         lcd_puts( "CurOn");\r
95         \r
96         /* wait until push button PD2 (INT0) is pressed */\r
97         wait_until_key_pressed();\r
98 \r
99 \r
100         /*\r
101          * Test 3: display shift\r
102          */\r
103         \r
104         lcd_clrscr();     /* clear display home cursor */\r
105 \r
106         /* put string from program memory to display */\r
107         lcd_puts_P( "Line 1 longer than 14 characters\n" );\r
108         lcd_puts_P( "Line 2 longer than 14 characters" );\r
109         \r
110         /* move BOTH lines one position to the left */\r
111         lcd_command(LCD_MOVE_DISP_LEFT);\r
112         \r
113         /* wait until push button PD2 (INT0) is pressed */\r
114         wait_until_key_pressed();\r
115 \r
116         /* turn off cursor */\r
117         lcd_command(LCD_DISP_ON);\r
118         \r
119         \r
120         /*\r
121          *   Test: Display integer values\r
122          */\r
123         \r
124         lcd_clrscr();   /* clear display home cursor */\r
125         \r
126         /* convert interger into string */\r
127         itoa( num , buffer, 10);\r
128         \r
129         /* put converted string to display */\r
130         lcd_puts(buffer);\r
131         \r
132         /* wait until push button PD2 (INT0) is pressed */\r
133         wait_until_key_pressed();\r
134         \r
135         \r
136         /*\r
137          *  Test: Display userdefined characters\r
138          */\r
139 \r
140        lcd_clrscr();   /* clear display home cursor */\r
141        \r
142        lcd_puts("Copyright: ");\r
143        \r
144        /*\r
145         * load two userdefined characters from program memory\r
146         * into LCD controller CG RAM location 0 and 1\r
147         */\r
148        lcd_command(_BV(LCD_CGRAM));  /* set CG RAM start address 0 */\r
149        for(i=0; i<16; i++)\r
150        {\r
151            lcd_data(pgm_read_byte_near(&copyRightChar[i]));\r
152        }\r
153        \r
154        /* move cursor to position 0 on line 2 */\r
155        /* Note: this switched back to DD RAM adresses */\r
156        lcd_gotoxy(0,1);\r
157        \r
158        /* display user defined (c), built using two user defined chars */\r
159        lcd_putc(0);\r
160        lcd_putc(1);\r
161        \r
162 \r
163        /* wait until push button PD2 (INT0) is pressed */\r
164        wait_until_key_pressed();\r
165               \r
166     }\r
167 }\r