#include<msp430x14x.h>
void Ucb0I2c_Init(void)
{
P3SEL|=0x06;
UCB0CTL1|=UCSWRST; //复位所有寄存器
UCB0CTL0|=UCMST+UCMODE_3+UCSYNC; //IIC主设备通讯模式
UCB0CTL1=UCSSEL_2+UCSWRST; //时钟为SMCLK,保持复位
UCB0BR0=12; //IIC通讯频率
UCB0BR1=0;
UCB0I2CSA=0x00d0; //从设备地址
UCB0CTL1&=~UCSWRST; //正常操作
}
void Ucb0I2c_Start(unsigned int deviceaddress)
{
UCB0I2CSA=deviceaddress;
while(UCB0CTL1 & UCTXSTP);//确认停止信号是否发送
UCB0CTL1|=UCTR+UCTXSTT; //发送一个开始信号
while(!(IFG2 & UCB0TXIFG));//等待传送结束
IFG2 &=~UCB0TXIFG; //清除USCI_B0的发射标志位
}
void Ucb0I2c_Writebyte(unsigned char wdata)
{
UCB0TXBUF=wdata;
while(!(IFG2 & UCB0TXIFG));
IFG2&=~UCB0TXIFG;
}
void Ucb0I2c_WriteNbyte(unsigned char *index,unsigned char n)
{
unsigned char i;
for(i=0;i<n;i++)
{
Ucb0I2c_Writebyte(*index);
index++;
}
}
unsigned char Ucb0I2c_Readbyte(void)
{
unsigned char Rdata;
UCB0CTL1&=~UCTR; //接受方式
UCB0CTL1|=UCTXSTT;//发送一个开始信号
while(!(IFG2 & UCB0RXIFG));
IFG2&=~UCB0RXIFG;
UCB0CTL1|=UCTXSTP;//发送一个停止信号
Rdata=UCB0RXBUF;
return(Rdata);
}
void I2C_Writebyte(unsigned char wdata,unsigned int dataaddress,unsigned int Eep-
romType,unsigned int daviceaddress)
{
Ucb0I2c_Start(deviceaddress);
if(EepromType>16)//如果从设备的容量大于16KB
{
Ucb0I2c_Writebyte((unsigned char)(dataaaddress>>8));//高8位数据地址
}
Ucb0I2c_Writebyte(dataaddress);//低8位数据地址
Ucb0I2c_Writebyte(wdata);
UCB0CTL1|=UCTXSTP;
}
void I2C_WriteNbyte(unsigned char *index,unsigned char n,unsigned int dataaddress,
unsigned int Eeprom Type,unsigned int deviceaddress)
{
Ucb0I2c_Start(deviceaddress);
if(Eeprom Type>16)
{
Ucb0I2c_Writebyte((unsigned char)(dataaaddress>>8));//高8位数据地址
}
Ucb0I2c_Writebyte(dataaddress);//低8位数据地址
Ucb0I2c_WriteNbyte(index,n);
UCB0CTL1|=UCTXSTP;
}
unsigned char I2C_Readbyte(unsigned int dataaddress,unsigned int Eeprom Type,
unsigned int deviceaddress)
{
volatile unsigned char Rdata;
Ucb0I2c_Start(daviceaddress);
if(Eeprom Type>16)
{
Ucb0I2c_Writebyte((unsigned char)(dataaaddress>>8));//高8位数据地址
}
Ucb0I2c_Writebyte(dataaddress);//低8位数据地址
Rdata=Ucb0I2c_Readbyte();
return(Rdata);
}
void I2C_ReadNbyte(unsigned char *index,unsigned char n,unsigned int dataaddress,
unsigned int Eeprom Type,unsigned int deviceaddress)
{
unsigned char i;
Ucb0I2c_Start(deviceaddress);
if(Eeprom Type>16)
{
Ucb0I2c_Writebyte((unsigned char)(dataaaddress>>8));//高8位数据地址
}
Ucb0I2c_Writebyte(dataaddress);//低8位数据地址
UCB0CTL1&=~UCTR;
UCB0CTL1|=UCTXSTT;
for(i=0,i<n,i++)
{
while(!(IFG2 & UCB0RXIFG));
IFG2&=~UCB0RXIFG;
*index=UCB0RXBUF;
index++;
}
UCB0CTL1|=UCTXSTP;
}
IIC.rar_msp430f149 IIc_msp430f149i2c
版权申诉
5星 · 超过95%的资源 142 浏览量
2022-09-20
23:06:12
上传
评论 1
收藏 14KB RAR 举报
刘良运
- 粉丝: 66
- 资源: 1万+
评论1