这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 综合技术 » 基础知识 » iccavr 谁有iccavr读写外部存储器的程序,请赐教

共3条 1/1 1 跳转至

iccavr 谁有iccavr读写外部存储器的程序,请赐教

院士
2006-09-17 18:14:16     打赏
iccavr 谁有iccavr读写外部存储器的程序,请赐教



关键词: iccavr     谁有     读写     外部     存储器     程序     赐教    

院士
2006-12-22 22:43:00     打赏
2楼
问 谢谢先 1: 我也想要 2: 想当年呀,想当年我也和你们一样在这里求要I2C的程序,可是,却没有人会理会你的,我也说不出来是什么原因,在这里交流的人总是想得到别人的东西,却很少有人提供什么东西,这迫使你自己去写,自己去看datasheet,也许有利于一个人的经验值增长,但是对整个IC界来说,这种藏私的现象却让它发展慢之又慢。
还是给你们发一下吧:


//*** Functions declare for I2C operations ***//
  void twi_init       (void);
  void I2C_SendByte   (unsigned char Byte  );
  void I2C_ReceiveByte(unsigned char SetACK);
  unsigned char DataBuffer[100];
  unsigned char ReceiveBuffer[100];
  unsigned char I2C_Wait   (void);
  unsigned char I2C_CurRead(
                unsigned char DeviceAddr);
  unsigned char I2C_WriteByte(
                unsigned char H_Addr,
                unsigned int  D_Addr,
                unsigned char Data);
  unsigned char I2C_WriteBytes(
                unsigned char DeviceAddr,
                unsigned int  DataAddr,
                unsigned char *Buffer,
                unsigned char Length);
  unsigned char I2C_ReadBytes(
                unsigned char DeviceAddr,
                unsigned int  D_Addr,
                unsigned char *Buffer ,
                unsigned char Length);
//defines for I2C operations
#define I2C_START        TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN)
#define I2C_STOP        TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWSTO)
//以下的错误是自定义的
#define START_ERROR            0xAA
#define RESTART_ERROR                 0x9F
#define SLA_W_ERROR            0xAB
#define SLA_R_ERROR            0xAC
#define DATA_W_ERROR                 0xAD
#define DATA_R_ERROR                 0xAE
#define TIMEOUT                0xAF

#define Ok                     0x01
#define Error                0x00

//*************************************************************************//
//***  以下进行I2C总线的操作,包括启动停止应答发送接收数据等所有的功能 ****//
//*************************************************************************//
void twi_init(void)
{
    PORTD|=0x01;
    PORTD|=0x02;
     TWBR  =  0x48;
    TWAR  =     0x00;
    TWSR  =     0x00;
    TWCR  =     0xC4;
}
unsigned char I2C_Wait(void)
{
   unsigned int timeout=10000;
   while(!(TWCR&(1<<TWINT))&&(timeout>0))
     timeout--;
   if(!timeout)
     return Error;
   return Ok;
}

void I2C_SendByte(unsigned char Byte)  //发送一个字节到I2C Bus上
{
   unsigned int c;
   TWDR=Byte;
   TWCR=(1<<TWINT)|(1<<TWEN);
   for ( c=0;c<1000;c++ );
}

void I2C_ReceiveByte(unsigned char SetACK) //接收应答
{
    if(SetACK)
        TWCR = (1<<TWINT) | (1 << TWEN ) | (1 << TWEA);
    else
        TWCR = (1<<TWINT) | (1 << TWEN );
}

unsigned char I2C_CurRead(unsigned char DeviceAddr)//读当前位置
{
    I2C_START;
    if(!I2C_Wait())
        {
           I2C_STOP;
           return TIMEOUT;
        }        
    if ((TWSR&0xF8)!=0x08)
    {
        I2C_STOP;    
        return START_ERROR;
    }
    I2C_SendByte(DeviceAddr | 0x01);
    if(!I2C_Wait())
        {
           I2C_STOP;
           return TIMEOUT;
        }    
    if ((TWSR&0xF8)!=0x40)
    {
        I2C_STOP;    
        return SLA_R_ERROR;
    }
    I2C_ReceiveByte(0); //Receive current byte. NACK, master requests no more data
    if(!I2C_Wait())
        {
           I2C_STOP;
           return TIMEOUT;
        }    
    ReceiveBuffer[0]=TWDR;
    if ((TWSR&0xF8)!=0x58)
    {
        I2C_STOP;    
        return DATA_R_ERROR;
    }
                
    I2C_STOP;    
    
    return Ok;
}

