-
Notifications
You must be signed in to change notification settings - Fork 0
/
Muses72323_on_UNO.ino
176 lines (148 loc) · 4.91 KB
/
Muses72323_on_UNO.ino
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
使用ESP32-S3-DevKitC-1模块控制Muses72323电子音量芯片
By Jerry Long, 10 Feb 2024
旋转编码器接线:
CLK(A)——>GPIO37
DT(B) ——>GPIO36
SW ——>GPIO35
Muses72323芯片SPI接线:
Latch(20#)——>SDL——>GPIO10(SS)
Clock(19#)——>SCL——>GPIO12(SCK)
Data(18#) ——>SDA——>GPIO11(MOSI)
Muses72323芯片数字供电:
D_IN(16#) ——>5V
D_REF(32#)——>D_GND
Muses72323芯片模拟供电:
外接±15V电源,地A_GND
*/
#include <SPI.h>
//#define MAX_ATTENUATION -472 //最大衰减量
//#define STEP 0.25 //每步衰减量
//定义Muses72323芯片的地址(根据ADR0-#30和ADR1-#31脚位的高低电位)
const uint8_t MUSES_ADDRESS = 0;
//定义SPI脚位
#define MUSES_CLK 13
#define MUSES_MOSI 11
#define MUSES_CS 10
//定义默认音量,最大和最小音量
const long defaultVolume = 232; //重启后的默认音量 232=-50dB
long currentVolume = 32; //当前音量,从32=0dB开始
const long minVolume = 479; //最小音量479=-111.75dB
const long maxVolume = 32; //最大音量 32=0dB
//控制信号寄存器参数
const uint8_t musesLeftAtt = 0b0010000; //控制左声道,Soft step功能打开
const uint8_t musesRightAtt = 0b0010100; //控制右声道,Soft step功能打开
const uint16_t setInternalClock = 0b0000001000001100; //使用内部时钟
const uint16_t setZeroCrossingOn = 0b0000000000001000; //开启Zero Cross检测电路
void setup() {
//设置串口波特率
Serial.begin(115200);
//设置CS脚位状态
pinMode(MUSES_CS, OUTPUT);
digitalWrite(MUSES_CS, HIGH);
//初始化Muses72323
SPI.begin();
SPI.beginTransaction(SPISettings(250000, MSBFIRST, SPI_MODE0));
//启用Muses72323内部时钟
// 拉低CS脚位电平,准备写入
digitalWrite(MUSES_CS, LOW);
delayMicroseconds(1); // this can be improved
// send in the address and value via SPI
SPI.transfer(highByte(setInternalClock));
SPI.transfer(lowByte(setInternalClock));
// 拉高CS脚位电平,完成写入
delayMicroseconds(1); // this can be improved
digitalWrite(MUSES_CS, HIGH);
SPI.endTransaction();
//启用Muses72323的Zero Cross检测电路
// 拉低CS脚位电平,准备写入
digitalWrite(MUSES_CS, LOW);
delayMicroseconds(1); // this can be improved
// send in the address and value via SPI
SPI.transfer(highByte(setZeroCrossingOn));
SPI.transfer(lowByte(setZeroCrossingOn));
// 拉高CS脚位电平,完成写入
delayMicroseconds(1); // this can be improved
digitalWrite(MUSES_CS, HIGH);
SPI.endTransaction();
}
void loop() {
// put your main code here, to run repeatedly:
for(int i = 32; i<200; i++)
{
currentVolume = i;
Serial.print("currentVolume = ");
Serial.println(currentVolume);
musesWriteVolume(currentVolume, currentVolume);
delay(10);
}
for (int i = 200; i>32; i--)
{
currentVolume = i;
Serial.print("currentVolume = ");
Serial.println(currentVolume);
musesWriteVolume(currentVolume, currentVolume);
delay(10);
}
// You can continue with the rest of your loop code here.
delay(100);
}
void musesWriteVolume(long leftVolume, long rightVolume)
{
uint8_t address;
long data;
//写入左声道寄存器
address = musesLeftAtt;
data = leftVolume;
musesWriteRaw(address, data);
//写入右声道寄存器
address = musesRightAtt;
data = rightVolume;
musesWriteRaw(address, data);
}
void musesWriteRaw(uint8_t address, long data) {
uint16_t value = 0; //写入Muses72323芯片的寄存器数值D15~D0;
uint8_t value1 = 0;
uint8_t value2 = 0;
int temp = 0;
for(int i = 0; i<7; i++) //将地址数值写入data的0~6位
{
temp = bitRead(address, i);
bitWrite(value, i, temp);
}
for(int i = 0; i<9; i++) //将音量控制数据写入data的7~15位
{
temp = bitRead(data, i);
bitWrite(value, i+7, temp);
}
//通过串口监视器显示写入的数值,测试用
Serial.print("value:");
Serial.println(value, BIN);
/* for(int i=0; i<8; i++)
{
temp = bitRead(value, i);
bitWrite(value1, i, temp);
}
Serial.print("value1:");
Serial.println(value1, BIN);
for(int i=0; i<8; i++)
{
temp = bitRead(value, i+8);
bitWrite(value2, i, temp);
}
Serial.print("value2:");
Serial.println(value2, BIN);
*/
// 拉低CS脚位电平,准备写入
digitalWrite(MUSES_CS, LOW);
delayMicroseconds(1); // this can be improved
// send in the address and value via SPI
SPI.transfer(highByte(value));
SPI.transfer(lowByte(value));
//SPI.transfer(value2);
//SPI.transfer(value1);
// 拉高CS脚位电平,完成写入
delayMicroseconds(1); // this can be improved
digitalWrite(MUSES_CS, HIGH);
SPI.endTransaction();
}