|
MSP430g2333的I2C程序,模拟的,硬件的都有,但不知道哪里出了错,各位大侠,知道的看看
eeprom用的是AT24C02,a1,a2,a0都接的高电平,WP引脚接的地
//*****************************************************************************//
下面是硬件的
#include
void I2C_write(unsigned char,unsigned char);
void I2C_read(unsigned char);
void delay(void);
unsigned char RX_data;
void I2C_Init(void)
{
UCB0CTL1 |= UCSWRST;
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC;
UCB0CTL1 |= UCSSEL_2 + UCSWRST;
UCB0BR0 = 10; //波特率100khz
UCB0BR1 = 0;
UCB0I2CSA = (0xae>>1); //7位地址
UCB0CTL1 &= ~UCSWRST;
}
void I2C_write(unsigned char address,unsigned char write_word)
{
I2C_Init();
while(UCB0CTL1 & UCTXSTP);
UCB0CTL1 |= UCTXSTT + UCTR;
UCB0TXBUF = address;
while((IFG2 & UCB0TXIFG) == 0); //停在这里不动了,IFG2在执行UCTXSTT之后,其UCB0TXIFG被置位,在执行完UCB0TXBUF之后被清除,然后就停在这里死
//循环,求解
UCB0TXBUF = write_word;
while((IFG2 & UCB0TXIFG) == 0);
UCB0CTL1 |= UCTXSTP + UCTXNACK;
while((UCB0CTL1 & UCTXSTP) == 1);
}
void I2C_read(unsigned char address)
{
I2C_Init();
while(UCB0CTL1 & UCTXSTP);
UCB0CTL1 |= UCTXSTT + UCTR;
UCB0TXBUF = address;
while((IFG2 & UCB0TXIFG) == 0);
IFG2 &= ~UCB0TXIFG;
UCB0CTL1 &= ~UCTR;
while(UCB0CTL1 & UCTXSTP);
UCB0CTL1 |= UCTXSTT;
while((UCB0CTL1 & UCTXSTT)==1);
while((IFG2 & UCB0RXIFG)==0);
RX_data = UCB0RXBUF;
delay();
UCB0CTL1 |= UCTXSTP +UCTXNACK;
while((UCB0CTL1 & UCTXSTP)==1);
}
void delay(void)
{
unsigned int i,n;
for(i=0;i<100;i++)
for(n=0;n<0xff;n++);
}
void main(void)
{
WDTCTL = WDTPW + WDTHOLD;
BCSCTL1 = CALBC1_1MHZ; //设置1MHz
DCOCTL = CALDCO_1MHZ;
P2DIR = 0xff;
P2OUT = 0x00;
P1SEL |= (BIT6 + BIT7);
P1SEL2|= (BIT6 + BIT7);
RX_data = 0;
I2C_write(3,0x10);
delay();
delay();
I2C_read(3);
while(1)
{
P2OUT = RX_data;
}
}
模拟的
//**************************************************************/
#include
#define TRUE 1
#define FALSE 0
#define ACKERROR 0x55
#define SDA_IN P1DIR &= ~BIT7
#define SDA_OUT P1DIR |= BIT7
#define SDA_L P1OUT &= ~BIT7
#define SDA_H P1OUT |= BIT7
#define SCL_IN P1DIR &= ~BIT6
#define SCL_OUT P1DIR |= BIT6
#define SCL_L P1OUT &= ~BIT6
#define SCL_H P1OUT |= BIT6
#define uchar unsigned char
void delay_ms(uchar i)
{
uchar us,j;
for(us=0;us<10;us++)
{
for(j=0;j
}
}
void init(void)
{
SDA_OUT;
SCL_OUT;
SCL_H;
_NOP();_NOP();
SDA_H;
_NOP();_NOP();
//wp = 0;
//P2 = 0xff;
}
//开始信号
void I2C_Start(void)
{
SDA_OUT;
SDA_H;
_NOP();_NOP();
SCL_H;
_NOP();_NOP();_NOP();_NOP();_NOP();
SDA_L;
_NOP();_NOP();_NOP();_NOP();_NOP();
SCL_L;
}
//停止信号
void I2C_Stop(void)
{
SDA_OUT;
SDA_L;
_NOP();
SCL_H;
_NOP();_NOP();_NOP();_NOP();_NOP();
SDA_H;
_NOP();_NOP();_NOP();_NOP();_NOP();
SCL_L;
}
//应答信号
void I2C_Ack(void)
{
SDA_OUT;
SDA_L;
_NOP();_NOP();_NOP();
SCL_H;
_NOP();_NOP();_NOP();_NOP();_NOP();
SCL_L;
_NOP();_NOP();
}
//非应答信号
void I2C_NoAck(void)
{
SDA_OUT;
SDA_H;
_NOP();_NOP();_NOP();
SCL_H;
_NOP();_NOP();_NOP();_NOP();_NOP();
SCL_L;
_NOP();_NOP();
}
unsigned char I2C_CheckAck(void)
{
unsigned char AckStatus;
SDA_IN;
SCL_H;
delay_ms(10);
if((P1IN & BIT7) == 0)
{
AckStatus = TRUE;
}
else
{
AckStatus = FALSE;
}
SCL_L;
delay_ms(10);
SDA_OUT;
return AckStatus;
}
//写入字节
void I2C_WriteByte(uchar w_data)
{
uchar i=0;
SDA_OUT;
for(i=0;i<8;i++)
{
if(w_data & 0x80)
{
SDA_H;
}
else
{
SDA_L;
}
_NOP();_NOP();_NOP();_NOP();_NOP();
SCL_H;
_NOP();_NOP();_NOP();_NOP();_NOP();
SCL_L;
_NOP();_NOP();_NOP();_NOP();_NOP();
w_data <<=1;
}
delay_ms(10);
}
//读取字节
uchar I2C_ReadByte(void)
{
uchar ReadBuf,n=0;
SDA_IN;
SCL_L;
delay_ms(10);
for(n=0;n<8;n++)
{
SCL_H;
_NOP();_NOP();_NOP();_NOP();_NOP();
ReadBuf = ReadBuf<<1;
if((P1IN&BIT7)==0)
{
ReadBuf |= 0x01;
}
else
{
ReadBuf &= 0xfe;
}
SCL_L;
_NOP();_NOP();_NOP();_NOP();_NOP();
}
SDA_OUT;
_NOP();_NOP();
return ReadBuf;
}
//指定地址写入数据
unsigned char I2C_Write_add(uchar address,uchar write_data)
{
I2C_Start();
I2C_WriteByte(0xae);
if(I2C_CheckAck()==FALSE)
{
return ACKERROR;
}
I2C_WriteByte(address);
if(I2C_CheckAck()==FALSE)
{
return ACKERROR;
}
I2C_WriteByte(write_data);
if(I2C_CheckAck()==FALSE)
{
return ACKERROR;
}
I2C_Stop();
return 0;
}
//指定地址读取数据
uchar I2C_Read_add(uchar address)
{
uchar read_buf;
I2C_Start();
I2C_WriteByte(0xae);
if(I2C_CheckAck()==FALSE)
{
return ACKERROR;
}
I2C_WriteByte(address);
if(I2C_CheckAck()==FALSE)
{
return ACKERROR;
}
//delay_ms(100);
I2C_Start();
I2C_WriteByte(0xaf);
if(I2C_CheckAck()==FALSE)
{
return ACKERROR;
}
read_buf = I2C_ReadByte();
I2C_NoAck();
I2C_Stop();
return read_buf;
}
void main(void)
{
unsigned char temp;
init();
while(1)
{
I2C_Write_add(3,0xaa);
delay_ms(100);
temp = I2C_Read_add(3);
}
}
|
|