51/AVR单片机技术驿站!  <在线翻译>     便利工具    特色网址   无弹窗、无插件的绿色站点...  英才招聘   学历查询  喜欢>>收藏我站 

当前位置:首页 > 单片机技术文章 > MSP430单片机 > 详细内容
MSP430 IIC总线程序
发布时间:2009/7/31  阅读次数:1139  字体大小: 【】 【】【
#ifndef _I2C_H
#define _I2C_H

#define SDA_1           P4OUT|=0x02                        //SDA = 1
#define SDA_0           P4OUT&=~0x02                      //SDA = 0
#define SCL_1           P4OUT|=0x01                        //SCL = 1
#define SCL_0           P4OUT&=~0x01                      //SCL = 0
#define SDA_IN         P4DIR&=~0x02;                     //I/O口为输入
#define SDA_OUT       P4DIR|=0x02                        //I/0口为输出
#define J_SDA_1_or_0    (P4IN&0x02)==0x02
//--------------------------------------------
//--------以下为24C64读写子函数
//DELAY 函数程序//功能:延时子程序
//==========================================
static void Delay(unsigned int n)
{
while(n!=0)
{n--;}
}
//=========================================
//START 函数程序
//功能:开始闪存操作
//==========================================
void Start(void)
{
   SDA_OUT;
   SDA_1;
   Delay(10);
   SCL_1;
   Delay(10);
   SDA_0;
   Delay(10);
   SCL_0;
   Delay(10);
}

//=========================================
//STOP 函数程序
//功能:停止闪存操作
//==========================================
void Stop(void)
{
   SDA_0;
   Delay(10);
   SCL_1;
   Delay(10);
   SDA_1;
   Delay(10);
}



void TX_Byte(unsigned char WriteData)
{
   unsigned char i,j;
   j=WriteData;
   SDA_OUT;
   for (i=0; i<8; i++)
   {
      if ((j&0x80)==0)
      { SDA_0;}
      else
      { SDA_1;}
      j <<=1;
      Delay(10);
      SCL_1;
      Delay(10);
      SCL_0;
      Delay(10);
   }
}



unsigned char RX_Byte(void)
{
   unsigned char i;
   unsigned char TempData = 0;
   SDA_IN;
   for (i=0; i<8; i++)
   {
      Delay(10);
      SCL_1;
      Delay(10);
      TempData <<= 1;
      if (J_SDA_1_or_0)
      { TempData++; }
      SCL_0;
      Delay(10);
   }
   SDA_OUT;
   return(TempData);
}
//=========================================
//ACK 函数程序
//功能:挂高电平表示闪存时钟信号
//==========================================
void ReceiveAck(void)
{
   SDA_IN;
   SCL_1;
   Delay(10);
   while (J_SDA_1_or_0)
   {   }
   SCL_0;
   SDA_OUT;
   Delay(10);
}



unsigned char ReadWord(unsigned long int unit/*address*/)
{
   unsigned char LowAdd   = 0;
   unsigned char HighAdd = 0;
   unsigned char   TempData = 0;
   LowAdd   = (unsigned char)unit;
   HighAdd = (unsigned char)(unit >> 8);
   SDA_OUT;
   Start();
   TX_Byte(0xa0);
   ReceiveAck();
   TX_Byte(HighAdd);
   ReceiveAck();
   TX_Byte(LowAdd);
   ReceiveAck();
   Start();
   TX_Byte(0xa1);
   ReceiveAck();
   TempData   = RX_Byte();   
   Stop();
   Delay(8000);   
   return(TempData);
}


void WriteWord(unsigned long int unit/*address*/, unsigned char WriteData)
{
   unsigned char LowAdd   = 0;
   unsigned char HighAdd = 0;
   LowAdd   = (unsigned char)unit;
   HighAdd = (unsigned char)(unit >> 8);
   Start();
   TX_Byte(0xa0);
   ReceiveAck();
   TX_Byte(HighAdd);
   ReceiveAck();
   TX_Byte(LowAdd);
   ReceiveAck();
   TX_Byte(WriteData);
   ReceiveAck();
   Stop();
   Delay(8000);
}

#endif
我要评论
  • 匿名发表
  • [添加到收藏夹]
  • 发表评论:(匿名发表无需登录,已登录用户可直接发表。) 登录状态:未登录
最新评论
所有评论[0]
    暂无已审核评论!

网站导航 管理登陆 ┊ 免责声明 问题反馈  友链说明
本站部分内容来自网络共享资源,如有冒犯您的权利请来信告之删除或纠正!
不得对本站进行复制、盗链或镜像,转载内容须获得同意或授权;欢迎友情链接、站务合作!

    我要报警 Alexa
 mcusy_cn#126.com (请把#改成@) 交流:522422171
本站学习交流群:138..158(高级群1-)、77930286(高级群2)、61804809(群3)
Copyright© MCUSY All Rights Reserved
本站网警备案号: WZ36040002485
  ICP备案证书号:粤ICP备09034963号