搜索
bottom↓
回复: 0

F247 i2c设置请教

[复制链接]

出0入0汤圆

发表于 2010-5-7 16:54:11 | 显示全部楼层 |阅读模式
有人使用过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周年了!感谢大家的支持与爱护!!

知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-7-6 12:27

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表