导航: 老古网老古论坛XMOS公共讨论区XMOS开源项目区单片机程序设计嵌入式系统广告区域
→请教关于MEGA16L和AT24C512通信的问题[alone6230]

 *第41694篇: 请教关于MEGA16L和AT24C512通信的问题

  
楼 主:alone6230 2006年6月28日14:32
 请教关于MEGA16L和AT24C512通信的问题

我初学MEGA16 
一下代码运行到函数“stop()"的时候就出错了——估计是#define stop 0X30 有问题
可是我也不知道应该值是多少。

我的本意是向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(); 
          }
}


>>>>>>对该主题发表你的看法

本主题贴数1,分页: [第1页]


[上一篇主题]:招"硬件系统工程师,视频应用系统工程师,音频应用系统工程师,IC(集成电路)设计验证工程师

[下一篇主题]:请RTL8019读写操作时序