51单片机AD转换-PCF8591

#include <reg52.h>

#include <intrins.h>

#define DATAOUTPUT P0
unsigned char code num[] = "0123456789ABCDEF";

sbit SDA = P2^0;
sbit SCL = P2^1;
sbit LED = P2^2;

sbit RS = P2^4;
sbit RW = P2^5;
sbit EN = P2^6;

//unsigned char code table[] = {0x1c,0X3B,0X2C,0X2D,0X5A,0X5C,0XC5,0X5b};


void delayms(unsigned int t)
{
    unsigned int i,j;
    for(i = t; i > 0; i --)
    for(j = 110; j > 0; j --);
}
void delay()
{
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        
}

void Write_Data(unsigned char dat)
{
    EN = 0;
    RS = 1;
    RW = 0;

    DATAOUTPUT = dat;
//       delayms(5);
    EN = 1;
    delayms(5);
    EN = 0;
}

void Write_Cmd(unsigned char cmd)
{
    EN = 0;
    RS = 0;
    RW = 0;

    DATAOUTPUT = cmd;
//       delayms(5);
    EN = 1;
    delayms(5);
    EN = 0;
}

void Write_Str(unsigned char *s)
{
    while(*s != '\0')
    {
        Write_Data(*s);
        delayms(5);
        s ++;
    }
}

void start()
{
        SCL = 1;
        SDA = 1;
        delay();
        SDA = 0;
        delay();
        SCL = 0;
                
}

void stop()
{
        
        SDA = 0;
        delay();
        SCL = 1;
        delay();
        SDA = 1;
        delay();

}

void ack()
{
        unsigned char i = 0;
        SCL = 0;

        delay();
        while(SDA&&(i<100))i++;
        SCL = 1;
        delay();
        
        SCL = 0;
}

void noack()
{
        SCL = 0;
        
        SDA = 1;
        SCL = 1;
        delay();
        SCL = 0;

}

void writebyte(unsigned char byte)
{
        //MSB first
        unsigned char i = 0;
        SCL = 0;
        for(i = 0; i < 8; i ++)
        {
                byte <<= 1;
                SDA = CY;
                delay();
                SCL = 1;
                delay();
                SCL = 0;
        }
        
//        SDA = 1;//waiting for ack
//        delay();
}

unsigned char readbyte()
{
        unsigned char i = 0,val = 0;
        SCL = 0;
        for(i = 0; i < 8 ; i ++)
        {
                val <<= 1;
                delay();
                SCL = 1;
                delay();
                if(SDA)
                {
                        val |= 0x01;
                }        
                else
                {
                        val |= 0x00;
                }
                SCL = 0;
                
        }

        SCL = 0;//release IIC bus
        

        return val;

}

/**
void write_data(unsigned char add, unsigned char byte)
{
         start();
         writebyte(0xae);
         ack();
         writebyte(add);
         ack();
         writebyte(byte);
         ack();

         stop();
}

unsigned char read_data(unsigned char add)
{
        unsigned char val = 0;
        start();
        writebyte(0xae);
        ack();
        writebyte(add);
        ack();
        start();
        writebyte(0xaf);
        ack();
        val = readbyte();
        noack();
        stop();

        return val;
}

void pagewrite(unsigned char add)
{
    unsigned char i;
    start();
    writebyte(0xae);
    ack();
    writebyte(add);
    ack();
    for(i = 0; i < 8; i ++)
    {
        writebyte(table[i]);
        ack();
    }
    stop();
}

unsigned char *pageread(unsigned char add)
{
    unsigned char value[8],i;
    start();
    writebyte(0xae);
    ack();
    writebyte(add);
    ack();

    start();
    writebyte(0xaf);
    ack();
    for(i = 0; i < 8; i ++)
    {
        value[i] = readbyte();
        if(i == 7)
        {
            noack();
        }
        else
        {
            SCL = 0;  //主机来应答
    
            delay();
            SDA = 0;
            delay();
            SCL = 1;
            delay();
            
            SCL = 0;
            delay();
            SDA = 1;
            delay();
            
        }
        delay();
    }
    stop();

    return value;
    

}  **/

unsigned char PCF8591_read(unsigned char ch)
{
    unsigned char readout;
    start();
    writebyte(0x90);
    ack();
    writebyte(ch);
    ack();

    start();
    writebyte(0x91);
    ack();
    readout = readbyte();
//    ack();
//
//    readout[1] = readbyte();
    noack();

    stop();

    return readout;


}
void main()
{
        unsigned char TMP_VAL;

        Write_Cmd(0x01);
    Write_Cmd(0x38);
    Write_Cmd(0x0f);
    Write_Cmd(0x06);

    
    
        while(1)
        {
               unsigned char a = 0;
            for(a = 0; a<5; a ++)      //没有这句,调CH0的电阻会显示到0xc0位置 ,而非0x80位置
            TMP_VAL = PCF8591_read(0x00);
            Write_Cmd(0x80); //AD CH0
            Write_Str("CH0 : 0x");
            Write_Cmd(0x88);
            Write_Data(num[TMP_VAL/16]);
            Write_Cmd(0x89);
            Write_Data(num[TMP_VAL%16]);
//delayms(200);
            for(a = 0; a<5; a ++)
            TMP_VAL = PCF8591_read(0x03);
            Write_Cmd(0x80+0x40); //AD CH3
            Write_Str("CH3 : 0x"); 
            Write_Cmd(0xc8);
            Write_Data(num[TMP_VAL/16]);
            Write_Cmd(0xc9);
            Write_Data(num[TMP_VAL%16]);
            delayms(200);

            
            
        }
}

AD转换时序,PCF8591数据手册没有写AD转换的完整时序,还是参考EE的来才能读出来的

posted @ 2024-05-18 10:28  了解2号  阅读(26)  评论(0编辑  收藏  举报