標題: IIC協(xié)議發(fā)送多字節(jié)的數(shù)字,NFC M24R64 [打印本頁]

作者: 917106794    時間: 2020-10-13 17:17
標題: IIC協(xié)議發(fā)送多字節(jié)的數(shù)字,NFC M24R64
/*******************************************************************************
* 函數(shù)名         : void At24c02Write(unsigned char addr,unsigned char dat)
* 函數(shù)功能                   : 往24c02的一個地址寫入一個數(shù)據(jù)
* 輸入           : 無
* 輸出                  : 無
*******************************************************************************/

void At24c02Write(unsigned int addr, unsigned char dat)
{
        I2cStart();
        I2cSendByte(0xa6);//發(fā)送寫器件地址
        I2cSendByte(addr>>8);//發(fā)送要寫入內(nèi)存地址
        I2cSendByte(addr&0xff);//發(fā)送要寫入內(nèi)存地址
//        I2cSendByte(dat<<8);        //發(fā)送數(shù)據(jù)
        //I2cSendByte(addL);//發(fā)送要寫入內(nèi)存地址
        I2cSendByte(dat);        //發(fā)送數(shù)據(jù)
        I2cStop();
}

/*******************************************************************************
* 函數(shù)名         : unsigned char At24c02Read(unsigned char addr)
* 函數(shù)功能                   : 讀取24c02的一個地址的一個數(shù)據(jù)
* 輸入           : 無
* 輸出                  : 無
*******************************************************************************/

unsigned char At24c02Read(unsigned int addr)
{
        unsigned char num;
        I2cStart();
        I2cSendByte(0xa6); //發(fā)送寫器件地址
        I2cSendByte(addr>>8); //發(fā)送要讀取的高八位地址
        I2cSendByte(addr&0xff); //發(fā)送要讀取低八位的地址
//        I2cSendByte(addL); //發(fā)送要讀取的地址
        I2cStart();
        I2cSendByte(0xa7); //發(fā)送讀器件地址
        num=I2cReadByte(); //讀取數(shù)據(jù)
//        num=num<<8|I2cReadByte(); //讀取數(shù)據(jù)
        I2cStop();
        return num;       
}



void write_m_data_24c64(u16 addr,u8 *m_data_24c64,u8 n) //從某地址開始連續(xù)多字節(jié)寫入
{
    u8 i;
                I2cStart();
                I2cSendByte(0xa6);//發(fā)送寫器件地址
                I2cSendByte(addr>>8);//發(fā)送要寫入內(nèi)存地址
                I2cSendByte(addr&0xff);//發(fā)送要寫入內(nèi)存地址

    for(i=0; i<n; i++)
    {
        I2cSendByte(*(m_data_24c64++));//寫儲存數(shù)據(jù)
        Delay10us();
    }
   I2cStop();
}


void read_m_data_24c64(u16 addr,u8 n)//從某地址開始連續(xù)讀取n個數(shù)據(jù)
        {
                u8 i;
                for(i=0;i<n;i++)
                {
                        I2cStart();
                        I2cSendByte(0xa6); //發(fā)送寫器件地址
                        I2cSendByte(addr>>8); //發(fā)送要讀取的高八位地址
                        I2cSendByte(addr&0xff); //發(fā)送要讀取低八位的地址
                        addr++;
                        I2cStart();
                        I2cSendByte(0xa7); //發(fā)送讀器件地址
                        receive[i]= I2cReadByte();
                        Delay10us();
                        I2cStop();
                }
        }

對于單字節(jié)相當于頁寫,普通的是8位數(shù)據(jù),這個傳遞是32位的數(shù)據(jù)


程序.zip

71.89 KB, 下載次數(shù): 4, 下載積分: 黑幣 -5






歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1