-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/unistd.h>
- #include <sys/stat.h>
- #include <linux/fcntl.h>
- #include <linux/ioctl.h>
-
- #include "adc.h"
-
- int main(int argc, char *argv[])
- {
- if (argc < 2) {
- printf("Useage: %s dev\n", argv[0]);
- exit(0);
- }
-
- struct adc_mode mode = {
- .trigger = ADC_TRIGGER_SOFT,
- .trigger_time = 1000,
- .resolution = 0,
- .sleep_mode = 1,
- .startup_time = 5,
- .sample_time = 8,
- .adc_clock = 8,
- };
-
- int fd = open(argv[1], O_RDWR);
- if (fd == -1){
- perror("open:");
- exit(-1);
- }
-
- int data;
- int ret;
- float tdata=0,a=3.3;
- if (ioctl(fd, ADCCTL_SETMODE, &mode))
- perror("ioctl:ADCCTL_SETMODE");
-
- memset(&mode, 0, sizeof(struct adc_mode));
- ioctl(fd, ADCCTL_GETMODE, &mode);
- printf("adc_mode: f => v\n"
- "trigger => %d\n"
- "trigger_time => %d\n"
- "resolution => %d\n"
- "startup_time => %d\n"
- "sample_time => %d\n"
- "adc_clock => %d\n",
- mode.trigger, mode.trigger_time,
- mode.resolution, mode.startup_time,
- mode.sample_time, mode.adc_clock);
-
- while(1)
- {
- usleep(3000000);
- ioctl(fd, ADCCTL_START);
- ioctl(fd, ADCCTL_GETSTATUS, &ret);
- printf("status: 0x%08x\n", ret);
- ret = 0;
-
-
-
- ioctl(fd, ADCCTL_GETDATA, &data);
- printf("AD_VAL: %d\n", data);
- tdata=(float)data;
- tdata=tdata/1024*a;
- printf("the voltage is %fV\n",tdata);
-
- }
-
- close(fd);
-
- return 0;
-
- }