Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1585072
  • 博文数量: 239
  • 博客积分: 1760
  • 博客等级: 上尉
  • 技术积分: 1595
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-08 23:53
文章分类

全部博文(239)

文章存档

2016年(1)

2015年(28)

2014年(53)

2013年(42)

2012年(50)

2011年(65)

分类: 嵌入式

2011-01-24 23:53:19


int f_nGetACK;
/*********************************************************************************
* name:  iic_test
* func:  test iic
* para:  none
* ret:  none
* modify:
* comment:  
*********************************************************************************/
void iic_test(void)
{
    char szData[16] = "liuzhiping";
    unsigned int  i;
    
    Uart_Printf("\n IIC Protocol Test Example, using AT24C04...\n");
    Uart_Printf(" Write char 0-f into AT24C04\n");
   
    f_nGetACK = 0;
   
    // Enable interrupt
    rINTMOD  = 0x0;
    ClearPending(BIT_IIC);
    rINTMSK &= ~BIT_IIC;
    pISR_IIC = (unsigned)iic_int_24c04;
   
    // Initialize iic
    rIICADD = 0x10;            // S3C2410X slave address
    rIICCON = 0xaf;            // Enable ACK, interrupt, SET IICCLK=MCLK/16
    rIICSTAT = 0x10;           // Enable TX/RX

    // Write 0 - 16 to 24C04
    for(i=0; i<16; i++)
    {
        iic_write_24c040(0xa0, i, szData[i]);
        Uart_Printf(" %d", szData[i]);
        Delay(10);
    }

    // Clear array
    for(i=0; i<16; i++)
        szData[i]=0;

    // Read 16 byte from 24C04
    for(i=0; i<16; i++)
        iic_read_24c040(0xa0, i, &(szData[i]));
   
    // Printf read data
    Uart_Printf(" \nRead 16 bytes from AT24C04\n");
    for(i=0; i<16; i++)
    {
        Uart_Printf(" %c ", szData[i]);
    }

    rINTMSK |= BIT_IIC;
    Uart_Printf("\n end.\n");
}
/*********************************************************************************
* name:  iic_write_24c040
* func:  write data to 24C040
* para:  unSlaveAddr --- input, chip slave address
*   unAddr  --- input, data address
*   ucData     --- input, data value
* ret:  none
* modify:
* comment: 
*********************************************************************************/
void iic_write_24c040(unsigned int unSlaveAddr, unsigned int unAddr, char ucData)
{
    f_nGetACK = 0;

    // Send control byte
    rIICDS = unSlaveAddr;          // 0xa0
    rIICSTAT = 0xf0;           // Master Tx,Start
    while(f_nGetACK == 0)          // Wait ACK
    {
        Delay(1);/*必须延时1ms*/
    }
    f_nGetACK = 0;

    //Send address
    rIICDS = unAddr;
    rIICCON = 0xaf;            // Resumes IIC operation.

    while(f_nGetACK == 0)          // Wait ACK
    {
        Delay(1);/*必须延时1ms*/
    }
    f_nGetACK = 0;

    // Send data
    rIICDS = ucData;
    rIICCON = 0xaf;            // Resumes IIC operation.

    while(f_nGetACK == 0)          // Wait ACK
    {
        Delay(1);/*必须延时1ms*/
    }
    f_nGetACK = 0;

    // End send
    rIICSTAT = 0xd0;           // Stop Master Tx condition
    rIICCON = 0xaf;            // Resumes IIC operation.
    Delay(5);             // Wait until stop condtion is in effect.
}
 
/********************************************************************************
* name:  iic_read_24c040
* func:  read data from 24C040
* para:  unSlaveAddr --- input, chip slave address
*   unAddr  --- input, data address
*   pData     --- output, data pointer
* ret:  none
* modify:
* comment: 
*********************************************************************************/
void iic_read_24c040(unsigned int unSlaveAddr, unsigned int unAddr, char *pData)
{
    char cRecvByte;

    f_nGetACK = 0;
   
    //Send control byte
    rIICDS = unSlaveAddr;          // 0xa0
    rIICSTAT = 0xf0;           // Master Tx,Start
    while(f_nGetACK == 0)          // Wait ACK
    {
        Delay(1);/*必须延时1ms*/
    }
    f_nGetACK = 0;
   
    // Send address
    rIICDS = unAddr;
    rIICCON = 0xaf;            // Resumes IIC operation.
    while(f_nGetACK == 0)          // Wait ACK
    {
        Delay(1);/*必须延时1ms*/
    }
    f_nGetACK = 0;
   
    //Send control byte
    rIICDS = unSlaveAddr;          // 0xa0
    rIICSTAT = 0xb0;           // Master Rx,Start
    rIICCON = 0xaf;            // Resumes IIC operation.  

    while(f_nGetACK == 0)          // Wait ACK
    {
        Delay(1);/*必须延时1ms*/
    }
    f_nGetACK = 0;

    //Get data
    cRecvByte = rIICDS;
    rIICCON = 0x2f;
    Delay(1);

    // Get data
    cRecvByte = rIICDS;

    // End receive
    rIICSTAT = 0x90;           // Stop Master Rx condition
    rIICCON = 0xaf;            // Resumes IIC operation.
    Delay(5);             // Wait until stop condtion is in effect.

    *pData = cRecvByte;
}
/*********************************************************************************
* name:  iic_int_24c04()
* func:  IIC interrupt handler
* para:  none
* ret:  none
* modify:
* comment: 
*********************************************************************************/
void __irq iic_int_24c04(void)
{
    ClearPending(BIT_IIC);
    f_nGetACK = 1;
}

芯片:S3C2440A
iic从设备:AT24C04

这份代码基本是从网上拷贝的,在此谢谢网上的大牛们,小弟向你们致敬,忘了原创大牛是谁,就没写转载地址,请大牛谅解哦。

心得:开始这份代码怎么也不能用,后来在每个while(f_nGetACK == 0)循环中添加了个延时,就可以用了,但还不知道原理,先用着吧!希望给后来的兄弟有帮助。
阅读(1784) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~