-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPIC24H.c
128 lines (104 loc) · 4.26 KB
/
PIC24H.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
/**
\file PIC24H.c
\brief Functions relating to Microchip PIC24H (16-bit).
For use with xc16 compiler.
\author Andy Gock
*/
/*
Copyright (c) 2013, Andy Gock
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Andy Gock nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL ANDY GOCK BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "glcd.h"
#if defined(GLCD_DEVICE_USER)
#include "glcd_user_config.h"
#include "glcd_user_config.c"
#else
#include "PIC24H.h"
void glcd_init(void)
{
/* Set up remappable outputs for PIC24H, SPI: DO and SCK */
OSCCONbits.IOLOCK = 0;
REGISTER_MAP_SPI_DO = 0b00111; /* Set as SPI DO */
REGISTER_MAP_SPI_SCK = 0b01000; /* Set as SCK */
OSCCONbits.IOLOCK = 1;
/* Set DO and SCK pins as output */
CONTROLLER_MOSI_TRIS = 0;
CONTROLLER_SCK_TRIS = 0;
/* If the pins is also an ADC port, set these as digital mode */
// Do we really need to do this, or will setting RPxR deal with this
//_PCFG9 = 1;
//_PCFG10 = 1;
/* Set SS, DC and RST pins as output */
CONTROLLER_SS_TRIS = 0;
CONTROLLER_DC_TRIS = 0;
CONTROLLER_RST_TRIS = 0;
/* Deselect LCD */
GLCD_DESELECT();
/* SPI setup based on sample code from datasheet */
/* The following code shows the SPI register configuration for Master mode */
IFS0bits.SPI1IF = 0; /* Clear the Interrupt Flag */
IEC0bits.SPI1IE = 0; /* Disable the Interrupt */
/* SPI1CON1 Register Settings */
SPI1CON1bits.DISSCK = 0; /* Internal Serial Clock is Enabled */
SPI1CON1bits.DISSDO = 0; /* SDOx pin is controlled by the module */
SPI1CON1bits.MODE16 = 0; /* Communication is word-wide (16 bits) */
SPI1CON1bits.SMP = 0; /* Input data is sampled at the middle of data */
SPI1CON1bits.CKE = 0; /* Serial output data changes on transition */
SPI1CON1bits.CKP = 0; /* Idle state for clock is a low level */
SPI1CON1bits.PPRE = 0b11; /* Primary prescale = 1:1 */
SPI1CON1bits.SPRE = 0b001; /* Secondary prescale = 4:1 */
SPI1CON1bits.MSTEN = 1; /* Master mode Enabled */
SPI1STATbits.SPIEN = 1; /* Enable SPI module */
/* Interrupt Controller Settings */
// we're not using interrupts to handle SPI
//IFS0bits.SPI1IF = 0; /* Clear the Interrupt Flag */
//IEC0bits.SPI1IE = 1; /* Enable the Interrupt */
/* Send reset pulse to LCD */
glcd_reset();
/* Initialise the display */
glcd_PCD8544_init();
/* Select screen buffer */
glcd_select_screen(glcd_buffer,&glcd_bbox);
/* Clear screen, we are now ready to go */
glcd_clear();
}
void glcd_spi_write(uint8_t c)
{
GLCD_SELECT();
while (SPI1STATbits.SPITBF); /* Loop until TX buffer is empty */
SPI1BUF = (c & 0x00FF); /* Write data to be transmitted */
while(!SPI1STATbits.SPIRBF); /* Wait until TX finished */
uint8_t read;
read = SPI1BUF; /* Throw away the data (not sure if this is neccesary, to force reading of SPI1BUF) */
GLCD_DESELECT();
}
void glcd_reset(void)
{
/* Toggle RST low to reset. Minimum pulse 100ns on datasheet. */
GLCD_SELECT();
GLCD_RESET_LOW();
__delay_ms(GLCD_RESET_TIME);
GLCD_RESET_HIGH();
GLCD_DESELECT();
}
#endif /* GLCD_DEVICE_USER */