⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ad-232-test2.c

📁 将s3c2410片内ad转换的数据通过串口上传
💻 C
字号:
#include<sys/stat.h>
#include  <fcntl.h>
#include  <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include  <unistd.h>
#include <termios.h>
#include <sys/signal.h>
#include  <pthread.h>
#include  <stdlib.h>
#include  <sys/ioctl.h>
#include  "s3c2410-adc.h"
#include  <sys/ipc.h>

#define   ADC_DEV                  "/dev/adc/0raw"
#define    COM2                     "/dev/ttyS1"

volatile  int     com2_fd;
static    int     ad_fd;
static    int     stop=0;
static    int     get(int channel);
          int     i;
          float   d;          
//        char    *ptr;
          char    buf[3];
struct   termios  option;
void     com2_ini(void);
static   void* comMonitor(void* data)
{
        getchar();
        stop=1;
        return NULL;
}

/******************************************************************************/
main()
{void     *retval;
          char  b=' ';
          char  c='\n';
          int   f;
 pthread_t th_com;
 if((ad_fd=open(ADC_DEV, O_RDWR))<0)
    {
    printf("Error opening %s adc device\n", ADC_DEV);
    return  -1;
    }
    com2_fd = open(COM2, O_RDWR );  //| O_NOCTTY | O_NDELAY
 if(com2_fd==-1)	
    { 			
		perror("Can't Open Serial Port");
		return -1;		
    }
 com2_ini();
// printf("init ok\n");
pthread_create(&th_com, NULL, comMonitor, 0);
printf("\nPress Enter key exit!\n");
 while(stop==0) {
    for(i=0,f='0'; i<3; i++,f++)
      {
       d=((float)get(i)*3.3)/1024.0;
       printf("a%d=%8.4f\t",i,d);
//     usleep(10);
//     printf("\n")
//     printf(strlen(d));
//     gcvt(d,2,ptr);
       gcvt(d,3,buf);//将浮点数转化为字符串
//     j=j+4;
//     printf("success\n");
//     printf("%s",buf);
//     write(com2_fd,&i,2);
       write(com2_fd,&f,1);
       write(com2_fd,&b,1);
       write(com2_fd,buf,3);
       usleep(1);
       write(com2_fd,&c,1);
      } 
      printf("\r");
     }  
 pthread_join(th_com, &retval);
 printf("\n");
        close(com2_fd);
        return 0;

}
/******************************************************************************/
void com2_ini(void)
   {
        //**********************************************************
        //波特率设置
        //*********************************************************
        tcgetattr(com2_fd,&option); /* save current modem settings */
        cfsetispeed(&option,B9600); /*设置为9600Bps*/
        cfsetospeed(&option,B9600);
        tcsetattr(com2_fd,TCSANOW,&option);
        //**********************************************************
        //无效验, 8 位
        //*********************************************************
        option.c_cflag &= ~PARENB;
        option.c_cflag &= ~CSTOPB;
        option.c_cflag &= ~CSIZE;
        option.c_cflag |= ~CS8;
        //**********************************************************
        //  1  位停止位
        //*********************************************************
        option.c_cflag &= ~CSTOPB;
        //**********************************************************
        //    原始模式(Raw Mode)方式来通讯
        //*********************************************************
        option.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Input*/
        option.c_oflag &= ~OPOST;
    }

static int get(int channel)
   {
   int PRESCALE=0xFF;
   int data=ADC_WRITE(channel, PRESCALE);
   write(ad_fd, &data, sizeof(data));
    read(ad_fd, &data, sizeof(data));
    return data;
  } 


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -