我的本意是向AT24C512写入数组 data[8], 再读入MEGA16,然后求数组的和;
写入前和为0 读出后和不为0.
能否请高手帮忙看看问题出在什么地方!
#include <mega16.h>
#include <iom16v.h>
#include <macros.h>
#define START 0x08
#define MT_SLA_ACK 0x18
#define MR_SLA_ACK 0x40
#define STOP 0x30
void start_i() //initialise Mega16 start
{
while (!(TWCR & (1 < <TWINT)))
TWCR=(1 < <TWINT)|(1 < <TWSTA)|(1 < <TWEN);
if ((TWSR & 0XF8)!= START) ERROR_l();
}
//following function sends a "start" byte to AT24C512,
//including device address and a read state code(the last 1)
void read_a()
{
while (!(TWCR & (1 < <TWINT)))
{
TWDR=0XA1;
TWCR=(1 < <TWINT)|(1 < <TWEN);
if ((TWSR &0XF8)!=MR_SLA_ACK ) ERROR_l();
}
}
//following function read one bit and save it in data_r
int read_b ()
{
int data_r=0;
while (!(TWCR & (1 < <TWINT)))
{
data_r=TWDR;
TWCR=(1 < <TWINT)|(1 < <TWEN);
if ((TWSR &0XF8)!=MR_SLA_ACK ) ERROR_l();
return data_r;
}
}
//following function sends a "start" byte to AT24C512,
//including device address and a write state code(the last 0)
void write_d_a()
{
while (!(TWCR & (1 < <TWINT)))
{
TWDR=0XA0;
TWCR=(1 < <TWINT)|(1 < <TWEN);
if ((TWSR &0XF8)!=MT_SLA_ACK ) ERROR_l();
}
}
void write_w_a(int a,int b)
{
while (!(TWCR & (1 < <TWINT)))
{
TWDR= a;
TWCR=(1 < <TWINT)|(1 < <TWEN);
if ((TWSR &0XF8)!=MT_SLA_ACK ) ERROR_l();
}
while (!(TWCR & (1 < <TWINT)))
{
TWDR= b;
TWCR=(1 < <TWINT)|(1 < <TWEN);
if ((TWSR &0XF8)!=MT_SLA_ACK ) ERROR_l();
}
}
void write_b (int data_w)
{
while (!(TWCR & (1 < <TWINT)))
{
TWDR=data_w;
TWCR=(1 < <TWINT)|(1 < <TWEN);
if ((TWSR &0XF8)!=MT_SLA_ACK ) ERROR_l();
}
}
void stop()
{
while (!(TWCR & (1 < <TWINT)))
TWCR=(1 < <TWINT)|(1 < <TWSTO)|(1 < <TWEN);
if ((TWSR &0XF8)!=MR_SLA_ACK ) ERROR_l();
}
void main(void)
{
int i=0;
int w_a1=0x05,w_a2=0x07;
int data[8]={0,0,0,0,0,0,0,0}; // data to be sent
int Temp=0;
// 2 Wire Bus initialization
// Generate Acknowledge Pulse: on
// 2 Wire Bus Slave Address: 0h
// General Call Recognition: Off
// Bit Rate: 250.000 kHz
TWBR=0x00;
TWAR=0x00;
TWCR=0x44;
while (1)
{
flash_l();
start_i();
write_d_a();
write_w_a(w_a1,w_a2);
for(i=0;i <8;i++)
{
write_b(data[i]);
}
stop();
start_i();
read_a();
write_w_a(w_a1,w_a2);
for(i=0;i <8;i++)
{
data[i]=read_b();
Temp+=data[i];
}
if (Temp!=0)
{
green_l();
}
else
{
red_l();
}
}