首页 > 其他分享 >51单片机AD转换-PCF8591

51单片机AD转换-PCF8591

时间:2024-05-18 10:29:47浏览次数:25  
标签:P2 char EN void 51 unsigned delay 单片机 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的来才能读出来的

标签:P2,char,EN,void,51,unsigned,delay,单片机,PCF8591
From: https://www.cnblogs.com/chillytao-suiyuan/p/18199100

相关文章

  • P9517 drink 题解
    题目传送门这道题考场上用的查找做的。先用一个结构体分别表示firstlast,然后进行查找即可,两个for循环分别计算出first和last,最后计算它们的差值。(注意,计算差值时要加1)然后你就会发现一个问题:只有\(90\)分。所以我们来思考一下哪里出现了问题。你会发现:如果全都是......
  • 51模拟IIC-页读写操作
    51代码页读写IIC--模拟IIC#include<reg52.h>#include<intrins.h>sbitSDA=P0^0;sbitSCL=P0^1;sbitLED=P2^0;unsignedcharcodetable[]={0x1c,0X3B,0X2C,0X2D,0X5A,0X5C,0XC5,0X5b};voiddelayms(unsignedintt){unsignedinti,j;fo......
  • 群晖ds1517+解决第三方Marvell AQC107 10Gbe网卡驱动问题
    群晖ds1517+解决第三方MarvellAQC10710Gbe网卡驱动问题转载注明来源:本文链接来自osnosn的博客,写于2024-05-15.说明这是网友mohawk解决问题的经过,征得同意后,贴在这里。给大家参考。背景好友打算升级到全屋有线万兆2.5g网络,陆续装备了路由器、交换机等,但家里的群晖ds......
  • 代码随想录算法训练营第第八天 | 344.反转字符串 、541. 反转字符串II、卡码网:54.替
    344.反转字符串建议:本题是字符串基础题目,就是考察reverse函数的实现,同时也明确一下平时刷题什么时候用库函数,什么时候不用库函数题目链接/文章讲解/视频讲解:https://programmercarl.com/0344.反转字符串.html/***@param{character[]}s*@return{void}Donotret......
  • 3SRB2516-ASEMI适配大功率充电桩3SRB2516
    编辑:ll3SRB2516-ASEMI适配大功率充电桩3SRB2516型号:3SRB2516品牌:ASEMI封装:SGBJ-5正向电流(Id):25A反向耐压(VRRM):1600V正向浪涌电流:400A正向电压(VF):1.05V引脚数量:5芯片个数:5芯片尺寸:88MIL功率(Pd):大功率设备工作温度:-55°C~150°C类型:整流桥、插件整流桥3SRB2516整流桥描......
  • 基于UltraScale架构的XCVU3P-3FFVC1517E XCVU3P-2FFVC1517I XCVU3P-1FFVC1517E高性能
    概述VirtexUltraScale+器件是基于14nm/16nmFinFET节点的高性能FPGA,支持3DIC技术和多种计算密集型应用。AMD第三代3DIC使用堆叠硅片互联(SSI)技术打破了摩尔定律的限制,并且实现了最高信号处理和串行I/O带宽,以满足最严格的设计要求。它还提供了一个虚拟的单片设......
  • 一键自动化博客发布工具,用过的人都说好(51cto篇)
    51cto是一个优秀的博客平台,今天给大家讲解一下blog-auto-publishing-tools如何自动发布博客到51cto上。当然在实现过程中有可能会遇到各种困难,不过不用担心,我们一个个来解决。前提条件前提条件当然是先下载blog-auto-publishing-tools这个博客自动发布工具,地址如下:https://gi......
  • AtCoder Beginner Contest 351 E - Jump Distance Sum
    题目链接Hint1:只能斜着走,容易想到黑白棋盘,\((x+y)\%2==0\)位于一个团,\((x+y)\%2==1\)位于另一个团,分别求和。Hint2:\(dist=max(|x1-x2|,|y1-y2|)\),这是切比雪夫距离,将坐标系倾斜\(45^{\circ}\),改为曼哈顿距离\(dist=|x1-x2|+|y1-y2|\),即\(X=x+y,Y=x-y\),这样就可以将横纵坐标......
  • 20240514打卡
    第十二周第一天第二天第三天第四天第五天第六天第七天所花时间1h0h代码量(行)1440博客量(篇)11知识点了解界面美化CSS,mdn官网学习团队趣味活动合作......
  • 干电池升压芯片PW5100:高效转换,赋能小型设备
    内容简述目录:1,干电池的工作电压范围2,平芯微根据干电池规格书整理的干电池输出电流性能表3,干电池负载能力的理解和解释4,专用干电池升压芯片PW51005,干电池升压芯片采用0603,0805,1206电感和输出电流测试6,干电池升压芯片真关断控制版本PW51037,电荷泵升压IC和专用干电池升压芯片......