-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindustrialli_analog_input.cpp
85 lines (65 loc) · 2.76 KB
/
industrialli_analog_input.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
#include "analog_input/industrialli_analog_input.h"
void industrialli_analog_input::begin(){
analog_input[A01] = {PB1, PA9, READ_10V};
analog_input[A02] = {PC5, PA10, READ_10V};
analog_input[A03] = {PC4, PD0, READ_10V};
analog_input[A04] = {PA0, PD1, READ_10V};
pinMode(analog_input[A01].port, INPUT);
pinMode(analog_input[A02].port, INPUT);
pinMode(analog_input[A03].port, INPUT);
pinMode(analog_input[A04].port, INPUT);
pinMode(analog_input[A01].sel, OUTPUT);
pinMode(analog_input[A02].sel, OUTPUT);
pinMode(analog_input[A03].sel, OUTPUT);
pinMode(analog_input[A04].sel, OUTPUT);
digitalWrite(analog_input[A01].sel, READ_10V);
digitalWrite(analog_input[A02].sel, READ_10V);
digitalWrite(analog_input[A03].sel, READ_10V);
digitalWrite(analog_input[A04].sel, READ_10V);
set_resolution(12);
}
void industrialli_analog_input::set_read_mode(uint8_t _pin, uint8_t _read_mode){
digitalWrite(analog_input[_pin].sel, _read_mode);
analog_input[_pin].read_mode = _read_mode;
leds.set_led(_pin, HIGH);
}
void industrialli_analog_input::set_resolution(uint16_t _resolution){
analogReadResolution(_resolution);
bits_resolution = (1 << _resolution) - 1;
}
double industrialli_analog_input::map(double _value, double _in_min, double _in_max, double _out_min, double _out_max){
return (_value - _in_min) * (_out_max - _out_min) / (_in_max - _in_min) + _out_min;
}
double industrialli_analog_input::map_pin(uint8_t _pin, double _in_min, double _in_max, double _out_min, double _out_max){
double value = analog_read(_pin);
return (value - _in_min) * (_out_max - _out_min) / (_in_max - _in_min) + _out_min;
}
double industrialli_analog_input::analog_read(uint8_t _pin){
double vdda = (3.3 * get_vrefint_cal()) / get_vrefint_data();
double vchannel_internal = (vdda / bits_resolution) * analogRead(analog_input[_pin].port);
double vchannel_external;
if(analog_input[_pin].read_mode == READ_10V){
vchannel_external = (10 * vchannel_internal) / 3.3;
}
if(analog_input[_pin].read_mode == READ_20mA){
vchannel_external = (20 * vchannel_internal) / 3.3;
}
return vchannel_external;
}
bool industrialli_analog_input::alarm_020mA(float _mA, float _threshold){
return _mA < _threshold;
}
uint16_t industrialli_analog_input::get_vrefint_cal(){
return *((uint16_t *)0x1FF1E860);
}
uint16_t industrialli_analog_input::get_vrefint_data(){
return analogRead(AVREF);
}
// int industrialli_analogInputsHub::getIntParamTSCAL1(){
// _TS_CAL1 = *((unsigned short *)0x1FF1E820); // TEMP_RAW1 = 3.3V@30degreeC
// return _TS_CAL1;
// }
// int industrialli_analogInputsHub::getIntParamTSCAL2(){
// _TS_CAL2 = *((unsigned short *)0x1FF1E840);
// return _TS_CAL2;
// }