Debian Codegraph @

microhope/microhope-doc/prog/pmdk_lcd.c

raw
1
2 /* lcd.c -- routines for handling a text mode LCD display
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
15 // LCD control bits of Port C on Phoenix MDK. Refer to the Schematic
16 #define ENBIT 0x8
17 #define RWBIT 0x4
18 #define RSBIT 0x2
19
20 void delay(uint16_t k)
21 {
22 volatile uint16_t x = k;
23 while(x)
24 --x;
25 }
26
27
28 uint8_t cpos = 0; // LCD cursor position
29
30 void lcd_command (uint8_t cmd)
31 {
32 PORTC &= 1; // Clear bits used by LCD
33 PORTC |= (cmd & 0xF0); // Put 4 MSBs, RS, RW & EN Low
34 PORTC |= ENBIT; PORTC &= ~ENBIT; // Pulse on EN pin
35 PORTC &= 1;
36 PORTC |= (cmd << 4); // Put 4 LSBs
37 PORTC |= ENBIT; PORTC &= ~ENBIT; // Pulse on EN pin
38 delay (10000);
39 }
40
41
42 void lcd_init (void)
43 {
44 delay(10000);
45 DDRC |= 254; // Except PC0 all are outputs
46 lcd_command (32 + 8 + 4); // 4 bit data mode
47 lcd_command (4 + 2); // Entry mode
48 lcd_command (8 + 4); // display ON, no cursor
49 lcd_command (1); // Clear
50 cpos = 0; // Set cursor position variable
51 delay(10000);
52 }
53
54
55 void lcd_clear (void)
56 {
57 lcd_command(1);
58 }
59
60
61 void lcd_put_char (char c)
62 {
63 PORTC &= 1; // Clear bits used by LCD
64 PORTC |= RSBIT | (c & 0xF0); // Put 4 MSBs, RS High, RW & EN Low
65 PORTC |= ENBIT; PORTC &= ~ENBIT; // Pulse on EN pin
66 PORTC &= 1;
67 PORTC |= RSBIT | (c << 4); // Put 4 LSBs
68 PORTC |= ENBIT; PORTC &= ~ENBIT; // Pulse on EN pin
69 delay(1000);
70 ++cpos; if(cpos == 8)
71 lcd_command(128 + 32 + 8); // 1 x 16 display
72 }
73
74
75 void lcd_put_string(char *p)
76 {
77 while(*p) {
78 lcd_put_char(*p);
79 ++p;
80 }
81 }
82
83 void lcd_put_byte(uint8_t i)
84 {
85 uint8_t pos100 = 0;
86
87 if(i/100) {
88 pos100 = 1;
89 lcd_put_char('0' + i/100);
90 i %= 100;
91 }
92
93 if( (i/10) || pos100) {
94 lcd_put_char('0' + i/10);
95 i %= 10;
96 }
97 lcd_put_char('0' + i);
98 }
99
100 void lcd_put_int(uint16_t val)
101 {
102 char ss[8];
103 uint8_t k;
104
105 if(val ==0) {
106 lcd_put_char('0');
107 return;
108 }
109
110 k = 0;
111 while(val > 0) {
112 ss[k++] = '0' + (val % 10);
113 val /= 10;
114 }
115 while(k) {
116 lcd_put_char(ss[k-1]);
117 --k;
118 }
119 }