-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSPI_LCD.c
118 lines (92 loc) · 3.25 KB
/
SPI_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
/*------------------------------------------------------------------------------
* Simple LCD library for DSPIC33FJ32MC204
* LM044L alpha LCD via MCP23S17 SPI IO extension
* Samuel CIULLA - Microcontroleurs 2021
*----------------------------------------------------------------------------*/
#include <xc.h>
#include "SPI_LCD.h"
/*------------------------------------------------------------------------------
* Initialise SPI, MCP and LCD
*----------------------------------------------------------------------------*/
void Init_Alpha_LCD(void) {
TRISCbits.TRISC0 = 0; // Configure SPI pins
TRISCbits.TRISC2 = 0;
TRISCbits.TRISC3 = 0;
TRISCbits.TRISC4 = 1;
TRISCbits.TRISC5 = 0;
TRISCbits.TRISC6 = 0;
TRISCbits.TRISC7 = 0;
SPI_CS1 = 1;
Send_MCP(MCP_IODIRA, 0x00); // Configure MCP via SPI
Send_MCP(MCP_GPIOA, 0x00);
Send_MCP(MCP_IODIRB, 0x00);
Send_MCP(MCP_GPIOB, 0x00);
Send_Cmd_LCD(0x33); // Configure LCD via MCP
__delay_ms(10);
Send_Cmd_LCD(0x33);
__delay_ms(10);
Send_Cmd_LCD(0x38);
__delay_ms(10);
Send_Cmd_LCD(0x0C);
Send_Cmd_LCD(0x06);
Send_Cmd_LCD(LCD_CLEAR);
Send_Cmd_LCD(LCD_LINE_1);
return;
}
/*------------------------------------------------------------------------------
* Send command and related data to MCP via SPI_CS1
*----------------------------------------------------------------------------*/
void Send_MCP(char cmd, char dat) {
SPI_CS1 = 0;
IFS0bits.SPI1IF = 0;
SPI1BUF = 0x40;
while(!IFS0bits.SPI1IF);
IFS0bits.SPI1IF = 0;
SPI1BUF = cmd;
while(!IFS0bits.SPI1IF);
IFS0bits.SPI1IF = 0;
SPI1BUF = dat;
while(!IFS0bits.SPI1IF);
SPI_CS1 = 1;
return;
}
/*------------------------------------------------------------------------------
* Send a command to LCD
*----------------------------------------------------------------------------*/
void Send_Cmd_LCD(char cmd) {
Send_MCP(MCP_GPIOA, 0x00); // RS to 0
Send_MCP(MCP_GPIOB, cmd); // Send cmd
Send_MCP(MCP_GPIOA, 0x80); // EN to 1
Send_MCP(MCP_GPIOA, 0x00); // EN to 0
__delay_ms(4);
return;
}
/*------------------------------------------------------------------------------
* Send a character to LCD
*----------------------------------------------------------------------------*/
void Send_Chr_LCD(char dat) {
Send_MCP(MCP_GPIOA, 0x40); // RS to 1
Send_MCP(MCP_GPIOB, dat); // Send data
Send_MCP(MCP_GPIOA, 0xC0); // EN to 1
Send_MCP(MCP_GPIOA, 0x40); // EN to 0
__delay_us(10);
return;
}
/*------------------------------------------------------------------------------
* Display a string on line 1,2,3,4, at cursor otherwise
*----------------------------------------------------------------------------*/
void Send_Txt_LCD(char * txt, char lnr) {
switch (lnr) {
case 0: break;
case 1: Send_Cmd_LCD(LCD_LINE_1); break;
case 2: Send_Cmd_LCD(LCD_LINE_2); break;
case 3: Send_Cmd_LCD(LCD_LINE_3); break;
case 4: Send_Cmd_LCD(LCD_LINE_4); break;
default: break;
}
while(*txt) {
Send_Chr_LCD(*txt);
txt++;
}
return;
}