发表于:2014/5/27 10:24:08
#0楼
各位高手,小弟正在基于STM32单片机开发音频采集应用,运行程序后,发现存储的音频文件无内容。调试后发现
调用VS1003_ReadReg(SPI_HDAT1)读出的值不在256和896之间,无法执行后续的操作。请教其中的原因。以下是主函数的代码。谢谢!
u16 w;
FIL* f_rec=0;
__WaveHeader *wavhead=0;
u8 *recbuf;
u8 *pname=0;
u16 idx=0;
u32 sectorsize=0;
RCC_Configuration(); //设置内部时钟及外设时钟使能
if (SysTick_Config(720)) //时钟节拍中断时10us一次 用于定时
{
/* Capture error */
while (1);
}
NVIC_Configuration(); //中断源配置
//xRST =1
Usart1_Init(); //串口1初始化
SPI_VS1003_Init(); //VS1003 初始化
Delay_us(10000); //延时100ms
SD_TEST(); //SD卡测试函数
disk_initialize(0); //fatfs可以管理多个介质的分区, 所以把物理存储介质SST25VF016B标示为0区,相当于磁盘编号
//VS1003_start();/*启动VS1003*/
f_mount(0, &fs); //将文件系统设置到0区
f_rec = (FIL *)malloc (sizeof(FIL)); //开辟FIL字节的内存区域
if (f_rec == NULL)
{
USART_OUT(USART1,"\n f_rec malloc error \n");
}
wavhead = (__WaveHeader*)malloc (sizeof(__WaveHeader));/*分配WAV音频头文件存储空间*/
if (wavhead == NULL)
{
USART_OUT(USART1,"\n wavhead malloc error \n");
}
/*分配接收缓冲区内存空间*/
recbuf = malloc (512);
if(recbuf==NULL)
{
USART_OUT(USART1,"\n recbuf malloc error \n");
}
/*分配pname存储空间*/
pname = malloc(30); //申请30个字节内存,类似"0:RECORDER/REC00001.wav"
if(pname==NULL)
{
USART_OUT(USART1,"\n pname malloc error \n");
}
VS1003_start();/*启动VS1003*/
while (VS1003_ReadReg(SPI_HDAT1)>>8);/*清空缓冲区*/
pname[0]=0; //pname没有任何文件名
/*开始录音*/
recoder_wav_init(wavhead); //初始化wav数据
/*创建音频文件*/
//if (f_open(&fsrc , "0:/audio.wav" , FA_READ | FA_WRITE | FA_CREATE_NEW) != FR_OK)
if (f_open(f_rec , "0:/audio5.wav" , FA_CREATE_ALWAYS | FA_WRITE)!= FR_OK)
{
USART_OUT(USART1,"\n open error \n");
}
USART_OUT(USART1,"\n open success \n");
/*将音频头写入文件*/
if (f_write(f_rec , (const void*)wavhead , sizeof(__WaveHeader) , &bw) != FR_OK)
{
USART_OUT(USART1,"\n write error \n");
}
USART_OUT(USART1,"\n write success \n");
if (f_sync (f_rec) != FR_OK)
{
USART_OUT(USART1,"\n sync error \n");
}
while (1)
{
w=VS1003_ReadReg(SPI_HDAT1);
USART_OUT(USART1,"\n w = %x\n" , w);
if((w>=256)&&(w<896))
{
USART_OUT(USART1,"\n enter if((w>=256)&&(w<896)) \n");
idx=0;
while(idx<512) //一次读取512字节
{
w=VS1003_ReadReg(SPI_HDAT0);
recbuf[idx++]=w&0XFF;
recbuf[idx++]=w>>8;
}
res=f_write(f_rec ,recbuf,512,&bw);//写入文件
if(res)
{
printf("err:%d\r\n",res);
printf("bw:%d\r\n",bw);
break;//写入出错.
}
if (f_sync (f_rec) != FR_OK)
{
USART_OUT(USART1,"\n sync error \n");
}
sectorsize++;//扇区数增加1,约为32ms
}
}
调用VS1003_ReadReg(SPI_HDAT1)读出的值不在256和896之间,无法执行后续的操作。请教其中的原因。以下是主函数的代码。谢谢!
u16 w;
FIL* f_rec=0;
__WaveHeader *wavhead=0;
u8 *recbuf;
u8 *pname=0;
u16 idx=0;
u32 sectorsize=0;
RCC_Configuration(); //设置内部时钟及外设时钟使能
if (SysTick_Config(720)) //时钟节拍中断时10us一次 用于定时
{
/* Capture error */
while (1);
}
NVIC_Configuration(); //中断源配置
//xRST =1
Usart1_Init(); //串口1初始化
SPI_VS1003_Init(); //VS1003 初始化
Delay_us(10000); //延时100ms
SD_TEST(); //SD卡测试函数
disk_initialize(0); //fatfs可以管理多个介质的分区, 所以把物理存储介质SST25VF016B标示为0区,相当于磁盘编号
//VS1003_start();/*启动VS1003*/
f_mount(0, &fs); //将文件系统设置到0区
f_rec = (FIL *)malloc (sizeof(FIL)); //开辟FIL字节的内存区域
if (f_rec == NULL)
{
USART_OUT(USART1,"\n f_rec malloc error \n");
}
wavhead = (__WaveHeader*)malloc (sizeof(__WaveHeader));/*分配WAV音频头文件存储空间*/
if (wavhead == NULL)
{
USART_OUT(USART1,"\n wavhead malloc error \n");
}
/*分配接收缓冲区内存空间*/
recbuf = malloc (512);
if(recbuf==NULL)
{
USART_OUT(USART1,"\n recbuf malloc error \n");
}
/*分配pname存储空间*/
pname = malloc(30); //申请30个字节内存,类似"0:RECORDER/REC00001.wav"
if(pname==NULL)
{
USART_OUT(USART1,"\n pname malloc error \n");
}
VS1003_start();/*启动VS1003*/
while (VS1003_ReadReg(SPI_HDAT1)>>8);/*清空缓冲区*/
pname[0]=0; //pname没有任何文件名
/*开始录音*/
recoder_wav_init(wavhead); //初始化wav数据
/*创建音频文件*/
//if (f_open(&fsrc , "0:/audio.wav" , FA_READ | FA_WRITE | FA_CREATE_NEW) != FR_OK)
if (f_open(f_rec , "0:/audio5.wav" , FA_CREATE_ALWAYS | FA_WRITE)!= FR_OK)
{
USART_OUT(USART1,"\n open error \n");
}
USART_OUT(USART1,"\n open success \n");
/*将音频头写入文件*/
if (f_write(f_rec , (const void*)wavhead , sizeof(__WaveHeader) , &bw) != FR_OK)
{
USART_OUT(USART1,"\n write error \n");
}
USART_OUT(USART1,"\n write success \n");
if (f_sync (f_rec) != FR_OK)
{
USART_OUT(USART1,"\n sync error \n");
}
while (1)
{
w=VS1003_ReadReg(SPI_HDAT1);
USART_OUT(USART1,"\n w = %x\n" , w);
if((w>=256)&&(w<896))
{
USART_OUT(USART1,"\n enter if((w>=256)&&(w<896)) \n");
idx=0;
while(idx<512) //一次读取512字节
{
w=VS1003_ReadReg(SPI_HDAT0);
recbuf[idx++]=w&0XFF;
recbuf[idx++]=w>>8;
}
res=f_write(f_rec ,recbuf,512,&bw);//写入文件
if(res)
{
printf("err:%d\r\n",res);
printf("bw:%d\r\n",bw);
break;//写入出错.
}
if (f_sync (f_rec) != FR_OK)
{
USART_OUT(USART1,"\n sync error \n");
}
sectorsize++;//扇区数增加1,约为32ms
}
}
5分悬赏