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

技術(shù)熱線(xiàn): 4007-888-234

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

提供開(kāi)發(fā)工具、應(yīng)用測(cè)試 完善的開(kāi)發(fā)代碼案例庫(kù)分享

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

技術(shù)支持

PIC24F單片機(jī)之EEPROM讀寫(xiě)中斷事件處理函數(shù)要點(diǎn)及說(shuō)明

更新時(shí)間: 2019-03-23

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

/*-------------------------------------------------------------------------------------------------
                        PIC24F之EEPROM讀寫(xiě)中斷事件處理函數(shù)要點(diǎn)及說(shuō)明
注意: 這是一個(gè)通用的I2C/SMBUS通訊中斷處理程序
對(duì)于EEPROM來(lái)講,從機(jī)后面需要跟EEPROM需要讀寫(xiě)的地址(I2CRegs.RWAddr)
對(duì)于SMBUS來(lái)說(shuō),從機(jī)后面需要跟SMBUS需要的命令(I2CRegs.RWAddr改為I2CRegs.CMD即可)
由于PIC24F的I2C不太標(biāo)準(zhǔn),I2C1STAT被搞得很倒塌?。。∫稽c(diǎn)都沒(méi)I2C的"大家閨秀"的樣子~~~
不過(guò)它的STOP還能激活中斷確實(shí)比LPCARM/AVR好一點(diǎn)點(diǎn)~~~

--------------------------------------------------------------------------------------------------*/
#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;//寫(xiě)保護(hù)
    I2CRegs.MaxCount = 0x200;//8KByte
    I2CRegs.I2CAddr = 0xa0;//器件地址
    I2CRegs.RWAddr = 0;//EEPROM讀寫(xiě)地址
    I2CRegs.TxCount = 0;//發(fā)送數(shù)據(jù)字節(jié)個(gè)數(shù)
    I2CRegs.RxCount = 0;//接收數(shù)據(jù)字節(jié)個(gè)數(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;//波特率計(jì)算
/*------------------------------------------------------------------------
    定義I2C串口2中斷優(yōu)先級(jí)位1111)
-------------------------------------------------------------------------*/

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

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

//    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寫(xiě)塊函數(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();
}

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

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


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

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

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

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

/*------------------------------------------------------------------
     EEPROM讀寫(xiě)中斷事件處理函數(shù)(說(shuō)明見(jiàn)文件頭部)
-------------------------------------------------------------------*/
void I2CExec(void)
{
    if (I2C1STATbits.S)//收到Start過(guò)信號(hào)
    {
        switch (I2CRegs.State)
        {

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

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

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

 if (!I2C1STATbits.ACKSTAT)//收到Ack信號(hào)
                {
                    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)//應(yīng)該開(kāi)始接收數(shù)據(jù)
                        {
                            I2cReStart();//發(fā)送重復(fù)位命令
                        }
                        else//只寫(xiě)退出
                        {
                            I2cStop();//正常發(fā)送結(jié)束
                        }
                    }
                    else//干擾出錯(cuò)
                    {
                        I2cExit();//錯(cuò)誤

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

信號(hào)
                {
                    I2C1CONbits.RCEN = 1;//開(kāi)始接收數(shù)據(jù)
                    I2CRegs.State = I2C_MR_DATA;//下次應(yīng)該收接收數(shù)據(jù)
                }    
                else//收到NAck信號(hào)
                {
                    I2cExit();//錯(cuò)誤的ACK信號(hào)    
                }    
                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;//應(yīng)答子機(jī)
                        I2CRegs.State = I2C_MR_DATA_EN;//下次應(yīng)該收到器件允許繼續(xù)讀信號(hào)
                    }

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

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

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

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

404
返回首頁(yè) |  返回上一頁(yè)
长葛市| 辉南县| 冕宁县| 洪雅县| 武汉市| 刚察县| 湾仔区| 平乡县| 沧源| 凤凰县| 峡江县| 兴国县| 洱源县| 五指山市| 浮山县| 房山区| 扶沟县| 顺昌县| 曲麻莱县| 渝中区| 祁门县| 新建县| 临西县| 新泰市| 云阳县| 白朗县| 临洮县| 宣恩县| 莱州市| 龙江县| 武义县| 祁东县| 长海县| 黄陵县| 柳江县| 彰化县| 乳山市| 宁夏| 天津市| 东丰县| 开封市|