⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 avr的iic接口程序(philip saa1064).txt

📁 AVR的IIC接口程序
💻 TXT
字号:
/******************************************************************
    Title:    ATS90S8515 I2C interface with SAA1064
    Note:     To contact me, mail to dailizhou@up369.com
              You might find more free stuff at my homepage:
              http://www.dailzh.net
*******************************************************************/

#include <io8515v.h>

const unsigned char db[10]=
    { 0xb7,0x06,0x73,0x57,0xc6,0xd5,0xf5,0x07,0xf7,0xd7};

#define Set_SDA  	asm("sbi 0x18,4")  //PORTB=PORTB|0x10   
#define Cls_SDA  	asm("cbi 0x18,4")  // PORTB=PORTB&0xEF
#define Set_SCL  	asm("sbi 0x18,2")  //PORTB=PORTB|0x04
#define Cls_SCL  	asm("cbi 0x18,2")  //PORTB=PORTB&0xFB

#define SDA_High 	PINB&0x10


//********************function declaration***************************

void I2C_Error(void);
void I2C_Start(void);
void I2C_Stop(void);
void I2C_Write(unsigned char wb);
unsigned char I2C_Read(unsigned char bEnd);

void I2C_Error(void)
{
  //no instruction
}

void I2C_Delay(void)
{
     char ri;
     for( ri=0; ri<100; ri++ );
}

void I2C_Start(void)
{
     Cls_SCL;
     I2C_Delay();
     Set_SDA;
     I2C_Delay();

     Set_SCL;
     I2C_Delay();
     Cls_SDA;
     I2C_Delay();

     Cls_SCL;
     I2C_Delay();	     
}

void I2C_Stop(void)
{
     Cls_SDA;
     I2C_Delay();
     
     Set_SCL;
     I2C_Delay();
     Set_SDA;
     I2C_Delay();

     Set_SCL;
     I2C_Delay();
}

void I2C_Write(unsigned char wb)
{
     register unsigned char i;
 
     for(i=0;i<8;i++)
     {
         if( wb&0x80 ) Set_SDA;
         else          Cls_SDA;
         wb=wb<<1;
         I2C_Delay();
         
         Set_SCL;
         I2C_Delay();
         I2C_Delay();
         Cls_SCL;
         I2C_Delay();
     }

     Set_SDA;
     I2C_Delay();
     Set_SCL;
     I2C_Delay();

     if( SDA_High ) I2C_Error();
     else
     {
        Cls_SCL;
        I2C_Delay();
     }

}
/***************** These code isn't tested!!!!!!!!! *********
unsigned char I2C_Read(unsigned char bEnd)
{
    register unsigned char i,mb;

    mb=0;
    for(i=0;i<8;i++)
    {
       I2C_Delay();

       Set_SCL;
       I2C_Delay();
       if( SDA_High ) mb++;
       mb=mb<<1;
       I2C_Delay();
     
       Cls_SCL;
       I2C_Delay();

    }

    if( bEnd )      Set_SDA;
    else            Cls_SDA;
    I2C_Delay();

    Set_SCL;
    I2C_Delay();
    I2C_Delay();

    Cls_SCL;
    I2C_Delay();
    
    return mb;

}
****************************************************/

/*******  follow are test codes ********************/
void I2C_SAA1064(int dn)
{

     unsigned char i1,i2,i3,i4;
     
     i1=dn%10;
     dn=dn/10;
     i2=dn%10;
     dn=dn/10;
     i3=dn%10;
     dn=dn/10;
     i4=dn%10;
     
     I2C_Start();
     I2C_Write( 0x70 );        /* write command */
     I2C_Write( 0 );           /* write command */
     I2C_Write( 0x37 );        /* write command */
     I2C_Write( db[i1] );      /* write command */
     I2C_Write( db[i2] );      /* write command */
     I2C_Write( db[i3] );      /* write command */
     I2C_Write( db[i4] );      /* write command */
     I2C_Stop();
        
}


void Delay(void)
{
     long ld;
     for(ld=0;ld<80000;ld++);
}

int main()
{
   int dd;
   DDRB=0xff;
   PORTB=0xff;
   dd=0;
   while(1)
   {
     Delay();
     I2C_SAA1064(dd++);   
	 Delay();
     I2C_SAA1064(dd++);	 
     if(dd>9999) dd=0;     
   }
   return 0;
}
/* ---------------------------------End----------------------------*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -