中文字幕无码不卡一区二区三区_少妇被又大又粗又爽毛片久久黑人_91精品国产在热久久无毒不卡_久久久久久亚洲综合网站

技術熱線: 4007-888-234
設計開發(fā)

專注差異化嵌入式產(chǎn)品解決方案 給智能產(chǎn)品定制注入靈魂給予生命

開發(fā)工具

提供開發(fā)工具、應用測試 完善的開發(fā)代碼案例庫分享

技術支持

從全面的產(chǎn)品導入到強大技術支援服務 全程貼心伴隨服務,創(chuàng)造無限潛能!

新品推廣

提供新的芯片及解決方案,提升客戶產(chǎn)品競爭力

新聞中心

提供最新的單片機資訊,行業(yè)消息以及公司新聞動態(tài)

PIC24F單片機之EEPROM讀寫中斷事件處理函數(shù)要點及說明

更新時間: 2019-03-23
閱讀量:2281

十年專注單片機方案開發(fā)的方案公司英銳恩,分享PIC24F單片機之EEPROM讀寫中斷事件處理函數(shù)要點及說明。英銳恩現(xiàn)提供服務產(chǎn)品涉及主控芯片:8位單片機、16位單片機、32位單片機及各類運算放大器等。

/*-------------------------------------------------------------------------------------------------
                        PIC24F之EEPROM讀寫中斷事件處理函數(shù)要點及說明
注意: 這是一個通用的I2C/SMBUS通訊中斷處理程序
對于EEPROM來講,從機后面需要跟EEPROM需要讀寫的地址(I2CRegs.RWAddr)
對于SMBUS來說,從機后面需要跟SMBUS需要的命令(I2CRegs.RWAddr改為I2CRegs.CMD即可)
由于PIC24F的I2C不太標準,I2C1STAT被搞得很倒塌!?。∫稽c都沒I2C的"大家閨秀"的樣子~~~
不過它的STOP還能激活中斷確實比LPCARM/AVR好一點點~~~

--------------------------------------------------------------------------------------------------*/
#include "i2c.h"

_PERSISTENT volatile I2CREGS I2CRegs;
_PERSISTENT volatile I2CBITS I2CBits;

void I2cInit(void)
{
unsigned int i;
    TRIS_WP   = PORTOUTMODE;//定義WP為輸出IO
    TRIS_SCL1 = PORTOUTMODE;//定義SCL為輸出IO
    TRIS_SDA1 = PORTINPUTMODE;//定義SDA為輸出入IO
    ODC_SCL1 = 1;//OC輸出
    ODC_SDA1 = 1;//OC輸出
    WP = 1;//寫保護
    I2CRegs.MaxCount = 0x200;//8KByte
    I2CRegs.I2CAddr = 0xa0;//器件地址
    I2CRegs.RWAddr = 0;//EEPROM讀寫地址
    I2CRegs.TxCount = 0;//發(fā)送數(shù)據(jù)字節(jié)個數(shù)
    I2CRegs.RxCount = 0;//接收數(shù)據(jù)字節(jié)個數(shù)
    for (i = 0; i < 16; i ++)
    {
        I2CRegs.TxBuffer[i] = 0;//發(fā)送緩沖區(qū)清零
    }
    for (i = 0; i < 256; i ++)
    {
        I2CRegs.RxBuffer[i] = 0;//接收緩沖區(qū)清零
    }

 I2C1CON = 0;
//    I2C1CONbits.A10M = 0;//7位地址模式
    I2C1CONbits.SCLREL = 1;
    I2C1MSK = 0;
    I2C1STAT = 0;
    _MI2C1IF = 0;
    _SI2C1IF = 0;
    I2C1BRG = (FCY / (2 * I2CBAUD)) - 1;//波特率計算
/*------------------------------------------------------------------------
    定義I2C串口2中斷優(yōu)先級位1111)
-------------------------------------------------------------------------*/

 IPC4bits.MI2C1P0 = 1;
    IPC4bits.MI2C1P1 = 1;
    IPC4bits.MI2C1P2 = 1;

    I2C1CONbits.I2CEN = 1;//允許I2C功能
    _MI2C1IE = 1;//允許主設備中斷

//    I2cStop();
}

