-
Notifications
You must be signed in to change notification settings - Fork 2
/
ADE7763LL.ino
105 lines (79 loc) · 2.44 KB
/
ADE7763LL.ino
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
/**********************************************************************
*
* Low Level routines for ADE7763
*
*
*********************************************************************/
#include "Pins.h"
#include "ADE7763Reg.h"
#include "ADE7763.h"
#include "ADE7763LL.h"
/**************************************************************************
* ADE_read
*
* This routine enables the chip select and read the selected number
* of bytes from the ADE.
*
* ADE is big endian and the Arduino is little. Therefore items are
* assembled in revese order.
*************************************************************************/
void ADE_read (unsigned char addr,
unsigned char * data,
unsigned char count)
{
unsigned char i;
/* If a 3 byte read, zero out the MSB of the 4 byte data */
if (count == 3) {
//*((unsigned long *)(data+4-1)) = 0;
}
/* Select the chip */
digitalWrite(PIN_ADE_CS, LOW);
delayMicroseconds(10);
/* Point to the Arduino MSB */
data += (count-1);
/* Write the address to access */
SPI.transfer(addr);
/* Must wait 4 us for data to become valid */
delayMicroseconds(4);
// Do for each byte in transfer
for (i=0; i<count; i++)
{
/* Transer the byte */
*data = SPI.transfer (0x00);
data--;
}
/* Deselect the chip */
digitalWrite(PIN_ADE_CS, HIGH);
delayMicroseconds(10);
} /* end ADE_read */
/**************************************************************************
* ADE_write
* This routine enables chip select and reads the selected number of
* bytes from the ADE
*
* ADE is big endian and the Arduino is little. Therefore items are
* assembled in revese order.
*************************************************************************/
void ADE_write(unsigned char addr,
unsigned char * data,
unsigned char count)
{
unsigned char i;
/* Select the chip */
digitalWrite(PIN_ADE_CS, LOW);
delayMicroseconds(10);
/* Point to the Arduino MSB */
data += (count-1);
/* Write the address to access */
SPI.transfer(addr | ADE_WRITE_FLAG);
// Do for each byte */
for (i=0; i<count; i++)
{
/* Transer the byte */
SPI.transfer(*data); // write all the bytes
data--;
}
/* Deselect chip */
digitalWrite(PIN_ADE_CS, HIGH);
delayMicroseconds(10);
} /* end ADE_write */