⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iic.c

📁 TI的16位单片机的IIC驱动程序
💻 C
字号:
#include<MSP430x14x.h>

                              //P3.6  SCL
                              //P3.7  SDA

    #define SDA_1   P3OUT |=  BIT7              
                              //SDA = 1
    #define SDA_0   P3OUT &=~ BIT7              
                              //SDA = 0
    #define SCL_1   P3OUT |=  BIT6              
                              //SCL = 1
    #define SCL_0   P3OUT &=~ BIT6              
                              //SCL = 0
    #define DIR_IN  P3DIR &=~ BIT7  //SDA_1      
                              //I/O口为输入
    #define DIR_OUT P3DIR |=  BIT7             
                              //I/0口为输出
    #define SDA_IN  (P3IN&BIT7)>> 7        
                         //Read SDA
  void tt(void);             	
    void Init(void);
    void Start(void);
    void Stop(void);
    void WriteByte(unsigned char WriteData);
    unsigned char ReadByte(void);
    void ReceiveAck(void);
    void Acknowledge(void);
    void No_Acknowledge(void);
void twi_Write_eeprom(unsigned char unit_addr, unsigned char unit_data,unsigned char geshu);//24lc16
void Delay(int te);
unsigned char twi_read_eeprom(unsigned char firsttype,unsigned char count,unsigned char *buff);
unsigned char TempBit,TData,WData,HData,messageBuf,eep[5],a[];
//static int temp;              
void Delay(int te)
{ unsigned int i;
        for (i=0;i<te;i++);
}
void Init(void)
{
// P3DIR |=BIT7+BIT6;
 SCL_1;
 Delay(1);
 SDA_1;
 Delay(1);
}
void Start(void)
{  SDA_1;
  SCL_1;
 Delay(5);
  SDA_0;
 Delay(5);
  SCL_0;
//SDA_1;
//Delay(10);
//SCL_1;
//Delay(80);
//SDA_0;
//Delay(10);
//SCL_0;
//Delay(50);
}
void Stop(void)
{  SDA_0;
 Delay(5);
  SCL_1;
 Delay(5);
  SDA_1;
 Delay(5);
//SCL_0;
//Delay(1);
//SDA_0;
//Delay(50);
//SCL_1;
//Delay(10);
//SDA_1;
//Delay(70);
//SCL_0;
}
void WriteByte(unsigned char WData)
{ // unsigned char i;
  //for (i=0; i<8; i++)
  //{
   // //SCL_0;
  //  Delay(5);
   // if (((WData >> 7) & 0x01) == 0x01)
    //{
   //   SDA_1;
   // }
   // else
   // {
   //   SDA_0;
   // }
   // Delay(5);
   // WData = WData << 1;
   // SCL_1;
   // Delay(10);
	//SCL_0;
  //}
 //Delay(5);
//volatile 
  int j;
  for (j=0; j<8; j++)
  {    
    SCL_0;
    Delay(1);
   if (((WData >> 7) & 0x01) == 0x01)
    {
      SDA_1;
    }
    else
    {
      SDA_0;
    }
    //Delay(1);
    SCL_1;
    Delay(4);
    WData = WData << 1;
  }
  SCL_0;//用于接收ACK
  SDA_0;
  //Delay(8);
}
unsigned char ReadByte(void)
{
  unsigned char i;
  unsigned char TempBit  = 0;
  unsigned char TempData = 0;
  SDA_1;
  DIR_IN;
  Delay(5);
  for (i=0; i<8; i++)
  {
    Delay(5);
    SCL_1;
    Delay(5);
    if (SDA_IN == 0x01 )  /*sda==1*/
    {
      TempBit = 1;
    }
    else
    {
      TempBit = 0;
    }
    Delay(2);
    SCL_0;
    TempData = (TempData << 1) | TempBit;
    
  }
  DIR_OUT;
  SDA_1;
  Delay(5);
  return(TempData);
  // int k;
   //for (k=0; k<8; k++)
  // {      Delay(4);
   //   DIR_IN;
   //  SCL_1;
    //  if(SDA_IN==1)
    //  {
    //    TempBit = 1;
    //  }
     // else if(SDA_IN==0)
     // {
     //    TempBit = 0;
    //  }
    //  Delay(2);
     // DIR_OUT;
     // TData = (TData << 1) | TempBit;
    //  SCL_0;
   //}
  // return TData;
}
void ReceiveAck(void)
{volatile int i=0;
  DIR_IN;
  SCL_1;
  Delay(10);
  while (SDA_IN== 0x01&&i<255)
  { i++; }
  SCL_0;
  DIR_OUT;
  Delay(10);
}
void Acknowledge(void)
{
  SCL_0;
  Delay(2);
  DIR_OUT;
  SDA_0;
  SCL_1;
  Delay(4);
  SCL_0;
}
void Nacknowledge(void)
{
  SCL_0;
  Delay(2);
  DIR_OUT;
  SDA_1;
  SCL_1;
  Delay(4);
  SCL_0;
}
void twi_Write_eeprom(unsigned char unit_addr, unsigned char unit_data,unsigned char geshu)//24lc16
{	unsigned char i;
        Start();
        WriteByte(0xa0);//已知eeprom器件地址,是固定的
        ReceiveAck();
        WriteByte(unit_addr);//要写数据的eeprom起始地址,是不固定的,需要改变
        ReceiveAck();
        for(i=0;i<geshu;i++)
        {
          WriteByte(unit_data+i);
          ReceiveAck(); 
        }      
        Stop();
        Delay(200); 
}
void main(void)
{//WDTCTL = WDTPW + WDTHOLD;
P3DIR |=  BIT6+BIT7;
tt();
}                              
void tt(void)
{//char ii;
twi_read_eeprom(0x02,2,eep); // 读取时间
  //ReceiveAck();
  //Acknowledge();
   //for(uu=0;uu<7;uu++)
 //GetPCF8563(0x55,0x02,*a);

   //twi_Write_eeprom(0x55,0x59,5);
   //}
}
unsigned char twi_read_eeprom(unsigned char firsttype,unsigned char count,unsigned char*buff)
{
unsigned char i;
Start();       // 启动总线
WriteByte(0xa0); // 发送器件写地址
ReceiveAck();
WriteByte(firsttype);
ReceiveAck();
Start();
WriteByte(0xa1);
ReceiveAck();
for(i=0;i<count;i++)
  {
  *buff=ReadByte();
  if(i!=count-1)//除最后一个字节外,其他都要从MASTER发应答。
    {
    Acknowledge();
    }
  buff++;
  }
Nacknowledge();
Stop();
 return buff[0];
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -