MSP430 IIC总线程序

来源:本站
导读:目前正在解读《MSP430 IIC总线程序》的相关信息,《MSP430 IIC总线程序》是由用户自行发布的知识型内容!下面请观看由(电工技术网 - www.9ddd.net)用户发布《MSP430 IIC总线程序》的详细说明。
简介:本文给大家分享了MSP430 IIC总线程序。

#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){  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

提醒:《MSP430 IIC总线程序》最后刷新时间 2024-03-14 00:58:04,本站为公益型个人网站,仅供个人学习和记录信息,不进行任何商业性质的盈利。如果内容、图片资源失效或内容涉及侵权,请反馈至,我们会及时处理。本站只保证内容的可读性,无法保证真实性,《MSP430 IIC总线程序》该内容的真实性请自行鉴别。