登录|立即注册|使用QQ帐号登录
论坛 > 51单片机
发帖|
看3220|回0|收藏
楼主 ID:700436 只看他
2020-3-1 00:26
引入该头文件,直接调用uchar readbyte (uint add)   void writebyteseq(uint add,uchar *ptr,uint writelen)   函数即可

注意FM_DATA引脚必须接上拉电阻,否则不能正常读取数据,数据时和时钟线接51单片机准双向口。



        #include<reg52.h>
        #include<intrins.h>
        #include "stc12c5a16s2.h"
        #define IIC_READ 0XA1    //定义读指令
        #define IIC_WRITE 0XA0   //定义写指令
        #define uchar unsigned char //定义一下方便使用
        #define uint  unsigned int
        #define NOP _nop_()


       
                /***************************************************************/

        uchar idata buff[32];        //save the byte read out  form iic device in test operation
        uchar idata readbuff[32];//测试数组,暂存写入读出数据
        //uchar writebuff[32];
        //===============================
        sbit SDA = P4^5;        //THE SDA BIT IS PORT 4 BIT 2
        sbit SCL = P4^4; //THE SCL BIT IS PORT 1 BIT 0

        //===============================
        //define a bit_operation byte to use in shift operation
        //use this mode can achieve high operation speed
        uchar bdata bbyte;//定义位操作用数组,采用此方法可提高位操作速度
        sbit a0 = bbyte^0;
        sbit a1 = bbyte^1;
        sbit a2 = bbyte^2;
        sbit a3 = bbyte^3;
        sbit a4 = bbyte^4;
        sbit a5 = bbyte^5;
        sbit a6 = bbyte^6;
        sbit a7 = bbyte^7;
        //========================================
        bit IFACK;        //record the SDA state to confirm if ACK has happened
        bit NO_ACK;        //no ack flag


        /***************************************************************/


        //FUNCTION:ROUTES TO PROVIDE A START SIGNAL
        void start(void)
        {
                SCL=0;SDA=1;SCL=1;SDA=0;SCL=0;
        }
        //=======================================
        //FUNCTION:ROUTES TO PROVIDE A STOP SIGNAL
        void stop(void)
        {
                SCL=0;SDA=0;SCL=1;SDA=1;SCL=0;
        }
        //=====================================
        //FUNCTION:ROUTES TO PROVIDE ACK SINGAL
        void ack(void)
        {
                SCL=0;SDA=0;SCL=1;SCL=0;
        }
        //=====================================
       
                //FUNCTION:ROUTES TO RELEASE THE SDA TO RECEIVE A ACK SIGNAL
        //                     OR TO PROVIDE A NO_ACK SIGNAL
        //type=1等待应答信号
        //type=0 产生无应答信号
        void nack(uchar type)
        {
                SCL=0;SDA=1;SCL=1;
                IFACK=SDA; SCL=0;
                if(type)
                {
                        if(IFACK)//如果无应答信号,则置标志位NO_ACK,程序中止
                        {       
                                NO_ACK=1;//用户可以加入自己的异常处理程序
                                //while(1);
                        }
                        else NO_ACK=0;
                }
        }

        //=======================================================
        //FUNCTION:THE IIC DEVICE SHIFT OUT A BYTE  TO THE MASTER
        unsigned char  inbyte(void)
        {                                                                //从IIC器件中读出数据
       
                unsigned char get_value,i;
            get_value=0;
            SCL=0;
            for(i=0;i<8;i++)
            {
                SDA=1;
                SCL = 1;
                get_value <<= 1;//首先接收到的为最高位
                if(SDA==1)get_value += 1;
                SCL = 0;
            }
            return get_value;
        }
        //=======================================================
        //FUNCTION:THE IIC DEVICE SHIFT IN A BYTE FROM THE MASTER
        void outbyte(unsigned char outdata) {//将数据写入IIC器件
       
                unsigned int i;
            SCL=0;
            for(i=0;i<8;i++)
            {
                if(outdata & (0x80>>i))    // 1000 0000
                {
                    SDA=1;
                    SCL=1; //高电平数据有效
                    SCL=0;
                }
                else
                {
                    SDA=0;
                    SCL=1; //高电平数据有效
                    SCL=0;
                }
            }
        }
        //======================================================
        //FUNCTION:BYTE WRITE.  'add'     THE WRITE ADDRESS, 'wbyte'   THE DATA WANT TO WRITE
        void writebyte(uint add,uchar wbyte)//add为写入地址,wbyte为写入数据
        {
                uchar temp;
                temp=IIC_WRITE;        //A0
                if(add>>8){temp|=0x02;}          //add = 0x01,temp= A0 else temp = A2          10000000
                start();                         //开始信号
                outbyte(temp);                        //写命令
                nack(1);                        //等待应答
                outbyte((uchar)add);                //写地址
                nack(1);                        //等待应答
                outbyte(wbyte);                        //写数据
                nack(1);                        //等待应答
                stop();                                //停止信号
        }
        //=====================================================
        //FUNCTION:RANDOM READ.'add' IS THE ADDRESS WANT TO READ
        uchar readbyte (uint add)//add为读地址
        {
                uchar temp,tempr;               
                temp=IIC_WRITE;          //  A0;
                tempr=IIC_READ;
                if(add>>8){temp|=0x02;tempr|=0x02;}
                start();                        //开始信号
                outbyte(temp);                        //写命令
                nack(1);                        //等待应答
                outbyte((uchar)add);                //写地址
                nack(1);                        //等待应答
                start();                        //开始信号
                outbyte(tempr);                        //读命令
                nack(1);                        //等待应答
                temp=inbyte();                        //读数据
                nack(0);                        //无应答
                stop();                                //停止信号          */
                return(temp);
        }
        //=================================================
        //连写函数
        //add为读起始地址,ptr数据保存指针,writelen为写入数据长度
        void writebyteseq(uint add,uchar *ptr,uint writelen)
        {
                uchar temp;
                uint i;
                temp=IIC_WRITE;
                if(add>>8){temp|=0x02;}
                start();                
                outbyte(temp);               
                nack(1);               
                outbyte((uchar)add);
                nack(1);
                for(i=0;i<writelen;i++)
                {
                        outbyte(*(ptr+i));
                        nack(1);
                }
                stop();       
        }
        //=================================================
        //连读函数
        //add为读起始地址,ptr数据保存指针,writelen为读出数据长度
        void readbyteseq(uint add,uchar *ptr,uint readlen)
        {
                uchar temp,tempr;
                uint i;
                temp=IIC_WRITE;
                tempr=IIC_READ;
                if(add>>8){temp|=0x02;tempr|=0x02;}
                start();
                outbyte(temp);
                nack(1);
                outbyte((uchar)add);
                nack(1);
                start();
                outbyte(tempr);
                nack(1);
                for(i=0;i<readlen-1;i++)
                {
                        *(ptr+i)=inbyte();
                        ack();
                       
                }
                *(ptr+readlen-1)=inbyte();
                nack();
                stop();
        }
        //=======================================================




附件列表
FM24CL04B.jpg (2020-3-1 00:24 上传)

原图尺寸 73.97 KB, 下载次数: 56

电路图

电路图

fm24cl04b.zip (2020-3-1 00:21 上传)

1.87 KB, 下载次数: 9, 下载积分: 黑币 -5

51黑电子论坛

Powered by Discuz! X3.1

首页|标准版|触屏版|电脑版