描述
<p>* 1、项目功能介绍</p>
<p>这次制作一个带有USB声卡功能的鼠标,表面上看着是一个鼠标,其实它内置喇叭能够直接发声。</p>
<p>*2、项目属性<br>首次公开,原创。</p>
<p><br>* 3、开源协议<br>GPL3.0开源协议</p>
<p>*4、硬件部分</p>
<p>硬件上使用了 USB Hub 芯片对鼠标的 USB 信号进行扩展,转出2个USB接口,一个继续给鼠标使用,另外一个给 Arduino Leonardo (32U4)使用。理论上可以将32u4和USB Hub 芯片放在同一个 PCB 上,但是目前 32U4太贵了(立创商城 65元)。所以选择独立的"Badusb迷你型开发板 Beetle USB ATMEGA32U4虚拟键盘模块 "(22.88 元)。</p>
<p>32u4 带有 USB Device 功能,通过编程,让它报告自己为一个 USB 声卡设备。这样插入电脑后,Windows 会增加一个音频输出设备,选择从这个设备输出后, Windows 会将音频发送到 32U4上。最终我们使用 PWM pin 作为模拟输出驱动喇叭发声。</p>
<p>代码基于 Lufa USB 库开发,主要代码如下:</p>
<p> </p>
<p>#include "AudioOutput.h"</p>
<p>/** LUFA Audio Class driver interface configuration and state information. This structure is<br> * passed to all Audio Class driver functions, so that multiple instances of the same class<br> * within a device can be differentiated from one another.<br> */<br>USB_ClassInfo_Audio_Device_t Speaker_Audio_Interface =<br> {<br> .Config =<br> {<br> .ControlInterfaceNumber = INTERFACE_ID_AudioControl,<br> .StreamingInterfaceNumber = INTERFACE_ID_AudioStream,<br> .DataOUTEndpoint =<br> {<br> .Address = AUDIO_STREAM_EPADDR,<br> .Size = AUDIO_STREAM_EPSIZE,<br> .Banks = 2,<br> },<br> },<br> };</p>
<p>/** Current audio sampling frequency of the streaming audio endpoint. */<br>static uint32_t CurrentAudioSampleFrequency = 48000;</p>
<p><br>/** Main program entry point. This routine contains the overall program flow, including initial<br> * setup of all components and the main program loop.<br> */<br>int main(void)<br>{<br> SetupHardware();</p>
<p> //LAB_ZDebug LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);<br> GlobalInterruptEnable();</p>
<p> for (;;)<br> {<br> Audio_Device_USBTask(&Speaker_Audio_Interface);<br> USB_USBTask();<br> }<br>}</p>
<p>/** Configures the board hardware and chip peripherals for the demo's functionality. */<br>void SetupHardware(void)<br>{<br>#if (ARCH == ARCH_AVR8)<br> /* Disable watchdog if enabled by bootloader/fuses */<br> MCUSR &= ~(1 > 8);<br> int8_t RightSample_8Bit = (Audio_Device_ReadSample16(&Speaker_Audio_Interface) >> 8);</p>
<p> /* Mix the two channels together to produce a mono, 8-bit sample */<br> int8_t MixedSample_8Bit = (((int16_t)LeftSample_8Bit + (int16_t)RightSample_8Bit) >> 1);</p>
<p> #if defined(AUDIO_OUT_MONO)<br> /* Load the sample into the PWM timer channel */<br> OCR1A = (MixedSample_8Bit ^ (1 4)<br> LEDMask = (LEDS_LED1 | LEDS_LED2);<br> else if (MixedSample_8Bit > 2)<br> LEDMask = (LEDS_LED1);</p>
<p> LEDs_SetAllLEDs(LEDMask);<br> */<br> }</p>
<p> Endpoint_SelectEndpoint(PrevEndpoint);<br>}</p>
<p>/** Event handler for the library USB Connection event. */<br>void EVENT_USB_Device_Connect(void)<br>{<br> //LAB_ZDebug LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);</p>
<p> /* Sample reload timer initialization */<br> TIMSK0 = (1 </p>
评论(1)