马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
x
MSP430F449与24C01通讯程序 调试成功
#include "msp430x44x.h"
//P4.7 SDA
//P4.6 SCL
#define SDA_0 (P4DIR|=BIT7)
#define SDA_1 (P4DIR&=~BIT7)
#define SCL_0 (P4DIR|=BIT6)
#define SCL_1 (P4DIR&=~BIT6)
#define DIR_OUT (P4DIR|=BIT7)
#define DIR_IN (P4DIR&=~BIT7)
void Delay(unsigned int n);
void Start(void);
void Stop(void);
void Send_Byte(unsigned char data);
unsigned char Receive_Byte(void);
void Ack(void);
void I2C_ACK(unsigned char tm);
void Send_word(void);
void Receive_word(void);
unsigned char *D,*M,ADD,Ack_Flag;
unsigned int MPM[16];
unsigned int DDT[16]={0xf316,0x6921,0xd315,0xc523,0xb628,0x8525,0xc324,0xd716,0xf316,0x6921,0xd315,0xc523,0xb628,0x8525,0xc324,0xd716};
void main(void)
{ unsigned int i;
WDTCTL=WDTPW+WDTHOLD;
D=(char*)DDT;
ADD=0; //24C01内数据读写首地址
M=(char*)MPM;
for(i=0;i<4;i++)
{ Send_word();
Delay(1000);
ADD+=8;
}
ADD=0;
Receive_word();
}
void Delay(unsigned int n)
{
while(n-- > 0);
}
//*****************************************开始
void Start(void)
{ P4OUT&=0x3F;
SDA_1;
SCL_1;
SDA_0;
SCL_0;
}
//******************************************停止
void Stop(void)
{ SCL_0;
SDA_0;
SCL_1;
SDA_1;
}
//***************************************发送一个字节
void Send_Byte(unsigned char data)
{ unsigned char BI,i;
BI=0x80;
for(i=0;i<8;i++)
{ if((data&BI)==BI) SDA_1;
else SDA_0;
SCL_1;
Delay(50);
SCL_0;
BI>>=1;
}
}
//********************************************写完一个字节判断应答
void Ack(void)
{ DIR_IN;
SCL_1;
Ack_Flag = 0;
Delay(10);
if ((P4IN & BIT7))
{
Ack_Flag = 1;
}
SCL_0;
Delay(10);
}
//*******************************************接收一个字节
unsigned char Receive_Byte(void)
{ unsigned char i,temp=0x80,tempdata;
tempdata=0;
DIR_IN;
for(i=0;i<8;i++)
{
SCL_1;
if((P4IN&BIT7)==BIT7) tempdata|=temp;
temp>>=1;
SCL_0;
}
return(tempdata);
}
//************************************************接收一个字节后发送应答信号
void I2C_ACK(unsigned char tm)
{
if(tm==0) SDA_0;
else SDA_1;
Delay(10);
SCL_1;
Delay(10);
SCL_0;
Delay(10);
}
//*************************************************发送一个数组
void Send_word(void)
{ unsigned char i;
while(1)
{ Start();
Delay(20);
Send_Byte(0xA0);
Ack();
if (Ack_Flag) continue;
Send_Byte(ADD);
Ack();
if (Ack_Flag) continue;
for(i=0;i<8;i++)
{
Send_Byte(*D++);
Ack();
if (Ack_Flag) continue;
}
if (!Ack_Flag) break;
}
Stop();
}
//************************************************接收一个数组
void Receive_word(void)
{ unsigned char i;
while(1)
{ Start();
Send_Byte(0xA0);
Ack();
if (Ack_Flag) continue;
Send_Byte(ADD);
Ack();
if (Ack_Flag) continue;
Start();
Send_Byte(0xA1);
Ack();
if (Ack_Flag) continue;
for(i=0;i<31;i++)
{ *M++=Receive_Byte();
I2C_ACK(0);
}
Delay(5);
*M=Receive_Byte();
I2C_ACK(1);
if (!Ack_Flag) break;
}
Stop();
} |