求最简单的A/D转换程序

2020-02-01 16:28发布

本人刚学单片机不久,自学《十天学会单片机和C语言》,现在学到A/D,D/A转换,这方面想了大半天都看不懂例程序,求最简单的A/D转换程序,要加说明
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
40条回答
李鸿鸿
2020-02-03 21:20
jhjh630 发表于 2012-12-3 11:37
把你的例句和你的的程序一起发给我,我帮你看下,看能不能帮你的忙!QQ:290412297 ...

教程上用的是ADC0804,我的开发板用的是PCF8591,程序编写方法都一样吧?我想原来的程序不用管了,芯片都不同。
PCF8591的例题是这样的:
#ifndef __PCF8591_H__
#define __PCF8591_H__

#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char

//该ADC和DAC是串口输出,只要接两根线
sbit SCL=P3^6;//时钟脉冲
sbit SDA=P3^7;//双向输入输出数据端

#define SCL_SET SCL=1
#define SCL_CLR SCL=0
#define SDA_SET SDA=1
#define SDA_CLR SDA=0
#define AddWr 0x92   //写数据地址
#define AddRd 0x93   //读数据地址
#define adCon 0x40   //AD控制字节

//延时1US
void PDFDelay(uint cnt)
{
        while(--cnt);
}

void start()
{
        SDA_SET;
        PDFDelay(1);
        SCL_SET;
        PDFDelay(5);
        SDA_CLR;
}

void stop()
{
        SDA_CLR;
        PDFDelay(1);
        SCL_SET;
        PDFDelay(5);
        SDA_SET;  
}

void ack()
{
        SDA_CLR;
        SCL_SET;
        PDFDelay(1);
        SCL_CLR;
}

void noAck()
{
        SDA_SET;
        SCL_SET;
        PDFDelay(1);
        SCL_CLR;
}

void send(uchar Data)
{
        uchar i=0;
        uchar temp=0;
        temp=Data;
        for(i=0; i<8; i++)
        {
                SCL_CLR;
                PDFDelay(1);
                if(temp&0x80) SDA_SET;
                else SDA_CLR;
                PDFDelay(1);
                SCL_SET;
                PDFDelay(1);
                temp<<=1;
        }
        SCL_CLR;
}

uchar recive()
{
        uchar i=0;
        uchar temp=0;
        SDA_SET;//必须设置
        for(i=0; i<8; i++)
        {
                SCL_CLR;//拉低允许数据改变
                PDFDelay(1);
                SCL_SET;//拉高保持数据,等待读走
                PDFDelay(2);
                if(SDA) temp|=0x01;
                else temp&=0xfe;
                if(i<7)   temp<<=1;//最低位发送完成不能移位,否则出错
        }
        SCL_CLR;
        return temp;
}

//ADC读函数。输入参数ch为通道数:0至3通道
uchar ADCGet(uchar ch)
{
        uchar temp=0;
        start();
        send(AddWr);//确认芯片
        ack();
        send(adCon|ch);//确认通道
        ack();
        //读出数据,放进temp
        start();
        send(AddRd);
        ack();
        temp=recive();
        noAck();
        stop();
        return temp;
}

//DAC设定参。输入参数value为设定的数字量
void DACSet(uchar value)
{
        start();
        send(AddWr);
        ack();
        send(0x40);  //写入控制位,使能DAC输出
        ack();
        send(value);
        ack();
        stop();
}
//#include<reg52.h>
//#include"PCF8591.h"//自己封装的PCF8591芯片控制函数

void main()
{       
        while(1)
        {
                P1=ADCGet(2);//将2通道采样回来的值直接付给流水灯。二通道采样的是电位器的值,直接旋转电位器即可看到效果
        }
}

一周热门 更多>