用幾個按鍵連接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
}
}
}
}
|