?? isr_i2c.c
字號:
//*-----------------------------------------------------------------------------
//* 函數名稱 : I2CRxPrepareACK (I2Cdesc *I2C_pt)
//* Object : I2C
//* 輸入參數 : <I2C_pt> = 為I2C的描述指針,其結構定義在source.h中
//* 輸出變量 : 無
//* 功能 :為I2C的接收應答時序的前的準備
//*-----------------------------------------------------------------------------
void I2CRxPrepareACK (I2Cdesc *I2C_pt)
//* Begin
{
//* set SCL line
SCL_SET();
if(I2C_pt->mode == STOP)
//* change interrupt handler to
I2C_pt->I2CTCHandler = I2CTxPrepareSTOP;
else
//* change interrupt handler to I2CRxDoACK
I2C_pt->I2CTCHandler = I2CRxDoACK;
//* End
}
//*-----------------------------------------------------------------------------
//* 函數名稱 : I2CTxWaitACK (I2Cdesc *I2C_pt)
//* Object : I2C
//* 輸入參數 : <I2C_pt> = 為I2C的描述指針,其結構定義在source.h中
//* 輸出變量 : 無
//* 功能 :為I2C的等待芯片的應答
//*-----------------------------------------------------------------------------
void I2CTxWaitACK (I2Cdesc *I2C_pt){
if(SDA_VAL() == 0){ //* if ACK (SDA = 0)...
switch(I2C_pt->mode){
case STOP :
SCL_CLR(); //* clear SCL line
I2C_pt->I2CTCHandler = I2CTxPrepareSTOP; //* change interrupt handler to
break;
case DEVICE_READ :
SCL_CLR(); //* clear SCL line
I2C_pt->I2CTCHandler = I2CDeviceRead; //* change interrupt handler to
break;
case DATA_READ :
I2C_pt->I2CTCHandler = I2CRxReceiveByte; //* change interrupt handler to
break;
default :
SCL_CLR(); //* clear SCL line
//* change interrupt handler to
I2C_pt->I2CTCHandler = I2CTxSendByte;
break;
}
}
else{
I2C_pt->nb_ACK--; //Decrease the counter.
if(I2C_pt->nb_ACK == 0)
I2C_pt->I2CTCHandler = I2CError; //Timeout, change interrupt handler to I2C
}
}
//*-----------------------------------------------------------------------------
//* 函數名稱 : I2CRxDoACK (I2Cdesc *I2C_pt)
//* Object : I2C
//* 輸入參數 : <I2C_pt> = 為I2C的描述指針,其結構定義在source.h中
//* 輸出變量 : 無
//* 功能 :為主動發送應答給I2C設備
//*-----------------------------------------------------------------------------
void I2CRxDoACK (I2Cdesc *I2C_pt)
//* Begin
{
//* clear SCL line
SCL_CLR();
//* change interrupt handler to
I2C_pt->I2CTCHandler = I2CRxReceiveByte;
//* End
}
//*-----------------------------------------------------------------------------
//* 函數名稱 : I2CTxSetSCL (I2Cdesc *I2C_pt)
//* Object : I2C
//* 輸入參數 : <I2C_pt> = 為I2C的描述指針,其結構定義在source.h中
//* 輸出變量 : 無
//* 功能 :為在發送數據時把I2C的SCL線設置為高電平
//*-----------------------------------------------------------------------------
void I2CTxSetSCL (I2Cdesc *I2C_pt){
SCL_SET(); //* set SCL line
//* change interrupt handler to I2CTxSendBit
I2C_pt->I2CTCHandler = I2CTxSendBit;
}
//*-----------------------------------------------------------------------------
//* 函數名稱 : I2CTxClearSCL (I2Cdesc *I2C_pt)
//* Object : I2C
//* 輸入參數 : <I2C_pt> = 為I2C的描述指針,其結構定義在source.h中
//* 輸出變量 : 無
//* 功能 :為在發送數據時把SCL線設置為低電平
//*-----------------------------------------------------------------------------
void I2CTxClearSCL (I2Cdesc *I2C_pt)
//* Begin
{
//* Clear SCL line
SCL_CLR();
if(I2C_pt->countBit == 0)
//* set SDA line as an input
//* change interrupt handler to I2CTxSendBit
I2C_pt->I2CTCHandler = I2CTxSendBit;
//* End
}
//*-----------------------------------------------------------------------------
//* 函數名稱 : I2CRxSetSCL (I2Cdesc *I2C_pt)
//* Object : I2C
//* 輸入參數 : <I2C_pt> = 為I2C的描述指針,其結構定義在source.h中
//* 輸出變量 : 無
//* 功能 :為在接收數據時把SCL線設置為高電平
//*-----------------------------------------------------------------------------
void I2CRxSetSCL (I2Cdesc *I2C_pt)
//* Begin
{
//* set SCL line
SCL_SET();
//* change interrupt handler to I2CRxReceiveBit
I2C_pt->I2CTCHandler = I2CRxReceiveBit;
//* End
}
//*-----------------------------------------------------------------------------
//* 函數名稱 : I2CRxClearSCL (I2Cdesc *I2C_pt)
//* Object : I2C
//* 輸入參數 : <I2C_pt> = 為I2C的描述指針,其結構定義在source.h中
//* 輸出變量 : 無
//* 功能 :為在接收數據時把SCL線設置為低電平
//*-----------------------------------------------------------------------------
void I2CRxClearSCL (I2Cdesc *I2C_pt)
//* Begin
{
//* Clear SCL line
SCL_CLR();
if(I2C_pt->countBit == 0)
{
//* change interrupt handler to I2CI2CRxPrepareACK
I2C_pt->I2CTCHandler = I2CRxPrepareACK;
//* set SDA line as an output
//* clear SDA line
SDA_CLR();
}
else
//* change interrupt handler to I2CRx
I2C_pt->I2CTCHandler = I2CRxReceiveBit;
//* End
}
//*-----------------------------------------------------------------------------
//* 函數名稱 : I2CRxReceiveByte (I2Cdesc *I2C_pt)
//* Object : I2C
//* 輸入參數 : <I2C_pt> = 為I2C的描述指針,其結構定義在source.h中
//* 輸出變量 : 無
//* 功能 :為在從I2C中接收一個字節的數據
//*-----------------------------------------------------------------------------
unsigned int ttmp=0;
void I2CRxReceiveByte (I2Cdesc *I2C_pt)
//* Begin
{
//* set SDA line as an input
//* clear SCL line
SCL_CLR();
//* initialize countBit
I2C_pt->countBit = 8;
*I2C_pt->RxEnd=0;
ttmp=0;
I2C_pt->nbI2CByte--;
if(I2C_pt->nbI2CByte == 0)
//* next mode
I2C_pt->mode = STOP;
//* change interrupt handler to I2C
I2C_pt->I2CTCHandler = I2CRxReceiveBit;
//* End
}
//*-----------------------------------------------------------------------------
//* 函數名稱 : I2CRxReceiveBit (I2Cdesc *I2C_pt)
//* Object : I2C
//* 輸入參數 : <I2C_pt> = 為I2C的描述指針,其結構定義在source.h中
//* 輸出變量 : 無
//* 功能 :為在從I2C中接收一個比特的數據
//*-----------------------------------------------------------------------------
void I2CRxReceiveBit (I2Cdesc *I2C_pt)
//* Begin
{
unsigned char bit_val;
//* set SCL line
SCL_SET();
//* decrement countBit
I2C_pt->countBit--;
//* get bit
if(SDA_VAL()){
bit_val = 1;
//printk("r1 ");
}
else{
bit_val = 0;
//printk("r0 ");
}
//* if bit counter > 0
if(I2C_pt->countBit > 0){
//* update working byte
*I2C_pt->RxEnd |= (bit_val << I2C_pt->countBit);
ttmp |= (bit_val << I2C_pt->countBit);
}
else{
*(I2C_pt->RxEnd++) |= (bit_val << I2C_pt->countBit) ;
ttmp |= (bit_val << I2C_pt->countBit);
//printk("\r\n ");
}
//* change interrupt handler to I2CRxClearSCL
I2C_pt->I2CTCHandler = I2CRxClearSCL;
//* End
}
//*-----------------------------------------------------------------------------
//* 函數名稱 : Timer_I2C (I2Cdesc *I2C_pt)
//* Object : I2C
//* 輸入參數 : 無
//* 輸出變量 : 無
//* 功能 :為I2C的中斷定時器,I2C的一切函數都由此中斷來驅動
//*-----------------------------------------------------------------------------
void Timer_I2C() interrupt T0_INT {//timer 1 interrupt.
if(!I2c_timer_cnt){
(*(I2c.I2CTCHandler))(&I2c); //call automate state handler
}
else{
I2c_timer_cnt--;
}
}
//*-----------------------------------------------------------------------------
//* 函數名稱 : I2C_lineOpen (I2Cdesc *I2C_pt, unsigned int us)
//* Object : I2C
//* 輸入參數 : <I2C_pt> = 為I2C的描述指針,其結構定義在source.h中
//* :us 為I2C中斷的時中斷時間長度,此參數表示I2C的SCL的頻率
//* 輸出變量 : 無
//* 功能 :初始化I2C的設備和定時器
//*-----------------------------------------------------------------------------
void I2C_lineOpen (I2Cdesc *I2C_pt, unsigned int us){
/*****Set Up Paramter ***/
int tmp;
I2C_pt->deviceAddress = 0;
/*****Set Up Line****/
I2C_pt->state = OK;
SDA_SET();; //* set SDA line high
SCL_SET(); //* set SCL line high
/*****Setup Interrupt **/
/*****Setup Timer **/
I2C_pt->Timer_Gap_us=us;
Init_I2c_Timer(us);
Start_I2c_Timer(us);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -