📄 twi.c
字号:
#define TWI_C
#include "includes.h"
static uchar TWI_buf[TWI_BUFFER_SIZE];
static uchar TWI_msgSize;
static uchar TWI_state = TWI_NO_STATE;
static uchar TWI_statusReg;// = 0;
/*******************************************************
初始化TWI为I2C主机,并处于初始待机状态。注意,初始化完成后,
应在主程序中开放全局中断
********************************************************/
void TWI_Master_Initialise(void)
{
TWBR = TWI_TWBR;
TWCR = (1<<TWEN)|(0<<TWIE)|(0<<TWINT)|
(0<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|
(0<<TWWC);
}
/*******************************************************
Call this function to test if the TWI_ISR is busy transmitting
********************************************************/
char TWI_Transceiver_Busy(void)
{
return (TWCR &(1<<TWIE));
}
/*******************************************************
Call this function to fetch the state information of the previous
operation.
********************************************************/
char TWI_Get_State_Info(void)//发送数据包
{
while(TWI_Transceiver_Busy());
return(TWI_state);
}
/*******************************************************
Call this function to send a prepared message.
********************************************************/
void TWI_Start_Transceiver_With_Data(uchar *msg,uchar msgSize)//检验数据包是否发送完成
{
uchar temp;
while(TWI_Transceiver_Busy());
TWI_msgSize = msgSize;
TWI_buf[0] = msg[0];
if(!(msg[0] & (TRUE<<TWI_READ_BIT)))
{
for(temp = 1;temp < msgSize; temp++)
TWI_buf[temp] = msg[temp];
}
TWI_statusReg = FALSE;
TWI_state = TWI_NO_STATE;
TWCR = (1<<TWEN)|
(1<<TWIE)|(1<<TWINT)|
(0<<TWEA)|(1<<TWSTA)|(0<<TWSTO)|
(0<<TWWC);
}
/*******************************************************
Call this function to resend the last message.
********************************************************/
void TWI_Start_Transceiver(void)
{
while(TWI_Transceiver_Busy());
TWI_statusReg = FALSE;
TWI_state = TWI_NO_STATE;
TWCR = (1<<TWEN)|
(1<<TWIE)|(1<<TWINT)|
(0<<TWEA)|(1<<TWSTA)|(0<<TWSTO)|
(0<<TWWC);
}
/*******************************************************
Call this function to read out hte requested data from the
TWI transceiver buffer.
********************************************************/
uchar TWI_Get_Data_From_Transceiver(uchar *msg,uchar msgSize)
{
uchar i;
while(TWI_Transceiver_Busy());
if(TWI_statusReg)
{
for(i = 0;i < msgSize; i++)
{
msg[i] = TWI_buf[i];
}
}
return(TWI_statusReg);
}
//************************TWI 中断服务程序**************************//
#pragma interrupt_handler twi:34
void twi(void)
{
static uchar TWI_bufPtr;
switch(TWSR)
{
case TWI_START:
case TWI_REP_START:
TWI_bufPtr = 0;
case TWI_MTX_ADR_ACK:
case TWI_MTX_DATA_ACK:
if(TWI_bufPtr < TWI_msgSize)
{
TWDR = TWI_buf[TWI_bufPtr++];
TWCR = (1<<TWEN)|
(1<<TWIE)|(1<<TWINT)|
(0<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|
(0<<TWWC);
}
else
{
TWI_statusReg = TRUE;
TWCR = (1<<TWEN)|
(0<<TWIE)|(1<<TWINT)|
(0<<TWEA)|(0<<TWSTA)|(1<<TWSTO)|
(0<<TWWC);
}
break;
case TWI_MRX_DATA_ACK:
TWI_buf[TWI_bufPtr++] = TWDR;
case TWI_MRX_ADR_ACK:
if(TWI_bufPtr < (TWI_msgSize-1))
{
TWCR = (1<<TWEN)|
(1<<TWIE)|(1<<TWINT)|
(1<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|
(0<<TWWC);
}
else
{
TWCR = (1<<TWEN)|
(1<<TWIE)|(1<<TWINT)|
(0<<TWEA)|(0<<TWSTA)|(1<<TWSTO)|
(0<<TWWC);
}
break;
case TWI_MRX_DATA_NACK:
TWI_buf[TWI_bufPtr++] = TWDR;
TWI_statusReg = TRUE;
TWCR = (1<<TWEN)|
(0<<TWIE)|(1<<TWINT)|
(0<<TWEA)|(0<<TWSTA)|(1<<TWSTO)|
(0<<TWWC);
break;
case TWI_ARB_LOST:
TWCR = (1<<TWEN)|
(0<<TWIE)|(1<<TWINT)|
(0<<TWEA)|(1<<TWSTA)|(0<<TWSTO)|
(0<<TWWC);
break;
case TWI_MTX_ADR_NACK:
case TWI_MRX_ADR_NACK:
case TWI_MTX_DATA_NACK:
case TWI_BUS_ERROR:
default:
TWI_state = TWSR;
TWCR = (1<<TWEN)|
(0<<TWIE)|(0<<TWINT)|
(0<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|
(0<<TWWC);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -