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

📄 iic固件程序.txt

📁 PIC固件程序,已通过MPLAB IDE V7.10版本调试
💻 TXT
字号:
/*已通过MPLAB IDE V7.10版本调试,并且本人也用AT24C04搭了一个外围电路调试通过*/
/*完全正常*/

#include <pic.h>
#define send_address 0xA0
#define receive_address 0xA1
#define i2c_address 0x18
/*********************************************/
unsigned int i,k;
unsigned char send_data,receive_data;

/*********************************************/
void dlay(void)
{
 #asm
  nop;
  nop; 
  nop;
  nop;
 #endasm
  }

/*********************************************/
void delay(void)
{
for(i=0;i<50;i++)
 for(k=0;k<100;k++)
   continue;
}

/*********************************************/
void i2cint(void)
{
   SSPCON=0X08;
  TRISC3=1;
  TRISC4=1;
   SSPSTAT=0X80;
   SSPADD=0X09;
   SSPCON2=0X00;
 di();
  SSPIF=0; 
   SSPEN=1;
 }
/***********************************************************/
//==========================================================
/***********************************************************/

void i2c_write (void)
{
SEN=1;
do
{
dlay();
}
while(SSPIF==0);
    SSPIF=0;
    SSPBUF=send_address;
do
  { 
    dlay();
    }
while(SSPIF==0);
    SSPIF=0;
    SSPBUF=i2c_address;
 do
  { 
    dlay();
    }
while(SSPIF==0);
    SSPIF=0;
   SSPBUF=send_data;
do
  {
   dlay();
}
while(SSPIF==0);
   SSPIF=0;
   PEN=1;
do
   {
    dlay();
    }
while(SSPIF==0);
   SSPIF=0;
}
/*****************************************************/
//====================================================
/*****************************************************/
char i2c_read(void)
{
SEN=1;
do
{
dlay();
}
while(SSPIF==0);
    SSPIF=0;
SSPBUF=send_address;
do
{
dlay();
}
while(SSPIF==0);
SSPIF=0;
SSPBUF=i2c_address;
do
{
dlay();
}
while(SSPIF==0);
SSPIF=0;
RSEN=1;
do
{
dlay();
}
while(SSPIF==0);
SSPIF=0;
SSPBUF=receive_address;
do
{
dlay();
}
while(SSPIF==0);
SSPIF=0;
RCEN=1;
do
{
dlay();
}
while(SSPIF==0);
SSPIF=0;
receive_data=SSPBUF;
PEN=1;
do
{
dlay();
}
while(SSPIF==0);
SSPIF=0;
return(receive_data);
}

//=============================================

void main(void)
{
unsigned char good;
good=0x00;
i2cint(); 
while(1)
{
send_data=0x55;
i2c_write();
delay();
i2c_read();
if(receive_data==send_data)
good=0x88;
}
}

⌨️ 快捷键说明

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