本文主要是介绍day31.ADC模数转换,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
第31节:ADC模数转换1、编写编译运行测试
root@ubuntu:/home/linuxsystemcode/adc# cat adc.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
main(){
int fd;
char *adc ="/dev/adc";
char buffer[512];
int len = 0,r = 0;
memset(buffer,0,sizeof(buffer));
printf("adc ready!\n");
if((fd = open(adc,O_RDWR|O_NOCTTY|O_NDELAY))<0){
printf("open %s err\n",adc);
}
else{
printf("open adc success!\n");
len = read(fd,buffer,10);
if(len == 0)
printf("return null\n");
else{
r = atoi(buffer);
r =(int)(r*10000/4095);
printf("res value in %d\n",r);
}
}
}
2、进行文件编译
root@ubuntu:/home/linuxsystemcode/adc# arm-none-linux-gnueabi-gcc -o adc adc.c -static
root@ubuntu:/home/linuxsystemcode/adc# ls
adc adc.c
3、将文件拷贝到TF卡上,并传入开发板上运行结果
[root@iTOP-4412]# mount /dev/mmcblk1p1 /mnt
[root@iTOP-4412]# cd /mnt
[root@iTOP-4412]# ./adc
adc ready![ 1486.831707] adc opened
[ 1486.833399] sampling ...
[ 1486.836011] read 1: 0x8bf
[ 1486.838695] sampling ...
[ 1486.841206] read 1: 0x8c0
[ 1486.843759] sampling ...
[ 1486.846405] read 1: 0x8c2
[ 1486.848966] sampling ...
[ 1486.851612] read 1: 0x8c2
[ 1486.854175] sampling ...
[ 1486.857073] read 1: 0x8c3
[ 1486.859384] sampling ...
[ 1486.862042] read 1: 0x8c2
[ 1486.864591] sampling ...
[ 1486.867237] read 1: 0x8c6
[ 1486.869939] sampling ...
[ 1486.872452] read 1: 0x8c2
[ 1486.875006] sampling ...
[ 1486.877653] read 1: 0x8c6
[ 1486.880257] sampling ...
[ 1486.882814] read 1: 0x8bf
[ 1486.885467] value = 0x8c2
[ 1486.888626] adc closed
open adc success!
res value in 5474
这篇关于day31.ADC模数转换的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!