语音播报模块 JQ8400-FL 的使用

2019 宏晶杯单片机比赛 光源追踪器
2019 传感器原理与应用课程大作业 转盘测速仪

文章目录

            • 调用场景:
            • 依赖:
          • 代码:
            • 光源追踪器代码:
            • 转盘测速仪代码:

调用场景:
依赖:
  • STC 的 IAP15W4K58S4 超级通用串口程序(秘籍)
代码:
光源追踪器代码:

voice.h

#ifndef __VOICE_H
#define __VOICE_H

#include "15W4KxxS4.H"
#include "delay.h"
#include "uart.h"

#define Voice_UART4 1	  //编译开关  使用串口通信
#define Voice_OneLine 0   //编译开关  使用OneLine通信

sbit Voice_Busy = P0^4;	  //语音模块忙碌
extern unsigned char Voice_Volume;

#if(Voice_UART4)
//管脚定义
//相关宏定义
//函数声明
void Voice_Uart4_Init();
void Voice_Uart4_Play_Single(unsigned int index);
void Voice_Uart4_Play_Comb(unsigned char *Str);
void VOICE_UART4_playSpeed(unsigned char *Str);

#endif

#if(Voice_OneLine)
// 管脚定义
sbit VOICE_ONELINE = P0^0;

// 相关宏定义
#define VOICE_ONELINE_Set()   VOICE_ONELINE = 1
#define VOICE_ONELINE_Clear() VOICE_ONELINE = 0

// 函数声明
void Oneline_Send_Byte(unsigned char send_byte);
void Voice_Play_Audio(unsigned char audio_index);
void Voice_Play_Single();
#endif

#endif

voice.c

//  文 件 名   : voice.c 
//              说明: 
//              ----------------------------------------------------------------
//				注意ONELINE发送数据时,先发送低位
//              ----------------------------------------------------------------

#include "voice.h"

unsigned char Voice_Volume = 20;
#if(Voice_UART4)
void Voice_Uart4_Init() {
	UART4_Init(2, 9600);
	INT_CLKO |= 0x30; //xx11 xxxx 允许外部中断3,下降沿触发 允许外部中断2,下降沿触发
	// 音量设为20级
	UART4_PutChar(0xAA); 											//指令码
	UART4_PutChar(0x13); 											//指令类型
	UART4_PutChar(0x01);
	UART4_PutChar(0x14);
	UART4_PutChar(0xD2); 											
}
void Voice_Uart4_Play_Single(unsigned int index) {
	UART4_PutChar(0xAA); 											//指令码
	UART4_PutChar(0x07); 											//指令类型
	UART4_PutChar(0x02); 											//指令中数据部分字节数
	UART4_PutChar((unsigned char)(index>>8));						//所播曲目索引的高八位
	UART4_PutChar((unsigned char)index);							//所播曲目索引的低八位
	UART4_PutChar((unsigned char)(0xAA+0x07+0x02+(index>>8)+index));//之前所有字节的和校验
}

/*组合播放-转速*/
void VOICE_UART4_playSpeed(unsigned char *Str){
	unsigned char sum = 0x00;
	UART4_PutChar(0xAA);//指令码
	UART4_PutChar(0x1B);//指令类型
	UART4_PutChar(0x10);
	sum = 0xAA+0x1B+0x10+'S'+'D'+'2'+'1'+'0'+'D'+'D'+'0'+'0'+'R'+'P';
	UART4_PutChar('S');UART4_PutChar('D');
	UART4_PutChar('2');	sum += *Str;UART4_PutChar(*Str++);
	UART4_PutChar('1');	sum += *Str;UART4_PutChar(*Str++);
	UART4_PutChar('0');	sum += *Str;UART4_PutChar(*Str++);
	UART4_PutChar('D');	UART4_PutChar('D');
	UART4_PutChar('0');	sum += *Str;UART4_PutChar(*Str++);
	UART4_PutChar('0');	sum += *Str;UART4_PutChar(*Str++);
	UART4_PutChar('R');	UART4_PutChar('P');	
	UART4_PutChar(sum);
}