unsigned char I2C_ReadBytes(unsigned char DeviceAddr, unsigned int D_Addr,unsigned char *Buffer, unsigned char Length)
{
    unsigned char i;
    I2C_START;
    if(!I2C_Wait())
        {
           I2C_STOP;
           return TIMEOUT;
        }    
    if ((TWSR&0xF8)!=0x08)
    {
        I2C_STOP;    
        return START_ERROR;
    }
    I2C_SendByte(DeviceAddr & 0xFE);
    if(!I2C_Wait())
        {
           I2C_STOP;
           return TIMEOUT;
        }    
    if ((TWSR&0xF8)!=0x18)
    {
        I2C_STOP;    
        return SLA_W_ERROR;
    }
    I2C_SendByte(D_Addr>>8);
    if(!I2C_Wait())
        {
           I2C_STOP;
           return TIMEOUT;
        }    
    I2C_SendByte(D_Addr&0xFF);
    if(!I2C_Wait())
        {
           I2C_STOP;
           return TIMEOUT;
        }    
    I2C_START;
    if(!I2C_Wait())
        {
           I2C_STOP;
           return TIMEOUT;
        }    
    if ((TWSR&0xF8)!=0x10)
    {
        I2C_STOP;    
        
        return RESTART_ERROR;
    }
    I2C_SendByte(DeviceAddr | 0x01);
    if(!I2C_Wait())
        {
           I2C_STOP;
           return TIMEOUT;
        }    
    if ((TWSR&0xF8)!=0x40)
    {
        I2C_STOP;    
        return SLA_R_ERROR;
    }
    if (Length>1)
    {
        for (i=0;i<Length-1;i++)
        {
            I2C_ReceiveByte(1);
            if(!I2C_Wait())
            {
               I2C_STOP;
                  return TIMEOUT;
            }    
            *Buffer=TWDR;
            Buffer++;
        }
    }

    I2C_ReceiveByte(0); //Receive last or one byte. NACK, master requests no more data
    if(!I2C_Wait())
        {
           I2C_STOP;
           return TIMEOUT;
        }    
    *Buffer=TWDR;
    if ((TWSR&0xF8)!=0x58)
    {
        I2C_STOP;    
        return DATA_R_ERROR;
    }
                
    I2C_STOP;    
    
    return Ok;
}

unsigned char I2C_WriteByte(unsigned char H_Addr,unsigned int D_Addr,unsigned char Data)
{
   I2C_START;
   if(!I2C_Wait())
        {
           I2C_STOP;
           return TIMEOUT;
        }    
   if((TWSR&0xF8)!=0x08)
   {
       I2C_STOP;
       return Error;
   }
   I2C_SendByte(H_Addr&0xFE);
   if(!I2C_Wait())
        {
           I2C_STOP;
           return TIMEOUT;
        }    
   if((TWSR&0xF8)!=0x18)
   {
     I2C_STOP;
     return Error;
   }
   I2C_SendByte(D_Addr>>8);
   if(!I2C_Wait())
        {
           I2C_STOP;
           return TIMEOUT;
        }    
   I2C_SendByte(D_Addr&0xFF);
   if(!I2C_Wait())
        {
           I2C_STOP;
           return TIMEOUT;
        }    
   I2C_SendByte(Data);
   if(!I2C_Wait())
        {
           I2C_STOP;
           return TIMEOUT;
        }    
   if((TWSR&0xF8)!=0x28)
   {
    I2C_STOP;
    return Error;
   }
   I2C_STOP;
   return Ok;
}

unsigned char I2C_WriteBytes(unsigned char DeviceAddr, unsigned int DataAddr, unsigned char *Buffer, unsigned char Length)
{
    unsigned char i;
    // Send start condition
    I2C_START;
    if(!I2C_Wait())
    {
       I2C_STOP;
       return TIMEOUT;
    }    
    if ((TWSR&0xF8)!=0x08)
    {
        I2C_STOP;    
        return START_ERROR;
    }
    // Send device adress + W
    I2C_SendByte(DeviceAddr & 0xFE);
    if(!I2C_Wait())
    {
       I2C_STOP;
       return TIMEOUT;
    }    
    if ((TWSR&0xF8)!=0x18)
    {
        I2C_STOP;    
        return SLA_W_ERROR;
    }
    // Send data adress in device
    I2C_SendByte(DataAddr>>8);
    if(!I2C_Wait())
    {
       I2C_STOP;
       return TIMEOUT;
    }    
    I2C_SendByte(DataAddr&0xFF);
    if(!I2C_Wait())
    {
       I2C_STOP;
       return TIMEOUT;
    }
    // Send data in buffer

    while((Length--)>0)
    {
        I2C_SendByte(*(Buffer++));
        if(!I2C_Wait())
        {
           I2C_STOP;
           return TIMEOUT;
        }
    }
    if ((TWSR&0xF8)!=0x28)
    {
        I2C_STOP;    
        return DATA_W_ERROR;
    }
                
    I2C_STOP;    

    return Ok;
}
//************************The End********************************//

以上的程序是经过烧写成功运行的
外接存储器如At24cXXX都可以正常使用,像M16和M128只能接四片,具体接线方法请自已参照datasheet,不能什么都要我说出来,那就真的什么都学不到的

高工
2023-03-11 16:11:42     打赏
3楼

学习


共3条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]