写一个基于stm32使用AD控制jr6001语音模块播放不同音频的库函数
时间: 2024-01-24 15:05:04 浏览: 222
基于stm32语音播放系统设计
以下是一个基于STM32的AD控制JR6001语音模块播放不同音频的库函数示例代码:
```c
#include "stm32f10x.h"
#define AD_CHANNEL GPIO_Pin_0
#define AD_PORT GPIOA
#define JR6001_PORT GPIOB
#define JR6001_DATA_PIN GPIO_Pin_0
#define JR6001_CLK_PIN GPIO_Pin_1
#define JR6001_RST_PIN GPIO_Pin_2
void jr6001_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = JR6001_DATA_PIN | JR6001_CLK_PIN | JR6001_RST_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(JR6001_PORT, &GPIO_InitStructure);
GPIO_ResetBits(JR6001_PORT, JR6001_RST_PIN);
GPIO_SetBits(JR6001_PORT, JR6001_RST_PIN);
GPIO_ResetBits(JR6001_PORT, JR6001_DATA_PIN);
GPIO_SetBits(JR6001_PORT, JR6001_DATA_PIN);
GPIO_ResetBits(JR6001_PORT, JR6001_CLK_PIN);
GPIO_SetBits(JR6001_PORT, JR6001_CLK_PIN);
}
void jr6001_send_cmd(uint16_t cmd)
{
int i;
GPIO_ResetBits(JR6001_PORT, JR6001_CLK_PIN);
GPIO_SetBits(JR6001_PORT, JR6001_DATA_PIN);
for (i = 0; i < 16; i++) {
if (cmd & 0x8000) {
GPIO_SetBits(JR6001_PORT, JR6001_DATA_PIN);
} else {
GPIO_ResetBits(JR6001_PORT, JR6001_DATA_PIN);
}
cmd <<= 1;
GPIO_SetBits(JR6001_PORT, JR6001_CLK_PIN);
GPIO_ResetBits(JR6001_PORT, JR6001_CLK_PIN);
}
}
void jr6001_play_sound(uint16_t sound)
{
jr6001_send_cmd(0x7F00 | sound);
}
uint16_t read_adc(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
uint16_t ad_value;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_ADC1, ENABLE);
GPIO_InitStructure.GPIO_Pin = AD_CHANNEL;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(AD_PORT, &GPIO_InitStructure);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_55Cycles5);
ADC_Cmd(ADC1, ENABLE);
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while (!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
ad_value = ADC_GetConversionValue(ADC1);
ADC_Cmd(ADC1, DISABLE);
ADC_DeInit(ADC1);
return ad_value;
}
int main(void)
{
jr6001_init();
while (1) {
uint16_t ad_value = read_adc();
if (ad_value < 100) {
jr6001_play_sound(1);
} else if (ad_value < 200) {
jr6001_play_sound(2);
} else {
jr6001_play_sound(3);
}
}
}
```
在这个示例代码中,我们首先定义了JR6001语音模块的端口和引脚,然后实现了一个JR6001初始化函数和一个发送命令函数。主函数中我们使用了一个ADC来获取输入的模拟电压值,并根据不同的电压值调用不同的JR6001播放音频的函数。
阅读全文