void Voice_Uart4_Play_Comb(unsigned char *Str){
	unsigned char *p = Str;
	unsigned char sum = 0x00;
	UART4_PutChar(0xAA);//指令码
	UART4_PutChar(0x1B);//指令类型
	UART4_PutChar(0x1E);//AX;xx;xx;xx;DU;AY;xx;xx;xx;DU;JL;xx;xx;xx;CM 共30个字节
	
	sum = 0xAA+0x1B+0x1E +'A'+'X'+'2'+'1'+'0'+'D'+'U'+'A'+'Y'+'2'+'1'+'0'+'D'+'U'+'J'+'L'+'2'+'1'+'0'+'C'+'M';
	
	UART4_PutChar('A');
	UART4_PutChar('X');
	UART4_PutChar('2');
	sum += *Str;
	UART4_PutChar(*Str++);
	UART4_PutChar('1');
	sum += *Str;
	UART4_PutChar(*Str++);
	UART4_PutChar('0');
	sum += *Str;
	UART4_PutChar(*Str++);
	UART4_PutChar('D');
	UART4_PutChar('U');
	
	UART4_PutChar('A');
	UART4_PutChar('Y');
	UART4_PutChar('2');
	sum += *Str;
	UART4_PutChar(*Str++);
	UART4_PutChar('1');
	sum += *Str;
	UART4_PutChar(*Str++);
	UART4_PutChar('0');
	sum += *Str;
	UART4_PutChar(*Str++);
	UART4_PutChar('D');
	UART4_PutChar('U');
	
	UART4_PutChar('J');
	UART4_PutChar('L');
	UART4_PutChar('2');
	sum += *Str;
	UART4_PutChar(*Str++);
	UART4_PutChar('1');
	sum += *Str;
	UART4_PutChar(*Str++);
	UART4_PutChar('0');
	sum += *Str;
	UART4_PutChar(*Str++);
	UART4_PutChar('C');
	UART4_PutChar('M');
	
	UART4_PutChar(sum);
}

void Volume_Increase_Int() interrupt 11 {//音量增 使用~INT3
	UART4_PutChar(0xAA); 											//指令码
	UART4_PutChar(0x14); 											//指令类型
	UART4_PutChar(0x00);
	UART4_PutChar(0xBE);
	if(Voice_Volume < 30) {
		Voice_Volume++;
	}
	
}

void Volume_Reduce_Int() interrupt 10 {//音量减 使用~INT2
	UART4_PutChar(0xAA); 											//指令码
	UART4_PutChar(0x15); 											//指令类型
	UART4_PutChar(0x00);
	UART4_PutChar(0xBF);
	if (Voice_Volume > 0) {
		Voice_Volume--;
	}
}
#endif


#if(Voice_OneLine)
void Oneline_Send_0() {
	VOICE_ONELINE_Set();
	delay_10us(40);
	VOICE_ONELINE_Clear();
	delay_10us(120);
}

void Oneline_Send_1() {
	VOICE_ONELINE_Set();
	delay_10us(120);
	VOICE_ONELINE_Clear();
	delay_10us(40);
}

void Oneline_Send_Byte(unsigned char send_byte) {
	unsigned char i;
	VOICE_ONELINE_Set();
	delay_1ms(10);
	VOICE_ONELINE_Clear();
	delay_1ms(2);
	for (i = 0; i < 8; i++) {
		if ((send_byte & 0x01) == 0x01) {
			Oneline_Send_1();
		} else {
			Oneline_Send_0();
		}
		send_byte = send_byte >> 1;
	}
	VOICE_ONELINE_Set();
	
}

void Voice_Volume_Increase() {

}

void Voice_Volume_Reduce() {

}

void Voice_Play_Audio(unsigned char audio_index) {
	unsigned char num_bit_arr[3] = {0};
	char num_bit_count = 2;
	unsigned char num;
	bit flag = 0; // 非零最高位捕获标识
	num = audio_index;
	num_bit_arr[2] = num / 100;
	num %= 100;
	num_bit_arr[1] = num / 10;
	num %= 10;
	num_bit_arr[0] = num;
	Oneline_Send_Byte(0x0A);
	for (num_bit_count = 2; num_bit_count >= 0;  num_bit_count--) {
		if (num_bit_arr[num_bit_count] != 0) {
			flag = 1;
			Oneline_Send_Byte(num_bit_arr[num_bit_count]);
		} else if (flag == 1) {
			Oneline_Send_Byte(num_bit_arr[num_bit_count]);
		} else {
			;
		}

	}
	Oneline_Send_Byte(0x0B);
}

