/*------------------------------------------------------------------------------------------------- PIC24F之EEPROM讀寫中斷事件處理函數(shù)要點(diǎn)及說明 注意: 這是一個(gè)通用的I2C/SMBUS通訊中斷處理程序 對(duì)于EEPROM來講,從機(jī)后面需要跟EEPROM需要讀寫的地址(I2CRegs.RWAddr) 對(duì)于SMBUS來說,從機(jī)后面需要跟SMBUS需要的命令(I2CRegs.RWAddr改為I2CRegs.CMD即可) 由于PIC24F的I2C不太標(biāo)準(zhǔn),I2C1STAT被搞得很倒塌!!一點(diǎn)都沒I2C的"大家閨秀"的樣子~~~ 不過它的STOP還能激活中斷確實(shí)比LPCARM/AVR好一點(diǎn)點(diǎn)~~~ 為什么I2C收發(fā)都用中斷呢??? 這主要是為了高低速靈活變化的總線通訊所做,主要是SMBUS總線的通信. 菜農(nóng)在LPCARM/AVR上用此程序模板可謂不怕數(shù)據(jù)被干擾~~~ 如果為I2cExit()也配上鉤子函數(shù),那么任何錯(cuò)誤都在手掌中~~~ 這個(gè)PIC程序雖沒SMBUS的PEC校驗(yàn)部分,但"異步"還是完美的. 當(dāng)然也要注意對(duì)寫保護(hù)硬件管腳的控制時(shí)機(jī)的把握,原則是關(guān)保護(hù)的時(shí)間最短就更好~~~ 菜農(nóng)本來PIC24F菜鳥已“畢業(yè)”,但還是“忍痛”發(fā)表出來~~~ 主要看到人們編寫MCU程序太死板~~~特別是I2C程序.網(wǎng)上收發(fā)全中斷的很少,可以說幾乎沒有. 隨貼附老外倒塌的非中斷I2C狀態(tài)機(jī)讀寫程序i2cEmem.c~~~可以比較經(jīng)典和非典的差異在何處~~~ 菜農(nóng)近期將整理出LPCARM和AVR的I2C/SMBUS/TWI/USI收發(fā)全中斷實(shí)戰(zhàn)例程供大家“游玩”~~~ 如果精通DELPHI程序的人一定會(huì)為“事件驅(qū)動(dòng)”機(jī)制而癡迷~~~為什么不在MCU上"聲東擊西"呢??? "有事件才處理"---這才是編程的硬道理~~~輪循的“癡迷等待”最終還是“單相思”~~~ 本程序附實(shí)戰(zhàn)結(jié)果圖.(因?yàn)椴宿r(nóng)的程序從來不空談社會(huì)主義~~~) 原本是在"雞蛋節(jié)"獻(xiàn)給大家,由于"憶苦思甜"沒發(fā)~~~就算是“臭蛋節(jié)”的禮物吧~~~ 菜農(nóng) HotPower@126.com 2007.12.25 "雞蛋節(jié)"于大雁塔菜地 --------------------------------------------------------------------------------------------------*/ #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;//寫保護(hù) I2CRegs.MaxCount = 0x200;//8KByte I2CRegs.I2CAddr = 0xa0;//器件地址 I2CRegs.RWAddr = 0;//EEPROM讀寫地址 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 = 0;//發(fā)送緩沖區(qū)清零 } for (i = 0; i < 256; i ++) { I2CRegs.RxBuffer = 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寫塊函數(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讀寫啟動(dòng)函數(shù) -------------------------------------------------------------------*/ void I2cStart(void) { /*------------------------------------------------------------------------ //本程序在狀態(tài)I2C_MT_ADDRL_ACK下進(jìn)行瞬間打開,也可在此打開,不過安全不好 if (I2CRegs.TxCount)//需要寫入字節(jié) { WP = 0;//不寫保護(hù) } else { WP = 1;//寫保護(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讀再啟動(dòng)函數(shù) -------------------------------------------------------------------*/ void I2cReStart(void) { I2C1STATbits.IWCOL = 0; I2CBits.BusyFlag = 1; I2CRegs.State = I2C_REP_START;//主機(jī)準(zhǔn)備發(fā)送重新啟始位 I2CRegs.Count = 0;//發(fā)送數(shù)據(jù)個(gè)數(shù) I2C1CONbits.RSEN = 1;//發(fā)送ReStart信號(hào) I2C1CONbits.ACKEN = 0; } /*------------------------------------------------------------------ EEPROM讀寫正確停止函數(shù) -------------------------------------------------------------------*/ void I2cStop(void) { I2C1STATbits.IWCOL = 0; I2CBits.BusyFlag = 0; I2CRegs.State = I2C_SUCCEEDED;//通訊成功 I2C1CONbits.PEN = 1;//發(fā)送Stop信號(hào) WP = 1;//寫保護(hù) } /*------------------------------------------------------------------ EEPROM讀寫錯(cuò)誤退出函數(shù) -------------------------------------------------------------------*/ void I2cExit(void) { I2C1STATbits.IWCOL = 0; I2CBits.BusyFlag = 0; I2CRegs.State = I2C_FAILED; I2C1CONbits.PEN = 1;//發(fā)送Stop信號(hào) WP = 1;//寫保護(hù) } /*------------------------------------------------------------------ EEPROM讀寫中斷事件處理函數(shù)(說明見文件頭部) -------------------------------------------------------------------*/ void I2CExec(void) { if (I2C1STATbits.S)//收到Start過信號(hào) { switch (I2CRegs.State) { case I2C_START://收到Start信號(hào) I2C1TRN = I2CRegs.I2CAddr & 0xfe;//發(fā)送器件寫地址(通知從機(jī)只能聽) I2CRegs.State = I2C_MT_SLA_ACK;//下次應(yīng)該接收器件寫地址應(yīng)答信號(hào) break; case I2C_MT_SLA_ACK://收到器件寫地址應(yīng)答信號(hào) if (!I2C1STATbits.ACKSTAT)//收到Ack信號(hào) { if (I2CRegs.MaxCount > 0x100)//EEPROM容量超過256個(gè)字節(jié),EEPROM地址需要兩次發(fā)送 { I2C1TRN = I2CRegs.RWAddr >> 8;//發(fā)送EEPROM寫高8位地址 I2CRegs.State = I2C_MT_ADDRH_ACK;//下次應(yīng)該接收EEPROM寫高8位地址應(yīng)答信號(hào) } else//小容量只需一次發(fā)送!!! { I2C1TRN = I2CRegs.RWAddr;//發(fā)送EEPROM寫低8位地址 I2CRegs.State = I2C_MT_ADDRL_ACK;//下次應(yīng)該接收EEPROM寫低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寫高8位地址應(yīng)答信號(hào) if (!I2C1STATbits.ACKSTAT)//收到Ack信號(hào) { I2C1TRN = I2CRegs.RWAddr & 0xff;//發(fā)送EEPROM寫低8位地址 I2CRegs.State = I2C_MT_ADDRL_ACK;//下次應(yīng)該接收EEPROM寫低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寫高低8位地址應(yīng)答信號(hào) if (I2CRegs.TxCount)//寫保護(hù)只在寫入期間不保護(hù),增加了對(duì)誤寫入的安全防護(hù)能力!!! { WP = 0;//不寫保護(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)該開始接收數(shù)據(jù) { I2cReStart();//發(fā)送重復(fù)位命令 } else//只寫退出 { I2cStop();//正常發(fā)送結(jié)束 } } else//干擾出錯(cuò) { I2cExit();//錯(cuò)誤 } } else//收到NAck信號(hào)(可能被寫保護(hù)) { I2cExit();//錯(cuò)誤的ACK信號(hào) } break; case I2C_REP_START://收到ReStart信號(hào) I2C1TRN = I2CRegs.I2CAddr | I2C_READ;//發(fā)送器件讀地址(通知從機(jī)可以說話) I2CRegs.State = I2C_MR_SLA_ACK;//下次應(yīng)該接收器件寫讀地址應(yīng)答信號(hào) break; case I2C_MR_SLA_ACK://收到器件讀地址應(yīng)答信號(hào) if (!I2C1STATbits.ACKSTAT)//收到Ack信號(hào) { I2C1CONbits.RCEN = 1;//開始接收數(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;//開始接收數(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;//激活用戶讀回調(diào)函數(shù)I2CReadCallBack() } else//寫 { I2CBits.WriteFlag = 1;//激活用戶寫回調(diào)函數(shù)I2CWriteCallBack() } } } else//無法確認(rèn)的復(fù)雜錯(cuò)誤 { I2cExit();//錯(cuò)誤出錯(cuò)退出 } } |
歡迎光臨 電子工程網(wǎng) (http://m.qingdxww.cn/) | Powered by Discuz! X3.4 |