|
有人使用过MSP430f247的I2C吗?帮看看哪儿设置不对!
#include <msp430x24x.h>
#include "I2C.h"
//MCLK = SMCLK = BRCLK = default DCO = ~1.045Mhz
/*-----------------------------
功能描述:I2C初始化
-------------------------------*/
void Init_i2c(void)
{
P3SEL |= 0x06; //P3.1 P3.2配置为I2c引脚
UCB0CTL1 |= UCSWRST; // Enable SW reset,禁止I2C模块
UCB0CTL0 = UCMST+UCMODE_3 + UCSYNC; // 主机模式,I2C,同步,7位地址模式
UCB0CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset
UCB0BR0 = 12; // fSCL = SMCLK/12 = ~100kHz
UCB0BR1 = 0;
UCB0I2COA = 0x0005; // 设置本身的地址 05h
UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation打开I2C模块
}
/*---------------------------------------------------------
--功能描述:I2C写初始化发
--入口参数:SlaveAddress,从机器件地址
-----------------------------------------------------------*/
void InitI2cWrite(uint8 SlaveAddress)
{
UCB0I2CSA=SlaveAddress;//器件地址
//UCB0CTL1 |=UCTR //发送方式
// while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
while(UCBBUSY&UCB0STAT);//等待空闲
UCB0CTL1 |= UCTR +UCTXSTT;//开始传送
//while (UCB0CTL1 & UCTXSTT); // Loop until I2C STT is sent
}
/*---------------------------------------------------------
--功能描述:I2C读初始化发
--入口参数:SlaveAddress
-----------------------------------------------------------*/
void InitI2cRead(uint8 SlaveAddress)
{
UCB0I2CSA=SlaveAddress;//器件地址
UCB0CTL1 &= ~UCTR;//接收方式
UCB0CTL1 |= UCTXSTT; // I2C start condition
while (UCB0CTL1 & UCTXSTT); // Loop until I2C STT is sent
}
/*-------------------------
功能描述:I2C写一字节数据
--------------------------*/
void I2cWriteByte(uint8 Data)
{
while(!(IFG2&UCB0TXIFG));//等待TXBUF为空
UCB0TXBUF=Data;
while(UCB0STAT&UCNACKIFG)
UCB0CTL1 |= UCTXSTP; //无应答结束总线
}
/*-------------------------
功能描述:I2C读一字节数据
---------------------------*/
uint8 I2cReadByte(void)
{
while(!(IFG2&UCB0RXIFG));
return UCB0RXBUF;
}
/*-------------------------------------------------------
功能描述:访问地址为字节型器件的写操作
入口参数:SlaveAddress:从机地址, SubAddress:写入地址
*Data: 写入内容 ,Bytes:写入字节数
---------------------------------------------------------*/
void I2C_Write_AddrByte(uint8 SlaveAddress, uint8 SubAddress,uint8 *Data,uint8 Bytes)
{
InitI2cWrite(SlaveAddress);
I2cWriteByte(SubAddress);//写器件子地址
while(Bytes--){
I2cWriteByte(*Data); //发送数据
Data++;
}
while(!(IFG2&UCB0TXIFG));//等待TXBUF为空
UCB0CTL1 |=UCTXNACK;
// while (UCB0CTL1 & UCTXSTP);// Ensure stop condition got sent
UCB0CTL1 |= UCTXSTP;
//IFG2 &= ~UCB0TXIFG; // Clear USCI_B0 TX int flag
}
/*------------------------------------------------------
功能描述:访问地址为字节型器件的读操作
入口参数:SlaveAddress:从机地址, SubAddress:读取地址
*Data: 读取内容 ,Bytes:读取字节数
--------------------------------------------------------*/
void I2C_Read_AddrByte(uint8 SlaveAddress,uint8 SubAddress,uint8 *Data,uint8 Bytes)
{
uint8 i;
InitI2cWrite(SlaveAddress);
I2cWriteByte(SubAddress);//写器件子地址
while (UCB0CTL1 & UCTXSTP);// Ensure stop condition got sent
InitI2cRead(SlaveAddress);
for(i=0;i<Bytes;i++)
{
*Data=I2cReadByte();//读取数据
Data++;
}
UCB0CTL1 |=UCTXNACK;
// while((IFG2&UCB0RXIFG));
//while (UCB0CTL1 & UCTXSTP);// Ensure stop condition got sent
UCB0CTL1 |= UCTXSTP;
}
/*-------------------------------------------------------
功能描述:访问地址为整数型器件的写操作
入口参数:SlaveAddress:从机地址, SubAddress:写入地址
*Data: 写入内容 ,Bytes:写入字节数
---------------------------------------------------------*/
void I2C_Write_AddrInt(uint8 SlaveAddress,uint16 SubAddress,uint8 *Data,uint8 Bytes)
{
uint8 Hi_suba,Lo_suba;
Hi_suba = ( uint8 )( SubAddress >> 8 );
Lo_suba = ( uint8 )( SubAddress & 0x00ff );
InitI2cWrite(SlaveAddress);//初始化写
I2cWriteByte(Hi_suba);//写器件子地址
I2cWriteByte(Lo_suba);
while(Bytes--){
I2cWriteByte(*Data);//发送数据
Data++;
}
while (UCB0CTL1 & UCTXSTP);// Ensure stop condition got sent
}
/*------------------------------------------------------
功能描述:访问地址为整数型器件的读操作
入口参数:SlaveAddress:从机地址, SubAddress:读取地址
*Data: 读取内容 ,Bytes:读取字节数
--------------------------------------------------------*/
void I2C_Read_AddrInt(uint8 SlaveAddress,uint16 SubAddress,uint8 *Data,uint8 Bytes)
{
uint8 i;
uint8 Hi_suba,Lo_suba;
Hi_suba = (uint8)( SubAddress >> 8 );
Lo_suba = (uint8 )( SubAddress & 0x00ff );
InitI2cWrite(SlaveAddress);//初始化写
I2cWriteByte(Hi_suba);//写器件子地址
I2cWriteByte(Lo_suba);
while (UCB0CTL1 & UCTXSTP);// Ensure stop condition got sent
InitI2cRead(SlaveAddress);
for(i=0;i<Bytes;i++)
{
*Data=I2cReadByte();//读取数据
Data++;
}
while (UCB0CTL1 & UCTXSTP);// Ensure stop condition got sent
} |
阿莫论坛20周年了!感谢大家的支持与爱护!!
你熬了10碗粥,别人一桶水倒进去,淘走90碗,剩下10碗给你,你看似没亏,其实你那10碗已经没有之前的裹腹了,人家的一桶水换90碗,继续卖。说白了,通货膨胀就是,你的钱是挣来的,他的钱是印来的,掺和在一起,你的钱就贬值了。
|