#include <hidef.h> #include "derivative.h" #include "Global.h" //-----***-IIC延时-***-----// void IicWait(void) { asm("NOP"); //01 //asm("NOP"); //02 //asm("NOP"); //01 } //-----***-IIC启示-***-----// void IicStart(void) { IicSda=1; IicWait(); IicScl=1; IicWait(); IicSda=0; IicWait(); IicScl=0; IicWait(); } //-----***-IIC停止-***-----// void IicStop(void) { IicSda=0; IicWait(); IicScl=1; IicWait(); IicSda=1; IicWait(); } //-----***-IIC应答-***-----// void SendAcknowledge(unsigned char IicAck) { IicSda=IicAck; IicScl=1; IicWait(); IicScl=0; IicWait(); } //-----***-从slave端读取一个数据-***-----// unsigned char IicReceiveByte(void) { unsigned char i; unsigned char bytedata=0; IicSda=1; for(i=0;i<8;i++) { IicScl=1; IicWait(); bytedata<<=1; if (IicSda) bytedata |=0x01; IicScl=0; IicWait(); } return bytedata; } //-----***-传送一个 Byte 数据到 slave-***-----// unsigned char IicSentByte(unsigned char DataByte) { unsigned char i,IicAck; for(i=0;i<8;i++) { if(DataByte & 0x80) { IicSda=1; } else { IicSda=0; } DataByte <<=1; IicWait(); IicScl=1; IicWait(); IicScl=0; IicWait(); } IicSda=1; IicWait(); IicScl=1; IicWait(); IicAck=IicSda; IicScl=0; IicWait(); return IicAck; } //-----***-无扇区读---单字节-***-----// unsigned char FgTimeout; //读取指定器件无扇区指定地址单字节的数据 //Device=器件地址---单字节 //DataAdd=字节地址---单字节 unsigned char IicByteRead(unsigned char DataAdd) { unsigned char bytedata; DisableInterrupts;//关中断 IicStart(); IicSentByte(0xa0); IicSentByte(DataAdd); IicStart(); IicSentByte(0xa1); bytedata=IicReceiveByte(); //SendAcknowledge(1); IicStop(); EnableInterrupts;//开中断 return bytedata; } //-----***-无扇区写---单字节-***-----// //写入指定器件无扇区指定地址中的单字节数据 //Device=器件地址---单字节 //DataAdd=字节地址---双字节 void IicByteWrite(unsigned char DataAdd,unsigned char DataByte) { unsigned char i,IicAck; if(BitData&0x01) { DisableInterrupts;//关中断 for(i=0;i<3;i++) { IicStart(); IicAck=IicSentByte(0xa0);if(IicAck==1){IicStop();continue;} IicAck=IicSentByte(DataAdd);if(IicAck==1){IicStop();continue;} IicAck=IicSentByte(DataByte);if(IicAck==1){IicStop();continue;} IicStop(); if(IicAck==0)break; } EnableInterrupts;//开中断 Delay(100); } } |