📄 dac_max9850.c
字号:
/*= dac_max9850.c ==============================================================
*
* Copyright (C) 2005 Nordic Semiconductor
*
* This file is distributed in the hope that it will be useful, but WITHOUT
* WARRANTY OF ANY KIND.
*
* Author(s): B鴕ge Strand
*
* Description:
*
* DAC specific code for Maxim MAX9850
* This file must be ported to any other DAC to be used.
* It must implement all functions in dac.h
* All defines specific to MAX9850 reside in this file!
*
* Compiler: Tested with WinAVR, avr-gcc (GCC) 3.4.3
*
* Revision: 1.0
*
*==============================================================================
*/
#include "dac.h"
// Some forward declarations of functions that are not available externally
char dac_setvolume(void);
// Set up the DAC
char dac_init(void) {
extern char slaveoutbuf[SLAVEBUFSIZE]; // Global data buffer from MCU to Z1 slave
extern char volume;
int n; // Index to the data buffer
char temp;
if (volume == 0xFF) volume = 0x11; // If volume wasn't initiated, set it conveniently
temp = z1_flagready(RXEXEC);
if (temp==OKAY) { // Is TX ready to convey data?
// Set up a transfer from MCU through ATX and ARX to DAC
slaveoutbuf[n=0] = 0x02; // Data for RXDCMD, 2-wire at RX, 2-wire start/stop, 100kb/s
slaveoutbuf[++n] = 10; // Data for RXWCNT, sending 10 bytes to DAC
slaveoutbuf[++n] = 0; // Data for RXRCNT, reading 0 bytes from DAC
z1_multiwrite(RXDCMD, RXRCNT); // Send configuration data
slaveoutbuf[n=0] = 0x20; // Data for RXBUF_0, 2-w hardware adr. of MAX9850
slaveoutbuf[++n] = 0x02; // Data for RXBUF_1, internal address of setup register
slaveoutbuf[++n] = volume; // Data for RXBUF_2, MAX9850 address 0x02, "Volume"
slaveoutbuf[++n] = 0x00; // Data for RXBUF_3, MAX9850 address 0x03, "General Purpose"
slaveoutbuf[++n] = 0x00; // Data for RXBUF_4, MAX9850 address 0x04, "Interrupt Enable"
slaveoutbuf[++n] = 0xfd; // Data for RXBUF_5, MAX9850 address 0x05, "Enable"
slaveoutbuf[++n] = 0x00; // Data for RXBUF_6, MAX9850 address 0x06, "Clock"
slaveoutbuf[++n] = 0x00; // Data for RXBUF_7, MAX9850 address 0x07, "Charge Pump"
slaveoutbuf[++n] = 0x80; // Data for RXBUF_8, MAX9850 address 0x08, "LRCK MSB"
slaveoutbuf[++n] = 0x10; // Data for RXBUF_9, MAX9850 address 0x09, "LRCK LSB"
z1_multiwrite(RXBUF_0, RXBUF_9); // Move data through to DAC
z1_setflag(RXEXEC); // Execute transfer to RX
}
return temp; // Return whatever z1_flagready() said
}
// Increase volume
char dac_volume_up(void) {
extern char volume; // Global variable
if (volume > 0) // On MAX9850, 0x00 is highest volume, 0x27 is lowest
volume--;
return (dac_setvolume()); // Write the new volume setting to DAC
}
// Decrease volume
char dac_volume_down(void) {
extern char volume; // Global variable
if (volume < 0x27) // On MAX9850, 0x00 is highest volume, 0x27 is lowest
volume++;
return (dac_setvolume()); // Write the new volume setting to DAC
}
// Transmit volume information
char dac_setvolume(void) {
extern char slaveoutbuf[SLAVEBUFSIZE]; // Global data buffer from MCU to Z1 slave
extern char volume;
int n; // Index to the data buffer
char temp;
temp = z1_flagready(RXEXEC); // Is Z1 ready to push data to DAC?
if (temp==OKAY) { // Is TX ready to convey data?
// Set up a transfer from MCU through ATX and ARX to DAC
slaveoutbuf[n=0] = 0x02; // Data for RXDCMD, 2-wire at RX, 2-wire start/stop, 100kb/s
slaveoutbuf[++n] = 0x03; // Data for RXWCNT, sending 3 bytes to DAC
slaveoutbuf[++n] = 0x00; // Data for RXRCNT, reading 0 bytes from DAC
z1_multiwrite(RXDCMD, RXRCNT); // Send configuration data
slaveoutbuf[n=0] = 0x20; // Data for RXBUF_0, 2-w hardware adr. of MAX9850 FIX: define?
slaveoutbuf[++n] = 0x02; // Data for RXBUF_1, internal address of setup register
slaveoutbuf[++n] = volume; // Data for RXBUF_2, MAX9850 address 0x02, "Volume"
z1_multiwrite(RXBUF_0, RXBUF_2); // Move data through to DAC
z1_setflag(RXEXEC); // Execute transfer to RX
}
return temp; // Return whatever z1_flagready() said
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -