-
Notifications
You must be signed in to change notification settings - Fork 15
/
lcd.c
150 lines (114 loc) · 2.71 KB
/
lcd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* File: lcd.c
*
* Copyright (c) 2015 Manolis Agkopian
* See the file LICENSE for copying permission.
*
* LCD library source file.
*/
#define _XTAL_FREQ 8000000
#include <stdbool.h>
#include <xc.h>
#include "lcd.h"
LCD lcd;
void LCD_Out ( char c ) {
if ( c & 1 ){
*(lcd.PORT) |= 1 << lcd.D4;
}
else {
*(lcd.PORT) &= ~(1 << lcd.D4);
}
if ( c & 2 ) {
*(lcd.PORT) |= 1 << lcd.D5;
}
else {
*(lcd.PORT) &= ~(1 << lcd.D5);
}
if ( c & 4 ) {
*(lcd.PORT) |= 1 << lcd.D6;
}
else {
*(lcd.PORT) &= ~(1 << lcd.D6);
}
if ( c & 8 ) {
*(lcd.PORT) |= 1 << lcd.D7;
}
else {
*(lcd.PORT) &= ~(1 << lcd.D7);
}
}
void LCD_Write ( unsigned char c ) {
*(lcd.PORT) &= ~(1 << lcd.RS); // => RS = 0
LCD_Out(c);
*(lcd.PORT) |= 1 << lcd.EN; // => E = 1
__delay_ms(4);
*(lcd.PORT) &= ~(1 << lcd.EN); // => E = 0
}
bool LCD_Init ( LCD display ) {
lcd = display;
/*
* TODO:
* The function should clear only the appropriate bits, not the whole PORT
*
* TODO:
* "#if defined" needs to support more microcontrollers that have PORTD and PORTE
*/
if ( lcd.PORT == &PORTA ) {
TRISA = 0x00;
}
else if ( lcd.PORT == &PORTB ) {
TRISB = 0x00;
}
else if ( lcd.PORT == &PORTC ) {
TRISC = 0x00;
}
#if defined(_16F877) || defined(_16F877A)
else if ( lcd.PORT == &PORTD ) {
TRISD = 0x00;
}
else if ( lcd.PORT == &PORTE ) {
TRISE = 0x00;
}
#endif
else {
return false;
}
// Give some time to the LCD to start function properly
__delay_ms(20);
// Send reset signal to the LCD
LCD_Write(0x03);
__delay_ms(5);
LCD_Write(0x03);
__delay_ms(16);
LCD_Write(0x03);
// Specify the data lenght to 4 bits
LCD_Write(0x02);
// Set interface data length to 8 bits, number of display lines to 2 and font to 5x8 dots
LCD_Cmd(0x28);
// Set cursor to move from left to right
LCD_Cmd(0x06);
LCD_Display(true, false, false); // Turn on display and set cursor off
LCD_Clear();
return true;
}
void LCD_putc ( char c ) {
*(lcd.PORT) |= 1 << lcd.RS; // => RS = 1
LCD_Out((c & 0xF0) >> 4); //Data transfer
*(lcd.PORT) |= 1 << lcd.EN;
__delay_us(40);
*(lcd.PORT) &= ~(1 << lcd.EN);
LCD_Out(c & 0x0F);
*(lcd.PORT) |= 1 << lcd.EN;
__delay_us(40);
*(lcd.PORT) &= ~(1 << lcd.EN);
}
void LCD_puts ( char *a ) {
for ( int i = 0; a[i] != '\0'; ++i ) {
LCD_putc(a[i]);
}
}
void LCD_putrs ( const char *a ) {
for ( int i = 0; a[i] != '\0'; ++i ) {
LCD_putc(a[i]);
}
}