PIC24F之EEPROM讀寫中斷事件處理函數(shù)要點(diǎn)及說(shuō)明
/*----------------------------------------------------------------------
PIC24F之EEPROM讀寫中斷事件處理函數(shù)要點(diǎn)及說(shuō)明
注意:這是一個(gè)通用的I2C/SMBUS通訊中斷處理程序
對(duì)于EEPROM來(lái)講,從機(jī)后面需要跟EEPROM需要讀寫的地址(I2CRegs.RWAddr)
對(duì)于SMBUS來(lái)說(shuō),從機(jī)后面需要跟SMBUS需要的命令(I2CRegs.RWAddr改為I2CRegs.CMD即可)
由于PIC24F的I2C不太標(biāo)準(zhǔn),I2C1STAT被搞得很倒塌!??!一點(diǎn)都沒(méi)I2C的"大家閨秀"的樣子~~~
不過(guò)它的STOP還能激活中斷確實(shí)比LPCARM/AVR好一點(diǎn)點(diǎn)~~~
為什么I2C收發(fā)都用中斷呢???
這主要是為了高低速靈活變化的總線通訊所做,主要是SMBUS總線的通信.
如果為I2cExit()也配上鉤子函數(shù),那么任何錯(cuò)誤都在手掌中~~~
這個(gè)PIC程序雖沒(méi)SMBUS的PEC校驗(yàn)部分,但"異步"還是完美的.
當(dāng)然也要注意對(duì)寫保護(hù)硬件管腳的控制時(shí)機(jī)的把握,原則是關(guān)保護(hù)的時(shí)間最短就更好~~~
主要看到人們編寫MCU程序太死板~~~特別是I2C程序.網(wǎng)上收發(fā)全中斷的很少,可以說(shuō)幾乎沒(méi)有.
隨貼附老外倒塌的非中斷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上"聲東擊西"呢???
"有事件才處理"---這才是編程的硬道理~~~輪循的“癡迷等待”最終還是“單相思”~~~
-------------------------------------------------------------------------------------------------*/
#include"i2c.h"
_PERSISTENTvolatileI2CREGSI2CRegs;
_PERSISTENTvolatileI2CBITSI2CBits;
voidI2cInit(void)
{
unsignedinti;
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[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ù))
-------------------------------------------------------------------*/
voidI2CReadBuffers(unsignedintE2RomAddr,unsignedintReadSize)
{
if(ReadSize&&(ReadSize<=256))
{
I2CRegs.TxCount=0;
I2CRegs.RxCount=ReadSize;
I2CRegs.RWAddr=E2RomAddr;
I2CRegs.I2CAddr|=1;//0xa1
I2cStart();
}
}
voidI2CReadByte(unsignedintE2RomAddr)
{
I2CRegs.TxCount=0;
I2CRegs.RxCount=1;
I2CRegs.RWAddr=E2RomAddr;
I2CRegs.I2CAddr|=1;//0xa1
I2cStart();
}
/*------------------------------------------------------------------
EEPROM寫塊函數(shù)
-------------------------------------------------------------------*/
voidI2CWriteBuffers(unsignedintE2RomAddr,unsignedintWriteSize)
{
if(WriteSize&&(WriteSize<=16))
{
I2CRegs.TxCount=WriteSize;
I2CRegs.RxCount=0;
I2CRegs.RWAddr=E2RomAddr;
I2CRegs.I2CAddr&=0xfe;//0xa0
I2cStart();
}
}
voidI2CWriteByte(unsignedintE2RomAddr,unsignedcharcData)
{
I2CRegs.TxBuffer[0]=cData;
I2CRegs.TxCount=1;
I2CRegs.RxCount=0;
I2CRegs.RWAddr=E2RomAddr;
I2CRegs.I2CAddr&=0xfe;//0xa0
I2cStart();
}
/*------------------------------------------------------------------
用戶讀回調(diào)函數(shù)
-------------------------------------------------------------------*/
voidI2CReadCallBack(void)
{
if((I2CRegs.RWAddr+I2CRegs.RxCount)<=I2CRegs.MaxCount)
{
//I2CRegs.RWAddr+=I2CRegs.RxCount;
//I2CReadBuffers(I2CRegs.RWAddr,I2CRegs.RxCount);//繼續(xù)讀
}
}
/*------------------------------------------------------------------
用戶寫回調(diào)函數(shù)
-------------------------------------------------------------------*/
voidI2CWriteCallBack(void)
{
if((I2CRegs.RWAddr+I2CRegs.TxCount)<=I2CRegs.MaxCount)
{
//I2CRegs.RWAddr+=I2CRegs.TxCount;
//I2CWriteBuffers(I2CRegs.RWAddr,I2CRegs.TxCount);//繼續(xù)寫
}
}
/*------------------------------------------------------------------
EEPROM讀寫啟動(dòng)函數(shù)
-------------------------------------------------------------------*/
voidI2cStart(void)
{
/*------------------------------------------------------------------------
//本程序在狀態(tài)I2C_MT_ADDRL_ACK下進(jìn)行瞬間打開(kāi),也可在此打開(kāi),不過(guò)安全不好
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ù)
-------------------------------------------------------------------*/
voidI2cReStart(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ù)
-------------------------------------------------------------------*/
voidI2cStop(void)
{
I2C1STATbits.IWCOL=0;
I2CBits.BusyFlag=0;
I2CRegs.State=I2C_SUCCEEDED;//通訊成功
I2C1CONbits.PEN=1;//發(fā)送Stop信號(hào)
WP=1;//寫保護(hù)
}
/*------------------------------------------------------------------
EEPROM讀寫錯(cuò)誤退出函數(shù)
-------------------------------------------------------------------*/
voidI2cExit(void)
{
I2C1STATbits.IWCOL=0;
I2CBits.BusyFlag=0;
I2CRegs.State=I2C_FAILED;
I2C1CONbits.PEN=1;//發(fā)送Stop信號(hào)
WP=1;//寫保護(hù)
}
/*------------------------------------------------------------------
EEPROM讀寫中斷事件處理函數(shù)(說(shuō)明見(jiàn)文件頭部)
-------------------------------------------------------------------*/
voidI2CExec(void)
{
if(I2C1STATbits.S)//收到Start過(guò)信號(hào)
{
switch(I2CRegs.State)
{
caseI2C_START://收到Start信號(hào)
I2C1TRN=I2CRegs.I2CAddr&0xfe;//發(fā)送器件寫地址(通知從機(jī)只能聽(tīng))
I2CRegs.State=I2C_MT_SLA_ACK;//下次應(yīng)該接收器件寫地址應(yīng)答信號(hào)
break;
caseI2C_MT_SLA_ACK://收到器件寫地址應(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寫高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;
caseI2C_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;
caseI2C_MT_ADDRL_ACK://收到EEPROM寫高低8位地址應(yīng)答信號(hào)
if(I2CRegs.TxCount)//寫保護(hù)只在寫入期間不保護(hù),增加了對(duì)誤寫入的安全防護(hù)能力!!!
{
WP=0;//不寫保護(hù)
}
caseI2C_MT_DATA_ACK://收到應(yīng)答信號(hào)
if(!I2C1STATbits.ACKSTAT)//收到Ack信號(hào)
{
if(I2CRegs.Count
I2C1TRN=I2CRegs.TxBuffer[I2CRegs.Count++];//繼續(xù)發(fā)送數(shù)據(jù)
}
elseif(I2CRegs.Count==I2CRegs.TxCount)//緩沖區(qū)已空
{
if(I2CRegs.I2CAddr&1)//應(yīng)該開(kāi)始接收數(shù)據(jù)
{
I2cReStart();//發(fā)送重復(fù)位命令
}
else//只寫退出
{
I2cStop();//正常發(fā)送結(jié)束
}
}
else//干擾出錯(cuò)
{
I2cExit();//錯(cuò)誤
}
}
else//收到NAck信號(hào)(可能被寫保護(hù))
{
I2cExit();//錯(cuò)誤的ACK信號(hào)
}
break;
caseI2C_REP_START://收到ReStart信號(hào)
I2C1TRN=I2CRegs.I2CAddr|I2C_READ;//發(fā)送器件讀地址(通知從機(jī)可以說(shuō)話)
I2CRegs.State=I2C_MR_SLA_ACK;//下次應(yīng)該接收器件寫讀地址應(yīng)答信號(hào)
break;
caseI2C_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;
caseI2C_MR_DATA://收到接收數(shù)據(jù)
if(I2CRegs.Count
//I2C1STATbits.I2COV=0;
I2CRegs.RxBuffer[I2CRegs.Count++]=I2C1RCV;
if(I2CRegs.Count
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;
caseI2C_MR_DATA_EN://收到器件允許繼續(xù)讀信號(hào)
I2C1CONbits.RCEN=1;//開(kāi)始接收數(shù)據(jù)
I2CRegs.State=I2C_MR_DATA;//下次應(yīng)該繼續(xù)接收數(shù)據(jù)
break;
caseI2C_MR_DATA_STOP://收到器件退出信號(hào)
I2cStop();//正常接收結(jié)束
break;
default://其他不可預(yù)料的錯(cuò)誤
I2cExit();//錯(cuò)誤
}
}
elseif(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//無(wú)法確認(rèn)的復(fù)雜錯(cuò)誤
{
I2cExit();//錯(cuò)誤出錯(cuò)退出
}
}