This repository has been archived by the owner on Jan 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ldc.cpp
150 lines (133 loc) · 4.19 KB
/
ldc.cpp
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
#include <SPI.h>
#define LDC1000_CMD_REVID 0x00
#define LDC1000_CMD_RPMAX 0x01
#define LDC1000_CMD_RPMIN 0x02
#define LDC1000_CMD_SENSORFREQ 0x03
#define LDC1000_CMD_LDCCONFIG 0x04
#define LDC1000_CMD_CLKCONFIG 0x05
#define LDC1000_CMD_THRESHILSB 0x06
#define LDC1000_CMD_THRESHIMSB 0x07
#define LDC1000_CMD_THRESLOLSB 0x08
#define LDC1000_CMD_THRESLOMSB 0x09
#define LDC1000_CMD_INTCONFIG 0x0A
#define LDC1000_CMD_PWRCONFIG 0x0B
#define LDC1000_CMD_STATUS 0x20
#define LDC1000_CMD_PROXLSB 0x21
#define LDC1000_CMD_PROXMSB 0x22
#define LDC1000_CMD_FREQCTRLSB 0x23
#define LDC1000_CMD_FREQCTRMID 0x24
#define LDC1000_CMD_FREQCTRMSB 0x25
#define RPMAX 0x13 /**< maximum calibration value for RPMAX */
#define RPMIN 0x3A /**< minimum calibration value for RPMAX */
unsigned char proximtyData[2];
unsigned char frequencyData[3];
unsigned int proximtyDataMAX;
unsigned int frequencyDataMAX;
unsigned int proximtyDataTEMP;
unsigned int frequencyDataTEMP;
const int chipSelectPin = 53;
static byte txlen;
static byte rxlen;
static byte *txbuf;
static byte *rxbuf;
static byte txaddr;
static byte wordbuf;
void setup() {
byte orgVal[20];
int i;
// set the slaveSelectPin as an output:
pinMode (chipSelectPin, OUTPUT);
// initialize SPI:
Serial.begin(9600);
// start the SPI library:
SPI.begin();
SPI.begin();
SPI.setDataMode(SPI_MODE0);
SPI.setBitOrder(MSBFIRST);
pinMode(chipSelectPin, OUTPUT);
delay(100);
spi_writeByte(LDC1000_CMD_RPMAX, RPMAX);
spi_writeByte(LDC1000_CMD_RPMIN, RPMIN);
spi_writeByte(LDC1000_CMD_SENSORFREQ, 0x94);
spi_writeByte(LDC1000_CMD_LDCCONFIG, 0x17);
spi_writeByte(LDC1000_CMD_CLKCONFIG, 0x02);
spi_writeByte(LDC1000_CMD_INTCONFIG, 0x02);
spi_writeByte(LDC1000_CMD_THRESHILSB, 0x50);
spi_writeByte(LDC1000_CMD_THRESHIMSB, 0x14);
spi_writeByte(LDC1000_CMD_THRESLOLSB, 0xC0);
spi_writeByte(LDC1000_CMD_THRESLOMSB, 0x12);
spi_writeByte(LDC1000_CMD_PWRCONFIG, 0x01);
//read all registers
spi_readBytes(LDC1000_CMD_REVID, &orgVal[0],12);
for(i=0;i<12;i++)
{
Serial.print("orgVal=");
Serial.print(orgVal[i],HEX);
Serial.print("\n");
}
}
void loop() {
spi_readBytes(LDC1000_CMD_PROXLSB,&proximtyData[0],2);
spi_readBytes(LDC1000_CMD_FREQCTRLSB,&frequencyData[0],3);
/* Serial.print("pro[0]=");
Serial.print(proximtyData[0],DEC);
Serial.print(" ");
Serial.print("pro[1]=");
Serial.print(proximtyData[1],DEC);
Serial.print("\n");*/
proximtyDataTEMP = ((unsigned char)proximtyData[1]<<8) + proximtyData [0];
frequencyDataTEMP = ((unsigned char)frequencyData[1]<<8) + frequencyData[0];
Serial.print("proximty=");
Serial.print(proximtyDataTEMP,DEC);
Serial.print(" ");
Serial.print("proximty=");
Serial.print(proximtyDataTEMP,DEC);
Serial.print("\n");
}
byte spi_writeByte(byte addr, byte data)
{
wordbuf = data; // copy from stack to memory
txbuf = &wordbuf;
txaddr = addr & ~0x80;
digitalWrite(chipSelectPin,LOW); //P1OUT &= ~BIT0;
SPI.transfer(txaddr);//while (!(IFG2&UCA0TXIFG));
//UCA0TXBUF = txaddr;
//while (!(IFG2&UCA0TXIFG));
//UCA0TXBUF = *txbuf;
SPI.transfer(data);
digitalWrite(chipSelectPin,HIGH); //P1OUT |= BIT0;
return 0;
}
byte spi_readByte( byte addr, byte data)
{
//rxbuf = data;
txaddr = addr | 0x80;
digitalWrite(chipSelectPin,LOW);//P1OUT &= ~BIT0;
SPI.transfer(txaddr);//while (!(IFG2&UCA0TXIFG));
//UCA0TXBUF = txaddr;
//while (!(IFG2&UCA0TXIFG));
//UCA0TXBUF = 0;
//while (UCA0STAT & UCBUSY);
data = SPI.transfer(0x00);//* rxbuf = UCA0RXBUF;
//while (UCA0STAT & UCBUSY);
digitalWrite(chipSelectPin,HIGH);//P1OUT |= BIT0;
return 0;
}
byte spi_readBytes( byte addr, byte * buffer, byte len)
{
int rxlen;
rxlen = len;
rxbuf = buffer;
txaddr = addr | 0x80;
digitalWrite(chipSelectPin,LOW);//P1OUT &= ~BIT0;
//while (!(IFG2&UCA0TXIFG));
SPI.transfer(txaddr);//UCA0TXBUF = txaddr;
while (rxlen > 0) {
//SPI.transfer(0x00);
*rxbuf = SPI.transfer(0x00);
rxbuf++;
rxlen--;
}
digitalWrite(chipSelectPin,HIGH);
return 0;
}