/*------------------------------------------------------------------
    EEPROM讀塊函數(shù)(只能在回調(diào)函數(shù)I2CReadCallBack中得到讀出的數(shù)據(jù))
-------------------------------------------------------------------*/
void I2CReadBuffers(unsigned int E2RomAddr, unsigned int ReadSize)
{
    if (ReadSize && (ReadSize <= 256))
    {
        I2CRegs.TxCount = 0;
        I2CRegs.RxCount = ReadSize;
        I2CRegs.RWAddr = E2RomAddr;
        I2CRegs.I2CAddr |= 1;//0xa1
        I2cStart();
    }

}

void I2CReadByte(unsigned int E2RomAddr)
{
    I2CRegs.TxCount = 0;
    I2CRegs.RxCount = 1;
    I2CRegs.RWAddr = E2RomAddr;
    I2CRegs.I2CAddr |= 1;//0xa1
    I2cStart();
}

/*------------------------------------------------------------------
    EEPROM寫塊函數(shù)
-------------------------------------------------------------------*/
void I2CWriteBuffers(unsigned int E2RomAddr, unsigned int WriteSize)
{
    if (WriteSize && (WriteSize <= 16))
    {
        I2CRegs.TxCount = WriteSize;
        I2CRegs.RxCount = 0;

 I2CRegs.RWAddr = E2RomAddr;
        I2CRegs.I2CAddr &= 0xfe;//0xa0
        I2cStart();
    }
}

void I2CWriteByte(unsigned int E2RomAddr, unsigned char cData)
{
    I2CRegs.TxBuffer[0] = cData;
    I2CRegs.TxCount = 1;
    I2CRegs.RxCount = 0;
    I2CRegs.RWAddr = E2RomAddr;
    I2CRegs.I2CAddr &= 0xfe;//0xa0
    I2cStart();
}

/*------------------------------------------------------------------
    用戶讀回調(diào)函數(shù)
-------------------------------------------------------------------*/
void I2CReadCallBack(void)
{
    if ((I2CRegs.RWAddr + I2CRegs.RxCount) <= I2CRegs.MaxCount)
    {
//        I2CRegs.RWAddr += I2CRegs.RxCount;
//        I2CReadBuffers(I2CRegs.RWAddr, I2CRegs.RxCount);//繼續(xù)讀
    }
}

/*------------------------------------------------------------------
    用戶寫回調(diào)函數(shù)
-------------------------------------------------------------------*/
void I2CWriteCallBack(void)
{
    if ((I2CRegs.RWAddr + I2CRegs.TxCount) <= I2CRegs.MaxCount)
    {
//        I2CRegs.RWAddr += I2CRegs.TxCount;
//        I2CWriteBuffers(I2CRegs.RWAddr, I2CRegs.TxCount);//繼續(xù)寫
    }
}


/*-----------------------------------------------------------------

 EEPROM讀寫啟動函數(shù)
-------------------------------------------------------------------*/
void I2cStart(void)
{
/*------------------------------------------------------------------------
//本程序在狀態(tài)I2C_MT_ADDRL_ACK下進行瞬間打開,也可在此打開,不過安全不好
    if (I2CRegs.TxCount)//需要寫入字節(jié)
    {
        WP = 0;//不寫保護
    }
    else
    {
        WP = 1;//寫保護
    }
--------------------------------------------------------------------------*/
    I2C1STATbits.IWCOL = 0;
    I2CBits.BusyFlag = 1;
    I2CRegs.State = I2C_START;//主機準備發(fā)送啟始位
    I2CRegs.Count = 0;//發(fā)送數(shù)據(jù)個數(shù)
    I2CBits.I2CFlag = 0;
    I2C1CONbits.SEN = 1;//發(fā)送Start信號
}

