sam9260 adc 测试

  1. /* 
  2.  * adc_test.c 
  3.  * 
  4.  * Copyright (C) 2007 Mengrz 
  5.  */  
  6.   
  7. #include <stdio.h>  
  8. #include <stdlib.h>  
  9. #include <string.h>  
  10. #include <unistd.h>  
  11. #include <sys/unistd.h>  
  12. #include <sys/stat.h>  
  13. #include <linux/fcntl.h>  
  14. #include <linux/ioctl.h>  
  15.   
  16. #include "adc.h"  
  17.   
  18. int main(int argc, char *argv[])  
  19. {  
  20.     if (argc < 2) {  
  21.     printf("Useage: %s dev ", argv[0]);  
  22.     exit(0);  
  23.     }  
  24.       
  25.     struct adc_mode mode = {  
  26.     .trigger = ADC_TRIGGER_SOFT,  
  27.     .trigger_time = 1000,  
  28.     .resolution = 0,  
  29.     .sleep_mode = 1,  
  30.     .startup_time = 5,  
  31.     .sample_time = 8,  
  32.     .adc_clock = 8,  
  33.     };  
  34.       
  35.     int fd = open(argv[1], O_RDWR);  
  36.     if (fd == -1){  
  37.     perror("open:");  
  38.     exit(-1);  
  39.     }  
  40.       
  41.     int data;  
  42.     int ret;  
  43.     float  tdata=0,a=3.3;  
  44.     if (ioctl(fd, ADCCTL_SETMODE, &mode))  
  45.         perror("ioctl:ADCCTL_SETMODE");  
  46.   
  47.     memset(&mode, 0, sizeof(struct adc_mode));  
  48.     ioctl(fd, ADCCTL_GETMODE, &mode);  
  49.     printf("adc_mode:  f => v "  
  50.        "trigger => %d "  
  51.        "trigger_time => %d "  
  52.        "resolution   => %d "  
  53.        "startup_time => %d "  
  54.        "sample_time  => %d "  
  55.        "adc_clock    => %d ",  
  56.         mode.trigger, mode.trigger_time,  
  57.         mode.resolution, mode.startup_time,  
  58.         mode.sample_time, mode.adc_clock);  
  59.       
  60.     while(1)  
  61.     {  
  62.     usleep(3000000);  
  63.     ioctl(fd, ADCCTL_START);  
  64.     ioctl(fd, ADCCTL_GETSTATUS, &ret);  
  65.     printf("status: 0x%08x ", ret);  
  66.     ret = 0;  
  67.     //while (1)  
  68. //      //ioctl(fd, ADCCTL_GETCNT, &ret);  
  69.     //printf("count: %d ", ret);  
  70.     //    while (1) {  
  71.         ioctl(fd, ADCCTL_GETDATA, &data);  
  72.         printf("AD_VAL: %d ", data);  
  73.         tdata=(float)data;  
  74.         tdata=tdata/1024*a;  
  75.         printf("the voltage is %fV ",tdata);  
  76.     //    ret--;  
  77. //      usleep(200000);  
  78. //  }     
  79.     }  
  80.       
  81.     close(fd);  
  82.       
  83.     return 0;  
  84.       
原文地址:https://www.cnblogs.com/zym0805/p/5863737.html