新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 實用的單片機模擬I2C總線控制EEPROM讀寫程序

        實用的單片機模擬I2C總線控制EEPROM讀寫程序

        作者: 時間:2016-11-18 來源:網絡 收藏
        之前寫的EEPROM程序雖然能夠軟仿成功,但燒到單片機里的時候卻不能用,無疑是時序的問題,今天修正了時序,總算硬仿成功了。對照上次的程序可以發現就是添加了頭函數:,這樣就可以通過“_nop_()”指令較為準確的控制時序。

        上次那個問題依然沒有解決:就是接收緩沖區的數據是從readbuf[6]開始的,以這個程序為例:

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

        readbuf[6]中存放0x96

        readbuf[7]中存放0x84

        readbuf[8]中存放0xd5

        readbuf[9]中存放0x63

        readbuf[10]中存放0x7c

        readbuf[11]中存放0x8c

        其實我是想把收到的數據存放在readbuf[0]~[5]中的,我也不知道為什么結果會這樣,有知道的麻煩指教下。

        不多說了,上程序:

        #include
        #include
        #define unit unsigned int
        #define uchar unsigned char

        uchar num=6;
        uchar idata sendbuf[6]={0x96,0x84,0xd5,0x63,0x7c,0x8c};
        uchar idata readbuf[6];

        sbit scl=P2^0;
        sbit sda=P2^1;

        start(void) //start
        {
        sda=1;
        _nop_();
        _nop_();
        scl=1;
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        sda=0;
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        scl=0;
        }

        stop(void) //stop
        {
        sda=0;
        _nop_();
        _nop_();
        scl=1;
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        sda=1;
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        scl=0;
        }

        answer(void) //answer
        {
        sda=0;
        _nop_();
        _nop_();
        _nop_();
        scl=1;
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        scl=0;
        }

        noanswer(void)//no answer
        {
        sda=1;
        _nop_();
        _nop_();
        _nop_();
        scl=1;
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        scl=0;
        }

        checkanswer(void) //check answer
        {
        F0=0;
        sda=1;
        _nop_();
        _nop_();
        scl=1;
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        if(sda==1) F0=1;
        scl=0;
        }

        sendabyte(uchar idata *saddress) //send a byte
        {
        uchar n=8,temp=*saddress;
        while(n--)
        {
        if((temp&0x80)==0x80) sda=1;
        else sda=0;
        _nop_();
        scl=1;
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        scl=0;
        temp=temp<<1;
        }
        checkanswer();
        if(F0==1) return;
        }

        sendnbyte(uchar n) //send n byte
        {
        uchar idata *ps;
        ps=&sendbuf[0];

        while(n--)
        {
        sendabyte(ps);
        ps++;
        }
        stop();
        }

        readabyte(uchar idata *raddress) //read a byte
        {
        uchar n=8,temp=0;
        while(n--)
        {
        _nop_();
        _nop_();
        scl=1;
        _nop_();
        _nop_();
        temp=temp<<1;
        if(sda==1)
        temp=temp|0x01;
        else
        temp=temp&0xfe;
        _nop_();
        _nop_();
        _nop_();
        scl=0;
        _nop_();
        _nop_();
        _nop_();
        }
        *raddress=temp;
        }

        readnbyte(uchar n) //read n byte
        {
        uchar idata *pr;
        pr=&readbuf[0];
        while(n--)
        {
        readabyte(pr);
        answer();
        pr++;
        }
        noanswer();
        stop();
        }

        main(void) //MAIN
        {
        start();

        sendabyte(0xa0);

        sendabyte(0x00);

        sendnbyte(num);

        /*-----------------------*/
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();

        start();

        sendabyte(0xa0);

        sendabyte(0x00);

        sendabyte(0xa1);

        readnbyte(num);

        P1=readbuf[11];

        while(1);
        }



        評論


        技術專區

        關閉
        主站蜘蛛池模板: 巴青县| 全南县| 郁南县| 沂源县| 崇明县| 左云县| 新龙县| 伊通| 宁夏| 修文县| 农安县| 嘉兴市| 连云港市| 德江县| 文化| 吴忠市| 营口市| 来安县| 额敏县| 六盘水市| 疏勒县| 德庆县| 咸宁市| 蒙阴县| 托克逊县| 山丹县| 长岛县| 丰台区| 泗水县| 忻州市| 尤溪县| 曲水县| 巴彦淖尔市| 祁连县| 富阳市| 林州市| 天门市| 漯河市| 饶平县| 北辰区| 晋城|