用TQ2440开发板ADC做声音采集,AD转换频率是0.5MHz,AD转换时间是10us,采样频率是25KHz,采样点数是1024。采集数据写入文件,之后用Matlab画出采样点数和数值的波形,图1和图2分别是单频率音乐和拍掌一次声音波形,采集数据代码如下:AD转换时间为5个时钟周期,t=5*(99+1)/50 MHz=10us
(在上层应用ioctl()控制ADC通道切换,四通道引脚悬空或接地或接3.3V时数据均能够正常显示,但是在接入麦克风之后出现波形如图所示那样,跪求大神指导)
#define DEVICE_NAME "/dev/sample"
#define COUNT 1024
int main ()
{
FILE *fp0, *fp1, *fp2, *fp3;
int count = 0;
int fd, ret, value;
char buffer[30];
float temp;
char channel;
// unsigned long arg = 0x63;
fd = open(DEVICE_NAME, 0);
fp0 = fopen("sample1.txt", "w");
fp1 = fopen("sample2.txt", "w");
fp2 = fopen("sample3.txt", "w");
fp3 = fopen("sample4.txt", "w");
if(fp0 == NULL || fp1 == NULL || fp2 == NULL || fp3 == NULL)
{
printf("Open file error!\n");
exit(1);
}
printf("open fd = %d\n", fd);
if(fd < 0)
{
perror("Open ADC Device Failed!\n");
exit(1);
}
while(1)
{
if(count < COUNT)
{
count++;
for(channel = 0; channel < 4; channel++)
{
ioctl(fd, channel, 1);
memset(buffer, 0, 30);
ret = read(fd, buffer, sizeof(buffer) - 1);
if(ret > 0)
{
buffer[ret] = '\0';
sscanf(buffer, "%d\n", &value);
temp =((float)value*3.3)/1024.0;
switch(channel)
{
case 0: fprintf(fp0, "%f\n", temp);
break;
case 1: fprintf(fp1, "%f\n", temp);
break;
case 2: fprintf(fp2, "%f\n", temp);
break;
case 3: fprintf(fp3, "%f\n", temp);
break;
}
// printf("ADC AIN[%d]:voltage:%4.2f Value: %d\n", channel, temp, value);
}
else
{
perror("read ADC device!\n");
exit(1);
}
}
}
else
{
printf("The count of number is OK!\n");
break;
}
// sleep(1);
}
fclose(fp0);
fclose(fp1);
fclose(fp2);
fclose(fp3);
close(fd);
return 0;
} |
|