#include "STC8H.H"
#define IS_WAIT 0X40
#define COM_START 0X01
#define COM_SENDDATA 0X02
#define COM_RECVACK 0X03
#define COM_STOP 0X06
sbit SDA=P1^4;
sbit SCL=P1^5;
void Wait();
void Start();
void SendData(char dat);
void RecvACK();
void Stop();
void I2C_Init();
void I2C_MEM_Write(unsigned char DevAddr,unsigned char Mode,unsigned char Data);
#include "i2c.h"
void Wait()
{
while(!(I2CMSST & IS_WAIT));
I2CMSST &=~IS_WAIT;
}
void Start(){
I2CMSCR=COM_START;
Wait();
}
void SendData(char dat){
I2CTXD=dat;
I2CMSCR=COM_SENDDATA;
Wait();
}
void RecvACK(){
I2CMSCR=COM_RECVACK;
Wait();
}
void Stop(){
I2CMSCR=COM_STOP;
Wait();
}
void I2C_Init(){
P_SW2=0X80;
I2CCFG=0XC0;
I2CMSST=0X00;
}
void I2C_MEM_Write(unsigned char DevAddr,unsigned char Mode,unsigned char Data){
Start();
SendData(DevAddr);
RecvACK();
SendData(Mode);
RecvACK();
SendData(Data);
RecvACK();
Stop();
}
为了方便使用对STC8H写I2C的功能仿照stm32组合了一下。