CubieBoard中文论坛

 找回密码
 立即注册
搜索
热搜: unable
查看: 5454|回复: 3

想用cubieboard2实现单片机的功能可以吗。。

[复制链接]
发表于 2016-7-16 19:54:32 | 显示全部楼层 |阅读模式
请教各位大神 目前手上有一个传感器和cb2  想通过一个ADC把传感器的模拟信号转换成数字信号 然后用cubieboard2做上位机实现信息收集和处理可以做到吗???早知道就直接买一个单片机了。。感觉会更简单一点  心里苦啊 QAQ
回复

使用道具 举报

发表于 2016-7-18 11:03:29 | 显示全部楼层
你的模拟信号范围是多少?cb2 的uart只能支持0-3V的模拟信号支持输入
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-7-18 11:46:15 | 显示全部楼层
Reachy 发表于 2016-7-18 11:03
你的模拟信号范围是多少?cb2 的uart只能支持0-3V的模拟信号支持输入

谢谢! 是0-10V的  所以我才想加外一个ADC模块  但不知道要怎么调用引脚
回复 支持 反对

使用道具 举报

发表于 2016-7-18 15:22:17 | 显示全部楼层
chalot 发表于 2016-7-18 11:46
谢谢! 是0-10V的  所以我才想加外一个ADC模块  但不知道要怎么调用引脚

通过串口读adc模块传回来的数据吧,
cb2 有很多个串口可以用的,去修改script.fex可以开启需要的串口
首先你要挂载
mount  /dev/mmcblk1p1 /mnt
bin2fex  script.bin script.fex
vi  script.fex
查找uart 开启你需要的串口

这个我烟雾传感器与cb2 串口和蜂鸣器实现报警的情况
开了uart3

