MSP430 IIC bus program

Publisher:小悟空111Latest update time:2016-01-18 Source: eefocusKeywords:MSP430  IIC Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#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 port is input
#define SDA_OUT     P4DIR|=0x02               //I/0 port is output
#define J_SDA_1_or_0   (P4IN&0x02)==0x02
//--------------------------------------------
//--------The following is the 24C64 read and write sub-function
//DELAY Function program //Function: delay subroutine
//==============================================
static void Delay(unsigned int n)
{
while(n!=0)
{n--;}
}
//===========================================
//START Function program
//Function: start flash memory operation
/==============================================
void Start(void)
{
  SDA_OUT;
  SDA_1;
  Delay(10);
  SCL_1;
  Delay(10);
  SDA_0;
  Delay(10);
  SCL_0;
  Delay(10);
}
//===========================================
//STOP function program
//Function: stop flash memory operation
//===============================================
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 function program
//Function: high level indicates flash memory clock signal
//==============================================
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)
{
  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, 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
Keywords:MSP430  IIC Reference address:MSP430 IIC bus program

Previous article:Wireless transmission protocol based on MSP430
Next article:MSP430 Development Summary

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号