-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathM62429.cpp
More file actions
executable file
·70 lines (59 loc) · 1.9 KB
/
Copy pathM62429.cpp
File metadata and controls
executable file
·70 lines (59 loc) · 1.9 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
/*!
* This is a library for M62429/FM62429 Serial data control dual electronics volume chips.
*
* Written by CGrassin.
*/
#include "Arduino.h"
#include "M62429.h"
M62429::M62429 (uint8_t pin_clk, uint8_t pin_data){
_pin_data = pin_data;
_pin_clk = pin_clk;
}
/*
* Sets the volume of one on both channel of the 62429.
*
* Parameters:
* attenuation, from 0 to 83dB
* both, 0 (default)=both channels or 1=individual
* channel, 0=channel 1, 1 = channel 2, no effect if both=0
*/
void M62429::_setVolume(uint8_t attenuation, uint8_t channel, uint8_t both){
uint16_t data_word = 0x00;
// Sanity checks on parameters
if(both>1 || channel>1)
return;
if(attenuation > 83)
attenuation = 83;
// Convert volume to attenuation
//uint8_t attenuation = (volume > 100) ? 0 : (((volume * 83) / -100) + 83);
// Build control word (11 bits)
data_word |= (channel << 0); // Channel select: 0=ch1, 1=ch2
data_word |= (both << 1); // Individual/both select: 0=both, 1=individual
data_word |= ((21 - (attenuation / 4)) << 2); // ATT1: coarse attenuator, steps of -4dB
data_word |= ((3 - (attenuation % 4)) << 7); // ATT2: fine attenuator, steps of -1dB)
data_word |= (0b11 << 9); // 2 last bits must be 1
// Send control word
for (uint8_t i = 0; i < 11; i++) {
delayMicroseconds (2);
digitalWrite (_pin_data, 0);
delayMicroseconds (2);
digitalWrite (_pin_clk, 0);
delayMicroseconds (2);
digitalWrite (_pin_data, (data_word >> i) & 0x01);
delayMicroseconds (2);
digitalWrite (_pin_clk, 1);
}
delayMicroseconds (2);
digitalWrite (_pin_data, 1);
delayMicroseconds (2);
digitalWrite (_pin_clk, 0);
}
void M62429::setVolumeBoth(uint8_t attenuation){
_setVolume(attenuation, 0, 0);
}
void M62429::setVolumeCh1(uint8_t attenuation){
_setVolume(attenuation, 0, 1);
}
void M62429::setVolumeCh2(uint8_t attenuation){
_setVolume(attenuation, 1, 1);
}