?? iic固件程序.txt
字號:
/*已通過MPLAB IDE V7.10版本調(diào)試,并且本人也用AT24C04搭了一個外圍電路調(diào)試通過*/
/*完全正常*/
#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;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -