极海APM32F407IG Tiny Board开发板测评9:利用DAC实现音频输出
利用DAC实现音频输出
DAC数模转换器DAC与ADC模数转换相反,是将数字量转换为模拟量输出,APM32F407提供了2个12位的DAC,可配置为输入8位或12位数据。
先利用DAC和定时器输出方波三角波正弦波等各种波形,能用于DAC输出的只有这两个IO
选用PA4,用按键切换波形
代码实现
uint8_t dacvalue = 0;
uint8_t wavetype = 0;
uint16_t wavestep = 0;
uint8_t sinvalues[91] = {0,4,9,13,18,22,27,31,35,40,44,49,53,57,62,66,70,75,79,83,87,91,96,100,104,108,112,116,120,124,127,131,135,139,143,146,150,153,157,160,164,167,171,174,177,180,183,186,190,192,195,198,201,204,206,209,211,214,216,219,221,223,225,227,229,231,233,235,236,238,240,241,243,244,245,246,247,248,249,250,251,252,253,253,254,254,254,255,255,255,255};
void tim3_init()
{
TMR_BaseConfig_T TMR_TimeBaseStruct;
RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_TMR3);
TMR_TimeBaseStruct.clockDivision = TMR_CLOCK_DIV_1;
TMR_TimeBaseStruct.countMode = TMR_COUNTER_MODE_UP;
TMR_TimeBaseStruct.division = 41;
TMR_TimeBaseStruct.repetitionCounter = 0;
TMR_TimeBaseStruct.period = 999;
TMR_ConfigTimeBase(TMR3, &TMR_TimeBaseStruct);
TMR_EnableInterrupt(TMR3,TMR_INT_UPDATE);
NVIC_EnableIRQRequest(TMR3_IRQn, 0, 0);
TMR_Enable(TMR3);
}
void dac_init()
{
GPIO_Config_T gpioConfig;
DAC_Config_T dacConfig;
/* Enable GPIOA clock */
RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOA);
/* DAC out PA4 pin configuration */
GPIO_ConfigStructInit(&gpioConfig);
gpioConfig.mode = GPIO_MODE_AN;
gpioConfig.pupd = GPIO_PUPD_NOPULL;
gpioConfig.pin = GPIO_PIN_4;
GPIO_Config(GPIOA, &gpioConfig);
/* Enable DAC clock */
RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_DAC);
/* DAC channel 1 configuration */
DAC_ConfigStructInit(&dacConfig);
dacConfig.trigger = DAC_TRIGGER_NONE;
dacConfig.waveGeneration = DAC_WAVE_GENERATION_NONE;
dacConfig.maskAmplitudeSelect = DAC_LFSR_MASK_BIT11_1;
dacConfig.outputBuffer = DAC_OUTPUT_BUFFER_ENABLE;
DAC_Config(DAC_CHANNEL_1, &dacConfig);
/* Enable DAC channel 1 */
DAC_Enable(DAC_CHANNEL_1);
}
void proc_dac()
{
uint8_t changed = 0;
switch(wavetype)
{
case 0:
if(wavestep > 0)
wavestep -= 1;
else
{
if(dacvalue == 0)
dacvalue = 255;
else
dacvalue = 0;
changed = 1;
wavestep = 99;
}
break;
case 1:
dacvalue+=1;
changed = 1;
break;
case 2:
if(wavestep > 0)
wavestep -= 1;
else
{
wavestep = 10;
dacvalue+=15;
changed = 1;
}
break;
case 3:
if(wavestep < 255)
{
dacvalue=wavestep;
changed = 1;
}
else if(wavestep > 300 && wavestep < 556)
{
dacvalue= 555 - wavestep;
changed = 1;
}
if(wavestep < 600)
wavestep++;
else
wavestep = 0;
break;
case 4:
if(wavestep < 90)
{
dacvalue = sinvalues[wavestep];
changed = 1;
}
else
{
dacvalue = sinvalues[180 - wavestep];
changed = 1;
}
if(wavestep < 179)
wavestep++;
else
wavestep = 0;
break;
case 5:
if(wavestep < 90)
{
dacvalue = 127 + (sinvalues[wavestep]/2);
changed = 1;
}
else
{
dacvalue = 128 - (sinvalues[180 - wavestep]/2);
changed = 1;
}
if(wavestep < 179)
wavestep++;
else
wavestep = 0;
break;
case 6:
if(wavestep < 90)
{
dacvalue = 127 + (sinvalues[wavestep]/2);
changed = 1;
}
else if(wavestep < 180)
{
dacvalue = 127 + (sinvalues[180 - wavestep]/2);
changed = 1;
}
else if(wavestep < 270)
{
dacvalue = 128 - (sinvalues[wavestep - 180]/2);
changed = 1;
}
else
{
dacvalue = 128 - (sinvalues[360 - wavestep]/2);
changed = 1;
}
if(wavestep < 359)
wavestep++;
else
wavestep = 0;
break;
default:
break;
}
if(changed > 0)
DAC_ConfigChannel1Data(DAC_ALIGN_8BIT_R,dacvalue);
}
int main(void)
{
APM_TINY_PBInit(BUTTON_KEY1,BUTTON_MODE_GPIO);
APM_TINY_PBInit(BUTTON_KEY2,BUTTON_MODE_GPIO);
dac_init();
tim3_init();
while (1)
{
if(APM_TINY_PBGetState(BUTTON_KEY1) == BIT_RESET)
{
yuyy_delay_ms(30);
if(APM_TINY_PBGetState(BUTTON_KEY1) == BIT_RESET)
{
while(APM_TINY_PBGetState(BUTTON_KEY1) == BIT_RESET);
wavetype += 1;
wavestep = 0;
if(wavetype > 6)
wavetype = 0;
}
}
}
}
void TMR3_IRQHandler(void)
{
if(TMR_ReadIntFlag(TMR3, TMR_INT_UPDATE) == SET)
{
proc_dac();
TMR_ClearIntFlag(TMR3, TMR_INT_UPDATE);
}
}
运行效果
这样就实现了可控的电压输出,我们生活中所用的耳机音箱等最终也是向喇叭输出模拟量,所以理论上是能够利用这个DAC来输出音频的,接下来尝试一下。
先选一段音频,用Adobe Audition转换为WAV,为了少占用点空间加之DAC最大只支持12位,这里按如下参数导出。
之所以要转成这个格式是因为这个格式下文件内存储的就是数字电平信息。
用winhex打开转换后的wav文件,复制中间的一段内容,用这个选项可以直接以数组的形式复制,把复制的数组粘贴到代码中。
音频的采样频率是6000Hz,定时器这里也要调整参数以匹配采样率。
uint16_t wavestep = 0;
void tim3_init()
{
TMR_BaseConfig_T TMR_TimeBaseStruct;
RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_TMR3);
TMR_TimeBaseStruct.clockDivision = TMR_CLOCK_DIV_1;
TMR_TimeBaseStruct.countMode = TMR_COUNTER_MODE_UP;
TMR_TimeBaseStruct.division = 13;
TMR_TimeBaseStruct.repetitionCounter = 0;
TMR_TimeBaseStruct.period = 999;
TMR_ConfigTimeBase(TMR3, &TMR_TimeBaseStruct);
TMR_EnableInterrupt(TMR3,TMR_INT_UPDATE);
NVIC_EnableIRQRequest(TMR3_IRQn, 0, 0);
TMR_Enable(TMR3);
}
DAC初始化代码和之前一样,播放代码
#define AUDIO_DATA_LEN 57552
uint8_t play = 0;
const uint8_t audio_data[57552] = {/*音频数组*/};
int main(void)
{
APM_TINY_PBInit(BUTTON_KEY1,BUTTON_MODE_GPIO);
APM_TINY_PBInit(BUTTON_KEY2,BUTTON_MODE_GPIO);
dac_init();
tim3_init();
while (1)
{
if(APM_TINY_PBGetState(BUTTON_KEY1) == BIT_RESET)
{
yuyy_delay_ms(30);
if(APM_TINY_PBGetState(BUTTON_KEY1) == BIT_RESET)
{
while(APM_TINY_PBGetState(BUTTON_KEY1) == BIT_RESET);
play = 1-play;
}
}
}
}
void TMR3_IRQHandler(void)
{
if(TMR_ReadIntFlag(TMR3, TMR_INT_UPDATE) == SET)
{
if(play > 0)
{
DAC_ConfigChannel1Data(DAC_ALIGN_8BIT_R,audio_data[wavestep]);
wavestep += 1;
if(wavestep == AUDIO_DATA_LEN)
wavestep = 0;
}
TMR_ClearIntFlag(TMR3, TMR_INT_UPDATE);
}
}
运行效果,没有放大电路直接接喇叭几乎听不到声音,接到音箱上还可以。
成功用DAC播放了音频,开发板上还有USB和网口,可以尝试结合USB做个USB声卡,或者通过USB读取U盘中的WAV文件进行播放,或者利用以太网口通过网络播放,或者还可以用ADC连接MIC实现录音再用DAC播放,这里就不展开了,有兴趣的可以去尝试一下。
- |
- +1 赞 0
- 收藏
- 评论 0
本文由咪猫转载自世强,原文标题为:【极海APM32F407IG Tiny Board开发板测评】9.利用DAC实现音频输出,本站所有转载文章系出于传递更多信息之目的,且明确注明来源,不希望被转载的媒体或个人可与我们联系,我们将立即进行删除处理。
相关研发服务和供应服务
相关推荐
极海APM32F407IG Tiny Board开发板测评2:Keil MDK开发环境搭建
上文我们对极海APM32F407IG Tiny Board开发板进行了开箱测评,本文将介绍Keil MDK开发环境搭建。
设计经验 发布时间 : 2023-10-17
极海APM32F407IG Tiny Board开发板测评5:UART通讯+SPI驱动LCD12864+I2C读取D
上文对极海APM32F407IG Tiny Board开发板进行了按键+定时器测试,本文将就UART通讯+SPI驱动LCD12864+I2C读取D展开测试。
设计经验 发布时间 : 2023-10-26
tandby模式下,如何唤醒MCU APM32的RTC与WKUP?
APM32F103系列低功耗模式有三种:睡眠模式、停止模式和待机模式。通过关闭内核、时钟源、设置调压器来降低功耗。本文极海半导体解析了APM32的tandby模式下的RTC唤醒与WKUP唤醒功能如何实现。
设计经验 发布时间 : 2024-01-27
极海半导体(Geehy Semiconductor)微控制器MCU选型指南(中文)
描述- 珠海极海半导体有限公司是一家致力于开发工业级/车规级微控制器、模拟与混合信号IC及系统级芯片的集成电路设计型企业。极海团队拥有20年集成电路设计经验和嵌入式系统开发能力,可为客户提供核心可靠的芯片产品及方案,实现准确感应、安全传输和实时控制,助力客户在智慧家居、高端消费电子、工业控制、汽车电子、智慧能源以及通信设施等领域的拓展创新。
型号- APM32F465RET6,APM32F030,APM32F051K6T6,APM32F1,APM32F4,APM32F405RGT6,APM32F003F6P6,APM32F003F6P7,APM32F103VET6,APM32E103,APM32F103VET7,APM32F051C6T6,APM32F0,APM32F103RCT6,APM32F103RCT7,APM32E103RCT6,APM32E103VET6,APM32F105RCT6,APM32F035,APM32F407RGT6,GALT61120,APM32F030RCT6,APM32F091RCT6,APM32E103CET6,AGW32F103T4T6S,GW8811,APM32F407VET6,APM32A407ZGT7,GURC01,APM32A091RCT7,APM32F003F4U6,APM32A091,APM32F411VET6,GHD系列,APM32F051C6U6,APM32F103TBU7,APM32F103TBU6,APM32F107RBT6,G32A1445,APM32F1系列,GALT系列,APM32F051R8T6,APM32E1 系列,APM32F417ZGT6,APM32F411CET6,G32A,APM32F405ZGT6,APM32F051K6U6,APM32F091VBT6,APM32M3514C8T7,APM32F030C8T6,G32A1445UAT0MLL,APM32系列,APM32F091CBT6,AGM32F103T4T6S,G32A1445UAT0MLH,APM32F103C8T6,APM32F103RBT7,APM32F411,GW3323HGU6,APM32F030K8T6,APM32F103RBT6,GURC系列,APM32A407,APM32F417,APM32F091CBU6,APM32M3514,GW系列,APM32F411RET6,APM32F407ZET6,APM32F072VBT6,APM32M3514C8U7,APM32E1,APM32,APM32F091CCU6,APM32E103CEU6,APM32F072R8T6,APM32 PROG,APM32F003F4M6,APM32F107RCT6,APM32F072CBT6,APM32F407IGT6,APM32F003,APM32A103VET7,APM32F072CBT7,APM32F105RBT6,APM32F407,APM32F405,APM32F091,APW32F103T4T6S,APM32F405VGT6,APM32F035C8T7,GHD,APM32F091VCT6,APM32F072,APM32E103CCT6,APM32F091CCT6,APM32F103CCT6,APM32F051K8T6,APM32E103VCT6,APM32F051C8T6,APM32F072CBU6,APM32E103ZET6,GHD3125R,APM32F103VCT6,APM32F103ZET6,GEEHY-LINK,APM32F107VBT6,AP/GM/W32F103T4T6S,APM32A407VGT7,APM32A103,APM32F003F6U7,APM32F407RET6,APM32F407VGT6,GW3323,APM32F003F6U6,APM32F417IGT6,APM32F103T8U6,APM32F051K8U6,APM32F051C8U6,APM32F107,APM32F105,APM32F465,APM32F105VCT6,APM32F103,APM32F417VGT6,GHD3440R5,APM32F051R6T6,APM32F103RET6,GHD3440R3,APM32F003F4P6,APM32F465CEU6,APM32F035K8T7,APM32F465VET6,GURC,APM32F030CCT6,APM32F030C6T6,APM32F103R8T6,GALT,APM32F051,APM32F103CBT7,G32A1465UAT0MLL,APM32F103CBT6,GHD3440QE,G32A系列,G32A1465UAT0MLH,APM32F030K6T6,APM32F030R8T6,APM32F103VBT6,APM32F030K6T7,APM32F072C8T6,APM32F107VCT6,APM32F103T4T6S,APM32A103CBT7,APM32F411CEU6,APM32F091RBT6,GW8811KEU6,GW8811CEU6,APM32F407ZGT6,APM32F072RBT6,APM32F072RBT7,APM32F003F6M6,GHD3440PF,APM32F407IGH6,APM32E103CCU6,GHD1620T,APM32A103RET7,APM32F105VBT6,APM32F030K6U6,GW,APM32E103RET6,APM32F072C8U6,APM32F4系列,G32A1465,APM32F0系列,APM32F072V8T6
【经验】极海MCU APM32F103 IAP的实现方式
拿到了一块APM32F103VC的MINI开发板,在学习了一段时间后发现其有非常丰富的外设资源,主频能达到96Mhz。最近在项目中使用到了IAP(In Application Programming)功能,特来评估一下APM32F103的IAP实现方式。
设计经验 发布时间 : 2022-12-30
极海半导体(Geehy Semiconductor)微控制器MCU选型指南(英文)
描述- Geehy Semiconductor Co., Ltd. is an IC Fabless company dedicated to developing industrial & automotive-grade MCUs, analog & mixed-signal ICs, and SoCs.
型号- APM32F030,APM32F051K6T6,APM32F4,APM32F405RGT6,APM32F003F6P6,APM32F003F6P7,APM32F103VET6,APM32E103,APM32F103VET7,APM32F051C6T6,APM32F0,APM32 SERIES,APM32F103RCT6,APM32F103RCT7,APM32E103RCT6,APM32E103VET6,APM32F105RCT6,APM32F035,APM32F407RGT6,GALT61120,APM32F030RCT6,APM32E103CET6,AGW32F103T4T6S,GW8811,APM32F407VET6,APM32A407ZGT7,GURC01,APM32A091RCT7,APM32F003F4U6,APM32A091,APM32F411VET6,APM32F051C6U6,APM32F103TBU7,APM32F103TBU6,APM32F107RBT6,G32A1445,APM32F051R8T6,APM32F417ZGT6,APM32F411CET6,G32A,APM32F405ZGT6,APM32F051K6U6,APM32M3514C8T7,APM32F030C8T6,G32A1445UAT0MLL,AGM32F103T4T6S,G32A1445UAT0MLH,APM32F103C8T6,APM32F103RBT7,APM32F411,GW3323HGU6,APM32F030K8T6,APM32F103RBT6,APM32A407,APM32F417,APM32F091CBU6,APM32M3514,APM32F407ZET6,APM32F072VBT6,APM32M3514C8U7,APM32E1,APM32,APM32F091CCU6,APM32E103CEU6,APM32F072R8T6,APM32F003F4M6,GW SERIES,APM32F107RCT6,APM32F072CBT6,APM32F407IGT6,APM32F003,APM32A103VET7,APM32F072CBT7,APM32F105RBT6,APM32F407,APM32F405,APM32F091,APW32F103T4T6S,APM32F405VGT6,APM32F035C8T7,GHD,APM32F072,APM32E103CCT6,APM32F103CCT6,APM32F051K8T6,APM32E103VCT6,APM32F051C8T6,APM32F072CBU6,APM32E103ZET6,GHD3125R,APM32F103VCT6,APM32F103ZET6,GEEHY-LINK,AP/GM/W32F103T4T6S,APM32F107VBT6,APM32A407VGT7,APM32A103,APM32F003F6U7,APM32F407RET6,APM32F407VGT6,GW3323,APM32F003F6U6,APM32F417IGT6,APM32F103T8U6,APM32F051K8U6,APM32F051C8U6,APM32F107,APM32F4T1RET6,APM32F105,APM32F105VCT6,APM32F103,APM32F417VGT6,GHD3440R5,APM32F051R6T6,APM32F103RET6,GHD3440R3,APM32F003F4P6,APM32F035K8T7,GURC,APM32F030CCT6,APM32F030C6T6,APM32F103R8T6,GALT,APM32F051,APM32F103CBT7,G32A1465UAT0MLL,APM32F103CBT6,GHD3440QE,G32A1465UAT0MLH,APM32F030K6T6,APM32F030R8T6,APM32F103VBT6,APM32F030K6T7,APM32F072C8T6,APM32F107VCT6,APM32F103T4T6S,APM32A103CBT7,APM32EF1,APM32F411CEU6,GW8811KEU6,GW8811CEU6,APM32F407ZGT6,APM32F072RBT6,APM32F072RBT7,APM32F003F6M6,GHD3440PF,APM32F407IGH6,APM32E103CCU6,GHD1620T,APM32A103RET7,APM32F105VBT6,APM32F030K6U6,GW,APM32E103RET6,APM32F072C8U6,G32A1465,APM32F072V8T6
极海半导体APM32F407系列MCU支持国密算法,助力国产安全可控,适用于新能源等领域
极海推出的APM32F407系列MCU,结合当前环境要求,设计出了支持国密算法(SM2,SM3,SM4)的IP, 符合国家密码管理局认定和公布的密码算法标准及其应用规范,并凭借显著的性能优势,已应用至新能源、工业控制、医疗设备等众多领域。
原厂动态 发布时间 : 2023-01-07
【视频】极海APM32F407xG系列MCU培训
型号- APM32F405VGT6,APM32F091VCT6,APM32F051K6T6,APM32F091CCT6,APM32F003F6P6,APM32F051K8T6,APM32F103CCT6,APM32F405RGT6,APM32F003F6P7,APM32F103VET6,APM32E103VCT6,APM32F051C8T6,APM32F103RCT6,APM32E103ZET6,APM32F103RCT7,APM32F103VCT6,APM32F103ZET6,APM32E103RCT6,APM32E103VET6,APM32F415RGT6,APM32F407RGT6,APM32F030RCT6,APM32F091RCT6,APM32F003F6U7,APM32E103CET6,APM32F407RET6,APM32F407VGT6,APM32F415VGT6,APM32F407VET6,APM32F051K8U6,APM32F051C8U6,APM32F103TBU6,APM32F417VGT6,APM32F103RET6,APM32F051R8T6,APM32F417ZGT6,APM32F405ZGT6,APM32F051K6U6,APM32F030CCT6,APM32F030C8T6,APM32F103CBT6,APM32F103C8T6,APM32F030K6T6,APM32F030R8T6,APM32F103VBT6,APM32F103RBT6,APM32F407ZET6,APM32F407ZGT6,APM32F072VBT6,APM32F072RBT6,APM32F072RBT7,APM32F415ZGT6,APM32F407IET6,APM32F072CBT6,APM32F407IGT6,APM32F030K6U6,APM32E103RET6
极海联亮相2024全球MCU及嵌入式生态发展大会,展出新技术和产品并发表演讲
2024年7月25日,2024全球MCU及嵌入式生态发展大会在深圳君悦酒店举行,由AspenCore主办。极海作为特邀嘉宾在主论坛进行演讲,并展出新技术和产品,包括搭载Arm Cortex M52内核且采用Helium技术的G32R5系列实时控制MCU和针对电机市场的栅极驱动器与APM32M3514系列SoC等新品。
原厂动态 发布时间 : 2024-07-29
极海车规级MCU凭借可靠的产品品质,斩获“2023汽车芯片50强”与“MCU创新先锋”两项大奖
近日,极海车规级MCU凭借可靠的产品品质、专业周到的客户技术支持服务,以及量产级解决方案良好的市场表现,相继斩获“2023汽车芯片50强”、“MCU创新先锋奖”两项行业大奖。作为国内领先的32位车规级芯片设计企业,极海积极布局以服务汽车智能化、电动化、网联化的快速融合。
原厂动态 发布时间 : 2023-11-30
【应用】国产极海半导体APM32系列工业级MCU助力推动新型工业化发展
工业级MCU应用场景范围十分广泛,并对使用寿命、温度、湿度、电磁辐射等有着严格的品质要求。极海半导体长期深耕中高端工控市场,本文就以绝对值编码器、高性能伺服驱动器及变频器方案为例,详细介绍极海半导体32位APM32位工业级MCU在工控领域的出色表现。
应用方案 发布时间 : 2022-11-02
APM32F411xCxE Arm® Cortex®-M4F core-based 32-bit MCU User Manual
型号- APM32,APM32F411XCXE SERIES,APM32F411XCXE
【应用】国产工业级高性能MCU APM32F407VGT6用于PLC工控板,主频168MHz,通信外设丰富
某客户主要做各种工业自动控制系统装置,其中一款国产PLC工控板主控要更换成国产物料,推荐极海工业级高性能MCU APM32F407系列,主频高达168MHz、通信外设资源丰富,工作温度范围-40℃到+85℃。
应用方案 发布时间 : 2023-03-14
【应用】极海32位MCU用于光伏逆变器,内置3个精度12位的ADC,主频高达168MHz
极海半导体APM32F407VGT6在硬件上标准LQFP100封装,高达168MHz的主频,独立FPU模块,高达82个I/O,操作简单,提供标准DEMO驱动程序等优点,是光伏逆变器应用或者其他通用MCU场景的优秀选择。
应用方案 发布时间 : 2022-12-23
APM32F103xCxDxE Arm® Cortex® -M3 based 32-bit MCU User Manual
型号- APM32F103XCXDXE,APM32F103XDXE,APM32F103XCXDXE SERIES,APM32F103XC,APM32F
电子商城
现货市场
服务
可定制显示屏的尺寸0.96”~15.6”,分辨率80*160~3840*2160,TN/IPS视角,支持RGB、MCU、SPI、MIPI、LVDS、HDMI接口,配套定制玻璃、背光、FPCA/PCBA。
最小起订量: 1000 提交需求>
可烧录IC封装SOP/MSOP/SSOP/TSOP/TSSOP/PLCC/QFP/QFN/MLP/MLF/BGA/CSP/SOT/DFN;IC包装Tray/Tube/Tape;IC厂商不限,交期1-3天。支持IC测试(FT/SLT),管装、托盘装、卷带装包装转换,IC打印标记加工。
最小起订量: 1pcs 提交需求>
登录 | 立即注册
提交评论