题目3 AD实验
一、实验内容
程序开始运行后提示用户输入0~2选择通道
使用open函数打开AD设备
使用write函数写入正确的参数启动选择的通道进行AD转换
使用read函数读取AD转换结果
计算出实际电压值并用printf函数输出:a(0,1,2) = *.***V
使用循环结构实现连续转换和输出
转动电位器改变AD输入电压,观察输出值是否变化
二、实验代码及分析
主程序:
#include
#include
#include
#include
#include
#include
#include
#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();
getchar();
stop=1;
return NULL;
}
int main(void)
{
int i,x;
float d;
pthread_t th_com;
void * retval;
//set s3c44b0 AD register and start AD
if(init_ADdevice()<0)
return -1;
printf(“qing shu ru 0,1,2:”);
scanf(“%d”,&x);
/* Create the threads */
pthread_create(&th_com, NULL, comMonitor, 0);
while(stop==0)
{
usleep(1000);
printf("\r");}
}
/* Wait until producer and consumer finish. */
pthread_join(th_com, &retval);
printf("\n");
return 0;
}
Makefile程序:
all:ad.c
armv4l-unknow-linux-gcc -c ad.c -o ad.o
armv4l-unknow-linux-gcc ad.o -o ad
三、实验结果
四、实验总结
通过本次实验了解了在 linux 环境下对S3C2410 芯片的 8 通道10位 A/D 的操作与控制。
本文来源:https://www.2haoxitong.net/k/doc/9c10de626d175f0e7cd184254b35eefdc9d31541.html
文档为doc格式