【发布时间】:2019-09-08 16:20:24
【问题描述】:
我正在构建鼓机,我想要两个 16 位数组来定义机器的状态。一个数组“current_led”,在对应于当前播放的第 16 个音符的索引处设置了 1。
当用户对要播放的声音进行编程时,例如在第 1 步和第 4 步,我希望一个 16 位数组“selected_steps”在索引 0 和 3 处设置 1。
所以我希望,在每分钟节拍定义的每一步更新中,“current_led”移动位,但“selected_steps”是静态的。
我想要一个由
构造的最终数组“led_array”led_array = XOR(selected_steps,current_led)
这样我就可以在每一步更新时使用移位寄存器来点亮正确的 LED。
但是由于我在 C 中定义和使用位和数组时遇到了一些麻烦,所以我不明白如何正确初始化数组并使用它们。
我想要的东西像
int current_led[16];
int selected_steps[16];
int led_array[16];
//Function is called every 0.5 s if BPM 120.
void step(void) {
step_number = step_number < 15 ? step_number +1 : 0;
}
我正在使用 2 个 PISO 移位寄存器将 16 个按钮的输入输入到我的微控制器。我将并行负载引脚设置为高电平,这样每当用户按下按钮时,移位寄存器上的相应引脚设置为 1。因此我每次都读取每 16 个引脚以查看用户是否按下了任何按钮。
//Check which steps are selected by the user. This function is called every 1 ms
void scan_buttons() {
for (int j = 0; j<16 ; j++) {
if (PIND & 0b01000000){
selected_steps[j] = 1;
} else {
selected_steps[j] = 0;
}
void update_led(void) {
current_led = (1 << step_number);
led_array = current_led^selected_steps;
for (int j = 15; j>=0 ; j--) {
if (led_array[j] == 1) {
do something...
} else {
do something else...
}
}
}
为了清楚起见,下面是一个 LED 应如何表示该状态的示例。 如果 BPM 设置为 120,并且我们有 16 步(4 节拍),则步长应每 60/BPM 秒(0.5 秒)递增。当前步骤由一个明亮的 LED 指示。我还通过始终让 LED 在当前步骤亮起来向用户指示他/她在哪个步骤上编程了声音。
第 1 步:step_number = 0
LED: [1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
第 2 步:step_number = 1
LED: [0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
第三步:step_number = 2, selected_step[7] = 1, selected_step[11] = 1,
(用户在步骤 8 和 12 中选择通过按下按钮 8 和 12 输入声音)
LED: [0 0 1 0 0 0 0 1 0 0 0 1 0 0 0 0]
第 4 步:step_number = 3,selected_step[7] = 1,selected_step[11] = 1,
(自上一步以来用户没有按下任何按钮)
LED: [0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0]
但我不明白如何声明数组,以及编写正确的代码以正确设置位并执行 XOR 操作。