新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 24C02的存儲與讀取

        24C02的存儲與讀取

        作者: 時間:2016-11-18 來源:網絡 收藏
        /*

        ------24C02存儲與讀取-----------
        1、用24C02來保存一個數組的內容,然后再把它讀出來;
        2、應熟練掌握IIC總線的操作方法;
        3、啟動信號、停止信號,應答與非應答信號、測忙信號
        從當前位置讀一個字節數據子程序,從當前位置寫一個字節子程序,
        從指定位置讀一個字節數據子程序,從指定位置寫一個字節子程序。

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

        4、各種信號的操作時序;
        5、該程序也可作為IIC的通用子程序使用;
        6、該程序在普中單片機實訓箱上通過測試;

        */

        #include
        #include
        #define uint unsigned int
        #define uchar unsigned char
        unsigned char code dula[] = {0x3f,0x06,0x5b,0x4f,
        0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};
        uchar wula[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
        sbit sda=P2^1; //定義串行數據口
        sbit scl=P2^0; //定義串行時鐘口
        void delay(uint z);
        void start();
        void stop();
        void ack(bit aa);
        uchar iic_readbyte();
        void iic_writebyte(uchar date1);
        void checkack();
        uchar iic_addr_read(uchar addr);
        void iic_addr_write(uchar addr,uchar date1);
        void iic_addr_readn(uchar addr,uchar *s,uchar n);
        void iic_addr_writen(uchar addr,uchar *s,uchar n);
        void disp();
        void nop5();
        uchar write1[]={1,2,10,0,0,10,4,8,9,3};
        uchar read1[]={0,0,0,0,0,0,0,0,0,0};
        bit ack1;
        uchar temp3;
        //uchar flag1,flag2,num1,num2;
        void main()
        {
        /* 第一種方法從24C02中讀取和寫入多個字節
        uchar jj;
        for(jj=0;jj<8;jj++)
        {
        iic_addr_write(jj+5,write1[jj]);
        delay(10);//此延時很重要;
        }

        delay(10);
        for(jj=0;jj<8;jj++)
        {
        read1[jj]=iic_addr_read(jj+5);
        delay(10);//此延時很重要;
        } */
        //
        //第二種方法從24C02中讀取和寫入多個字節
        iic_addr_writen(8,write1,8);
        delay(50);
        iic_addr_readn(8,read1,8);

        while(1)
        {disp();
        }
        }


        //-----5US的延時子程序--------------
        void nop5()
        {
        _nop_();_nop_();_nop_();_nop_();_nop_();
        }
        //-------XMS延時子程序----------------
        void delay(uint z)
        {
        uint x,y;
        for(x=0;xfor(y=0;y<112;y++);
        }

        //-------啟動信號-----------
        void start()
        {
        sda=1;
        nop5();
        scl=1;
        nop5();
        sda=0;
        nop5();
        scl=0;
        nop5();
        }
        //-------停止信號-----------
        void stop()
        {
        sda=0;
        nop5();
        scl=1;
        nop5();
        sda=1;
        nop5();
        scl=1;
        nop5();
        }
        //=------應答與非應答信號---------
        void ack(bit aa)
        {
        scl=0;
        nop5();
        if(aa==1)
        sda=1;
        else
        sda=0;
        scl=1;
        nop5();
        scl=0;
        nop5();
        }
        //-------測忙信號------------
        void checkack()
        { uchar i;
        sda=1;
        scl=0;
        nop5();
        scl=1;
        nop5();
        while((sda==1)&&(i<250))
        {i++;}
        scl=0;
        nop5();
        }
        //-------從當前位置讀一個字節數據子程序---------------

        uchar iic_readbyte()
        {
        uchar i,temp;
        sda=1;
        scl=0;
        nop5();
        for(i=0;i<8;i++)
        {
        temp=temp<<1;
        scl=1;
        nop5();
        temp=temp|sda;
        scl=0;
        // nop5();
        // nop5();
        }
        scl=0;
        nop5();
        return(temp);
        }
        //-----從當前位置寫一個字節子程序-------------
        void iic_writebyte(uchar date1 )
        {
        uchar i,temp;
        temp=date1;
        for(i=0;i<8;i++)
        {
        temp=temp<<1;
        sda=CY;
        scl=1;
        nop5();
        scl=0;
        //nop5();
        // nop5();
        }
        checkack();
        delay(1);
        scl=0;
        nop5();
        }
        //---從指定位置讀一個字節數據子程序-----------------
        uchar iic_addr_read(uchar addr)
        { uchar k2;
        start();
        iic_writebyte(0xa0);
        delay(1);
        iic_writebyte(addr);
        delay(1);
        start();
        delay(10);
        iic_writebyte(0xa1);
        delay(10);
        k2=iic_readbyte();
        ack(1);
        stop();
        return(k2);
        }

        //---從指定位置寫一個字節子程序-------------
        void iic_addr_write(uchar addr,uchar date1)
        {
        start();
        iic_writebyte(0xa0);
        delay(1);
        iic_writebyte(addr);
        delay(1);
        iic_writebyte(date1);
        delay(1);
        stop();
        }
        //----------從指定位置寫多個字節子程序-------------------
        void iic_addr_writen(uchar addr,uchar *s,uchar n)
        {
        uchar i;
        start();
        iic_writebyte(0xa0);
        //delay(1);
        iic_writebyte(addr);
        // delay(1);
        for(i=0;i {
        iic_writebyte(*s);
        delay(20);//此延時一定要長一些,否則易出錯
        s++; //因為器件反應時間慢,給它留足夠的時間;
        }
        stop();
        }
        //-------從指定位置讀多個字節數據子程序-------------------------
        void iic_addr_readn(uchar addr,uchar *s,uchar n)
        {
        uchar i;
        start();
        iic_writebyte(0xa0);
        //delay(1);
        iic_writebyte(addr);
        //delay(10);
        start();
        //delay(15);
        iic_writebyte(0xa1);
        //delay(10);
        for(i=0;i{
        *s=iic_readbyte();
        delay(1);
        ack(0);
        delay(20);//此延時一定要長一些,否則易出錯
        s++; //因為器件反應時間慢,給它留足夠的時間;
        }
        *s=iic_readbyte();
        ack(1);
        stop();
        }

        //-----顯示數據----------
        void disp()
        {uchar i;
        for(i=0;i<8;i++)
        {P0=dula[read1[i]];
        P1=wula[i];
        delay(3);
        P0=0;
        }
        }



        關鍵詞: 24C02存儲與讀

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 拉萨市| 新闻| 寿阳县| 康乐县| 青冈县| 平湖市| 宜章县| 阿坝| 永靖县| 安康市| 宜都市| 莆田市| 东城区| 上蔡县| 突泉县| 江北区| 扬州市| 炎陵县| 阳高县| 永川市| 凤山市| 缙云县| 巫溪县| 伊宁市| 响水县| 长汀县| 新乡市| 遂溪县| 泸西县| 阿瓦提县| 宁都县| 舞钢市| 黎川县| 固始县| 福清市| 普陀区| 阿克苏市| 化州市| 磴口县| 鹿泉市| 东莞市|