/******************************************************************
**** File : I2C.c | Master Read Write |
** Version : 1.0
** Description : I2C 查询法读写24C02
** Author : LightWu
** Date : 2013-4-26
** 必须注意多字节数据读取时,发送停止位需要在最后一个字节读取之前
** 否则数据读取后,停止位发送容易失败,导致从机接收不到停止位。
*******************************************************************/
#include <msp430.h>
//注意:两次发送间隔必须要有延时,否则不能再次发送,串口发送格式:
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]);
}
}