/******************************************************************
**
** File : I2C.c “ Master Read Write |
** Version : 1.0
** DescripTIon: I2C 查询法读写24C02
** Author : LightWu
** Date : 2013-4-26
** 有必要留意多字节数据读取时,发送中止位需求在最终一个字节读取之前
** 不然数据读取后,中止位发送简单失利,导致从机接纳不到中止位。
*******************************************************************/
#include
//留意:两次发送距离有必要要有延时,不然不能再次发送,串口发送格局:
unsigned char PTxData[250]; // Pointer to TX data
unsigned char PRxData[250]; // Pointer to RX data
void UartInit(void)
{
if (CALBC1_1MHZ==0xFF) // If calibraTIon constant erased
{
while(1); // do not load, trap CPU!!
}
DCOCTL = 0; // Select lowest DCOx and MODx setTIngs
BCSCTL1 = CALBC1_1MHZ; // Set DCO
DCOCTL = CALDCO_1MHZ;
P3SEL |= 0x30; // P3.4,5 = USCI_A0 TXD/RXD
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 = 104; // 1MHz 9600; (104)decimal = 0x068h
UCA0BR1 = 0; // 1MHz 9600
UCA0MCTL = UCBRS0; // ModulaTIon UCBRSx = 1
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
}
void UartSend( unsigned char Data )
{
UCA0TXBUF = Data; // TX -》 RXed character
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?,发送缓冲区空
}
void IICInit(void)
{
UCB0CTL1 |= UCSWRST; // Enable SW reset
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode
UCB0CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset
UCB0BR0 = 12; // fSCL = SMCLK/12 = ~100kHz
UCB0BR1 = 0;
UCB0I2CSA = 0xA0》》1; // Slave Address is ,留意地址需求右移一位,24C02地址为0XA0,故要写入0X50
// 7位地址形式,器材会发送一位读写位,正好8位。
UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
}
/*******************************************
函数称号:Ucb0I2c_Start(void)
功 能:I2C主机形式,发送写开始条件
参 数:无
返回值 :无
********************************************/
void Ucb0I2c_Start(void)
{
UCB0I2CSA = 0x50; // Slave Address is 0xd0
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition
while(!(IFG2&UCB0TXIFG)); //等候传送完
IFG2 &= ~UCB0TXIFG; // Clear USCI_B0 TX int flag
}
void IICSendData( unsigned char Addr ,unsigned char Data )
{
int i;
for(i=3000;i》0;i–); //两次发送距离有必要要有延时,不然不能再次发送
Ucb0I2c_Start();
UCB0TXBUF = Addr; // Load TX buffer
while(!(IFG2&UCB0TXIFG)); //等候传送完结
IFG2 &= ~UCB0TXIFG; // Clear USCI_B0 TX int flag
UCB0TXBUF = Data; // Load TX buffer
while(!(IFG2&UCB0TXIFG)); //等候传送完结
IFG2 &= ~UCB0TXIFG; // Clear USCI_B0 TX int flag
UCB0CTL1 |= UCTXSTP; // I2C stop condition
}
void IICReadData( unsigned char Addr )
{
int i;
for(i=3000;i》0;i–); //两次发送距离有必要要有延时,不然不能再次发送
Ucb0I2c_Start();
UCB0TXBUF = Addr; // Load TX buffer
while(!(IFG2&UCB0TXIFG)); //等候传送完结
IFG2 &= ~UCB0TXIFG; // Clear USCI_B0 TX int flag
UCB0CTL1 &= ~UCTR ; // I2C RX, Read
UCB0CTL1 |= UCTXSTT; // I2C RX, start condition
while(!(IFG2&UCB0RXIFG)); //等候接纳完结
IFG2&=~UCB0RXIFG;
PRxData[0] = UCB0RXBUF; // Read RX buffer
while(!(IFG2&UCB0RXIFG)); //等候接纳完结
IFG2&=~UCB0RXIFG;
PRxData[1] = UCB0RXBUF; // Read RX buffer
while(!(IFG2&UCB0RXIFG)); //等候接纳完结
IFG2&=~UCB0RXIFG;
PRxData[2] = UCB0RXBUF; // Read RX buffer
while(!(IFG2&UCB0RXIFG)); //等候接纳完结
IFG2&=~UCB0RXIFG;
//有必要留意发送中止位在最终一个字节读取之前
UCB0CTL1 |= UCTXSTP; // I2C stop condition
PRxData[3] = UCB0RXBUF; // Read RX buffer
}
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P3SEL |= 0x06; // Assign I2C pins to USCI_B0
IICInit();
UartInit();
while (1)
{
UartSend(‘M’);
IICSendData(0X00,‘A’);
IICSendData(0X01,‘B’);
IICSendData(0X02,‘C’);
IICSendData(0X03,‘D’);
IICSendData(0X04,‘E’);
IICSendData(0X05,‘F’);
IICSendData(0X06,‘H’);
IICSendData(0X07,‘J’);
IICReadData(0x00);
UartSend(PRxData[0]);
UartSend(PRxData[1]);
UartSend(PRxData[2]);
UartSend(PRxData[3]);
}
}