用几个按键连接STC15W408AS,控制CH7003语音芯片,播放几段语音,为什么不成功,哪里出了问题?
以下是全部代码:
#include <STC15F2K60S2.H>
#include <intrins.h>
// 使用sbit定义按键引脚
sbit KEY1 = P3^2; // 按键1接P3.2
sbit KEY2 = P3^3; // 按键2接P3.3
sbit KEY3 = P3^4; // 按键3接P3.4
sbit KEY4 = P3^5; // 按键4接P3.5
sbit KEY5 = P3^6; // 按键5接P3.6
// CH7003协议定义
#define START_CODE 0x7E
#define END_CODE 0xEF
#define CMD_PLAY 0x13
// 延时函数
void delay_ms(unsigned int ms) {
unsigned int i, j;
for (i = 0; i < ms; i++)
for (j = 0; j < 120; j++);
}
// 串口初始化
void UART_Init() {
SCON = 0x50; // 8位数据,可变波特率
TMOD |= 0x20; // 定时器1模式2
TH1 = 0xFD; // 9600bps
TL1 = 0xFD;
TR1 = 1; // 启动定时器
}
// 发送1字节
void UART_SendByte(unsigned char dat) {
SBUF = dat;
while (!TI);
TI = 0;
}
// 发送播放指令
void CH7003_Play(unsigned char track) {
UART_SendByte(START_CODE);
UART_SendByte(CMD_PLAY);
UART_SendByte(0x00); // 数据长度高字节
UART_SendByte(0x02); // 数据长度低字节
UART_SendByte(0x00); // 曲目号高字节
UART_SendByte(track); // 曲目号低字节
UART_SendByte(END_CODE);
}
void main() {
// 配置所有按键IO为输入(P3.2-P3.6)
P3M0 &= 0x83; // 0b10000011,清空中间5位
P3M1 &= 0x83; // 启用内部上拉
UART_Init(); // 初始化串口
while (1) {
// 检测按键1
if (KEY1 == 0) {
delay_ms(20); // 消抖
if (KEY1 == 0) {
while (KEY1 == 0); // 等待释放
CH7003_Play(1); // 播放曲目1
}
}
// 检测按键2
if (KEY2 == 0) {
delay_ms(20);
if (KEY2 == 0) {
while (KEY2 == 0);
CH7003_Play(2); // 播放曲目2
}
}
// 检测按键3
if (KEY3 == 0) {
delay_ms(20);
if (KEY3 == 0) {
while (KEY3 == 0);
CH7003_Play(3); // 播放曲目3
}
}
// 检测按键4
if (KEY4 == 0) {
delay_ms(20);
if (KEY4 == 0) {
while (KEY4 == 0);
CH7003_Play(4); // 播放曲目4
}
}
// 检测按键5
if (KEY5 == 0) {
delay_ms(20);
if (KEY5 == 0) {
while (KEY5 == 0);
CH7003_Play(5); // 播放曲目5
}
}
}
}
|