#include "msp430g2553.h"
#include "i2c_master.h"
//=============================================================================
//=============================================================================
// I2C模块发送数据缓存
unsigned char g_I2cTxBuf[I2C_TXBUF_MAX_SIZE] = {0x36, 0x01,0x10,0x47,0x64,0x9b,0x06,0x07};
// I2C模块接收数据缓存
static unsigned char g_I2cRxBuf[I2C_RXBUF_MAX_SIZE] = {0,0,0,0,0,0,0,0,};
/*******************************************************************************
** Function : InitMstI2c **
** Parameter: **
** Output : **
** Retutn : **
================================================================================
** Author : **
** Date : **
*******************************************************************************/
void InitMstI2c(unsigned char DevAddr)
{
/*g_stI2cMachine.TxCnt = 0;
g_stI2cMachine.RxCnt = 0;
g_stI2cMachine.pTxBuf = g_I2cTxBuf;
g_stI2cMachine.pRxBuf = g_I2cRxBuf;
*/
P1SEL |= BIT6 + BIT7; // 分配P1.6 P1.7工作在USCI_B0的SDA和SCL模式
P1SEL2|= BIT6 + BIT7; //
UCB0CTL1 |= UCSWRST; // 配置I2C模块之前需要复位UCB0模块
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // 配置成7位主I2C模式,同步模式
UCB0CTL1 = UCSSEL_2 + UCSWRST; // 选择SMCLK时钟源
UCB0BR0 = 10; // 设置I2C波特率fSCL = SMCLK/10 = 100kHz
UCB0BR1 = 0;
UCB0I2CSA = DevAddr; // 设置默认的I2C从地址
UCB0CTL1 &= ~UCSWRST; // 清除复位标志位,恢复到运行模式
}
/*******************************************************************************
** Function : I2cMSendOneByte **
** Parameter: **
** Output : **
** Retutn : **
================================================================================
** Author : **
** Date : **
*******************************************************************************/
signed short int I2cMSendOneByte(unsigned char Addr, unsigned char Val)
{
unsigned short int TimeOut = 0;
if (UCB0STAT & UCBBUSY)
{
return I2C_ERRO_BUSY;
}
TimeOut = I2C_TIME_OUT;
while ((UCB0CTL1 & UCTXSTP) && (TimeOut--)); // 确保STOP
if (!TimeOut)
{
return I2C_ERRO_TIMEOUT1;
}
P1OUT |= BIT4;
P1OUT &= ~BIT4;
// 设置I2C从设备地址,
UCB0I2CSA = Addr;
UCB0CTL1 |= UCTR + UCTXSTT; // I2C模块为发送模式,启动START信号
TimeOut = I2C_TIME_OUT;
while ((!(IFG2 & UCB0TXIFG)) && (TimeOut--));
if (!TimeOut)
{
return I2C_ERRO_TIMEOUT2;
}
P1OUT |= BIT4;
P1OUT &= ~BIT4;
UCB0TXBUF = Val;
TimeOut = I2C_TIME_OUT;
while ((!(IFG2 & UCB0TXIFG)) && (TimeOut--));
if (!TimeOut)
{
return I2C_ERRO_TIMEOUT4;
}
P1OUT |= BIT4;
P1OUT &= ~BIT4;
// 清除I2C发送缓冲空的中断标志,发送 I2C STOP 信号
IFG2 &= ~UCB0TXIFG;
UCB0CTL1 |= UCTXSTP; // 发送I2C STOP 信号
TimeOut = I2C_TIME_OUT;
while ((UCB0CTL1 & UCTXSTP) && (TimeOut--)); // 确保STOP
if (!TimeOut)
{
return I2C_ERRO_TIMEOUT5;
}
P1OUT |= BIT4;
P1OUT &= ~BIT4;
return I2C_SUCCESSFUL;
}
/*******************************************************************************
** Function : I2cMSendBytes **
** Parameter: **
** Output : **
** Retutn : **
================================================================================
** Author : **
** Date : **
*******************************************************************************/
signed short int I2cMSendBytes(unsigned char DevAddr, unsigned char *Data, unsigned char Numb)
{
unsigned short int TimeOut = 0;
unsigned char i = 0;
if (UCB0STAT & UCBBUSY)
{
return I2C_ERRO_BUSY;
}
TimeOut = I2C_TIME_OUT;
while ((UCB0CTL1 & UCTXSTP) && (TimeOut--)); // 确保STOP
if (!TimeOut)
{
return I2C_ERRO_TIMEOUT1;
}
P1OUT |= BIT4;
P1OUT &= ~BIT4;
// 设置I2C从设备地址,
UCB0I2CSA = DevAddr;
UCB0CTL1 |= UCTR + UCTXSTT; // I2C模块为发送模式,启动START信号
TimeOut = I2C_TIME_OUT;
while ((!(IFG2 & UCB0TXIFG)) && (TimeOut--));
if (!TimeOut)
{
return I2C_ERRO_TIMEOUT2;
}
P1OUT |= BIT4;
P1OUT &= ~BIT4;
for (i=0;i
{
UCB0TXBUF = Data;
TimeOut = I2C_TIME_OUT;
while ((!(IFG2 & UCB0TXIFG)) && (TimeOut--));
if (!TimeOut)
{
return I2C_ERRO_TIMEOUT4;
}
P1OUT |= BIT4;
P1OUT &= ~BIT4;
}
// 清除I2C发送缓冲空的中断标志,发送 I2C STOP 信号
IFG2 &= ~UCB0TXIFG;
UCB0CTL1 |= UCTXSTP; // 发送I2C STOP 信号
TimeOut = I2C_TIME_OUT;
while ((UCB0CTL1 & UCTXSTP) && (TimeOut--)); // 确保STOP
if (!TimeOut)
{
return I2C_ERRO_TIMEOUT5;
}
P1OUT |= BIT4;
P1OUT &= ~BIT4;
return I2C_SUCCESSFUL;
}