-
Notifications
You must be signed in to change notification settings - Fork 0
/
DACActuator.cpp
55 lines (43 loc) · 1.07 KB
/
DACActuator.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
/*
* DACActuator.cpp
*
* Created on: Apr 1, 2013
* Author: dam7633
*/
#include "DACActuator.h"
#include <hw/inout.h>
#define MAX_DA_CODE (4095)
#define MIN_DA_CODE (0)
#define OUTPUT_REFERENCE (10)
#define DAC_CHANNEL (0)
DACActuator::DACActuator(uintptr_t s, uintptr_t m, uintptr_t l) :
Thread("DACActuator"),
status_handle(s),
msb_handle(m),
lsb_handle(l) {
}
DACActuator::~DACActuator() {
}
void *DACActuator::run() {
while (!killThread) {
}
return NULL;
}
void DACActuator::setValue(double value) {
// Convert the output value to a DAC code.
short daCode = (value / OUTPUT_REFERENCE) * 2048 + 2047;
// Clip the DAC code.
if (daCode > MAX_DA_CODE) {
daCode = MAX_DA_CODE;
} else if (daCode < MIN_DA_CODE) {
daCode = MIN_DA_CODE;
}
// Split the DAC code into LSB (8 bits) and MSB (4 bits).
unsigned char lsb = daCode & 255;
unsigned char msb = daCode / 256;
// Write to the specified DAC channel.
out8(lsb_handle, lsb);
out8(msb_handle, msb + (DAC_CHANNEL << 6));
// Wait for the D/A to finish.
while ((in8(status_handle) & 0b00001000) != 0) { }
}