终于找到原因了,我把串口发的数据先存到flash里,等串口发完后再把flash的数据转存到SD卡中,播放完全正常,这说明主要原因是串口再给SD卡的时候可能数据出错或是SD卡存写出错,其次是SD卡的读取速度和VS1003的速度不匹配,由于程序死在while (GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7) == 0);里,这个原因可能性很小,之后我把SD卡写入的数据打出来一个个的找,终于发现有的一个数据块完全正确,有的数据块的512个字节的第一个字节出错,很是纳闷,后来慢慢的调才缩小范围:是串口发512个字节时可能出错,我的串口中断程序如下:
void USART1_IRQHandler(void)
{
u8 recv_com1_data;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
recv_com1_data = USART_ReceiveData(USART1); //收下从串口1(计算机)发来的数据
/*
//接收计算机发来的命令和短消息 接收命令头0x55 0x55 结束命令0x55 0x35
com1_data_num++;
if(com1_data_num == 1) //接收命令头0x55
{
if(recv_com1_data != 0x55) //如果第一个字符不是0x33,不是有用的信息
{
com1_data_num = 0;
com1_data_sum = 0;
}
}
else if(com1_data_num == 2) //数据2是0X35
{
if((recv_com1_data != 0x55)&&(recv_com1_data != 0x35))
{
com1_data_num = 0; //收到了不认得的信息,不去理会
com1_data_sum = 0;
}
else if(recv_com1_data == 0x35)
{
com1_data_num = 0;
sd_read_start = 1;
}
}
else if(com1_data_num == 514) //单个数据块
{
com1_data_num = 2;
com1_shuju_buff[127] = com1_shuju_buff[127] | recv_com1_data; //单个数据块写
sd_start = 1;
}
else
{
if(recv_com1_data == 0x0F)
{
sd_jieshu = 1;
}
else if((recv_com1_data == 0x0D)&&(sd_jieshu == 1))
{
sd_jieshu = 2;
}
else if((recv_com1_data == 0x0A)&&(sd_jieshu == 2))
{
if((com1_data_num-5)%4==1)
{
com1_shuju_buff[(com1_data_num-5)/4] = com1_shuju_buff[(com1_data_num-5)/4]<<16;
}
else if((com1_data_num-5)%4==2)
{
com1_shuju_buff[(com1_data_num-5)/4] = com1_shuju_buff[(com1_data_num-5)/4]<<8;
}
for(i=((com1_data_num-5)/4+1);i<128;i++)
{
com1_shuju_buff[i] = 0;
}
com1_data_num = 0;
//sd_block_num++;
sd_start = 1;
sd_read_start = 1;
wenjian_end = 1;
}
else
{
sd_jieshu = 0;
if((com1_data_num-2)%4!=0)
{
com1_shuju_buff[(com1_data_num-2)/4] = com1_shuju_buff[(com1_data_num-2)/4] | recv_com1_data;
com1_shuju_buff[(com1_data_num-2)/4] = com1_shuju_buff[(com1_data_num-2)/4]<<8;
}
else
{
com1_shuju_buff[(com1_data_num-2)/4-1] = com1_shuju_buff[(com1_data_num-2)/4-1] | recv_com1_data;
com1_shuju_buff[(com1_data_num-2)/4]=0;
}
}
}
}
}
主函数如下:
while (1)
{
IWDG_ReloadCounter(); //喂狗
if(sd_start == 1)
{
IWDG_ReloadCounter(); //喂狗
Fill_Buffer_sd(Buffer_Block_Tx, com1_shuju_buff, BufferWordsSize); //单块写
if (Status == SD_OK)
{
Status = SD_Erase(sd_block_add, (sd_block_add + 512)); //擦除块数据
}
if (Status == SD_OK)
{
Status = SD_WriteBlock(sd_block_add, Buffer_Block_Tx, BlockSize); //单块写
sd_block_add = sd_block_add + 512; //单块写
}
com1_huancun_num++;
sd_start = 0;
}
if(sd_read_start == 1)
{
for(sd_block_add2 = 1024;sd_block_add2 < sd_block_add;)
{
IWDG_ReloadCounter(); //喂狗
if (Status == SD_OK)
{
Status = SD_ReadBlock(sd_block_add2, Buffer_Block_Rx, BlockSize); //单块
}
if (Status == SD_OK)
{
Fill_Buffer_sd(sd_block_buff, Buffer_Block_Rx, BufferWordsSize); //单块
com1_huancun_num++;
for(i=0;i<BufferWordsSize;i++)
{
mp3_date_ceshi[2*i] = sd_block_buff[i]>>16;
mp3_date_ceshi[2*i+1] = sd_block_buff[i];
//sd_block_buff[i] = 0;
}
for(j=0;j<16;j++)
{
GPIO_ResetBits(GPIOB, GPIO_Pin_8);
for(t=0;t<16;t++)
{
SPI_Readbyte(mp3_date_ceshi[16*j+t]>>8);
SPI_Readbyte(mp3_date_ceshi[16*j+t]);
}
GPIO_SetBits(GPIOB, GPIO_Pin_8);
while (GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7) == 0);//等待DREQ为高
IWDG_ReloadCounter();
}
}
sd_block_add2 = sd_block_add2+BlockSize; //单块
}
sd_read_start = 0;
}
if(sd_block_num == 1)
{
for(i = 0;i < (sd_block_add-1024)/4;i++)
{
USART_SendData(USART1, ceshi_buff[i]>>24);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, ceshi_buff[i]>>16);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, ceshi_buff[i]>>8);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, ceshi_buff[i]);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
Delay(10);
}
sd_block_num = 0;
}
}
望各位高手帮忙看看什么原因导致的,小弟先谢了!!! |