void Voice_Play_Start() {
	Oneline_Send_Byte(0x11);
}
#endif





转盘测速仪代码:

voice.h

#ifndef __VOICE_H
#define __VOICE_H

#include "15W4KxxS4.H"
#include "delay.h"
#include "uart.h"

#define VOICE_UART4 1	  //编译开关  使用串口通信
#define VOICE_OneLine 0   //编译开关  使用OneLine通信

sbit VOICE_Busy = P0^4;	  //语音模块忙碌
extern unsigned char VOICE_Var_Volume;


/*串口模式*/
#if(VOICE_UART4)

void VOICE_UART4_init();
void VOICE_UART4_playSingle(unsigned int index);
void VOICE_UART4_playComb(unsigned char *Str);
void VOICE_UART4_playSpeed(unsigned char *Str);
void VOICE_UART4_playAngle(unsigned char *Str, unsigned char flag);
void VOICE_UART4_playAll(unsigned char *Str);

#endif

/*one_line模式*/
#if(VOICE_OneLine)

sbit VOICE_ONELINE = P0^0;

#define VOICE_ONELINE_Set()   VOICE_ONELINE = 1
#define VOICE_ONELINE_Clear() VOICE_ONELINE = 0

void Oneline_Send_Byte(unsigned char send_byte);
void VOICE_Play_Audio(unsigned char audio_index);
void VOICE_Play_Single();
#endif

#endif

voice.c

//  文 件 名   : VOICE.c
//              说明: 
//              ----------------------------------------------------------------
//				注意ONELINE发送数据时,先发送低位
//              ----------------------------------------------------------------

#include "voice.h"

unsigned char VOICE_Volume = 20;
#if(VOICE_UART4)
/*语音模块初始化,使用串口4,波特率9600*/
void VOICE_UART4_init() {
	UART4_Init(2, 9600);
	INT_CLKO |= 0x30; //xx11 xxxx 允许外部中断3,下降沿触发 允许外部中断2,下降沿触发
	// 音量设为31级
	UART4_PutChar(0xAA); 											//指令码
	UART4_PutChar(0x13); 											//指令类型
	UART4_PutChar(0x01);
	UART4_PutChar(0x1D);
	UART4_PutChar(0xAA+0x13+0x01+0x1D); 											
}

/*播放单曲*/
void VOICE_UART4_playSingle(unsigned int index) {
	UART4_PutChar(0xAA); 											//指令码
	UART4_PutChar(0x07); 											//指令类型
	UART4_PutChar(0x02); 											//指令中数据部分字节数
	UART4_PutChar((unsigned char)(index>>8));						//所播曲目索引的高八位
	UART4_PutChar((unsigned char)index);							//所播曲目索引的低八位
	UART4_PutChar((unsigned char)(0xAA+0x07+0x02+(index>>8)+index));//之前所有字节的和校验
}


// 语音模块命名,得用小写字母,但是串口索引时需要大写字母
/*组合播放-旋转方向及速度*/
void VOICE_UART4_playSpeed(unsigned char *Str){
	unsigned char sum = 0x00, temp;
	
	UART4_PutChar(0xAA);//指令码
	UART4_PutChar(0x1B);//指令类型
	UART4_PutChar(0x14);//20个字符
	sum = 0xAA+0x1B+0x14+'F'+'X'+'3'+'S'+'D'+'2'+'1'+'0'+'D'+'D'+'0'+'0'+'R'+'P';
	// 方向
	UART4_PutChar('F');	UART4_PutChar('X');
	// 3S:'顺时针' 3N:'逆时针'
	temp = *Str++; temp = temp=='+'?'S':'N';
	UART4_PutChar('3');	sum += temp;UART4_PutChar(temp);
	// 转速
	UART4_PutChar('S');UART4_PutChar('D');
	UART4_PutChar('2');	sum += *Str;UART4_PutChar(*Str++);
	UART4_PutChar('1');	sum += *Str;UART4_PutChar(*Str++);
	UART4_PutChar('0');	sum += *Str;UART4_PutChar(*Str++);
	UART4_PutChar('D');	UART4_PutChar('D');
	UART4_PutChar('0');	sum += *Str;UART4_PutChar(*Str++);
	UART4_PutChar('0');	sum += *Str;UART4_PutChar(*Str++);
	UART4_PutChar('R');	UART4_PutChar('P');	

	UART4_PutChar(sum);
}

