-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPEFM.cpp
More file actions
111 lines (93 loc) · 2.33 KB
/
Copy pathPEFM.cpp
File metadata and controls
111 lines (93 loc) · 2.33 KB
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
#include <PEFM.h>
bool PEFM::setAddress(int a0, int a1, int a2, int a3, int mode){
if(a0 == 1){
addr = addr | 1;
}
if(a1 == 1){
addr = addr | 2;
}
if(a2 == 1){
addr = addr | 4;
}
if(a3 == 1){
addr = addr | 8;
}
if (!Wire.isEnabled()) {
Wire.begin();
}
Wire.beginTransmission(addr);
Wire.write(4);
Wire.write(mode);
byte status = Wire.endTransmission();
if(status != 0){
return false;
}
Wire.beginTransmission(addr); //Start communication
Wire.write(4); // Command header
Wire.endTransmission(); // Complete Transmission
Wire.requestFrom(addr, 1);
byte setFreq = Wire.read();
if(setFreq == 0){
setFrequency = LowFrequency;
}else{
setFrequency = HighFrequency;
}
return true;
}
float PEFM::readFreq(int channel){
int reg = 6;
if(channel == 2){
reg = 13;
}
Wire.beginTransmission(addr); //Start communication
Wire.write(reg); // Command header
Wire.endTransmission(); // Complete Transmission
Wire.requestFrom(addr, 2);
unsigned long MSB = Wire.read();
unsigned long LSB = Wire.read();
MSB=MSB*256;
MSB=MSB+LSB;
float freq;
if (setFrequency==0){
freq = ((float)MSB)/(float)100;
}
else{
freq = ((float)MSB);
}
return freq;
}
float PEFM::readPulseWidth(int channel){
int reg = 8;
if(channel == 2){
reg = 15;
}
Wire.beginTransmission(addr); //Start communication
Wire.write(reg); // Command header
Wire.endTransmission(); // Complete Transmission
Wire.requestFrom(addr, 3);
unsigned long MSB = Wire.read();
unsigned long MSB2 = Wire.read();
unsigned long LSB = Wire.read();
MSB=MSB*65535;
MSB2=MSB2*256;
MSB=MSB+MSB2+LSB;
float period;
if(setFrequency==0){
period = ((float)MSB)/(float)1000;
}
else{
period = ((float)MSB)/(float)10000;
}
return period;
}
int PEFM::readDuty(int channel){
int reg = 11;
if(channel == 2){
reg = 18;
}
Wire.beginTransmission(addr); //Start communication
Wire.write(reg); // Command header
Wire.endTransmission(); // Complete Transmission
Wire.requestFrom(addr, 1);
return(Wire.read());
}