|
这是小弟写的程序,就是没有返回的数据请大家帮忙看看那里错拉?
#include "pic.h"
#define uchar unsigned char
#define nop() asm("nop")
#define PORTCIT(adr,bit) ((unsigned)(&adr)*8+(bit))
static bit SDA @ PORTCIT (PORTC,4);
static bit SCL @ PORTCIT (PORTC,3);
void start_i2c();
void stop_i2c();
void send_byte(uchar c);
uchar receive_byte();
void I_send_str(void);
void delay_250ms();
void i2c_error ();
uchar ack;
void main_int(void)
{
PORTA=0X00;
PORTB=0X1f;
PORTC=0X00;
TRISA=0X00;
TRISB=0X00;
TRISC=0X00;
}
/****************************************
****************************************/
void main(void)
{
uchar data;
main_int();
I_send_str();
delay_250ms();
while (1)
{
start_i2c();
send_byte(0xa0);
if (ack==0) i2c_error();
send_byte(0x00);
if (ack==0) i2c_error();
start_i2c();
send_byte(0xa1);
if (ack==0) i2c_error();
data=receive_byte();
stop_i2c();
if(data==0x1f)
delay_250ms();
else
PORTA=data;
delay_250ms();
}
}
/*******************************************************************
起动总线函数
*******************************************************************/
void start_i2c()
{
SDA=1; nop();
SCL=1;
nop();nop();nop();nop();nop();
SDA=0;
nop();nop();nop();nop();nop();
SCL=0;
nop();nop();
}
/*******************************************************************
停止总线函数
*******************************************************************/
void stop_i2c()
{
SDA=0;
nop();
SCL=1;
nop();nop();nop();nop();nop();
SDA=1;
nop();nop();nop();nop();
}
/*=================================================================
字节数据传送函数
==================================================================*/
void send_byte(uchar c)
{
uchar bit_count;
for (bit_count=0;bit_count<8;bit_count++)
{
if ((c<<bit_count)&0x80) {SDA=1;}
else {SDA=0;}
nop();
SCL=1;
nop();nop();nop();nop();nop();
SCL=0;
}
nop();nop();
TRISC4=1;
nop();nop();
SCL=1;
nop();nop();nop();
if (RC4==1) ack=0;
else ack=1;
SCL=0;
nop();nop();
TRISC4=0;
}
/*==================================================================
字节数据接收函数
===================================================================*/
uchar receive_byte()
{
uchar retc,bit_count;
retc=0;
TRISC4=1;
SDA=1;
for (bit_count=0;bit_count<8;bit_count++)
{
nop();
SCL=0;
nop();nop();nop();nop();nop();
SCL=1;
nop();nop();
retc=retc<<1;
if (RC4==1) retc=retc+1;
nop();nop();
}
SCL=0;
nop();nop();
TRISC4=0;
return (retc);
}
/*================================================================
=================================================================*/
void I_send_str(void)
{
uchar i;
start_i2c();
send_byte(0xa0);
if (ack==0) i2c_error();
send_byte(0x00);
if (ack==0) i2c_error();
send_byte(0x1f);
if (ack==0) i2c_error();
stop_i2c();
}
/*****************************************************************
延时函数
*****************************************************************/
void delay_250ms()
{
unsigned int d=24999;
while (--d);
}
/*****************************************************************
总线错误函数
*****************************************************************/
void i2c_error ()
{
uchar i;
for (i=0;i<8;i++)
{
while(1);
}
}
/**********END**************/ |
|