一、本章简介
本章以core-G1【STM32F103C8T6】核心板 +Q1 cenozoic开发板 + QAU02模块 音频功率放大器实验 之入门系列12 工程为例,本例程结合QAU02模块与Q1 cenozoic开发板,通过将STM32通用定时器配置成脉冲宽度调制模式,来驱动音频功率放大器工作
二、实验平台
1、硬件:core-G1核心板、QAU02模块、cenozoic开发板
2、编译软件:MDK-RAM Plus V5.26.2.0
3、STM32F10X固件库版本:3.5版本
4、仿真器:QQ14A编译调试器
三、版权声明
作者:职业劝退师
声明:转载请注明出处。
纠错:1911996023@qq.com
本例程开源资料下载地址:http://www.enmaking.com/material/ma_F6UYxfNecore-G1【STM32F103C8T6】核心板购买链接:
QQ14A编译调试器购买链接:https://item.taobao.com/item.htm?spm=a1z10.3-c-s.w4002-22623360017.13.407d38d5ciC9zV&id=624413749124
四、实验前提
1、在进行本文步骤前,请先阅读以下章节
1)《STM32中文参考手册_V10》的十三至十五章节。
2)《STM32定时器的使用》全章内容
2、在进行本文步骤前,请先实现以下章节:
1)MDK5的软件安装。安装教程链接如下 http://www.enmaking.com/article/ar_cXHjU6Uj
2)阅读《core-G1【STM32F103C8T8】核心板+Q1cenozoic开发板 +QAU04模块蜂鸣片实验之入门系列11》文章,链接如下 http://www.enmaking.com/article/ar_RBNwclCH
五、硬件原理
1、实物图
2、连接说明
1)core-G1开发板插入pack-Q1底板的M1接口,并用螺丝固定住
2)蜂鸣片连接到QAU02的输出
3)用杜邦线连接以下接口 pack-Q1 QAU04 B20------>INP B11------>EN VDD------>VCC GND------>GND
连接后实物如下
六、基础知识
脉冲宽度调制模式可以产生一个由TIMx_ARR寄存器确定频率、由TIMx_CCRx寄存器确定占空比的信号。在TIMx_CCMRx寄存器中的OCxM位写入’110’(PWM模式1)或’111’(PWM模式2),能够独立地设置每个OCx输出通道产生一路PWM。必须设置TIMx_CCMRx寄存器OCxPE位以使能相应的预装载寄存器,最后还要设置TIMx_CR1寄存器的ARPE位,(在向上计数或中心对称模式中)使能自动重装载的预装载寄存器。
七、实验步骤
1、编写通用定时器初始化程序。
//PWM输出初始化
//arr:自动重装值
//psc:时钟预分频数
void TIM1_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler =psc;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3, ENABLE);
TIM_Cmd(TIM3, ENABLE);
}
2、将初始化程序添加到自己的工程中
3、编写自己的业务代码
void buzzerQuiet(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_Cmd(TIM3, DISABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB,GPIO_Pin_5);
}
void buzzerSound(unsigned short usFreq)
{
GPIO_InitTypeDef GPIO_InitStructure;
unsigned long ulVal;
if((usFreq<=20)||(usFreq>20000))
{
buzzerQuiet();
}
else
{
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
ulVal=7200000/usFreq;
TIM3->ARR =2*ulVal ;
TIM_SetCompare2(TIM3,ulVal);
TIM_Cmd(TIM3, ENABLE);
}
}
void musicPlay(void);
#define L1 262
#define L2 294
#define L3 330
#define L4 349
#define L5 392
#define L6 440
#define L7 494
#define M1 523
#define M2 587
#define M3 659
#define M4 698
#define M5 784
#define M6 880
#define M7 988
#define H1 1047
#define H2 1175
#define H3 1319
#define H4 1397
#define H5 1568
#define H6 1760
#define H7 1976
#define TT 2000
typedef struct
{
short mName;
short mTime;
}tNote;
const tNote MyScore[]=
{
{L6,TT/4},{L3,TT/8},{L5,TT/8},{L6,TT/4},{L6,TT/8},{M1,TT/8},{M1,TT/4},{M2,TT/8},{M1,TT/8},{L6,TT/2},{M1,TT/4},{M1,TT/8},
{L5,TT/8},{M1,TT/8},{M2,TT/8},{M3,TT/8},{M5,TT/8},{M5,TT/8},{M3,TT/8},{M2,TT/4},{M3,TT/2},{M6,TT/8},{M6,TT/8},{M6,TT/8},
{M5,TT/8},{M3,TT/8},{M3,TT/4},{M1,TT/8},{L6,TT/8},{L6,TT/8},{L6,TT/8},{M3,TT/8},{M2,TT/2},{M3,TT/8},{M3,TT/8},{M5,TT/8},
{M3,TT/8},{M2,TT/8},{M3,TT/8},{M2,TT/8},{M1,TT/8},{L6,TT/4},{L5,TT/4},{L6,TT/2}, {L6,TT/4},{L3,TT/8},{L5,TT/8},{L6,TT/4},
{L6,TT/8},{M1,TT/8},{M1,TT/4},{M2,TT/8},{M1,TT/8},{L6,TT/2},{M1,TT/4},{M1,TT/8},{L5,TT/8},{M1,TT/8},{M2,TT/8},{M3,TT/8},
{M5,TT/8},{M5,TT/8},{M3,TT/8},{M2,TT/4},{M3,TT/2},{M6,TT/8},{M6,TT/8},{M6,TT/8},{M5,TT/8},{M3,TT/8},{M3,TT/4},{M1,TT/8},
{L6,TT/8},{L6,TT/8},{L6,TT/8},{M3,TT/8},{M2,TT/2},{M3,TT/8},{M3,TT/8},{M5,TT/8},{M3,TT/8},{M2,TT/8},{M3,TT/8},{M2,TT/8},
{M1,TT/8},{L6,TT/4},{L5,TT/4},{L6,TT/2}, {M3,TT/8},{M3,TT/8},{M5,TT/8},{M3,TT/8},{M3,TT/8},{M5,TT/8},{M5,TT/8},{M6,TT/8},
{H1,TT/8},{M6,TT/8},{M5,TT/4},{M6,TT/2},{L6,TT/4},{L6,TT/8},{L5,TT/8},{L6,TT/4},{M1,TT/4},{M2,TT/8},{M3,TT/16},{M2,TT/16},
{M1,TT/8},{M2,TT/8},{M3,TT/2},{L6,TT/8},{M6,TT/8},{M6,TT/8},{M5,TT/8},{M2,TT/8},{M3,TT/16},{M2,TT/16},{M1,TT/8},{M2,TT/8},
{M3,TT/2},{M1,TT/8},{L6,TT/8},{L6,TT/8},{M1,TT/8},{M2,TT/4},{L5,TT/8},{L5,TT/8},{M3,TT/8},{M5,TT/8},{M3,TT/8},{M2,TT/8},
{M1,TT/2},{L6,TT/8},{M1,TT/8},{M2,TT/8},{M3,TT/8},{M2,TT/8},{M1,TT/8},{L5,TT/8},{L3,TT/8},{L6,TT/2},{L6,TT/4},{L6,TT/8},
{L5,TT/8},{L6,TT/4},{M1,TT/4},{M2,TT/8},{M3,TT/16},{M2,TT/16},{M1,TT/8},{M2,TT/8},{M3,TT/2},{L6,TT/8},{M6,TT/8},{M6,TT/8},
{M5,TT/8},{M2,TT/8},{M3,TT/16},{M2,TT/16},{M1,TT/8},{M2,TT/8},{M3,TT/2},{M1,TT/8},{L6,TT/8},{L6,TT/8},{M1,TT/8},{M2,TT/4},
{L5,TT/8},{L5,TT/8},{M3,TT/8},{M5,TT/8},{M3,TT/8},{M2,TT/8},{M1,TT/4+TT/8},{M1,TT/8},{L6,TT/8},{M1,TT/8},{M2,TT/8},{M3,TT/8},
{M5,TT/8},{M3,TT/8},{M3,TT/8},{M5,TT/8},{M6,TT/2},{M6,TT/2},{L6,TT/4},{L6,TT/8},{L5,TT/8},{L6,TT/4},{L6,TT/8},{M1,TT/8},{M2,TT/8},
{M3,TT/16},{M2,TT/16},{M1,TT/8},{M2,TT/8},{M3,TT/2},{M6,TT/8},{M5,TT/8},{M3,TT/8},{M2,TT/8},{M5,TT/8},{M3,TT/8},{M2,TT/8},{M1,TT/8},
{M1,TT/2},{L6,TT/4},{L6,TT/8},{L5,TT/8},{L6,TT/4},{M1,TT/4},{M2,TT/8},{M3,TT/16},{M2,TT/16},{M1,TT/8},{M2,TT/8},{M3,TT/2},{L6,TT/8},{M6,TT/8},
{M6,TT/8},{M5,TT/8},{M2,TT/8},{M3,TT/16},{M2,TT/16},{M1,TT/8},{M2,TT/8},{M3,TT/2},{M1,TT/8},{L6,TT/8},{L6,TT/8},{M1,TT/8},{M2,TT/4},{L5,TT/8},
{L5,TT/8},{M3,TT/8},{M5,TT/8},{M3,TT/8},{M2,TT/8},{M1,TT/2},{L6,TT/8},{M1,TT/8},{M2,TT/8},{M3,TT/8},{M2,TT/8},{M1,TT/8},{L5,TT/8},{L3,TT/8},
{L6,TT/2},{L6,TT/4},{L6,TT/8},{L5,TT/8},{L6,TT/4},{M1,TT/4},{M2,TT/8},{M3,TT/16},{M2,TT/16},{M1,TT/8},{M2,TT/8},{M3,TT/2},{L6,TT/8},{M6,TT/8},
{M6,TT/8},{M5,TT/8},{M2,TT/8},{M3,TT/16},{M2,TT/16},{M1,TT/8},{M2,TT/8},{M3,TT/2},{M1,TT/8},{L6,TT/8},{L6,TT/8},{M1,TT/8},{M2,TT/4},{L5,TT/8},
{L5,TT/8},{M3,TT/8},{M5,TT/8},{M3,TT/8},{M2,TT/8},{M1,TT/4+TT/8},{M1,TT/8},{L6,TT/8},{M1,TT/8},{M2,TT/8},{M3,TT/8},{M5,TT/8},{M3,TT/8},{M3,TT/8},
{M5,TT/8},{M6,TT/2},{M6,TT/2},{0,0},
};
const tNote MyScore1[]=
{
{L5, TT/8},{L5, TT/8},{L6, TT/4},{L5,TT/4},{M1,TT/4},{L7, TT/2},{L5, TT/8},{L5, TT/8},{L6, TT/4},{L5,TT/4},{M2,TT/4},{M1, TT/2},{L5, TT/8},{L5, TT/8},{M5, TT/4},{M3,
TT/4},{M1,TT/4},{L7, TT/4},{L6, TT/4},{M4, TT/8},{M4, TT/16},{M4, TT/16},{M3,TT/4},{M1, TT/4},{M2,TT/4},{M1, TT/2},
};
int main(void)
{
JTAG_Set(1);//ENABLE SWD
MY_NVIC_PriorityGroupConfig(2);//统一组2
delay_init(72);
TIM1_PWM_Init(14399,0);
LED_Init();
KEY_Init();
while (1)
{
musicPlay();
delay_ms(50);
}
}
void musicPlay(void)
{
u8 i=0;
while(1)
{
if (MyScore[i].mTime == 0) break;
buzzerSound(MyScore[i].mName); //播放音乐
delay_ms(MyScore[i].mTime);
i++;
buzzerQuiet(); // 蜂鸣器静音
delay_ms(10);// 10 ms
}
}
void EXTI0_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line0)!=RESET)//PA0
{
//delay_ms(10);
PAout(2)=~PAout(2);
EXTI_ClearITPendingBit(EXTI_Line0); //清除LINE0线路挂起位
}
}
最后编译并将程序烧录到开发版中。
八、注意事项
暂无
九、实验结果
上电后,喇叭无音频输出,按下K2按键,QAU02使能输出,喇叭发出悦耳的曲调,若再次按下K2按键,QAU02失能输出,喇叭无音频输出。与预期的结果一致。
![3605e49737603eafa52aec2b7918aac2.png](https://i-blog.csdnimg.cn/blog_migrate/3a67976dca4620aa262afac451511c09.png)