void VOICE_UART4_playAngle(unsigned char *Str, unsigned char flag) {
	unsigned char sum = 0x00, temp;
	UART4_PutChar(0xAA);//指令码
	UART4_PutChar(0x1B);//指令类型
	UART4_PutChar(0x12);//18个字符
	if(flag == 'x') {
		sum = 0xAA+0x1B+0x12+'H'+'X'+'4'+'2'+'1'+'0'+'D'+'D'+'0'+'0'+'D'+'U';
		// 航向角
		UART4_PutChar('H');UART4_PutChar('X');
	} else if(flag == 'y') {
		sum = 0xAA+0x1B+0x12+'F'+'Y'+'4'+'2'+'1'+'0'+'D'+'D'+'0'+'0'+'D'+'U';
		// 俯仰角
		UART4_PutChar('F');UART4_PutChar('Y');
	} else if(flag == 'z') {
		sum = 0xAA+0x1B+0x12+'H'+'G'+'4'+'2'+'1'+'0'+'D'+'D'+'0'+'0'+'D'+'U';
		// 横滚角
		UART4_PutChar('H');UART4_PutChar('G');
	}

	// 4Z:'正' 4F:'负'
	temp = *Str++; temp = temp=='+'?'Z':'F';
	UART4_PutChar('4');	sum += temp;UART4_PutChar(temp);
	UART4_PutChar('2');	sum += *Str;UART4_PutChar(*Str++);
	UART4_PutChar('1');	sum += *Str;UART4_PutChar(*Str++);
	UART4_PutChar('0');	sum += *Str;UART4_PutChar(*Str++);
	UART4_PutChar('D');	UART4_PutChar('D');
	UART4_PutChar('0');	sum += *Str;UART4_PutChar(*Str++);
	UART4_PutChar('0');	sum += *Str;UART4_PutChar(*Str++);
	UART4_PutChar('D');	UART4_PutChar('U');	

	UART4_PutChar(sum);
}

// 语音模块命名,得用小写字母,但是串口索引时需要大写字母
/*组合播放-旋转方向,速度,航向角,俯仰角及横滚角*/
//void VOICE_UART4_playAll(unsigned char *Str){
//	unsigned char sum = 0x00, temp, i, num;
//	num = 26;
//	UART4_PutChar(0xAA);//指令码
//	UART4_PutChar(0x1B);//指令类型
//	UART4_PutChar(num*2);//20个字符
//	sum = 0xAA+0x1B+num*2+num*'F'+num*'X';
//	for(i=0; i
//		UART4_PutChar('F');	UART4_PutChar('X');
//	}
//	UART4_PutChar(sum);
//}

void VOICE_UART4_playAll(unsigned char *Str){
	unsigned char sum = 0x00, temp;

	UART4_PutChar(0xAA);//指令码
	UART4_PutChar(0x1B);//指令类型
	UART4_PutChar(0x30);// 8*2*3 = 48字节
	sum = 0xAA+0x1B+0x30;
	sum += 'S'+'D'+'3'+'2'+'1'+'0'+'D'+'D'+'0'+'R'+'P'; //8个音频
	sum += 'H'+'G'+'4'+'2'+'1'+'0'+'D'+'D'+'0'+'D'+'U'; //12+6 ?
	sum += 'F'+'Y'+'4'+'2'+'1'+'0'+'D'+'D'+'0'+'D'+'U'; //
	

	// 转速
	UART4_PutChar('S');UART4_PutChar('D');
	// 3S:'顺时针' 3N:'逆时针'
	temp = *Str++; temp = temp=='+'?'S':'N';
	UART4_PutChar('3');	sum += temp;UART4_PutChar(temp);
	UART4_PutChar('2');	sum += *Str;UART4_PutChar(*Str++);
	UART4_PutChar('1');	sum += *Str;UART4_PutChar(*Str++);
	UART4_PutChar('0');	sum += *Str;UART4_PutChar(*Str++);
	UART4_PutChar('D');	UART4_PutChar('D');
	UART4_PutChar('0');	sum += *Str;UART4_PutChar(*Str++);
	Str++; // 漏播一个小数,一个参数有8个音频组合
	UART4_PutChar('R');	UART4_PutChar('P');

	//横滚角
	UART4_PutChar('H');UART4_PutChar('G');
	temp = *Str++; temp = temp=='+'?'Z':'F';
	UART4_PutChar('4');	sum += temp;UART4_PutChar(temp);
	UART4_PutChar('2');	sum += *Str;UART4_PutChar(*Str++);
	UART4_PutChar('1');	sum += *Str;UART4_PutChar(*Str++);
	UART4_PutChar('0');	sum += *Str;UART4_PutChar(*Str++);
	UART4_PutChar('D');	UART4_PutChar('D');
	UART4_PutChar('0');	sum += *Str;UART4_PutChar(*Str++);
	Str++; // 漏播一个小数,一个参数有8个音频组合
	UART4_PutChar('D');	UART4_PutChar('U');

	//俯仰角
	UART4_PutChar('F');UART4_PutChar('Y');
	temp = *Str++; temp = temp=='+'?'Z':'F';
	UART4_PutChar('4');	sum += temp;UART4_PutChar(temp);
	UART4_PutChar('2');	sum += *Str;UART4_PutChar(*Str++);
	UART4_PutChar('1');	sum += *Str;UART4_PutChar(*Str++);
	UART4_PutChar('0');	sum += *Str;UART4_PutChar(*Str++);
	UART4_PutChar('D');	UART4_PutChar('D');
	UART4_PutChar('0');	sum += *Str;UART4_PutChar(*Str++);
	Str++; // 漏播一个小数,一个参数有8个音频组合
	UART4_PutChar('D');	UART4_PutChar('U');


	UART4_PutChar(sum);
}

