-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConverter.cpp
91 lines (78 loc) · 2.42 KB
/
Converter.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
/*
* Converter class
* Represents the A/D Converter
*
* @author Christoffer Rosen
* @author Lennard Streat
*/
#include "Converter.h"
Converter::Converter() {
}
/**
* getData()
* @descr Read the data from the board after the
* A/D conversion
* @return A/D code between -32768 to 32767 from board
* @private
*/
int Converter::getData(){
char lsbData = in8(_lsbPtr); // 8 Bytes
char msbData = in8(_msbPtr); // 8 Bytes
return ( ( msbData << 8 ) + lsbData ); // convert to 16 Bytes
}
/**
* convert()
* @descr Performs the A/D conversion
*/
void Converter::convert() {
while (( in8( _inputPtr ) & WAIT_BIT)!= 0 ){} // Allow time for the analog input circuit to settle
out8( _lsbPtr, 0x80); // Perform the A/D conversion
while( in8( _inputPtr ) & 0x80); // wait for conversion to finish
}
/**
* getVoltage()
* @descr returns the input voltage.
* Formula = input voltage = A/D value * full-scale input range / 32768
* @return float The voltage from the A/D value from the board
*/
float Converter::getVoltage(){
int data = getData();
float voltage = ((float)(data) * (float)(FULL_SCALE_INPUT_RANGE) ) / (float)32768;
return voltage;
}
/**
* initalize()
* @descr Set up the A/D pointers and the A/D input range
* IMPORTANT: Assumes and requires that GPIO has access to
* the current thread
*/
void Converter::initalize(){
_lsbPtr = mmap_device_io( BYTE, BASE_ADDR + LSB_OFFSET );
_msbPtr = mmap_device_io( BYTE, BASE_ADDR + MSB_OFFSET );
_adChannelPtr = mmap_device_io ( BYTE, BASE_ADDR + AD_REGISTER_OFFSET);
/* Set the board to use channel 4 only */
out8( _adChannelPtr, AD_CHANEL);
_inputPtr = mmap_device_io ( BYTE, INPUT_RANGE_REGISTER);
out8( _inputPtr, VOLTAGE_RANGE );
}
/**
* getByteRepresentation()
* @descr Returns byte representation of a float voltage value to be used
* by the freescale implementation.
* @param A float voltage value ranging between -5 to 5.
* @return Returns a signed character byte representation of the voltage value
*/
signed char Converter::getByteRepresentation(float voltage){
signed char byteRep;
// scale voltage value
if( voltage > 0 ){
byteRep = ( (float)( (float)SIGNED_POSITIVE_MAX/(float)FULL_SCALE_INPUT_RANGE ) * voltage );
} else {
byteRep = ( (float)( (float)SIGNED_MINUS_MAX/(float)FULL_SCALE_INPUT_RANGE ) * voltage );
}
return byteRep;
}
// destructor
Converter::~Converter() {
// TODO Auto-generated destructor stub
}