【站友投递】IIC的範例程式

嵌入式技术

1378人已加入

描述


/****************************************
 * IIC初始值 function
 ***************************************/
void i2c_init(void)

  // unsigned char slave_address;  //CTP Slave address set
    //IICADD
      //slave_address=0x38;    //Device slave address

      //GPE set 55555515
      *(volatile unsigned int *)GPECON=0xF5555515;//set GPE15->IICSDA  GPE14->IICSCL
      *(volatile unsigned int *)GPECON=0xA5555515;//set GPE15->IICSDA  GPE14->IICSCL
      *(volatile unsigned int *)GPEUP=0xC00f ;     //GPE3=CTP Interrupt

      //IIC Initial
      *(volatile unsigned int *)0x54000008 = 0x38; // S3C2410A to IIC Save address
      *(volatile unsigned int *)0x54000000 = 0xAF; // ACK &IIC中斷,IICCLK=MCLK/16 ,ACK=fPCLK/16//(15+1)
      *(volatile unsigned int *)0x54000004 = 0x10; // Enable Tx/i2c_master_RX

}

/*************************************************
 * I2C Master TX function
 ************************************************/
void i2c_master_TX(unsigned char slave_add,unsigned char add1)
{
    unsigned char i=0;
   *(volatile unsigned int *)0x54000004 = 0xD0; // Master Tx mode
   *(volatile unsigned int *)0x5400000C = slave_add; // Master to Slave address
   *(volatile unsigned int *)0x54000004= 0xF0;//IICSTAT (M/R START) bit5 IIC Start single產生

         while((*(volatile unsigned char *)(0x54000000)&0x10 )!=0x10){;;} //Tx/i2c_master_RX Interrupt Enable
         *(volatile unsigned char *) 0x5400000C=add1 ;//Writer A DATA
         *(volatile unsigned int *)0x54000000=0xAF;  //Interrupt pending =0 
      
         while((*(volatile unsigned char *)(0x54000000)&0x10 )!=0x10){;;} //Tx/i2c_master_RX Interrupt Enable
         *(volatile unsigned int *)0x54000004=0xD0;//STOP
         *(volatile unsigned int *)0x54000000=0xAF;//ACK  
         Delay1us(10);
}

/*************************************************
 * I2C Master Receiver function read 26 byte

 ************************************************/
void i2c_master_RX(unsigned char slave_add) //
{
  unsigned int i=0;
  unsigned int temp1;
  unsigned char check_sum; 
   *(volatile unsigned int *)0x54000004 = 0x90; // Master i2c_master_RX mode
   *(volatile unsigned int *)0x5400000C = slave_add; // Master to Slave address
   *(volatile unsigned int *)0x54000004=0xB0;//IICSTAT (M/R START)bit5 IIC Start single產生
    while((*(volatile unsigned char *)(0x54000000) &0x10 )!=0x10){;;} //Tx/i2c_master_RX Interrupt Enable          
    *(volatile unsigned int *)0x54000000=0xAF;  //Interrupt pending =0      
    for(i=0;i<25;i++)
     {
       while((*(volatile unsigned char *)(0x54000000) &0x10 )!=0x10){;;} //Tx/i2c_master_RX Interrupt Enable  
          data1[i]=*(volatile unsigned char *) 0x5400000C ;//READ A DATA
         *(volatile unsigned int *)0x54000000=0xAF;  //Interrupt pending =0             }
             while((*(volatile unsigned char *)(0x54000000) &0x10 )!=0x10){;;} //Tx/i2c_master_RX Interrupt Enable       
            data1[25]=*(volatile unsigned char *)0x5400000C ;//READ 最後一筆資料 DATA
            *(volatile unsigned int *)0x54000004=0x90;//STOP
            *(volatile unsigned int *)0x54000000=0x2F;//NACK
             Delay1us(10);
}



打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 相关推荐

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分