// mcu stc89c52
// fsoc=11.0592M
///////////////////////////
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sfr P1M1=0X91;
sfr P1M0=0X90;
sbit SCL=P1^0;
sbit SDA=P1^1;
uchar Ans=0;
uchar Cap_Range=0;
//const uchar OffSet_Cap[2]={0xD2,0xF0};
const uchar OffSet_Cap[2]={0xD2,0xF0};
uchar i_receive,TC35_RECE_MAX=10,i_TC35_RECE=0;//,COM_Flg=1;
uchar TC35_RECE[10];
//////////////////////////////
void Delay_1(int i)
{
while(i--);
}
/////////////////////
/*void delay(unsigned char ms)
{ // 延时子程序
unsigned char i;
while(ms--)
{
for(i = 0; i< 250; i++)
{
_nop_();
_nop_();
_nop_();
_nop_();
}
}
}
/***********************************************************
函数名称:Ini_UART
函数功能:串口初始化设置 入口参数:无 出口参数:无
***********************************************************/
void uart_int(void)
{
// AUXR = 0x40; //timer0 12T ang timer1 1T
SCON = 0x50; //UART方式1:8位UART; REN=1:允许接收
PCON = 0x00; //SMOD=0:波特率不加倍
TMOD = 0x20; //T1方式2,用于UART波特率
TH1 = 0xFD;
TL1 = 0xFD; //UART波特率设置:9600
TR1 = 1;
EA=1;
ES=1;
}
/***********************************************************
函数名称 :delay
函数功能 :延时 入口参数:z 出口参数:无
***********************************************************/
/*void delay_SMS(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=55;y>0;y--);
}
/***********************************************************
函数名称 :send
函数功能 :入口参数:tab 出口参数 :无
***********************************************************/
void send(uchar *tab)
{
while((*tab)!= '\0')
{
SBUF =*tab;
while(TI ==0);
TI = 0;
tab++;
}
}
/***********************************************************
函数名称 :receive
函数功能 :入口参数:无 出口参数 :无
***********************************************************/
void serial() interrupt 4 using 3
{
if (RI)
{
RI = 0 ;
i_receive=SBUF;
if(i_TC35_RECE<TC35_RECE_MAX)
{
TC35_RECE[i_TC35_RECE++]=i_receive;
}
}
}
/*******************************************************************************
**函数名称:void IIC_Stop()
**函数功能:结束IIC通信
//Precondition: after reading or sending the data ,the SCL is in low state
********************************************************************************/
void IIC_Start()
{
SDA=1;SCL=1;
_nop_();
Delay_1(5) ;
SDA=0; //SDA low first
_nop_();
Delay_1(5) ;
SCL=0; //set SCL low
_nop_();
Delay_1(5) ;
}
/*******************************************************************************
**函数名称:void IIC_Stop()
**函数功能:结束IIC通信
//Precondition: after reading or sending the data ,the SCL is in low state
********************************************************************************/
void IIC_Stop()
{
SDA=0; //set SDA low first
_nop_();
Delay_1(5) ;
SCL=1; //Set SCL low
_nop_();
Delay_1(5) ;
SDA=1;
_nop_();
Delay_1(5) ;
}
/*******************************************************************************
**function name:void IIC_ReadAns()
**the use of function:Send the answer reading , increment the adress automatelly
**Precondition: The direction of the port is set for need
********************************************************************************/
void IIC_ReadAns(char a)
{
if(a==0) SDA=0;
else SDA=1;
_nop_();
Delay_1(5) ;
SCL=1;// Delay_1(10);
_nop_();
Delay_1(5) ;
SCL=0;// Delay_1(10);
_nop_();
Delay_1(5) ;
}
/*******************************************************************************
**function name:bool IIC_WriteAns()
**the use of function:Check the write acknowledge
**Precondition: The direction of the port is set for need
*******************************************************************************/
void IIC_WriteAns()
{
SDA=1; //free the bus
Ans=0;
SCL=1;
if(SDA==1)
Ans=0;
else
Ans=1; //record
SCL=0;
_nop_();
Delay_1(5) ;
}
/******************************************************************************
**function name:void Send_Byte(Uchar data)
**the use of function:Sent a byte to the device
*******************************************************************************/
void Send_Byte(uchar dat)
{
uchar count;
for(count=0;count<8;count++)
{
if(dat&0x80)
SDA = 1;
else
SDA = 0;
Delay_1(30) ;
SCL = 1; //Delay_1(5);
_nop_();
Delay_1(30) ;
_nop_();
SCL = 0; // Delay_1(5);
Delay_1(30) ;
dat = dat << 1;
}
Delay_1(30) ;
IIC_WriteAns();
}
/******************************************************************************
**function name:void Read_Byte()
**function description:Read a data from the IIC device
*******************************************************************************/
char Read_Byte()
{
uchar count;
uchar Indata=0;
SCL=0; //Delay_1(5); //Ready to Read
SDA=1;
for(count=0;count<8;count++)
{
Delay_1(30) ;
SCL=1;// Delay_1(5);
_nop_();
Delay_1(30) ;
Indata=Indata<<1;
if(SDA==1)
Indata=Indata+1;
SCL=0;
}
Delay_1(30) ;
return Indata;
}
/*******************************************************************************
**function name:bool Send_Word(char Adress, char data)
**function description:Sent sveral data to the IIC device
*******************************************************************************/
uchar Send_Word(char Adress,char dat)
{
IIC_Start(); //Start the IIC bus
Send_Byte(0x90); //Set the write mode
if(Ans==0) return 0;
Send_Byte(Adress); //Send the data adress
if(Ans==0) return 0;
Send_Byte(dat);
if(Ans==0) return 0; //Send the data
IIC_Stop();
return 1;
}
/******************************************************************************
**function name:unsigned long Read_Word(char Adress,char number)
**function description: Read data from the IIC device
*******************************************************************************/
unsigned long Read_Word(char Adress,char number)
{
unsigned long Read_data=0;
uchar count;
IIC_Start(); //Start the IIC bus
Send_Byte(0x90); //Set the write mode
if(Ans==0) return 0;
Send_Byte(Adress); //Send the data adress
if(Ans==0)
return 0;
IIC_Start();Send_Byte(0x91); //Set the read mode
if(Ans==0) return 0;
for(count=0;count<number-1;count++)
{
Read_data+=Read_Byte();
IIC_ReadAns(1);
IIC_Stop();
Read_data=Read_data<<8;
IIC_Start(); //Start the IIC bus
Send_Byte(0x90); //Set the write mode
if(Ans==0) return 0;
Send_Byte(Adress+count+1); //Send the data adress
if(Ans==0) return 0;
IIC_Start();
Send_Byte(0x91); //Set the read mode
if(Ans==0) return 0;
}
Read_data+=Read_Byte();
IIC_ReadAns(1);
IIC_Stop();
return Read_data;
}
/******************************************************************************
**function name:IIC_Reset()
**function description:Reset the IIC device
*******************************************************************************/
void IIC_Reset()
{ IIC_Start();
Send_Byte(0x90);
Send_Byte(0xBF); //Set the write mode
Delay_1(500);
// Delay_1(5000);
IIC_Stop();
}
/*******************************************