-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPIC2XJ50_SPI.h
183 lines (124 loc) · 3.7 KB
/
PIC2XJ50_SPI.h
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* Microchip Technology Inc. and its subsidiaries. You may use this software
* and any derivatives exclusively with Microchip products.
*
* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
* PARTICULAR PURPOSE, OR ITS INTERACTION WITH MICROCHIP PRODUCTS, COMBINATION
* WITH ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION.
*
* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS
* IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF
* ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
*
* MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF THESE
* TERMS.
*/
/*
* File:
* Author: Daniel Andres Ibarguen Mosquera
* Comments:
* Revision history:
*/
// This is a guard condition so that contents of this file are not included
// more than once.
#ifndef XC_HEADER_TEMPLATE_H
#define XC_HEADER_TEMPLATE_H
#include <xc.h> // include processor files - each processor file is guarded.
/*SPI modes you can see how the SPI modes work here
https://en.wikipedia.org/wiki/Serial_Peripheral_Interface*/
enum SPI_MODES{
CPOL_0_CPHA_0,
CPOL_0_CPHA_1,
CPOL_1_CPHA_0,
CPOL_1_CPHA_1};
enum Spi_master_clock_modes{
SPI_MASTER_CLOCK_DIV_4,
SPI_MASTER_CLOCK_DIV_16,
SPI_MASTER_CLOCK_DIV_64,
SPI_MASTER_CLOCK_DI_TMR2_2
};
void Spi_init()
{
TRISCbits.TRISC7 = 0;//Pin SDO
TRISBbits.TRISB5 = 1;//Pin SDI
TRISBbits.TRISB4 = 0;//Pin SCL
SSPCON1bits.SSPEN = 1;//SPI Enable
}
void Spi_mode(enum SPI_MODES data)
{
if(data == CPOL_0_CPHA_0)
{
SSPCON1bits.CKP = 0;
SSPSTATbits.CKE = 0;
}
else if(data == CPOL_0_CPHA_1)
{
SSPCON1bits.CKP = 0;
SSPSTATbits.CKE = 1;
}
else if(data == CPOL_1_CPHA_0)
{
SSPCON1bits.CKP = 1;
SSPSTATbits.CKE = 0;
}
else if(data == CPOL_1_CPHA_1)
{
SSPCON1bits.CKP = 1;
SSPSTATbits.CKE = 1;
}
}
void Spi_clock_mode(enum Spi_master_clock_modes data)
{
if(data == SPI_MASTER_CLOCK_DIV_4)
{
SSPM01 = 0;
SSPM11 = 0;
SSPM21 = 0;
SSPM31 = 0;
}
if(data == SPI_MASTER_CLOCK_DIV_16)
{
SSPM01 = 1;
SSPM11 = 0;
SSPM21 = 0;
SSPM31 = 0;
}
if(data == SPI_MASTER_CLOCK_DIV_64)
{
SSPM01 = 0;
SSPM11 = 1;
SSPM21 = 0;
SSPM31 = 0;
}
if(data == SPI_MASTER_CLOCK_DI_TMR2_2)
{
SSPM01 = 1;
SSPM11 = 1;
SSPM21 = 0;
SSPM31 = 0;
}
}
void write_command(int command)
{
CCS = 0;
SSPBUF = command;
while(SSPSTATbits.BF == 0);
while(PIR1bits.SSPIF == 0);
PIR1bits.SSPIF = 0;
CCS = 1;
}
void write_data(int data)
{
CCS = 0;
SSPBUF = data;
while(SSPSTATbits.BF == 0);
while(PIR1bits.SSPIF == 0);
PIR1bits.SSPIF = 0;
CCS = 1;
}
#endif /* XC_HEADER_TEMPLATE_H */