#include
#include
#define set_sda PORTC |= 0x01
#define clr_sda PORTC &= ~(0x01)
#define set_scl PORTC |= (1<<1)
#define clr_scl PORTC &= ~(1<<1)
#define read_sda (PORTC & (0x01))
#define out_sda DDRC |= 0x01
#define in_sda DDRC &= ~(0x01)
#define IC_WRITE (0x1D << 1)
#define IIIC_READ ((0x1D << 1) | 0x01)
uchar table1[16]=" LCSOFT X= 0.00";//1602显示数据
uchar table2[16]=" Y= 0.00 Z= 0.00";
uchar table3[16]="No acceleration!";
uchar table4[10]="0123456789";
uchar table5[8] =" MMA7455";
uchar table7[16]="Value: X=0.50 ";
void iic_start() //函数功能:I2C通信开始
{
set_scl;
_delay_us(30);
set_sda;
_delay_us(30);
set_scl;
_delay_us(30);
clr_sda;
_delay_us(30);
set_scl;
_delay_us(30);
}
void iic_stop() //函数功能:I2C通信停止
{
clr_sda;
_delay_us(30);
set_scl;
_delay_us(30);
set_sda;
_delay_us(30);
set_scl;
_delay_us(30);
}
void iic_ack() //函数功能:I2C通信查应答位
{
// set_sda;
set_scl;
_delay_us(30);
clr_scl;
_delay_us(30);
}
void iic_write_byte(uchar wdata)//函数功能:向I2C从机写入一个字节
{
uchar i,temp,temp1;
temp1=wdata;
for(i=0;i<8;i++)
{
clr_scl;
_delay_us(30);
temp=temp1;
temp=temp&0x80;
if(temp==0x80)
set_sda;
else
clr_sda;
_delay_us(30);
set_scl;
_delay_us(30);
clr_scl;
_delay_us(30);
temp1=temp1<<1;
}
}
char iic_read_byte(void)//函数功能:从I2C从机中读出一个字节
{
uchar x;
char data = 0;
in_sda;
_delay_us(30);
set_sda;
_delay_us(30);
for(x=0;x<8;x++)
{
data = data << 1;
_delay_us(30);
clr_scl;
_delay_us(30);
set_scl;
_delay_us(30);
if(read_sda)
data |= 0x01;
//else
// data&=0xfe;
}
out_sda;
return data;
}
void iic_write(uchar byte_add,uchar wdata)//函数功能:按地址写入一字节数据
{
uchar t;
t=(IIC_WRITE<<1);
iic_start();
iic_write_byte(t);
iic_ack();
iic_write_byte(byte_add);
iic_ack();
iic_write_byte(wdata);
iic_ack();
iic_stop();
}
char iic_read(uchar byte_add)//函数功能:按地址读出一字节数据
{
uchar t;
char x;
t=(IIC_WRITE<<1);
iic_start();
iic_write_byte(t);
iic_ack();
iic_write_byte(byte_add);
iic_ack();
t=((IIC_READ<<1)|0x01);
iic_start();
iic_write_byte(t);
iic_ack();
x=iic_read_byte();
iic_ack();
iic_stop();
return x;
}
修改一下,宏定义
#define IC_WRITE (0x1D << 1)
#define IIIC_READ ((0x1D << 1) | 0x01)
改为
#define IC_WRITE 0x1D << 1
#define IIIC_READ 0x1D << 1
之前改了忘记改回来。