gpt4 book ai didi

c - 为什么我的 SPI 通讯不工作? (Atmega644)

转载 作者:行者123 更新时间:2023-11-30 16:18:43 25 4
gpt4 key购买 nike

我正在构建一台鼓机,并且我已经存储了一个带有底鼓声音的示例头文件,其值介于 0 到 170 之间。我想通过 SPI 将其发送到 10 位 MCP4811 DAC,然后将其输出到3.5 毫米音频插孔。

我的 MISO、MOSI、SCK 和 RESET 引脚连接到我的 USB 编程器以及 DAC。

这是存储在“samples.h”中的音频文件的片段

unsigned const char sample_1[2221] PROGMEM = {0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, ...}
unsigned int sample_len[1] = {2221}

所以它是一个 2221 位的样本。我想使用频率 = 22 kHz 的 SPI 将其发送到 DAC。

我使用的是 16 MHz 晶体,因此我相应地设置了 fuse 以使用它。

我使用的定时器溢出 22 kHz。

volatile unsigned int sample_count[1] = {0};
volatile unsigned int audio_out = 0;
volatile unsigned char spi_junk;

int main (void)
sei();
DDRB = 0b10110000; //Set MOSI, SCK and SS as output.
PORTB = (1 << PINB4) //active low on SS.

TIMSK1 = (1<<OCIE1A); //Enable interrupt
TCCR1B = (1<<WGM12) | (1<<CS11); // set CTC mode and divide clk by 8
OCR1A = 91; //16 MHz/(8*91) ~ 22068 Hz

//SPI Init
SPCR = (1<<SPE) | (1<<MSTR); //master, 8 MHz
SPSR = (1<<SPI2X);

ISR (TIMER1_COMPA_vect) {
audio_out = 0;

//If play_track == 1, then the sound should be played back.
if (play_track && sample_count[0] < sample_len[0]){
audio_out += (pgm_read_byte(&(sample_1[sample_count[0]++)));

// send audio_out to 10-bit DAC on SPI
PORTB &= ~(1<<PINB4); // B.4 (DAC /CS)
SPDR = (char) ((audio_out >> 6) & 0x000f); //byte 1 0 0 0 0 b9 b8 b7 b6
while (!(SPSR & (1<<SPIF)));
spi_junk = SPDR;

SPDR = (char) ((audio_out & 0x003f) << 2); //byte 2 b5 b4 b3 b2 b1 b0 0 0
while (!(SPSR & (1<<SPIF)));
spi_junk = SPDR;
PORTB |= (1<<PINB4);
}

我的 PIN 设置是。

Atmega644 -> DAC

MOSI -> SDI

SCK -> SCK

SS -> /CS

在 MCP4811 上

Vdd -> 5V

Vss -> GND

V_out -> Audio jack.

MCP4811 上的其余引脚未连接任何东西。

通过在 LCD 屏幕上显示 audio_out 值,我发现 audio_out 正在按预期工作。但没有任何内容输出到 DAC。有人看出可能出了什么问题吗?

编辑:添加了我错过添加的 SPI init。

最佳答案

这里是你的线路

SPDR = (char) ((audio_out >> 6) & 0x000f);//字节1 0 0 0 0 b9 b8 b7 b6

将 ØSHDN 设置为 0,这将关闭 DAC

0 = 关闭设备。模拟输出不可用。 VOUT 引脚连接至 500 kohm(典型值)

将位 12 设置为 1

SPDR = (char) ((audio_out >> 6) & 0x0f)|0x10;//字节1 0 0 0 1 b9 b8 b7 b6

来自数据表

1 = 事件模式操作。 VOUT 可用。

关于c - 为什么我的 SPI 通讯不工作? (Atmega644),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55835248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com