avr gcc m16L 模拟IIC与24c02通讯老不成功 用51就可以
时间:10-02
整理:3721RD
点击:
//系统时钟1MHz
#include <avr/io.h>
#include<util/delay.h>
#define uint unsigned int
#define uchar unsigned char
#define BIT(x) (1<<x)
/*sbit sda=P2^0;
sbit scl=P2^1;
sbit rst=P2^4;*/
void delay()
{ asm("NOP"); asm("NOP"); }
void init()
{
DDRA=0XFF;
DDRB|=BIT(7);
DDRB|=BIT(0)|BIT(1);
PORTB&=~BIT(7);
PORTB|=BIT(0);
delay();
PORTB|=BIT(1);
delay();
}
void start()
{
PORTB|=BIT(0);
delay();
PORTB|=BIT(1);
delay();
PORTB&=~BIT(0);
delay();
}
void stop()
{
PORTB&=~BIT(0);
delay();
PORTB|=BIT(1);
delay();
PORTB|=BIT(0);
delay();
}
void ack()
{
uchar i;
PORTB|=BIT(1);
DDRB&=~BIT(0);
PORTB|=BIT(0);
delay();
while(((PINB&BIT(0))==1)&&(i<250))i++;
PORTB&=~BIT(1);
DDRB|=BIT(0);
delay();
}
void write(uchar date)
{
uchar i;
for(i=0;i<8;i++)
{
PORTB&=~BIT(1);
delay();
if(date&0x80)PORTB|=BIT(0);
else PORTB&=~BIT(0);
delay();
PORTB|=BIT(1);
delay();
date=date<<1;
}
PORTB&=~BIT(1);
delay();delay();
PORTB|=BIT(0);
delay();delay();
}
uchar read()
{
uchar i,k;
PORTB&=~BIT(1);
delay();
PORTB|=BIT(0);
delay();
for(i=0;i<8;i++)
{
PORTB|=BIT(1);
delay();
k=k<<1;
DDRB&=~BIT(0);
PORTB|=BIT(0);
if(PINB&BIT(0))k=k|0x01;
PORTB&=~BIT(1);
delay();
}
DDRB|=BIT(0);
return k;
}
void xie(uchar address,uchar date)
{
start();
write(0xa0);
ack();
write(address);
ack();
write(date);
ack();
stop();
}
uchar du(uchar address)
{
uchar a;
start();
write(0xa0);
ack();
write(address);
ack();
start();
write(0xa1);
ack();
a=read();
stop();
return a;
}
int main ()
{
init();
xie(2,0x0a);
_delay_ms(100);
while(1)
{
PORTA=0x0f;
_delay_ms(100);
PORTA=du(2);
_delay_ms(100);
}
}
#include <avr/io.h>
#include<util/delay.h>
#define uint unsigned int
#define uchar unsigned char
#define BIT(x) (1<<x)
/*sbit sda=P2^0;
sbit scl=P2^1;
sbit rst=P2^4;*/
void delay()
{ asm("NOP"); asm("NOP"); }
void init()
{
DDRA=0XFF;
DDRB|=BIT(7);
DDRB|=BIT(0)|BIT(1);
PORTB&=~BIT(7);
PORTB|=BIT(0);
delay();
PORTB|=BIT(1);
delay();
}
void start()
{
PORTB|=BIT(0);
delay();
PORTB|=BIT(1);
delay();
PORTB&=~BIT(0);
delay();
}
void stop()
{
PORTB&=~BIT(0);
delay();
PORTB|=BIT(1);
delay();
PORTB|=BIT(0);
delay();
}
void ack()
{
uchar i;
PORTB|=BIT(1);
DDRB&=~BIT(0);
PORTB|=BIT(0);
delay();
while(((PINB&BIT(0))==1)&&(i<250))i++;
PORTB&=~BIT(1);
DDRB|=BIT(0);
delay();
}
void write(uchar date)
{
uchar i;
for(i=0;i<8;i++)
{
PORTB&=~BIT(1);
delay();
if(date&0x80)PORTB|=BIT(0);
else PORTB&=~BIT(0);
delay();
PORTB|=BIT(1);
delay();
date=date<<1;
}
PORTB&=~BIT(1);
delay();delay();
PORTB|=BIT(0);
delay();delay();
}
uchar read()
{
uchar i,k;
PORTB&=~BIT(1);
delay();
PORTB|=BIT(0);
delay();
for(i=0;i<8;i++)
{
PORTB|=BIT(1);
delay();
k=k<<1;
DDRB&=~BIT(0);
PORTB|=BIT(0);
if(PINB&BIT(0))k=k|0x01;
PORTB&=~BIT(1);
delay();
}
DDRB|=BIT(0);
return k;
}
void xie(uchar address,uchar date)
{
start();
write(0xa0);
ack();
write(address);
ack();
write(date);
ack();
stop();
}
uchar du(uchar address)
{
uchar a;
start();
write(0xa0);
ack();
write(address);
ack();
start();
write(0xa1);
ack();
a=read();
stop();
return a;
}
int main ()
{
init();
xie(2,0x0a);
_delay_ms(100);
while(1)
{
PORTA=0x0f;
_delay_ms(100);
PORTA=du(2);
_delay_ms(100);
}
}