atmel7s256 的IIC程序

/********************************Copyright (c)**********************************
**文件名 iic.C
**创建: Sha zq
**E-mail:[email protected]
**日期:  2008-01-24
**描述: ZLG7290库函数程序,IIC程序接口
** 修改人:
** 版本:
** 日期:
** 描述:
*******************************************************************************/

#include "board.h"
#define AT91C_TWI_CLOCK  8000
#define zlg7290 (0x38<<16) //ZLG7290的IIC地址
#define zlg7291 (0x38<<16) //ZLG7290的IIC地址


/*============================================================
* 函 数 名: AT91F_SetTwiClock
* 参  数:Null
* 功能描述:
*     设置IIC的时钟
* 返 回 值:Null
* 作  者:Sha zq 日期:2008-01-24
============================================================*/ 
void AT91F_SetTwiClock(void)
{
 int sclock;

 /* Here, CKDIV = 1 and CHDIV=CLDIV  ==> CLDIV = CHDIV = 1/4*((Fmclk/FTWI) -6)*/

 sclock = (10*MCK /AT91C_TWI_CLOCK);
 if (sclock % 10 >= 5)
  sclock = (sclock /10) - 5;
 else
  sclock = (sclock /10)- 6;
 sclock = (sclock + (4 - sclock %4)) >> 2; // div 4

        AT91C_BASE_TWI->TWI_CWGR = ( 1<<16 ) | (sclock << 8) | sclock  ;
}

/*===========================================================
* 函 数 名: AT91F_TWI_WriteByte
* 参  数:pTwi,int_address,*data2send,size
* 功能描述:
*     通过硬件IIC接口,向IIC接口的器件写数据
* 返 回 值:Null
* 作  者:Sha zq 日期:2008-01-24
============================================================*/ 

int AT91F_TWI_WriteByte(const AT91PS_TWI pTwi , int int_address, char *data2send, int size)
{
 unsigned int status,sta,rece;
     
        // Set the TWI Master Mode Register
 pTwi->TWI_MMR = (zlg7290|AT91C_TWI_IADRSZ_1_BYTE) & ~AT91C_TWI_MREAD;
         
 // Set TWI Internal Address Register
 pTwi->TWI_IADR = int_address;

        status = pTwi->TWI_SR;
 pTwi->TWI_THR = *(data2send++);
       
        pTwi->TWI_CR = AT91C_TWI_START;       
      
        while(size-->1)
        {
           while (!(pTwi->TWI_SR & AT91C_TWI_TXRDY));
           pTwi->TWI_THR = *(data2send++);           
        }
        pTwi->TWI_CR = AT91C_TWI_STOP;
        status = pTwi->TWI_SR;
          
         while (!(pTwi->TWI_SR & AT91C_TWI_TXCOMP));
            return 1;
       
 
}

/*===========================================================
* 函 数 名: AT91F_TWI_ReadByte
* 参  数:pTwi,int_address,*data2send,size
* 功能描述:
*     通过硬件IIC接口,向IIC接口的器件读数据
* 返 回 值:Null
* 作  者:Sha zq 日期:2008-01-25
============================================================*/

int AT91F_TWI_ReadByte(const AT91PS_TWI pTwi , int int_address, char *data, int size)
{
 unsigned int status;
     
        // Set the TWI Master Mode Register
 pTwi->TWI_MMR = (zlg7291|AT91C_TWI_IADRSZ_1_BYTE) |AT91C_TWI_MREAD;
        // Set TWI Internal Address Register
 pTwi->TWI_IADR = int_address;
        pTwi->TWI_CR = AT91C_TWI_START;
        status = pTwi->TWI_SR;
          
        while(size-->1)
        {
           while (!(pTwi->TWI_SR & AT91C_TWI_RXRDY));
            *(data++)=pTwi->TWI_RHR ;           
        }
       
         pTwi->TWI_CR = AT91C_TWI_STOP;
         status = pTwi->TWI_SR;
          
         while (!(pTwi->TWI_SR & AT91C_TWI_TXCOMP));
         *data=pTwi->TWI_RHR ;
         return 1;
}

/*============================================================
* 函 数 名: AT91F_TWI_Open
* 参  数:null
* 功能描述:
*     初始化IIC
* 返 回 值:Null
* 作  者:Sha zq 日期:2008-01-24
============================================================*/

void AT91F_TWI_Open(void)
{
 // Configure TWI PIOs
 AT91F_TWI_CfgPIO ();       
        AT91F_PIO_CfgOpendrain(AT91C_BASE_PIOA,(unsigned int) AT91C_PA3_TWD);
        // Configure PMC by enabling TWI clock
 AT91F_TWI_CfgPMC ();
 // Configure TWI in master mode
 AT91F_TWI_Configure (AT91C_BASE_TWI);
 // Set TWI Clock Waveform Generator Register
 AT91F_SetTwiClock();
}

此程序,主要为ZLG7290。ZLG7290部分的代码,可以参考前面的文章呵呵

有问题欢迎交流,大家好才是真的好 嘿嘿

 

你可能感兴趣的:(经验技巧)