文章目录
模数转换需要read读函数

滑动变阻器2,往3方向拨动,XadcAIN0最多能读到1.8V,往1方向拨动则最多可以读到0V

模拟输入电压 0-1.8VAnalog Input Range:0 to 1.8V

AD寄存器AD DAT最大为0XFFF,最小为0
电阻最大为10K,最下为0
示例程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <errno.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <string.h> #include <stdint.h> #include <termios.h>
int main(void){ 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 adc err!\n"); 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 is %d\n",r); } } }
|