/*------------------------------------------------------------------
   

/*------------------------------------------------------------------
    EEPROM讀寫錯誤退出函數(shù)
-------------------------------------------------------------------*/
void I2cExit(void)
{
    I2C1STATbits.IWCOL = 0;
    I2CBits.BusyFlag = 0;
    I2CRegs.State = I2C_FAILED;
    I2C1CONbits.PEN = 1;//發(fā)送Stop信號
    WP = 1;//寫保護
}

/*------------------------------------------------------------------
     EEPROM讀寫中斷事件處理函數(shù)(說明見文件頭部)
-------------------------------------------------------------------*/
void I2CExec(void)
{
    if (I2C1STATbits.S)//收到Start過信號
    {
        switch (I2CRegs.State)
        {

case I2C_START://收到Start信號
                I2C1TRN = I2CRegs.I2CAddr & 0xfe;//發(fā)送器件寫地址(通知從機只能聽)
                I2CRegs.State = I2C_MT_SLA_ACK;//下次應該接收器件寫地址應答信號
                break;
            case I2C_MT_SLA_ACK://收到器件寫地址應答信號
                if (!I2C1STATbits.ACKSTAT)//收到Ack信號
                {
                    if (I2CRegs.MaxCount > 0x100)//EEPROM容量超過256個字節(jié),EEPROM地址需要兩次發(fā)送
                    {
                        I2C1TRN = I2CRegs.RWAddr >> 8;//發(fā)送EEPROM寫高8位地址
                        I2CRegs.State = I2C_MT_ADDRH_ACK;//下次應該接收EEPROM寫高8位地址應答信號

 }
                    else//小容量只需一次發(fā)送!!!
                    {
                        I2C1TRN = I2CRegs.RWAddr;//發(fā)送EEPROM寫低8位地址
                        I2CRegs.State = I2C_MT_ADDRL_ACK;//下次應該接收EEPROM寫低8位地址應答信號
                        I2CRegs.Count = 0;//清空發(fā)送緩沖計數(shù)器
                    }
                }    
                else//收到NAck信號
                {
                    I2cExit();//錯誤的ACK信號    
                }    
                break;
            case I2C_MT_ADDRH_ACK://收到EEPROM寫高8位地址應答信號
                if (!I2C1STATbits.ACKSTAT)//收到Ack信號
                {
                    I2C1TRN = I2CRegs.RWAddr & 0xff;//發(fā)送EEPROM寫低8位地址
                    I2CRegs.State = I2C_MT_ADDRL_ACK;//下次應該接收EEPROM寫低8位地址應答信號
                    I2CRegs.Count = 0;//清空發(fā)送緩沖計數(shù)器
                }    
                else//收到NAck信號
                {
                    I2cExit();//錯誤的ACK信號    

 }    
                break;
            case I2C_MT_ADDRL_ACK://收到EEPROM寫高低8位地址應答信號
                if (I2CRegs.TxCount)//寫保護只在寫入期間不保護,增加了對誤寫入的安全防護能力!!!
                {
                     WP = 0;//不寫保護
                }
            case I2C_MT_DATA_ACK://收到應答信號

 if (!I2C1STATbits.ACKSTAT)//收到Ack信號
                {
                    if (I2CRegs.Count < I2CRegs.TxCount)//緩沖區(qū)未空
                    {
                        I2C1TRN = I2CRegs.TxBuffer[I2CRegs.Count ++];//繼續(xù)發(fā)送數(shù)據(jù)    
                    }
                    else if (I2CRegs.Count == I2CRegs.TxCount)//緩沖區(qū)已空
                    {
                        if (I2CRegs.I2CAddr & 1)//應該開始接收數(shù)據(jù)
                        {
                            I2cReStart();//發(fā)送重復位命令
                        }
                        else//只寫退出
                        {
                            I2cStop();//正常發(fā)送結(jié)束
                        }
                    }
                    else//干擾出錯
                    {
                        I2cExit();//錯誤

 }
                }    
                else//收到NAck信號(可能被寫保護)
                {
                    I2cExit();//錯誤的ACK信號    
                }    
                break;
            case I2C_REP_START://收到ReStart信號
                I2C1TRN = I2CRegs.I2CAddr | I2C_READ;//發(fā)送器件讀地址(通知從機可以說話)
                I2CRegs.State = I2C_MR_SLA_ACK;//下次應該接收器件寫讀地址應答信號
                break;
            case I2C_MR_SLA_ACK://收到器件讀地址應答信號
                if (!I2C1STATbits.ACKSTAT)//收到Ack

信號
                {
                    I2C1CONbits.RCEN = 1;//開始接收數(shù)據(jù)
                    I2CRegs.State = I2C_MR_DATA;//下次應該收接收數(shù)據(jù)
                }    
                else//收到NAck信號
                {
                    I2cExit();//錯誤的ACK信號    
                }    
                break;
            case I2C_MR_DATA://收到接收數(shù)據(jù)
                if (I2CRegs.Count < I2CRegs.RxCount)
                {
//                    I2C1STATbits.I2COV = 0;
                    I2CRegs.RxBuffer[I2CRegs.Count ++] = I2C1RCV;
                    if (I2CRegs.Count < I2CRegs.RxCount)
                    {
                         I2C1CONbits.ACKDT = 0;//應答子機
                        I2CRegs.State = I2C_MR_DATA_EN;//下次應該收到器件允許繼續(xù)讀信號
                    }

 else
                    {
                        I2C1CONbits.ACKDT = 1;//非應答子機    
                        I2CRegs.State = I2C_MR_DATA_STOP;//下次應該收到退出信號
                    }
                     I2C1CONbits.ACKEN = 1;//向從機發(fā)送(非)應答信號
                }
                else//正確的狀態(tài)已分支到I2C_MR_DATA_STOP
                {
                    I2cExit();//錯誤
                }
                break;
            case I2C_MR_DATA_EN://收到器件允許繼續(xù)讀信號

 I2C1CONbits.RCEN = 1;//開始接收數(shù)據(jù)
                I2CRegs.State = I2C_MR_DATA;//下次應該繼續(xù)接收數(shù)據(jù)
                break;
            case I2C_MR_DATA_STOP://收到器件退出信號
                I2cStop();//正常接收結(jié)束
                break;
            default://其他不可預料的錯誤
                I2cExit();//錯誤
        }
    }
    else if (I2C1STATbits.P)//收到Stop信號
    {
        if (I2CRegs.State == I2C_SUCCEEDED)//成功,回調(diào)
        {
            if (I2CRegs.I2CAddr & 1)//讀
            {
                I2CBits.ReadFlag = 1;//激活用戶讀回調(diào)函數(shù)I2CReadCallBack()
            }
            else//寫
            {
                I2CBits.WriteFlag = 1;//激活用戶寫回調(diào)函數(shù)I2CWriteCallBack()
            }
        }
    }
    else//無法確認的復雜錯誤
    {

 I2cExit();//錯誤出錯退出
    }
}

(文源網(wǎng)絡,侵刪)

九龙城区| 利津县| 桑植县| 青浦区| 五常市| 蒲江县| 甘孜县| 沁源县| 金阳县| 怀远县| 绥德县| 松潘县| 东阿县| 邻水| 新津县| 金湖县| 苍山县| 山阴县| 焦作市| 伊金霍洛旗| 眉山市| 淅川县| 石林| 惠东县| 青川县| 潍坊市| 唐河县| 安庆市| 兴文县| 连城县| 叙永县| 玛纳斯县| 安顺市| 察雅县| 阿荣旗| 连南| 界首市| 余江县| 册亨县| 建平县| 新绛县|