|
代码如下,大牛们看看:主从机配置代码一样,只是命令码不同
#include"msp430x16x.h"
#pragma vector=USART0TX_VECTOR
__interrupt void I2C_ISR(void);
void IIC_senddat(unsigned char S_count,unsigned char *D);
void delay(unsigned int num);
void intIIC(void);
unsigned char TXdata[10]={0x42,0x50,};//0x0a};
char l=0;
void main(void)
{
WDTCTL=WDTPW+WDTHOLD;
P5DIR|=BIT0; // test_led
P5OUT&=~BIT0;
intIIC();
_EINT();
P5OUT|=BIT0;
delay(50);
P5OUT&=~BIT0;
TXdata[2]=0x81;
while(1)
{
if(TXdata[2]==0x81)
{
TXdata[2]=0x0a;
IIC_senddat(3,TXdata);
P5OUT^=BIT0;
delay(200);
intIIC();
}
}
}
#pragma vector=USART0TX_VECTOR
__interrupt void I2C_ISR(void)
{
switch(I2CIV)
{
case 6:_NOP();
break;
case 10 :// 接收
TXdata[l]=I2CDRB;
l++;
if(l>2)l=0;
break;
case 12 :// 发送
I2CDRB=TXdata[l];
l++;
if(l>9)l=0;
break;
default:break;
}
}
// IIC发送数据
void IIC_senddat(unsigned char S_count,unsigned char *D)
{
I2CNDAT=S_count;
U0CTL|=MST;
I2CSA=0X0048; // 主机发送目标地址:从地址
I2CTCTL |= I2CSTT+I2CTRX+ I2CSTP; // Initiate transfer - write to sl××e
while((I2CIFG & NACKIFG) == 0x02);
for(char k=0;k<S_count;k++)
{
while ((I2CIFG & TXRDYIFG) == 0);
I2CDRB=TXdata[k];
}
while ((I2CTCTL & I2CSTP) == 0x02); // Wait for Stop Condition
delay(50);
I2CTCTL &= ~I2CTRX;
}
void delay(unsigned int num)
{
unsigned char i;
unsigned int l;
for(l=0;l<num;l++)
for(i=0;i<200;i++);
}
void intIIC(void)
{
delay(50);
U0CTL=0;
U0CTL |= SWRST; // Reset USART state machine
U0CTL &= ~CHAR;
I2CIE&=~RXRDYIE;
I2CIFG=0;
P3SEL|=0X0A;
U0CTL|=I2C+SYNC;
U0CTL&=~I2CEN;
I2CTCTL|=I2CSSEL1;
I2COA=0X0050; // 本机地址
I2CIE|=RXRDYIE+OAIFG;
U0CTL|=I2CEN;
delay(50);
} |
|