adc测试程序

一、函数说明

1、open函数使用

int fd = open("/dev/adc", 0);

2、设置通道

ioctl(fd, ADC_SET_CHANNEL, channel);

3、读取ADC转化的值

int len = read(fd, buffer, sizeof buffer -1);

4、字符串转化整型函数

sscanf(buffer, "%d", &value);

5、输出格式

sprintf(outbuff, "AIN%d %d\n", channel, value);

6、输出结果

printf("%s",output);

二、测试程序

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/fs.h>
#include <errno.h>
#include <string.h>

#define ADC_SET_CHANNEL         0xc000fa01
#define ADC_SET_ADCTSC          0xc000fa02

/*
1) cd Linux-2.6.38
2) make menuconfig
3) enter Device Drivers--->Input device support  --->Touchscreens
4) unselect "S3C touchscreen driver for Mini6410"
5) make
*/

#define CHANNELCOUNT 6
int main(int argc, char* argv[])
{
    int channels[CHANNELCOUNT] = {0,1,4,5,6,7}; //for 6410
    // int channels[CHANNELCOUNT] = {0,1,6,7,8,9}; //for 210
    int channel;
    int i=0;
	fprintf(stderr, "press Ctrl-C to stop\n");
	int fd = open("/dev/adc", 0);
	if (fd < 0) {
		perror("open ADC device:");
		return 1;
	}

    char output[255];
	for(;;) {
        puts("\033[2J");
        output[0] = 0;
        for (i=0; i<CHANNELCOUNT; i++) {
            channel = channels[i];
			ioctl(fd, ADC_SET_CHANNEL, channel);

            char buffer[30];
            int len = read(fd, buffer, sizeof buffer -1);
            if (len > 0) {
                buffer[len] = '\0';
                int value = -1;
                sscanf(buffer, "%d", &value);
                char outbuff[255];
                sprintf(outbuff, "AIN%d %d\n", channel, value);
                strcat(output, outbuff);
            } 
        }
        printf("%s",output);
        usleep(300* 1000);
    }
	close(fd);
}

三、Makefile文件

# ----------------------------------------------------------------------------
# Makefile for building tapp
#
# Copyright 2010 FriendlyARM (http://www.arm9.net/)
#

ifndef DESTDIR
DESTDIR			   ?= /tmp/FriendlyARM/mini6410/rootfs
endif

CFLAGS				= -Wall -O2 -static
CC					= arm-linux-gcc
INSTALL				= install

TARGET				= adc-test


all: $(TARGET)

adc-test: adc-test.c
	$(CC) $(CFLAGS) $< -o $@


install: $(TARGET)
	$(INSTALL) $^ $(DESTDIR)/usr/bin

clean distclean:
	rm -rf *.o $(TARGET)


# ----------------------------------------------------------------------------

.PHONY: $(PHONY) install clean distclean

# End of file
# vim: syntax=make

猜你喜欢

转载自blog.csdn.net/danwuxie/article/details/82763347
ADC
今日推荐