-
Notifications
You must be signed in to change notification settings - Fork 20
/
softwareSPI.c
73 lines (63 loc) · 1.8 KB
/
softwareSPI.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
/*
Software SPI library functions - currently only works in mode 1 used for 25AA** EEPROM
*/
#include <xc.h>
#include "softwareSPI.h"
unsigned char writeSPIByte(unsigned char transmit){
unsigned char received = 0;
unsigned char bits = 8;
unsigned char tmpbit = 0;
SCK = 0;
//make the transmission
unsigned char mask = 0x80; //Initialize to write and read bit 7
unsigned char ret = 0; //Initialize read byte with 0
do{
//Clock out current bit onto SPI Out line
if (transmit & mask)
MOSI = 1;
else
MOSI = 0;
SCK = 1; //Set SPI Clock line
if (MISO)
ret |= mask; //Read current bit fromSPI In line
__delay_us(1); //Ensure minimum delay of 500nS between SPI Clock high and SPI Clock Low
SCK = 0; //Set SPI Clock line
mask = mask >> 1; //Shift mask so that next bit is written and read from SPI lines
__delay_us(1); //Ensure minimum delay of 1000ns between bits
}while (mask != 0);
return ret;
}
int writeSPIWord(unsigned short int setting)
{
int data;
unsigned char b1, b2;
b1 = writeSPIByte(setting >> 8);
b2 = writeSPIByte(setting);
data = b1 << 8 | b2;
__delay_us(50);
return data;
}
int writeSPI24bits(unsigned short long setting)
{
unsigned short long data;
unsigned char b1, b2, b3;
b1 = writeSPIByte(setting >> (unsigned short long)16);
b2 = writeSPIByte(setting >> 8);
b3 = writeSPIByte(setting);
data = (unsigned short long)b1 << (unsigned short long)16 | b2 << 8 | b3;
__delay_us(50);
return data;
}
int readSPIWord(){
int data;
unsigned char b1, b2;
b1 = writeSPIByte(0x00);
b2 = writeSPIByte(0x00);
data = b1 << 8 | b2;
return data;
}
unsigned char readSPIByte(){
unsigned char data;
data = writeSPIByte(0x00);
return data;
}