免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1620 | 回复: 1
打印 上一主题 下一主题

大家帮忙解释个程序 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-05-22 13:18 |只看该作者 |倒序浏览
以下是AD和DA接口的程序,小弟是初学者,请大家帮忙标注一下各个语句的意思,感激不尽。

AD:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/ioctl.h>
#include <pthread.h>
#include <fcntl.h>
#include "s3c2410-adc.h"

#define ADC_DEV                "/dev/adc/0raw"
static int adc_fd = -1;

static int init_ADdevice(void)
{
        if((adc_fd=open(ADC_DEV, O_RDWR))<0){
                printf("Error opening %s adc device\n", ADC_DEV);
                return -1;
        }
}

static int GetADresult(int channel)
{
        int PRESCALE=0XFF;
        int data=ADC_WRITE(channel, PRESCALE);
        write(adc_fd, &data, sizeof(data));
        read(adc_fd, &data, sizeof(data));
        return data;
}
static int stop=0;

static void* comMonitor(void* data)
{
        getchar();
        stop=1;
        return NULL;
}

int main(void)
{
        int i;
        float d;
        pthread_t th_com;
        void * retval;

        //set s3c44b0 AD register and start AD
        if(init_ADdevice()<0)
                return -1;
                                                                                
        /* Create the threads */
        pthread_create(&th_com, NULL, comMonitor, 0);

        printf("\nPress Enter key exit!\n");

        while( stop==0 ){
                for(i=0; i<=2; i++){//采样0~2路A/D值
                        d=((float)GetADresult(i)*3.3)/1024.0;
                        printf("a%d=%8.4f\t",i,d);
                }
                usleep(1);
                printf("\r");
        }
        /* Wait until producer and consumer finish. */
        pthread_join(th_com, &retval);

        printf("\n");
        return 0;
}


DA:
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>

#define DA_IOCTRL_WRITE         0x10
#define DA_IOCTRL_CLR                 0x12

#define Max504_FULL                        4.096f
static int da_fd = -1;
char *DA_DEV="/dev/exio/0raw";

void Delay(int t)
{
        int i;
        for(;t>0;t--)
                for(i=0;i<400;i++);
}

/****************************************************************/
int main(int argc, char **argv)
{
        float v;
        unsigned int value;
//         char *da_dev;
//        unsigned int da_num=0;
       
        if(argc <2){
                printf("\n");
                printf("Error parameter\n");               
                printf("Input as:\n");
                printf("[~]./da_main  num\n");               
                printf("    num: range 0.0 ~ 4.096\n");
                printf("\n");
                return 1;
        }
       
        sscanf(argv[1], "%f",&v);       
        if(v<0 || v>Max504_FULL){
                printf("DA out must between: 0 to %f\n", Max504_FULL);
                return 1;
        }
       
        value=(unsigned int)((v*1024.0f)/Max504_FULL);
       
        if((da_fd=open(DA_DEV, O_WRONLY))<0){
                printf("Error opening /dev/exio/0raw device\n");
                return 1;
        }
       
                ioctl(da_fd, DA_IOCTRL_CLR);                //clear da.
                ioctl(da_fd, DA_IOCTRL_WRITE, &value);               
        close(da_fd);                                                         
        printf("Current Voltage is %f v\n", v);
        return 0;
}

论坛徽章:
0
2 [报告]
发表于 2008-05-22 16:53 |只看该作者
程序设计方式和语言的基础是两码事。
我可以告诉你的是,里面不是你自己定义的函数,它们的用法都有手册可以查。

  1. man func
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP