Subversion Repositories Code-Repo

Rev

Rev 248 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 248 Rev 251
Line 4... Line 4...
4
 
4
 
5
/*
5
/*
6
 * Writes the given byte to the register at the address
6
 * Writes the given byte to the register at the address
7
 */
7
 */
8
void WriteSPI(unsigned char addr, unsigned char data) {
8
void WriteSPI(unsigned char addr, unsigned char data) {
-
 
9
	halSPISetPolarityPhase(1,1);		// ADXL345 operates in SPI(1,1)
9
	ADXL_CS_LOW();
10
	ADXL_CS_LOW();
10
	spiSendByte(addr & ~ADXL_BYTE_WRITE);
11
	spiSendByte(addr & ~ADXL_BYTE_WRITE);
11
	spiSendByte(data);
12
	spiSendByte(data);
12
	ADXL_CS_HIGH();
13
	ADXL_CS_HIGH();
-
 
14
	halSPISetPolarityPhase(0,0);		// Revert back to SPI(0,0)
13
}
15
}
14
 
16
 
15
/*
17
/*
16
 * Reads the value of the register at the address
18
 * Reads the value of the register at the address
17
 */
19
 */
18
unsigned char ReadSPI(unsigned char addr) {
20
unsigned char ReadSPI(unsigned char addr) {
19
	unsigned char ret;
21
	unsigned char ret;
-
 
22
	halSPISetPolarityPhase(1,1);		// ADXL345 operates in SPI(1,1)
20
	ADXL_CS_LOW();
23
	ADXL_CS_LOW();
21
	spiSendByte(addr | ADXL_BYTE_READ);
24
	spiSendByte(addr | ADXL_BYTE_READ);
22
	spiReadFrame(&ret, 1);
25
	spiReadFrame(&ret, 1);
23
	ADXL_CS_HIGH();
26
	ADXL_CS_HIGH();
-
 
27
	halSPISetPolarityPhase(0,0);		// Revert back to SPI(0,0)
24
	return ret;
28
	return ret;
25
}
29
}
26
 
30
 
27
/*
31
/*
28
 * Reads a 16-bit value of the register at the address
32
 * Reads a 16-bit value of the register at the address
29
 */
33
 */
30
int Read16SPI(unsigned char addr) {
34
int Read16SPI(unsigned char addr) {
31
	unsigned char recv[2];
35
	unsigned char recv[2];
32
	int ret;
36
	int ret;
-
 
37
	halSPISetPolarityPhase(1,1);		// ADXL345 operates in SPI(1,1)
33
	ADXL_CS_LOW();
38
	ADXL_CS_LOW();
34
	spiSendByte(addr | ADXL_MULTI_BYTE_READ);
39
	spiSendByte(addr | ADXL_MULTI_BYTE_READ);
35
	spiReadFrame(recv, 2);
40
	spiReadFrame(recv, 2);
36
	ADXL_CS_HIGH();
41
	ADXL_CS_HIGH();
-
 
42
	halSPISetPolarityPhase(0,0);		// Revert back to SPI(0,0)
37
	ret = recv[0];
43
	ret = recv[0];
38
	ret |= recv[1] << 8;
44
	ret |= recv[1] << 8;
39
	return ret;
45
	return ret;
40
}
46
}
41
 
47
 
Line 80... Line 86...
80
/*
86
/*
81
 * Sets up and enables the tap interrupt
87
 * Sets up and enables the tap interrupt
82
 */
88
 */
83
void ADXLInitInterrupts(void) {
89
void ADXLInitInterrupts(void) {
84
	// Set tap acceleration threshold  (62.5mg/LSB, 0xFF=16g)
90
	// Set tap acceleration threshold  (62.5mg/LSB, 0xFF=16g)
85
	WriteSPI(ADXL345_REG_THRESH_TAP, 0x20);
91
	WriteSPI(ADXL345_REG_THRESH_TAP, 0x18);
86
 
92
 
87
	// Set maximum tap duration (625us/LSB)
93
	// Set maximum tap duration (625us/LSB)
88
	WriteSPI(ADXL345_REG_DUR, 0xFF);
94
	WriteSPI(ADXL345_REG_DUR, 0xFF);
89
 
95
 
90
	// Set minimum time for second tap (1.25ms/LSB)
96
	// Set minimum time for second tap (1.25ms/LSB)
Line 105... Line 111...
105
 
111
 
106
/*
112
/*
107
 * Clears any active interrupt flags
113
 * Clears any active interrupt flags
108
 */
114
 */
109
void ADXLClearInterrupts(void) {
115
void ADXLClearInterrupts(void) {
-
 
116
	ReadSPI(ADXL345_REG_ACT_TAP_STATUS);
110
	ReadSPI(ADXL345_REG_INT_SOURCE);
117
	ReadSPI(ADXL345_REG_INT_SOURCE);
111
}
118
}
112
 
119
 
113
/*
120
/*
114
 * Returns sensor data
121
 * Returns sensor data