📄 lib.c
字号:
Write/Modify: Perry.Liang
Time: 2006-1-24- 15:40:54
*********************************************/
void Wait1ms(unsigned char time)
{
for(;time>0;time--)
{
Wait10us(100);
}
}
/*********************************************
Function: LibInit
Description:
Write/Modify: Perry.Liang
Time: 2006-1-24- 15:40:53
*********************************************/
void LibInit(void)
{
PD_SW_I2CData=1;
PO_SW_I2CData=0;
Pin_SW_I2CData=1;
PD_SW_I2CClk=1;
PO_SW_I2CClk=0;
Pin_SW_I2CClk=1;
PD_SW2_I2CData=1;
PO_SW2_I2CData=0;
Pin_SW2_I2CData=1;
PD_SW2_I2CClk=1;
PO_SW2_I2CClk=0;
Pin_SW2_I2CClk=1;
}
#if 0
/*********************************************
Function: I2C_Int
Description: I2C bus interrupt handler.
Write/Modify:
Time: 2006-06
*********************************************/
void HW_I2C_Int (void)
{
asm TNZ I2CSR1;
asm TNZ I2CSR2;
HW_I2C_MasterProcess();
}
/*********************************************
Function: HW_I2C_Reset
Description: I2C bus reset.
Write/Modify:
Time: 2006-06
*********************************************/
void HW_I2C_Reset (void)
{
I2CCCR = I2C_SPEED_100K; /*slow mode for main loop*/
I2COAR2 = 0; /* No 10 bit addressing (reset status).*/
asm TNZ I2CDR; /* Touch to clear any interrupt.*/
asm TNZ I2CSR1;
asm TNZ I2CSR2;
I2CCR =I2CCR = 0x25; /* I2C turn on w/ IT enable and Ack enable.*/
HW_I2C_FlagAllClear();
}
/*********************************************
Function: HW_I2C_MasterProcess
Description: I2C bus main process.
Write/Modify:
Time: 2006-06
*********************************************/
void HW_I2C_MasterProcess(void)
{
if ((I2C_AF==1)||(I2C_ARLO==1)||(I2C_BERR==1))/*error handle*/
{
if(mt_send_state!=MT_SEND_SUCCESS)
{
mt_send_state = MT_SEND_ERROR;
}
if(mr_rece_state!=MR_RECE_SUCCESS)
{
mr_rece_state = MR_RECE_ERROR;
}
HW_I2C_StopCom();
return;
}
switch(mt_send_state)
{
case MT_SEND_START:
if (I2C_SB==1) /*Ev5 evf=1,sb=1: start condition generated*/
{
I2CDR = i2c_dest;
mt_send_state = MT_SEND_DEST_ADDRESS;
}
break;
case MT_SEND_DEST_ADDRESS:
I2C_PE=1; /*EV6 write CR*/
if (I2C_BTF==0) /*EV8 check the trans success or not */
{
break;
}
else
{
mt_send_state = MT_SEND_DEST_DATA;
}
break;
case MT_SEND_DEST_DATA:
if (I2C_BTF==1) /*EV8 check the trans success or not */
{
if(i2c_nbyte_index <=Tx_length)
{
I2CDR =ACCORDO_Buffer_Tx[i2c_nbyte_index];
i2c_nbyte_index++;
}
else
{
mt_send_state = MT_SEND_STOP;
}
}
break;
case MT_SEND_STOP:
I2C_STOP=1;
mt_send_state = MT_SEND_SUCCESS;
case MT_SEND_SUCCESS:
break;
}
switch(mr_rece_state)
{
case MR_RECE_IDLE:
break;
case MR_RECE_START:
if (I2C_SB==1)
{
I2CDR = i2c_dest | 0x1;
mr_rece_state = MR_RECE_LEN;
}
break;
#if STCIS002
case MR_RECE_LEN:
I2C_PE=1;
if (I2C_BTF==1)
{
if(i2c_nbyte_index<3)
{
ACCORDO_Buffer_Rx[i2c_nbyte_index]= I2CDR;
i2c_nbyte_index++;
}
else
{
Rx_length=ACCORDO_Buffer_Rx[i2c_nbyte_index-1];
/*if((Rx_length>=RX_BUFFER_SIZE)||(Rx_length == 0))
{
mr_rece_state = MR_RECE_ERROR;
HW_I2C_StopCom();
}
else */
{
mr_rece_state = MR_RECE_DATA;
}
}
}
break;
case MR_RECE_DATA:
I2C_PE=1;
if (I2C_BTF==1)
{
if(Rx_length>0)
{
ACCORDO_Buffer_Rx[i2c_nbyte_index]= I2CDR;
Rx_crc += ACCORDO_Buffer_Rx[i2c_nbyte_index];
Rx_cxc ^= ACCORDO_Buffer_Rx[i2c_nbyte_index];
i2c_nbyte_index++;
Rx_length--;
}
else
{
ACCORDO_Buffer_Rx[i2c_nbyte_index]= I2CDR;
i2c_nbyte_index++;
I2C_ACK=0; /*not ack req*/
mr_rece_state = MR_RECE_LAST_DATA;
}
}
break;
case MR_RECE_LAST_DATA:
I2C_PE=1;
if (I2C_BTF==1)
{
I2C_STOP=1; /*Attention: Must first Set STOP Bit then read data!!!*/
ACCORDO_Buffer_Rx[i2c_nbyte_index] = I2CDR;
if ((ACCORDO_Buffer_Rx[i2c_nbyte_index-1] != Rx_crc)||(ACCORDO_Buffer_Rx[i2c_nbyte_index]!= Rx_cxc))
{
mr_rece_state = MR_RECE_ERROR;
//HW_I2C_StopCom();
}
else
{
mr_rece_state = MR_RECE_SUCCESS;
}
//HW_I2C_StopCom();
}
break;
#else
case MR_RECE_LEN:
I2C_PE=1;
if (I2C_BTF==1)
{
Rx_length= I2CDR;
if((Rx_length>RX_BUFFER_SIZE)||(Rx_length == 0))
//if(Rx_length>RX_BUFFER_SIZE)
{
mr_rece_state = MR_RECE_ERROR;
//HW_I2C_StopCom();
}
else
{ ACCORDO_Buffer_Rx[i2c_nbyte_index]= Rx_length;
i2c_nbyte_index++;
mr_rece_state = MR_RECE_DATA;
}
}
break;
case MR_RECE_DATA:
I2C_PE=1;
if (I2C_BTF==1)
{
ACCORDO_Buffer_Rx[i2c_nbyte_index]= I2CDR;
Rx_crc += ACCORDO_Buffer_Rx[i2c_nbyte_index];
i2c_nbyte_index++;
if (i2c_nbyte_index >= Rx_length)
{
I2C_ACK=0; /*not ack req*/
mr_rece_state = MR_RECE_LAST_DATA;
}
}
break;
case MR_RECE_LAST_DATA:
I2C_PE=1;
if (I2C_BTF==1)
{
I2C_STOP=1; /*Attention: Must first Set STOP Bit then read data!!!*/
ACCORDO_Buffer_Rx[i2c_nbyte_index] = I2CDR;
//if(Rx_crc==ACCORDO_Buffer_Rx[i2c_nbyte_index]+0x5a)
if(Rx_crc+0x5a==ACCORDO_Buffer_Rx[i2c_nbyte_index])
{
mr_rece_state = MR_RECE_SUCCESS;
}
else
{
mr_rece_state = MR_RECE_ERROR;
}
//HW_I2C_StopCom();
}
break;
#endif
case MR_RECE_SUCCESS:
break;
case MR_RECE_ERROR:
break;
}
}
unsigned char Rece_CheckSum(void)
{
unsigned char j;
unsigned char Sum;
Sum = 0;
Sum = Sum + 0x5a;
for(j=1;j<=Rx_length-1;j++)
Sum = Sum + ACCORDO_Buffer_Rx[j];
if(Sum!=ACCORDO_Buffer_Rx[Rx_length])
return 0;
else
return 1;
}
/*********************************************
Function: HW_I2C_StopCom
Description: I2C bus stop command.
Write/Modify:
Time: 2006-06
*********************************************/
void HW_I2C_StopCom(void)
{
I2C_STOP=1;
I2C_PE=0;
NOP;
HW_I2C_Reset(); /*restart I2C function*/
//I2CCCR = I2C_SPEED_100K; /*slow mode for main loop*/
//I2COAR2 = 0; /* No 10 bit addressing (reset status).*/
//asm TNZ I2CDR; /* Touch to clear any interrupt.*/
//asm TNZ I2CSR1;
//asm TNZ I2CSR2;
//I2CCR =I2CCR = 0x25; /* I2C turn on w/ IT enable and Ack enable.*/
//HW_I2C_FlagAllClear();
}
/*********************************************
Function: HW_I2C_StartCom
Description: I2C bus start command.
Write/Modify:
Time: 2006-06
*********************************************/
void HW_I2C_StartCom(void)
{
I2C_STOP=0;
I2C_START=1;
I2C_ACK=1;
I2C_ITE=1;
}
#if 0
/*/*********************************************
Function: HW_I2C_Send
Description: I2C bus send data
Write/Modify:
Time: 2006-06
*********************************************/
void HW_I2C_Send (unsigned char PeriphIdtemp)
{
i2c_dest = PeriphIdtemp;
i2c_nbyte_index = 0;
mt_send_state = MT_SEND_START;
HW_I2C_StartCom();
SendDelay= 3; /*Max delay is 20ms*/
while (SendDelay)
{
if (!mt_send_state)break;
}
if (mt_send_state)
{
HW_I2C_StopCom();
}
}
/*********************************************
Function: HW_I2C_Receive
Description: I2C bus receive data
Write/Modify:
Time: 2006-06
*********************************************/
void HW_I2C_Receive (unsigned char periphidtemp)
{
i2c_dest = periphidtemp;
i2c_nbyte_index = 0;
mr_rece_state = MR_RECE_START;
HW_I2C_StartCom();
ReceDelay= 2; /*Max delay is 20ms*/
while (ReceDelay)
{
if (!mr_rece_state)break;
}
if (mr_rece_state)
{
HW_I2C_StopCom();
}
}
#endif
void HW_I2C_FlagAllClear()
{
accordo_send_flag.flag.request = 0;
accordo_send_flag.flag.busy = 0;
//accordo_receive_flag.flag.request = 0;
accordo_receive_flag.flag.busy = 0;
}
#endif
void HWI2CSendData(unsigned char addr, unsigned char *sendbuff)
{
HWI2CAddress=addr;
HWI2CDataSend=sendbuff;
HWI2CMode=1;
I2C_PE=1;
I2C_ACK=1;
I2C_START=1;
}
void HWI2CReadData(unsigned char addr, unsigned char *readbuff)
{
HWI2CAddress=addr;
HWI2CDataRead=readbuff;
HWI2CMode=5;
I2C_PE=1;
I2C_ACK=1;
I2C_START=1;
}
void HWI2CInt(void)
{
asm TNZ I2CSR1;
asm TNZ I2CSR2;
if(I2C_BERR||I2C_AF||I2C_ARLO) return;
switch(HWI2CMode)
{
case 0:
break;
case 1:
if(I2C_SB)
{
I2CDR=HWI2CAddress;
HWI2CMode=2;
}
break;
case 2:
I2C_PE=1;
if(I2C_BTF)
{
I2CDR=*HWI2CDataSend;
HWI2CSendLen=*HWI2CDataSend;
HWI2CDataSend++;
HWI2CMode=3;
}
break;
case 3:
if(I2C_BTF)
{
if(HWI2CSendLen>0)
{
I2CDR=*HWI2CDataSend;
HWI2CDataSend++;
HWI2CSendLen--;
}
else
{
HWI2CMode=4;
}
}
break;
case 4:
//if(I2C_AF)
{
I2C_STOP=1;
HWI2CMode=0;
}
break;
case 5:
if(I2C_SB)
{
I2CDR=HWI2CAddress;
HWI2CMode=6;
}
break;
case 6:
I2C_PE=1;
if(I2C_BTF)
{
*HWI2CDataRead=I2CDR;
HWI2CReadLen=*HWI2CDataRead;
HWI2CDataRead++;
HWI2CMode=7;
}
break;
case 7:
I2C_PE=1;
if(I2C_BTF)
{
if(HWI2CReadLen>0)
{
if(HWI2CReadLen==1)
{
I2C_STOP=1;
HWI2CMode=0;
}
*HWI2CDataRead=I2CDR;
HWI2CDataRead++;
HWI2CReadLen--;
if(HWI2CReadLen==1)
I2C_ACK=0;
}
}
break;
}
}
/*********************************************************
File End
*********************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -