新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 24c64讀寫程序

        24c64讀寫程序

        作者: 時間:2016-11-23 來源:網絡 收藏
        #include //包括一個52標準內核的頭文件

        #define uchar unsigned char //定義一下方便使用
        #define uint unsigned int
        #define ulong unsigned long
        #define WriteDeviceAddress 0xa0 //定義器件在IIC總線中的地址
        #define ReadDviceAddress 0xa1
        sbit SCL=P2^7;
        sbit SDA=P2^6;
        sbit P20=P2^0;
        //定時函數
        void DelayMs(unsigned int number)
        {
        unsigned char temp;
        for(;number!=0;number--)
        {
        for(temp=112;temp!=0;temp--) ;
        }
        }
        //開始總線
        void Start()
        {
        SDA=1;
        SCL=1;
        SDA=0;
        SCL=0;
        }
        //結束總線
        void Stop()
        {
        SCL=0;
        SDA=0;
        SCL=1;
        SDA=1;
        }
        //發ACK0
        void NoAck()
        {
        SDA=1;
        SCL=1;
        SCL=0;
        }
        //測試ACK
        bit TestAck()
        {
        bit ErrorBit;
        SDA=1;
        SCL=1;
        ErrorBit=SDA;
        SCL=0;
        return(ErrorBit);
        }
        //寫入8個bit到24c02
        Write8Bit(unsigned char input)
        {
        unsigned char temp;
        for(temp=8;temp!=0;temp--)
        {
        SDA=(bit)(input&0x80);
        SCL=1;
        SCL=0;
        input=input<<1;
        }
        }
        //寫入一個字節到24c02中
        void Write24c64(uchar ch,uchar address)
        {
        Start();
        Write8Bit(WriteDeviceAddress);
        TestAck();
        Write8Bit(address);
        TestAck();
        Write8Bit(ch);
        TestAck();
        Stop();
        DelayMs(10);
        }
        //從24c02中讀出8個bit
        uchar Read8Bit()
        {
        unsigned char temp,rbyte=0;
        for(temp=8;temp!=0;temp--)
        {
        SCL=1;
        rbyte=rbyte<<1;
        rbyte=rbyte|((unsigned char)(SDA));
        SCL=0;
        }
        return(rbyte);
        }
        //從24c02中讀出1個字節
        uchar Read24c64(uchar address)
        {
        uchar ch;
        Start();
        Write8Bit(WriteDeviceAddress);
        TestAck();
        Write8Bit(address);
        TestAck();
        Start();
        Write8Bit(ReadDviceAddress);
        TestAck();
        ch=Read8Bit();
        NoAck();
        Stop();
        return(ch);
        }
        //本課試驗寫入一個字節到24c02并讀出來驗證
        void main(void) // 主程序
        {
        uchar c1;
        uchar address =0x01;
        for ( address =0x01;address<0xff;address++)
        {
        Write24c64(0x00,address);// 將0x88寫入到24c02的第2個地址空間
        c1=Read24c64(address);
        }
        P20=0;
        while(1); //程序掛起
        }

        本文引用地址:http://www.104case.com/article/201611/320562.htm


        關鍵詞: 24c64讀寫程

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 富蕴县| 张家港市| 辉县市| 高淳县| 苍溪县| 巫溪县| 昆山市| 平邑县| 浮梁县| 平陆县| 合阳县| 十堰市| 社会| 内江市| 东乡| 宜丰县| 自治县| 彩票| 寻乌县| 泰顺县| 沅陵县| 正宁县| 尼勒克县| 乌兰察布市| 南部县| 清远市| 临高县| 永兴县| 兴化市| 云林县| 万州区| 南丰县| 神木县| 罗山县| 阿鲁科尔沁旗| 澎湖县| 九江市| 道孚县| 鄂伦春自治旗| 大安市| 遂宁市|