📄 i2ctestmaster.c
字号:
delayus(10);
// start convertion and wait for interrupt
GODONE=1;
}
void delayus(unsigned char time)
{
time=time/2;
while(time-->0)
{
NOP();
}
}
void delayms(unsigned char time)
{
unsigned char i=0;
unsigned int j=0;
for(i=0;i<time;i++)
{
for(j=0;j<1000;j++)
{
NOP();
NOP();
NOP();
CLRWDT();
}
}
}
//---------name:device_initial---------//
//input: none
//output:none
//------function :initial device-------//
void device_initial(void)
{
// disable all interrupt
PEIE=0;
GIE=0;
// config rxd as digital io not a/d channel,important
ANSEL=0x00;
ANSELH=0x00;
// config RC0 as led
PORTC=0x00;
TRISC0=0;
RC0=0;
PORTA=0x00;
//TRISA2=1;
RABPU=0;
SCL_Derection=0;
SCL=1;
SDA_Derection=0;
SDA=1;
// watch dog and timer0 config
timer0_initial();
timer1_initial();
// Slave_I2C_Initial();
uart_initial();
// ad_initial();
// enable all interrupt
PEIE=1;
GIE=1;
CLRWDT();
}
//---------name:timer0_initial---------//
//input: none
//output:none
//------function :initial timer0-------//
void timer0_initial(void)
{
TMR0=0x00;
// internal instruction cycle
T0CS=0;
//prescaler is for watch dog,1:8
OPTION=0xDB;
T0IE=0;
//enable watch dog,8 second reset
WDTCON=0x15;
CLRWDT();
}
//---------name:timer1_initial---------//
//input: none
//output:none
//------function :initial timer1-------//
void timer1_initial(void)
{
TMR1IE=0;
// internal clock,8 divide,
T1CON=0x30;
//10ms for interrupt
TMR1L=0xD8;
TMR1H=0xF1;
//enable interrupt
TMR1IE=1;
TMR1ON=1;
CLRWDT();
}
//---------name:uart_deal_service---------//
//input: none
//output:none
//------function :uart deal events-------//
void uart_deal_service(void)
{
Rx_Start=0;
Rx_Over=0;
CLRWDT();
uart_send_data(Buffer2,URT_ptr);
ad_test();;
}
//----------name:ad_deal_service-----------//
//input: none
//output:none
//------function :a/d finish,routine-------//
void ad_deal_service(void)
{
Ad_Over=0;
CLRWDT();
uart_send_data(Ad_Buffer,2);
}
//----------name:i2c_rd_deal-----------//
//input: none
//output:none
//------function :master read over-----//
void i2c_rd_deal(void)
{
I2c_Rd_Over=0;;
}
//----------name:i2c_wr_deal-----------//
//input: none
//output:none
//-----function :master write over-----//
void i2c_wr_deal(void)
{
I2c_Wr_Start=0;
I2c_Wr_Over=0;
}
//------------name:pulse_test------------//
//input: none
//output:none
//---function :test the width of pulse---//
void pulse_test(void)
{
unsigned char timer0_count[3]={0,0,0};
unsigned int count=0;
second10ms=0;
wait1:
if(RA2==1)
{
if(second10ms>10)
return;
goto wait1;
}
wait2:
if(RA2==0)
{
if(second10ms>10)
return;
TMR0=0x00;
CLRWDT();
goto wait2;
}
while(1)
{
if(RA2==1)
{
if(T0IF==1)
{
T0IF=0;
count++;
}
}
else
{
T0IE=0;
break;
}
}
timer0_count[2]=TMR0;
timer0_count[0]=(count>>8)&0xff;
timer0_count[1]=(unsigned char)count&0xff;
//T0IE=0;
uart_send_data(timer0_count,3);
return;
}
//***************************************************************************************
//函数:void I2C_Start(void)
//功能:I2C起始条件,启动总线
//输入:
//输出:无
//=======================================================================================
void I2C_Start(void)
{
SDA_Derection=0;
//SDA_Derection=1;//
NOP();
NOP();
SDA=1;
// SDA_Derection=1;//
NOP();
SCL=1;
//SCL_Derection=1;//
NOP();
NOP();
NOP();
NOP();
NOP();
SDA=0;
//SDA_Derection=0;//
NOP();
NOP();
NOP();
NOP();
NOP();
SCL=0;
//SCL_Derection=0;//
NOP();
NOP();
}
//***************************************************************************************
//函数:void I2C_Stop(void)
//功能:结束I2C总线信号
//输入:
//
//输出:无
//=======================================================================================
void I2C_Stop(void)
{
SDA_Derection=0;
NOP();
NOP();
SDA=0;
NOP();
SCL=1;
// SCL_Derection=1;//
NOP();
NOP();
NOP();
NOP();
NOP();
SDA=1;
// SDA_Derection=1;//
NOP();
NOP();
NOP();
NOP();
SDA_Derection=1;
}
//***************************************************************************************
//函数:unsigned char I2C_CheckAcK(void)
//功能:检验I2C总线应答信号,有应答返回1,否则返回0
//输入:
//
//输出:0,失败;1,成功
//=======================================================================================
unsigned char I2C_CheckAcK(void)
{
unsigned char Err_time=255;
SDA_Derection=0;
SDA=1;
SDA_Derection=1;
NOP();
NOP();
NOP();
SCL=1;
//SCL_Derection=1;//
NOP();
NOP();
NOP();
NOP();
NOP();
while(SDA)
{
Err_time--;
if(Err_time==0)
{
I2C_Stop();
return 0x00;
}
}
SCL=0;
//SCL_Derection=0;//
NOP();
NOP();
return 0x01;
}
//***************************************************************************************
//函数:void I2C_SendB(unsigned char c)
//功能:向I2C总线发送一个字节
//输入:要发送的数据字节
//
//输出:无
//=======================================================================================
void I2C_SendB(unsigned char c)
{
unsigned char i,j;
SDA_Derection=0;
for(i=0;i<8;i++)
{
if((c<<i)&0x80)
{
SDA=1;
//SDA_Derection=1;
}
else
{
SDA=0;
//SDA_Derection=0;
}
NOP();
NOP();
SCL=1;
//SCL_Derection=1;//
NOP();
NOP();
NOP();
NOP();
NOP();
SCL=0;
//SCL_Derection=0;//
}
NOP();
NOP();
I2C_Ack=I2C_CheckAcK();
NOP();
NOP();
}
//***************************************************************************************
//函数:unsigned char I2C_RceB(void)
//功能:从I2C总线上接收1个字节
//输入:无
//
//输出:所接收的1个字节数据
//=======================================================================================
unsigned char I2C_RceB(void)
{
unsigned char i,Rcdata;
Rcdata=0;
SDA_Derection=1;
//delayus(8);//
//SCL_Derection=1;//
delayus(20);//
//while(!SCL);
// SCL_Derection=0;
// SCL=1;
for(i=0;i<8;i++)
{
NOP();
SCL=0;
//SCL_Derection=0;//
NOP();
NOP();
NOP();
NOP();
SCL=1;
//SCL_Derection=1;//
NOP();
NOP();
Rcdata=Rcdata<<1;
if(SDA==1)
{
Rcdata=Rcdata+1;
}
NOP();
NOP();
}
SCL=0;
// SCL_Derection=0;//
NOP();
NOP();
return (Rcdata);
}
//***************************************************************************************
//函数:void I2C_Ackn(unsigned char a)
//功能:接收数据后向I2C总线发送应答或非应答信号
//输入:0 发送应答信号,数据接收未完成
// 1 发送非应答信号,数据接收完毕
//输出:无
//=======================================================================================
void I2C_Ackn(unsigned char a)
{
SDA_Derection=0;
if(a==0)
{
SDA=0;
//SDA_Derection=0;//
}
else
{
SDA=1;
//SDA_Derection=1;//
}
NOP();
NOP();
NOP();
SCL=1;
// SCL_Derection=1;//
NOP();
NOP();
NOP();
NOP();
NOP();
SCL=0;
//SCL_Derection=0;//
NOP();
NOP();
}
//***************************************************************************************
//函数:unsigned char Send_Nbyte(unsigned char len,unsigned char *p)
//功能:向I2C总线发送N个字节
//输入:len:写入数据的字节长度
// p:数组的指针
//输出:0,发送失败;1,发送成功
//=======================================================================================
unsigned char Send_Nbyte(unsigned char len,unsigned char *p)
{
unsigned char i;
I2C_Start();
I2C_SendB(0x02);
for(i=0;i<len;i++)
{
I2C_SendB(*(p+i));
if(!I2C_Ack)
{
//RC0=1;
return 0;
}
}
//RC0=1;
I2C_Stop();
return 1;
}
//***************************************************************************************
//函数:unsigned char Read_RTC(unsigned char*timeptr)
//功能:从时钟芯片读时钟
//输入:接收时钟区指针,接收长度为7字节,分别为秒,分,时,星期,日,月,年
//
//输出:0 表示读取失败;1 表示读取成功;
//=======================================================================================
unsigned char Read_Nbyte(unsigned char num)
{
unsigned char i;
I2C_Start();
I2C_SendB(0x03);
if(!I2C_Ack)
{
RC0=0;
return 0;
}
else
RC0=1;
//delayus(8);
for(i=0;i<num;i++)
{
*(I2C_Buff+i)=I2C_RceB();
if(i<num-1)
{
I2C_Ackn(0);
}
else
{
I2C_Ackn(1);
}
//delayus(24);//18
}
I2C_Stop();
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -