查看: 3169|回复: 1

PSoC3 FirstTouch Starter Kit 之简单IIC通信

[复制链接]
  • TA的每日心情
    奋斗
    2018-8-29 20:40
  • 签到天数: 1341 天

    连续签到: 1 天

    [LV.10]以坛为家III

    发表于 2012-10-13 01:30:41 | 显示全部楼层 |阅读模式
    分享到:
    利用IIC通信实现了对AT24C02的读写
    首先看原理图
    QQ截图20121013011428.jpg
    简单就是指放了一个IIC组件和两个数字IO
    IIC组件的设置
    QQ截图20121013011439.jpg
    但要注意两个数字IO的管教配置
    每个IO要设置成双向,开漏低电平
    QQ截图20121013011451.jpg
    QQ截图20121013011501.jpg
    引脚分配 当不适用IIC唤醒的话 IIC的 SDA 和 SCL 基本上也会是自由分配的
    QQ截图20121013011523.jpg

    注意AT24C02的 电路连接 和 IIC通信的方法


    QQ截图20121009123419.jpg

    主要代码
    测试写入函数,address为要写入的芯片内部的地址,ver为写入的数据
    注意的是 器件地址, 由于PSoC的IIC函数中,对器件地址的规定是除去 W/R 判断位之后的右对齐
    比如说我的AT24C02 ,器件地址的高4位厂家规定为1010
    我器件的A2 A1 A0 都接地 为 000,则在PSoC3 中 器件地址为1010000即 0x50

    uint8 IIC_TestWriteByte(uint8 address, uint8 ver)
    {
            uint8 Status = 0;
            uint8 slaveAddr = 0x50; // device address , not contain the last W/R and right align
            uint8 count = 0;
            uint8 add[2] = {0};   
           
            add[0] = address;                // byte address
            add[1] = ver;                    // data
            I2C_MasterClearStatus();

            I2C_MasterWriteBuf(slaveAddr, add, 2, I2C_MODE_COMPLETE_XFER);
            Status = I2C_MasterStatus();
            while(Status & I2C_MSTAT_XFER_INP) /* Waits till the transfer is complete */
            {
                    count ++ ;
                    if(count == 255)
                    {
                            return Status;
                    }
                    Status = I2C_MasterStatus();
            }
            return Status;
            // Status = 0x02 = I2C_MSTAT_WR_CMPLT = Write complete */
    }


    测试读取函数
    uint8 IIC_TestReadByte(uint8 address)
    {
            uint8 Status = 0;
            uint8 slaveAddr = 0x50; // device address not contain the last W/R and right align
            uint8 count = 0;
            uint8 add[1] = {0};   
            uint8 read_value[2] = {0};
           
            add[0] = address;    //byte address
           
            I2C_MasterClearStatus();
            I2C_MasterWriteBuf(slaveAddr, add, 1, I2C_MODE_NO_STOP);
            Status = I2C_MasterStatus();
            // Status should be  I2C_MSTAT_WR_CMPLT | I2C_MSTAT_XFER_INP | I2C_MSTAT_XFER_HALT
            while((Status & I2C_MSTAT_WR_CMPLT)==0)
            {
                    count ++ ;
                    if(count == 255)
                    {
                            return Status;
                    }
                    Status = I2C_MasterStatus();
            }
                   
            count = 0;
        I2C_MasterReadBuf(slaveAddr, read_value, 1, I2C_MODE_REPEAT_START ); /* read_value will hold the two bytes read.This begins with a repeat start */

            while(I2C_MasterStatus() & I2C_MSTAT_XFER_INP);

            return read_value[0];
    }


    调试时候的截图
    QQ截图20121010115857.jpg
    注意看到能够正确读取出写入的数据

    实物图
    IMG_4476.JPG
    回复

    使用道具 举报

  • TA的每日心情
    奋斗
    2020-9-28 10:10
  • 签到天数: 1018 天

    连续签到: 1 天

    [LV.10]以坛为家III

    发表于 2012-10-14 09:59:43 | 显示全部楼层
    教程连载的好啊,顶个~~~
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-4-20 00:38 , Processed in 0.115570 second(s), 17 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.