3個atmega16之間的(TWI互相通信)+(中斷形式仿真)
//1號主機(jī):
#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");
//發(fā)送從機(jī)地址(寫),等待回應(yīng), 錯誤檢測
Load(k);
if(STATUS!=MT_SLA_WRITE_ACK) //{ERROR(STATUS);return 0;}
puts("SLA_W RROR");
//發(fā)送數(shù)據(jù),等待回應(yīng),錯誤檢測
Load(data);
if(STATUS!=MT_DATA_ACK) //{ERROR(STATUS);return 0;}
puts("Date Error");
Stop();
return 1;//發(fā)送成功,返回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號從機(jī) #include while(1) //3號從機(jī) #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;}
//發(fā)送從機(jī)地址(寫),等待回應(yīng), 錯誤檢測
Load(SLA_W);
if(STATUS!=MT_SLA_WRITE_ACK) {ERROR(STATUS);return 0;}
//發(fā)送數(shù)據(jù),等待回應(yīng),錯誤檢測
Load(data);
if(STATUS!=MT_DATA_ACK) {ERROR(STATUS);return 0;}
Stop();
return 1;//發(fā)送成功,返回1;
}
unsigned char ReadByte(unsigned char addr,
unsigned char *p_data)
{
//啟動I2C
Start();
if(STATUS!=START) {ERROR(STATUS);return 0;}
//發(fā)送從機(jī)地址(寫),等待回應(yīng), 錯誤檢測
Load(SLA_W);
if(STATUS!=MT_SLA_WRITE_ACK) {ERROR(STATUS);return 0;}
//發(fā)送EEPROM地址,等待回應(yīng),錯誤檢測
Load(addr);
if(STATUS!=MT_DATA_ACK) {ERROR(STATUS);return 0;}
//再次啟動I2C
Start();
if(STATUS!=RE_START) {ERROR(STATUS);return 0;}
//發(fā)送從機(jī)地址(讀),等待回應(yīng), 錯誤檢測
Load(SLA_R);
if(STATUS!=MT_SLA_READ_ACK) {ERROR(STATUS);return 0;}
//讀取I2C數(shù)據(jù)
Get(p_data);
if(STATUS!=MT_READ_ACK) {ERROR(STATUS);return 0;}
//發(fā)送Stop,結(jié)束讀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");
//發(fā)送從機(jī)地址(寫),等待回應(yīng), 錯誤檢測
Load(k);
if(STATUS!=MT_SLA_WRITE_ACK) //{ERROR(STATUS);return 0;}
puts("SLA_W RROR");
//發(fā)送數(shù)據(jù),等待回應(yīng),錯誤檢測
Load(data);
if(STATUS!=MT_DATA_ACK) //{ERROR(STATUS);return 0;}
puts("Date Error");
Stop();
return 1;//發(fā)送成功,返回1;
}
unsigned char *p_data)
{
//啟動I2C
Start();
if(STATUS!=START) {ERROR(STATUS);return 0;}
//發(fā)送從機(jī)地址(寫),等待回應(yīng), 錯誤檢測
Load(SLA_W);
if(STATUS!=MT_SLA_WRITE_ACK) {ERROR(STATUS);return 0;}