3个atmega16之间的(TWI互相通信)+(中断形式仿真)
扫描二维码
随时随地手机看文章
//1号主机:
#include
#include
#include
#include
#defineucharunsigned char
#defineSTATUS(TWSR&0xf8)
#defineSLA_W0x32
#defineSLA_R0x33
#defineSLAVER_ADDRESS0x32
#defineSTART0x08
#defineRE_START0x10
#defineMT_SLA_WRITE_ACK0x18
#defineMT_SLA_READ_ACK0x40
#defineMT_DATA_ACK0x28
#defineMT_READ_ACK0x58
void Start(void)
{
TWCR=(1<
void Stop(void)
{
TWCR=(1<
void Load(uchar i)
{
TWDR=i;
TWCR=(1<
void Get(unsigned char *d)
{
TWCR=(1<
}
void ERROR(uchar state)
{Stop();
}
uchar SendByte(uchar k,uchar data)
{
//启动I2C
Start();
if(STATUS!=START) //{ERROR(STATUS);return 0;}
puts("Start ERRor");
//发送从机地址(写),等待回应, 错误检测
Load(k);
if(STATUS!=MT_SLA_WRITE_ACK) //{ERROR(STATUS);return 0;}
puts("SLA_W RROR");
//发送数据,等待回应,错误检测
Load(data);
if(STATUS!=MT_DATA_ACK) //{ERROR(STATUS);return 0;}
puts("Date Error");
Stop();
return 1;//发送成功,返回1;
}
void Init_Io(void)
{
DDRC&=~((1<
sei();
}
void Init_cj(void)
{
DDRC&=~((1<
TWAR=0x04; //| (1<
}
void main()
{
uchar temp=0;
Init_Io();
SendByte(0x06,0xf3);
(TWCR = (1<
SendByte(0x08,0xff);
while(1);
}
SIGNAL(SIG_2WIRE_SERIAL)
{ } //2号从机 #include while(1) //3号从机 #include #include #define uchar unsigned char void Start(void) } void Get(unsigned char *d) unsigned char ReadByte(unsigned char addr,
uchar temp1,temp2;
temp1=STATUS;
temp2=TWDR;
if(temp1==0x80)
{
DDRA=0xff;
PORTA=temp2;
//DDRA=0xff;
//lcd(10,0,"ri");
// lcdnum(0,1,temp2);
}
TWCR=(1<
#include
#include
#define uchar unsigned char
#define STATUS (TWSR&0xf8)
#define SLA_W0x32
#define SLA_R0x33
#define TWI_ADDRESS0x32
#define START0x08
#define RE_START0x10
#define MT_SLA_WRITE_ACK0x18
#define MT_SLA_READ_ACK0x40
#define MT_DATA_ACK0x28
#define MT_READ_ACK0x58
#define TW_SR_DATA_ACK0x80
void Start(void)
{
TWCR=(1<
void Stop(void)
{
TWCR=(1<
void Load(uchar i)
{
TWDR=i;
TWCR=(1<
void Get(unsigned char *d)
{
TWCR=(1<
}
void ERROR(uchar state)
{Stop();
}
uchar WriteByte(uchar data)
{
//启动I2C
Start();
if(STATUS!=START) {ERROR(STATUS);return 0;}
//发送从机地址(写),等待回应, 错误检测
Load(SLA_W);
if(STATUS!=MT_SLA_WRITE_ACK) {ERROR(STATUS);return 0;}
//发送数据,等待回应,错误检测
Load(data);
if(STATUS!=MT_DATA_ACK) {ERROR(STATUS);return 0;}
Stop();
return 1;//发送成功,返回1;
}
unsigned char ReadByte(unsigned char addr,
unsigned char *p_data)
{
//启动I2C
Start();
if(STATUS!=START) {ERROR(STATUS);return 0;}
//发送从机地址(写),等待回应, 错误检测
Load(SLA_W);
if(STATUS!=MT_SLA_WRITE_ACK) {ERROR(STATUS);return 0;}
//发送EEPROM地址,等待回应,错误检测
Load(addr);
if(STATUS!=MT_DATA_ACK) {ERROR(STATUS);return 0;}
//再次启动I2C
Start();
if(STATUS!=RE_START) {ERROR(STATUS);return 0;}
//发送从机地址(读),等待回应, 错误检测
Load(SLA_R);
if(STATUS!=MT_SLA_READ_ACK) {ERROR(STATUS);return 0;}
//读取I2C数据
Get(p_data);
if(STATUS!=MT_READ_ACK) {ERROR(STATUS);return 0;}
//发送Stop,结束读I2C操作。
Stop();
return 1;
}
void Init_Io(void)
{
DDRC&=~((1<
TWAR=0x06; //| (1<
}
void main()
{
// uchar temp1,temp2;
Init_Io();
//startlcd();
//lcd(0,0,"nimabi");
_delay_ms(30);
DDRD=0xff;
PORTD=0xff;
{
}
}
SIGNAL(SIG_2WIRE_SERIAL)
{
uchar temp1,temp2;
temp1=STATUS;
temp2=TWDR;
if(temp1==0x80)
{
DDRA=0xff;
PORTA=temp2;
DDRA=0xff;
//lcd(10,0,"ri");
// lcdnum(0,1,temp2);
}
TWCR=(1<
#include
#define STATUS (TWSR&0xf8)
#define SLA_W0x32
#define SLA_R0x33
#define TWI_ADDRESS0x32
#define START0x08
#define RE_START0x10
#define MT_SLA_WRITE_ACK0x18
#define MT_SLA_READ_ACK0x40
#define MT_DATA_ACK0x28
#define MT_READ_ACK0x58
#define TW_SR_DATA_ACK0x80
{
TWCR=(1<
void Stop(void)
{
TWCR=(1<
void Load(uchar i)
{
TWDR=i;
TWCR=(1<
{
TWCR=(1<
}
void ERROR(uchar state)
{Stop();
}
uchar SendByte(uchar k,uchar data)
{
//启动I2C
Start();
if(STATUS!=START) //{ERROR(STATUS);return 0;}
puts("Start ERRor");
//发送从机地址(写),等待回应, 错误检测
Load(k);
if(STATUS!=MT_SLA_WRITE_ACK) //{ERROR(STATUS);return 0;}
puts("SLA_W RROR");
//发送数据,等待回应,错误检测
Load(data);
if(STATUS!=MT_DATA_ACK) //{ERROR(STATUS);return 0;}
puts("Date Error");
Stop();
return 1;//发送成功,返回1;
}
unsigned char *p_data)
{
//启动I2C
Start();
if(STATUS!=START) {ERROR(STATUS);return 0;}
//发送从机地址(写),等待回应, 错误检测
Load(SLA_W);
if(STATUS!=MT_SLA_WRITE_ACK) {ERROR(STATUS);return 0;}