-
Notifications
You must be signed in to change notification settings - Fork 0
/
LCD20x04.c
151 lines (120 loc) · 3.27 KB
/
LCD20x04.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
151
#include "stdint.h"
#include "delay.h"
#include "driverlib/gpio.h"
#include "inc/hw_memmap.h"
#include "driverlib/sysctl.h"
#include "LCD20x04.h"
#include "driverlib/rom_map.h"
// LCD connected to PC4-PC7 for DB4-DB7 respectively
// LCD RS, R/W connected to PA2, PA3 respectively
// EN pin is PA5
#define F 0<<2 //5x8 dots display
#define N 1<<3 // 2 lines display
#define LCD_DATA_PINS 0XF0
#define RS 1u<<2
//#define RW 1u<<3
#define EN 1u<<3
#define DCB 0x04 // display on ,cursor on, blinking on
#define ID 0x02 // increment right
#define S 0x00 // shift off
static void SendCommand( uint8_t command_id )
{
// send higher nibble
MAP_GPIOPinWrite( GPIO_PORTA_BASE, RS , 0x00 );
MAP_GPIOPinWrite( GPIO_PORTA_BASE, EN, 0xff );
MAP_GPIOPinWrite( GPIO_PORTC_BASE, LCD_DATA_PINS, command_id );
delay_us(2);
MAP_GPIOPinWrite( GPIO_PORTA_BASE, EN, 0);
delay_us(2);
// send lower nibble
MAP_GPIOPinWrite( GPIO_PORTA_BASE, RS , 0x00 );
MAP_GPIOPinWrite( GPIO_PORTA_BASE, EN, 0xff );
MAP_GPIOPinWrite( GPIO_PORTC_BASE, LCD_DATA_PINS, (command_id<<4) );
delay_us(2);
MAP_GPIOPinWrite( GPIO_PORTA_BASE, EN, 0);
delay_us(100);
}
static void SendData( uint8_t data)
{
// send highier nibble
MAP_GPIOPinWrite( GPIO_PORTA_BASE, RS, 0xff );
MAP_GPIOPinWrite( GPIO_PORTA_BASE, EN, 0xff );
MAP_GPIOPinWrite( GPIO_PORTC_BASE, LCD_DATA_PINS, data );
delay_us(2);
MAP_GPIOPinWrite( GPIO_PORTA_BASE, EN, 0);
delay_us(2);
// send lower nibble
MAP_GPIOPinWrite( GPIO_PORTA_BASE, RS, 0xff );
MAP_GPIOPinWrite( GPIO_PORTA_BASE, EN, 0xff );
MAP_GPIOPinWrite( GPIO_PORTC_BASE, LCD_DATA_PINS, (data<<4) );
delay_us(2);
MAP_GPIOPinWrite( GPIO_PORTA_BASE, EN, 0);
delay_us(100);
}
void LCD_Init(void)
{
Timer1_Init();
//initialize gpio PC4-PC7 for output
MAP_SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOC );
MAP_GPIOPinTypeGPIOOutput( GPIO_PORTC_BASE, LCD_DATA_PINS );
//initialize gpio PA2, PA3 and PA5 for output
MAP_SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOA );
MAP_GPIOPinTypeGPIOOutput( GPIO_PORTA_BASE, RS | EN );
MAP_GPIOPinWrite( GPIO_PORTA_BASE, RS | EN, 0x00 );
//Start initialization start
//delay 40ms after power up
delay_ms(100);
//function set
SendCommand(0x03);
//function set
SendCommand( 0x20 | N | F );
//function set
SendCommand( 0x20 | N | F );
// Display, Cursor, Blinking
SendCommand( 1<<3 | DCB );
//clear display
SendCommand( 0x01 );
delay_ms(2);
//entry mode
SendCommand( 1<<2 | ID | S );
LCD_GoToXy(0,0);
}
void LCD_Clear(void)
{
SendCommand( 0x01 );
delay_ms(2);
}
void LCD_GoToXy(uint8_t x, uint8_t y)
{
//move cursor according to x & y
// if line one is chosen addresses span from 00h to 0Fh
switch(y)
{
case 0: {SendCommand( 0x80 | x ); break;}
case 1: {SendCommand( 0x80 | ( 0x40 + x ) ); break;}
case 2: {SendCommand( 0x80 | ( 0x14 + x ) ); break;}
case 3: SendCommand( 0x80 | ( 0x54 + x ) );
}
}
void LCD_DispChar(char letter)
{
SendData( letter );
}
void LCD_DispString(char *StrPtr)
{
while( *StrPtr != '\0' )
{
SendData( *StrPtr );
StrPtr ++;
}
}
void LCD_DispCursor(void)
{
// Display, Cursor, Blinking
SendCommand( 1<<3 | 0x07 );
}
void LCD_HideCursor(void)
{
// Display, Cursor, Blinking
SendCommand( 1<<3 | 0x04 );
}