我的程序,你参考一下
  1. #include <stdio.h>   
  2. #include <stdlib.h>   
  3. #include <unistd.h>   
  4. #include <sys/types.h>  
  5. #include <sys/stat.h>  
  6. #include <fcntl.h>     
  7. #include <termios.h>   
  8. #include <errno.h>   
  9. #include <sys/time.h>
  10. #include <string.h>
  11. #define TRUE 1
  12. #define FALSE -1
  13. #define FREZZ_ON "echo 0 > /sys/class/gpio/gpio10_pd17/value"
  14. #define FREZZ_OFF "echo 1 > /sys/class/gpio/gpio10_pd17/value"
  15. int speed_arr[] = {B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300,
  16.             B38400, B19200, B9600, B4800, B2400, B1200, B300, };
  17. int name_arr[] = {115200, 38400,  19200,  9600,  4800,  2400,  1200,  300,
  18.             38400,  19200,  9600, 4800, 2400, 1200,  300, };
  19. void set_speed(int fd, int speed)
  20. {
  21.   int i;
  22.   int status;
  23.   struct termios Opt;
  24.   tcgetattr(fd,&Opt);
  25.   for (i= 0;i<sizeof(speed_arr)/sizeof(int);i++)
  26.   {       
  27.         if(speed == name_arr[i])
  28.            {
  29.                        tcflush(fd, TCIOFLUSH);       
  30.                     cfsetispeed(&Opt, speed_arr[i]);       
  31.                     cfsetospeed(&Opt, speed_arr[i]);       
  32.                     status = tcsetattr(fd, TCSANOW, &Opt);
  33.                     if(status != 0)
  34.                    perror("tcsetattr fd1");
  35.                              return;
  36.              }            tcflush(fd,TCIOFLUSH);
  37.    }
  38. }
  39. int set_Parity(int fd,int databits,int stopbits,int parity)
  40. {
  41.         struct termios options;
  42.    if( tcgetattr( fd,&options)!= 0)
  43.    {
  44.                   perror("SetupSerial 1");
  45.                   return(FALSE);
  46.         }
  47.    options.c_cflag &= ~CSIZE;
  48.   switch(databits)
  49.    {
  50.                  case 7:
  51.                   options.c_cflag |= CS7;
  52.                   break;
  53.            case 8:
  54.                 options.c_cflag |= CS8;
  55.                 break;
  56.            default:
  57.                 fprintf(stderr,"Unsupported data size\n");
  58.                 return (FALSE);
  59.         }
  60.    switch(parity)
  61.           {
  62.                   case 'n':
  63.                 case 'N':
  64.                 options.c_cflag &= ~PARENB;        /* Clear parity enable */
  65.                 options.c_iflag &= ~INPCK;        /* Enable parity checking */
  66.                 options.c_iflag &= ~(ICRNL|IGNCR);
  67.                 options.c_lflag &= ~(ICANON );
  68.                 break;       
  69.         case 'o':       
  70.         case 'O':
  71.                 options.c_cflag |= (PARODD | PARENB);
  72.                 options.c_iflag |= INPCK;        /* Disnable parity checking */
  73.                 break;       
  74.         case 'e':       
  75.         case 'E':
  76.                 options.c_cflag |= PARENB;        /* Enable parity */
  77.                 options.c_cflag &= ~PARODD;   
  78.                 options.c_iflag |= INPCK;        /* Disnable parity checking */
  79.                 break;       
  80.         case 'S':       
  81.         case 's':  /*as no parity*/
  82.                 options.c_cflag &= ~PARENB;
  83.                 options.c_cflag &= ~CSTOPB;
  84.                 break;
  85.                 default:
  86.                 fprintf(stderr,"Unsupported parity\n");
  87.                 return (FALSE);
  88.         }
  89.    switch(stopbits)
  90.           {       
  91.           case 1:       
  92.           options.c_cflag &= ~CSTOPB;       
  93.         break;       
  94.         case 2:       
  95.         options.c_cflag |= CSTOPB;       
  96.         break;       
  97.         default:       
  98.         fprintf(stderr,"Unsupported stop bits\n");               
  99.         return (FALSE);
  100.         }
  101.   /* Set input parity option */
  102.    if(parity != 'n')
  103.                   options.c_iflag |= INPCK;
  104.                   options.c_cc[VTIME] = 150; // 15 seconds
  105.        options.c_cc[VMIN] = 0;
  106.                 tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */
  107.   if(tcsetattr(fd,TCSANOW,&options) != 0)
  108.   {
  109.                   perror("SetupSerial 3");
  110.                 return (FALSE);
  111.         }
  112.    return (TRUE);
  113. }
  114. int main(int argc, char **argv)
  115. {
  116.         int fd;
  117.         int nread;        int nwrite;
  118.         int n=0;        int i=0;
  119.         char  buffer[200];
  120.         char devname_head[10] = "/dev/";
  121.         char dev_name[20];
  122.        
  123. #if 1
  124.         if(argc < 2)
  125.         {       
  126.                 printf("Please input './test_uart ttySx'\n");
  127.                   exit(1);
  128.         }
  129.         else
  130.         {
  131.                 strcpy(dev_name, devname_head);
  132.                     strcat(dev_name, argv[1]);
  133.         }       
  134.         fd = open(dev_name, O_RDWR);
  135.         if(fd < 0)
  136.         {
  137.                 perror("error to open /dev/ttySx\n");
  138.                 exit(1);
  139.         }
  140. #endif
  141.         if (fd > 0)
  142.         {
  143.                 set_speed(fd,9600);
  144.         }
  145.         else
  146.         {
  147.                 printf("Can't Open Serial Port!\n");
  148.                 exit(0);
  149.         }
  150.           if (set_Parity(fd,8,1,'N') == FALSE)
  151.           {
  152.                     printf("Set Parity Error\n");
  153.                     exit(1);
  154.           }
  155.        
  156.         system(FREZZ_OFF);
  157.            memset(buffer,0,sizeof(buffer));
  158.           while(1)
  159.           {
  160.                 nread = read(fd,&buffer,200);

  161.                 if(nread < 0)
  162.                 {
  163.                         printf("read error\n");
  164.                 }
  165.                        
  166.                 char a=atoi(buffer);
  167.   
  168.                
  169.                 if(a > 80)
  170.                 {
  171.                          system(FREZZ_ON);                              
  172.                     
  173.                 }
  174.                 else
  175.                 {
  176.                         system(FREZZ_OFF);
  177.                 }                                                      
  178.             memset(buffer,0,sizeof(buffer));

  179.         }       
  180. }
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|粤ICP备13051116号|cubie.cc---深刻的嵌入式技术讨论社区

GMT+8, 2024-5-3 01:59 , Processed in 0.022734 second(s), 16 queries .

Powered by Discuz! X3.4

© 2001-2012 Comsenz Inc. | Style by Coxxs

返回顶部