Debian Codegraph @

kpy/mh-lcd-plus.c

raw symbols SCIP
1
2 /* lcd.c -- routines for handling a text mode LCD display. Not the best ones available.
3
4 Copyright (C) 2008 Ajith Kumar, Inter-University Accelerator Centre,
5 New Delhi and Pramode C.E, GnuVision.com.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11 */
12
13 #include <avr/io.h>
14 #include <stdint.h>
15
16 // LCD control bits of Port C on Phoenix MDK. Refer to the Schematic
17 #define ENBIT 0x20
18 #define RWBIT 0x40
19 #define RSBIT 0x80
20
21 void delay(uint16_t k)
22 {
23 volatile uint16_t x = k;
24 while(x)
25 --x;
26 }
27
28
29 uint8_t cpos = 0; // LCD cursor position
30
31 void lcd_command (uint8_t cmd)
32 {
33 PORTC &= 16; // Clear bits used by LCD except PC4
34 PORTC |= (cmd >>4 ) & 0x0F; // Put 4 MSBs, RS, RW & EN Low
35 PORTC |= ENBIT; PORTC &= ~ENBIT; // Pulse on EN pin
36 PORTC &= 16;
37 PORTC |= (cmd ) &0x0F; // Put 4 LSBs
38 PORTC |= ENBIT; PORTC &= ~ENBIT; // Pulse on EN pin
39 delay (10000);
40 }
41
42
43 void lcd_init (void) // This needs rewriting
44 {
45 delay(10000);
46 DDRC |= 239; // Except PC4 all are outputs
47 lcd_command (32 + 8 + 4); // 4 bit data mode
48 lcd_command (4 + 2); // Entry mode
49 lcd_command (8 + 4); // display ON, no cursor
50 lcd_command (1); // Clear
51 cpos = 0; // Set cursor position variable
52 delay(10000);
53 DDRC |= 239; // Except PC0 all are outputs
54 lcd_command (32 + 8 + 4); // 4 bit data mode
55 lcd_command (4 + 2); // Entry mode
56 lcd_command (8 + 4); // display ON, no cursor
57 lcd_command (1); // Clear
58 cpos = 0; // Set cursor position variable
59 }
60
61
62 void lcd_clear (void)
63 {
64 lcd_command(1);
65 }
66
67
68 void lcd_put_char (char c)
69 {
70 PORTC &= 16; // Clear bits used by LCD except PC4 0x08
71 PORTC |= RSBIT | ((c>>4) & 0x0F); // Put 4 MSBs, RS High, RW & EN Low
72 PORTC |= ENBIT; PORTC &= ~ENBIT; // Pulse on EN pin
73 PORTC &= 16;
74 PORTC |= RSBIT | (c&0x0F); // Put 4 LSBs
75 PORTC |= ENBIT; PORTC &= ~ENBIT; // Pulse on EN pin
76 delay(1000);
77 ++cpos; if(cpos == 20) // change to 20 to 8 for 1x16 character displayy
78 lcd_command(128 + 32 + 8); // 4 x 20 display
79 }
80
81
82 void lcd_put_string(char *p)
83 {
84 while(*p) {
85 lcd_put_char(*p);
86 ++p;
87 }
88 }
89
90 void lcd_put_byte(uint8_t i)
91 {
92 uint8_t pos100 = 0;
93
94 if(i/100) {
95 pos100 = 1;
96 lcd_put_char('0' + i/100);
97 i %= 100;
98 }
99
100 if( (i/10) || pos100) {
101 lcd_put_char('0' + i/10);
102 i %= 10;
103 }
104 lcd_put_char('0' + i);
105 }
106
107 void lcd_put_int(uint16_t val)
108 {
109 char ss[8];
110 uint8_t k;
111
112 if(val ==0) {
113 lcd_put_char('0');
114 return;
115 }
116
117 k = 0;
118 while(val > 0) {
119 ss[k++] = '0' + (val % 10);
120 val /= 10;
121 }
122 while(k) {
123 lcd_put_char(ss[k-1]);
124 --k;
125 }
126 }
127
128 void lcd_put_long(uint32_t val)
129 {
130 char ss[8];
131 uint8_t k;
132
133 if(val ==0) {
134 lcd_put_char('0');
135 return;
136 }
137
138 k = 0;
139 while(val > 0) {
140 ss[k++] = '0' + (val % 10);
141 val /= 10;
142 }
143 while(k) {
144 lcd_put_char(ss[k-1]);
145 --k;
146 }
147 }
148