-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmcp42xxx.cpp
More file actions
36 lines (27 loc) · 730 Bytes
/
mcp42xxx.cpp
File metadata and controls
36 lines (27 loc) · 730 Bytes
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
#include "mcp42xxx.h"
MCP42xxx::MCP42xxx(int selectSlavePin) {
_selectSlavePin = selectSlavePin;
// set the slaveSelectPin as an output:
pinMode(selectSlavePin, OUTPUT);
}
void MCP42xxx::setValue(uint8_t channel, uint8_t value) {
//select chip
digitalWrite(_selectSlavePin,LOW);
//create command
uint8_t _cmd = 0x00;
_cmd |= (CMD_WRITE << 5);
_cmd |= (channel << 0);
//send command
SPI.transfer(_cmd);
//set channel value
SPI.transfer(value);
//deselect chip
digitalWrite(_selectSlavePin,HIGH);
}
void MCP42xxx::setPercent(uint8_t channel, uint8_t percentage) {
//calculate value
uint8_t value = 0;
if(percentage != 0)
value = (percentage / 100.0) * 255;
setValue(channel, value);
}