void Volume_Increase_Int() interrupt 11 {//音量增 使用~INT3
	UART4_PutChar(0xAA); 											//指令码
	UART4_PutChar(0x14); 											//指令类型
	UART4_PutChar(0x00);
	UART4_PutChar(0xBE);
	if(VOICE_Volume < 31) {
		VOICE_Volume++;
	}
	
}

void Volume_Reduce_Int() interrupt 10 {//音量减 使用~INT2
	UART4_PutChar(0xAA); 											//指令码
	UART4_PutChar(0x15); 											//指令类型
	UART4_PutChar(0x00);
	UART4_PutChar(0xBF);
	if (VOICE_Volume > 0) {
		VOICE_Volume--;
	}
}
#endif


#if(VOICE_OneLine)
void Oneline_Send_0() {
	VOICE_ONELINE_Set();
	delay_10us(40);
	VOICE_ONELINE_Clear();
	delay_10us(120);
}

void Oneline_Send_1() {
	VOICE_ONELINE_Set();
	delay_10us(120);
	VOICE_ONELINE_Clear();
	delay_10us(40);
}

void Oneline_Send_Byte(unsigned char send_byte) {
	unsigned char i;
	VOICE_ONELINE_Set();
	delay_1ms(10);
	VOICE_ONELINE_Clear();
	delay_1ms(2);
	for (i = 0; i < 8; i++) {
		if ((send_byte & 0x01) == 0x01) {
			Oneline_Send_1();
		} else {
			Oneline_Send_0();
		}
		send_byte = send_byte >> 1;
	}
	VOICE_ONELINE_Set();
	
}

void VOICE_Volume_Increase() {

}

void VOICE_Volume_Reduce() {

}

void VOICE_Play_Audio(unsigned char audio_index) {
	unsigned char num_bit_arr[3] = {0};
	char num_bit_count = 2;
	unsigned char num;
	bit flag = 0; // 非零最高位捕获标识
	num = audio_index;
	num_bit_arr[2] = num / 100;
	num %= 100;
	num_bit_arr[1] = num / 10;
	num %= 10;
	num_bit_arr[0] = num;
	Oneline_Send_Byte(0x0A);
	for (num_bit_count = 2; num_bit_count >= 0;  num_bit_count--) {
		if (num_bit_arr[num_bit_count] != 0) {
			flag = 1;
			Oneline_Send_Byte(num_bit_arr[num_bit_count]);
		} else if (flag == 1) {
			Oneline_Send_Byte(num_bit_arr[num_bit_count]);
		} else {
			;
		}

	}
	Oneline_Send_Byte(0x0B);
}

void VOICE_Play_Start() {
	Oneline_Send_Byte(0x11);
}
#endif

你可能感兴趣的:(外设)